From cbed60e900801c2dbc53b5abdfd339b4dd402e30 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 12 Aug 2016 18:03:47 -0700 Subject: [PATCH] wallet/http: fix events. --- lib/bcoin/http/client.js | 58 +++++++++++++--------------------------- lib/bcoin/http/server.js | 20 ++++++++------ lib/bcoin/http/wallet.js | 24 ++++++++++------- lib/bcoin/txdb.js | 10 ++++--- lib/bcoin/wallet.js | 1 + lib/bcoin/walletdb.js | 10 +++++++ test/http-test.js | 30 ++++++--------------- 7 files changed, 70 insertions(+), 83 deletions(-) diff --git a/lib/bcoin/http/client.js b/lib/bcoin/http/client.js index 9e019217..911ee3a3 100644 --- a/lib/bcoin/http/client.js +++ b/lib/bcoin/http/client.js @@ -89,61 +89,40 @@ HTTPClient.prototype._open = function _open(callback) { self.emit('error', new Error('Wrong network.')); }); - this.socket.on('wallet tx', function(tx, map) { - try { - tx = bcoin.tx.fromJSON(tx); - } catch (e) { - return self.emit('error', e); - } - self.emit('tx', tx, map); + this.socket.on('wallet tx', function(details) { + self.emit('tx', details); }); - this.socket.on('wallet confirmed', function(tx, map) { - try { - tx = bcoin.tx.fromJSON(tx); - } catch (e) { - return self.emit('error', e); - } - self.emit('confirmed', tx, map); + this.socket.on('wallet confirmed', function(details) { + self.emit('confirmed', details); }); - this.socket.on('wallet updated', function(tx, map) { - try { - tx = bcoin.tx.fromJSON(tx); - } catch (e) { - return self.emit('error', e); - } - self.emit('updated', tx, map); + this.socket.on('wallet unconfirmed', function(details) { + self.emit('unconfirmed', details); }); - this.socket.on('wallet address', function(receive, change, map) { + this.socket.on('wallet conflict', function(details) { + self.emit('conflict', details); + }); + + this.socket.on('wallet updated', function(details) { + self.emit('updated', details); + }); + + this.socket.on('wallet address', function(receive) { receive = receive.map(function(address) { return bcoin.keyring.fromJSON(address); }); - change = change.map(function(address) { - return bcoin.keyring.fromJSON(address); - }); - self.emit('address', receive, change, map); + self.emit('address', receive); }); - this.socket.on('wallet balance', function(balance, id) { + this.socket.on('wallet balance', function(balance) { self.emit('balance', { + id: balance.id, confirmed: utils.satoshi(balance.confirmed), unconfirmed: utils.satoshi(balance.unconfirmed), total: utils.satoshi(balance.total) - }, id); - }); - - this.socket.on('wallet balances', function(json) { - var balances = {}; - Object.keys(json).forEach(function(id) { - balances[id] = { - confirmed: utils.satoshi(json[id].confirmed), - unconfirmed: utils.satoshi(json[id].unconfirmed), - total: utils.satoshi(json[id].total) - }; }); - self.emit('balances', balances); }); this.socket.on('connect', function() { @@ -484,6 +463,7 @@ HTTPClient.prototype.getWalletBalance = function getBalance(id, account, callbac return callback(new Error('Not found.')); return callback(null, { + id: body.id, confirmed: utils.satoshi(body.confirmed), unconfirmed: utils.satoshi(body.unconfirmed), total: utils.satoshi(body.total) diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index dfb4cefd..239a6312 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -655,9 +655,10 @@ HTTPServer.prototype._init = function _init() { // Fill TX this.post('/wallet/:id/fill', function(req, res, next, send) { + var id = req.options.id; var tx = req.options.tx; - self.walletdb.fillHistory(tx, function(err) { + self.walletdb.fillHistory(id, tx, function(err) { if (err) return next(err); @@ -755,9 +756,10 @@ HTTPServer.prototype._init = function _init() { // Wallet Coin this.get('/wallet/:id/coin/:hash/:index', function(req, res, next, send) { + var id = req.options.id; var hash = req.options.hash; var index = req.options.index; - self.walletdb.getCoin(hash, index, function(err, coin) { + self.walletdb.getCoin(id, hash, index, function(err, coin) { if (err) return next(err); @@ -780,7 +782,7 @@ HTTPServer.prototype._init = function _init() { return send(404); utils.forEachSerial(txs, function(tx, next) { - self.walletdb.fillHistory(tx, next); + self.walletdb.fillHistory(id, tx, next); }, function(err) { if (err) return next(err); @@ -804,7 +806,7 @@ HTTPServer.prototype._init = function _init() { return send(404); utils.forEachSerial(txs, function(tx, next) { - self.walletdb.fillHistory(tx, next); + self.walletdb.fillHistory(id, tx, next); }, function(err) { if (err) return next(err); @@ -829,7 +831,7 @@ HTTPServer.prototype._init = function _init() { return send(404); utils.forEachSerial(txs, function(tx, next) { - self.walletdb.fillHistory(tx, next); + self.walletdb.fillHistory(id, tx, next); }, function(err) { if (err) return next(err); @@ -854,7 +856,7 @@ HTTPServer.prototype._init = function _init() { return send(404); utils.forEachSerial(txs, function(tx, next) { - self.walletdb.fillHistory(tx, next); + self.walletdb.fillHistory(id, tx, next); }, function(err) { if (err) return next(err); @@ -868,14 +870,16 @@ HTTPServer.prototype._init = function _init() { // Wallet TX this.get('/wallet/:id/tx/:hash', function(req, res, next, send) { - self.walletdb.getTX(req.options.hash, function(err, tx) { + var id = req.options.id; + var hash = req.options.hash; + self.walletdb.getTX(id, hash, function(err, tx) { if (err) return next(err); if (!tx) return send(404); - self.walletdb.fillHistory(tx, function(err) { + self.walletdb.fillHistory(id, tx, function(err) { if (err) return next(err); send(200, tx.toJSON()); diff --git a/lib/bcoin/http/wallet.js b/lib/bcoin/http/wallet.js index 920e1ab0..edabebdd 100644 --- a/lib/bcoin/http/wallet.js +++ b/lib/bcoin/http/wallet.js @@ -53,24 +53,28 @@ utils.inherits(HTTPWallet, EventEmitter); HTTPWallet.prototype._init = function _init() { var self = this; - this.client.on('tx', function(tx, map) { - self.emit('tx', tx, map); + this.client.on('tx', function(details) { + self.emit('tx', details); }); - this.client.on('confirmed', function(tx, map) { - self.emit('confirmed', tx, map); + this.client.on('confirmed', function(details) { + self.emit('confirmed', details); }); - this.client.on('updated', function(tx, map) { - self.emit('updated', tx, map); + this.client.on('unconfirmed', function(tx, details) { + self.emit('unconfirmed', details); }); - this.client.on('balance', function(balance, id) { - self.emit('balance', balance, id); + this.client.on('conflict', function(tx, details) { + self.emit('conflict', details); }); - this.client.on('address', function(receive, change, map) { - self.emit('address', receive, change, map); + this.client.on('balance', function(balance) { + self.emit('balance', balance); + }); + + this.client.on('address', function(receive) { + self.emit('address', receive); }); this.client.on('error', function(err) { diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 20f67d2b..1127c5f5 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -73,7 +73,7 @@ TXDB.prototype.prefix = function prefix(key) { */ TXDB.prototype.emit = function emit(event, tx, info) { - this.db.emit(event, info.id, tx, info); + this.walletdb.emit(event, info.id, tx, info); this.wallet.emit(event, tx, info); }; @@ -1695,7 +1695,7 @@ TXDB.prototype.hasCoin = function hasCoin(hash, index, callback) { TXDB.prototype.getBalance = function getBalance(account, callback) { var self = this; - var balance = new Balance(); + var balance = new Balance(this.wallet.id); if (typeof account === 'function') { callback = account; @@ -1748,7 +1748,7 @@ TXDB.prototype.getBalance = function getBalance(account, callback) { TXDB.prototype.getAccountBalance = function getBalance(account, callback) { var self = this; - var balance = new Balance(); + var balance = new Balance(this.wallet.id); var key, coin; function parse(data) { @@ -1974,7 +1974,8 @@ DetailsMember.prototype.toJSON = function toJSON() { * Balance */ -function Balance() { +function Balance(id) { + this.id = id; this.unconfirmed = 0; this.confirmed = 0; this.total = 0; @@ -2008,6 +2009,7 @@ Balance.prototype.unconfirm = function unconfirm(value) { Balance.prototype.toJSON = function toJSON() { return { + id: this.id, unconfirmed: utils.btc(this.unconfirmed), confirmed: utils.btc(this.confirmed), total: utils.btc(this.total) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index cffbd46a..5e35e0b6 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -51,6 +51,7 @@ function Wallet(db, options) { this.db = db; this.network = db.network; + this.logger = db.logger; this.workerPool = db.workerPool; this.writeLock = new bcoin.locker(this); this.fundLock = new bcoin.locker(this); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 90dead00..04ec92d2 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -1286,6 +1286,16 @@ WalletDB.prototype.getPath = function getPath(id, address, callback) { }); }; +/** + * @see {@link TXDB#toDetails}. + */ + +WalletDB.prototype.toDetails = function toDetails(id, tx, callback) { + this.fetchWallet(id, callback, function(wallet, callback) { + wallet.tx.toDetails(tx, callback); + }); +}; + /** * @see {@link TXDB#getTX}. */ diff --git a/test/http-test.js b/test/http-test.js index 6c5949fa..e8e58466 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -81,19 +81,8 @@ describe('HTTP', function() { }); }); -/* - it('should get internal wallet', function(cb) { - node.walletdb.get('test', function(err, w_) { - assert.ifError(err); - assert(w_); - w = w_; - cb(); - }); - }); -*/ - it('should fill with funds', function(cb) { - var balance, receive, tx; + var balance, receive, details; // Coinbase var t1 = bcoin.mtx() @@ -104,16 +93,16 @@ describe('HTTP', function() { t1.addInput(dummyInput); - wallet.once('balance', function(b, id) { + wallet.once('balance', function(b) { balance = b; }); - wallet.once('address', function(r, c, map) { + wallet.once('address', function(r) { receive = r[0]; }); - wallet.once('tx', function(t, map) { - tx = t; + wallet.once('tx', function(d) { + details = d; }); node.walletdb.addTX(t1, function(err) { @@ -127,8 +116,8 @@ describe('HTTP', function() { assert.equal(balance.confirmed, 0); assert.equal(balance.unconfirmed, 201840); assert.equal(balance.total, 201840); - assert(tx); - assert.equal(tx.hash('hex'), t1.hash('hex')); + assert(details); + assert.equal(details.hash, t1.rhash); cb(); }, 300); }); @@ -160,10 +149,7 @@ describe('HTTP', function() { assert.equal(tx.outputs.length, 2); assert.equal(tx.getOutputValue(), 48190); hash = tx.hash('hex'); - node.walletdb.addTX(tx, function(err) { - assert.ifError(err); - cb(); - }); + cb(); }); });