Skip to content

Commit

Permalink
feat<wallet>: Neutrino Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
manavdesai27 committed Jul 20, 2023
1 parent a4bf281 commit 496e866
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/wallet/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ class WalletClient extends NodeClient {
return super.setFilter(filter.toRaw());
}

/**
* Check filter against wallet key ring
* @param {WalletKey} ring
* @param {Filter} filter
* @returns {Promise}
*/

async getBlockFromNode(hash) {
// return super.getBlockPeer(hash);
console.log('getBlockFromNode');
}

async rescan(start) {
if (Buffer.isBuffer(start))
start = util.revHex(start);
Expand Down
19 changes: 19 additions & 0 deletions lib/wallet/nodeclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class NodeClient extends AsyncEmitter {

init() {
this.node.chain.on('connect', async (entry, block) => {
if (!this.opened || this.node.neutrino)
return;

await this.emitAsync('block connect', entry, block.txs);
});

this.node.chain.on('getblockpeer', async (entry, block) => {
if (!this.opened)
return;

Expand All @@ -50,6 +57,13 @@ class NodeClient extends AsyncEmitter {
await this.emitAsync('block disconnect', entry);
});

this.node.pool.on('cfilter', async (blockHeight, filter) => {
if (!this.opened)
return;

await this.emitAsync('cfilter', blockHeight, filter);
});

this.node.on('tx', (tx) => {
if (!this.opened)
return;
Expand Down Expand Up @@ -134,6 +148,11 @@ class NodeClient extends AsyncEmitter {
return entry;
}

async getBlockFromNode(hash) {
// await this.node.chain.getBlockPeer(hash);
console.log('getBlockFromNode');
}

/**
* Send a transaction. Do not wait for promise.
* @param {TX} tx
Expand Down
11 changes: 11 additions & 0 deletions lib/wallet/nullclient.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,17 @@ class NullClient extends EventEmitter {
this.wdb.emit('reset filter');
}

/**
* Check filter against wallet key ring
* @param {WalletKey} ring
* @param {Filter} filter
* @returns {Promise}
*/

async getBlockFromNode(hash) {
;
}

/**
* Esimate smart fee.
* @param {Number?} blocks
Expand Down
44 changes: 44 additions & 0 deletions lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const Outpoint = require('../primitives/outpoint');
const layouts = require('./layout');
const records = require('./records');
const NullClient = require('./nullclient');
const Script = require('../script/script');
const Address = require('../primitives/address');
const layout = layouts.wdb;
const tlayout = layouts.txdb;

Expand Down Expand Up @@ -65,10 +67,12 @@ class WalletDB extends EventEmitter {
this.state = new ChainState();
this.confirming = false;
this.height = 0;
this.filterHeight = 0;
this.wallets = new Map();
this.depth = 0;
this.rescanning = false;
this.filterSent = false;
this.isWitness = false;

// Wallet read lock.
this.readLock = new MapLock();
Expand Down Expand Up @@ -169,6 +173,14 @@ class WalletDB extends EventEmitter {
this.emit('error', e);
}
});

this.client.bind('cfilter', async (blockHeight, filter) => {
try {
await this.checkFilter(blockHeight, filter);
} catch (e) {
this.emit('error', e);
}
});
}

/**
Expand Down Expand Up @@ -201,6 +213,9 @@ class WalletDB extends EventEmitter {
id: 'primary'
});

const account = await wallet.getAccount(wallet.wid);
this.isWitness = account.witness;

const addr = await wallet.receiveAddress();

this.logger.info(
Expand Down Expand Up @@ -568,6 +583,35 @@ class WalletDB extends EventEmitter {
return this.client.resetFilter();
}

async checkFilter (blockHash, filter) {
this.filterHeight = this.filterHeight + 1;
const gcsKey = blockHash.slice(0, 16);

const piter = this.db.iterator({
gte: layout.p.min(),
lte: layout.p.max()
});

await piter.each(async (key) => {
const [data] = layout.p.decode(key);
let address = null;
if (data.length === 20) {
if (this.isWitness)
address = Address.fromWitnessPubkeyhash(data);
else
address = Address.fromPubkeyhash(data);
} else if (data.length === 32) {
address = Address.fromWitnessScripthash(data);
}
const script = Script.fromAddress(address);
const match = filter.match(gcsKey, script.toRaw());
if (match) {
await this.client.getBlockFromNode(blockHash);
return;
}
});
}

/**
* Backup the wallet db.
* @param {String} path
Expand Down

0 comments on commit 496e866

Please sign in to comment.