diff --git a/lib/bcoin/http/client.js b/lib/bcoin/http/client.js index 91195ee6..c29ebbbe 100644 --- a/lib/bcoin/http/client.js +++ b/lib/bcoin/http/client.js @@ -504,6 +504,24 @@ Client.prototype.walletSend = function walletSend(id, options, callback) { }); }; +Client.prototype.zapWallet = function zapWallet(id, now, age, callback) { + var body = { + now: now, + age: age + }; + + assert(utils.isFinite(now)); + assert(utils.isFinite(age)); + assert(now >= age); + + callback = utils.ensure(callback); + + return this._post('/wallet/' + id + '/zap', body, function(err) { + if (err) + return callback(err); + return callback(); + }); +}; /** * Expose diff --git a/lib/bcoin/http/provider.js b/lib/bcoin/http/provider.js index 0be54103..0cb792ac 100644 --- a/lib/bcoin/http/provider.js +++ b/lib/bcoin/http/provider.js @@ -125,6 +125,10 @@ Provider.prototype.sync = function sync(wallet, address) { }); }; +Provider.prototype.zap = function zap(now, age, callback) { + return this.client.zapWallet(this.id, now, age, callback); +}; + /** * Expose */ diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index 7b57964b..5a484774 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -276,6 +276,19 @@ NodeServer.prototype._init = function _init() { }); }); + this.post('/wallet/:id/zap', function(req, res, next, send) { + var id = req.options.id; + var now = req.options.now; + var age = req.options.age; + + self.walletdb.zapWallet(id, now, age, function(err, wallet) { + if (err) + return next(err); + + send(200, { success: true }); + }); + }); + // Update wallet / sync address depth this.put('/wallet/:id', function(req, res, next, send) { var id = req.options.id; diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 20a843b0..ed09519d 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -1728,16 +1728,28 @@ TXPool.prototype.removeUnchecked = function removeUnchecked(hash, callback, forc }); }; -TXPool.prototype.zap = function zap(now, age, callback, force) { +TXPool.prototype.zap = function zap(address, now, age, callback, force) { var self = this; - var unlock = this._lock(zap, [tip, callback], force); + if (typeof address !== 'string') { + force = callback; + callback = age; + age = now; + now = address; + address = null; + } + + var unlock = this._lock(zap, [address, now, age, callback], force); if (!unlock) return; callback = utils.wrap(callback, unlock); - return this.getRange(null, { + assert(utils.isFinite(now)); + assert(utils.isFinite(age)); + assert(now >= age); + + return this.getRange(address, { start: 0, end: now - age }, function(err, txs) { diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index d91d6ff5..73192f6e 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -735,6 +735,14 @@ Wallet.prototype.getRedeem = function getRedeem(hash, prefix) { return address.script; }; +Wallet.prototype.zap = function zap(now, age, callback) { + if (!this.provider.zap) { + return utils.asyncify(callback)( + new Error('Provider does not support zapping.')); + } + return this.provider.zap(now, age, callback); +}; + Wallet.prototype.scan = function scan(txByAddress, callback) { var self = this; var res = false; diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 1805f45e..e0b5076e 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -586,6 +586,15 @@ WalletDB.prototype.removeBlock = function removeBlock(block, callback) { }, callback); }; +WalletDB.prototype.zap = function zap(now, age, callback) { + return this.tx.zap(now, age, callback); +}; + +WalletDB.prototype.zapWallet = function zapWallet(id, now, age, callback) { + id = id.id || id; + return this.tx.zap(id, now, age, callback); +}; + /** * Provider */ @@ -740,6 +749,10 @@ Provider.prototype.update = function update(wallet, address) { return this.db.update(wallet, address); }; +Provider.prototype.zap = function zap(wallet, address) { + return this.db.zapWallet(wallet, address); +}; + /** * Expose */