wallet/http: fix events.

This commit is contained in:
Christopher Jeffrey 2016-08-12 18:03:47 -07:00
parent 4b9753d3c3
commit cbed60e900
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 70 additions and 83 deletions

View File

@ -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)

View File

@ -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());

View File

@ -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) {

View File

@ -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)

View File

@ -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);

View File

@ -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}.
*/

View File

@ -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();
});
});