wallet/http: fix events.
This commit is contained in:
parent
4b9753d3c3
commit
cbed60e900
@ -89,61 +89,40 @@ HTTPClient.prototype._open = function _open(callback) {
|
|||||||
self.emit('error', new Error('Wrong network.'));
|
self.emit('error', new Error('Wrong network.'));
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('wallet tx', function(tx, map) {
|
this.socket.on('wallet tx', function(details) {
|
||||||
try {
|
self.emit('tx', details);
|
||||||
tx = bcoin.tx.fromJSON(tx);
|
|
||||||
} catch (e) {
|
|
||||||
return self.emit('error', e);
|
|
||||||
}
|
|
||||||
self.emit('tx', tx, map);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('wallet confirmed', function(tx, map) {
|
this.socket.on('wallet confirmed', function(details) {
|
||||||
try {
|
self.emit('confirmed', details);
|
||||||
tx = bcoin.tx.fromJSON(tx);
|
|
||||||
} catch (e) {
|
|
||||||
return self.emit('error', e);
|
|
||||||
}
|
|
||||||
self.emit('confirmed', tx, map);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('wallet updated', function(tx, map) {
|
this.socket.on('wallet unconfirmed', function(details) {
|
||||||
try {
|
self.emit('unconfirmed', details);
|
||||||
tx = bcoin.tx.fromJSON(tx);
|
|
||||||
} catch (e) {
|
|
||||||
return self.emit('error', e);
|
|
||||||
}
|
|
||||||
self.emit('updated', tx, map);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
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) {
|
receive = receive.map(function(address) {
|
||||||
return bcoin.keyring.fromJSON(address);
|
return bcoin.keyring.fromJSON(address);
|
||||||
});
|
});
|
||||||
change = change.map(function(address) {
|
self.emit('address', receive);
|
||||||
return bcoin.keyring.fromJSON(address);
|
|
||||||
});
|
|
||||||
self.emit('address', receive, change, map);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.socket.on('wallet balance', function(balance, id) {
|
this.socket.on('wallet balance', function(balance) {
|
||||||
self.emit('balance', {
|
self.emit('balance', {
|
||||||
|
id: balance.id,
|
||||||
confirmed: utils.satoshi(balance.confirmed),
|
confirmed: utils.satoshi(balance.confirmed),
|
||||||
unconfirmed: utils.satoshi(balance.unconfirmed),
|
unconfirmed: utils.satoshi(balance.unconfirmed),
|
||||||
total: utils.satoshi(balance.total)
|
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() {
|
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(new Error('Not found.'));
|
||||||
|
|
||||||
return callback(null, {
|
return callback(null, {
|
||||||
|
id: body.id,
|
||||||
confirmed: utils.satoshi(body.confirmed),
|
confirmed: utils.satoshi(body.confirmed),
|
||||||
unconfirmed: utils.satoshi(body.unconfirmed),
|
unconfirmed: utils.satoshi(body.unconfirmed),
|
||||||
total: utils.satoshi(body.total)
|
total: utils.satoshi(body.total)
|
||||||
|
|||||||
@ -655,9 +655,10 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
|
|
||||||
// Fill TX
|
// Fill TX
|
||||||
this.post('/wallet/:id/fill', function(req, res, next, send) {
|
this.post('/wallet/:id/fill', function(req, res, next, send) {
|
||||||
|
var id = req.options.id;
|
||||||
var tx = req.options.tx;
|
var tx = req.options.tx;
|
||||||
|
|
||||||
self.walletdb.fillHistory(tx, function(err) {
|
self.walletdb.fillHistory(id, tx, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
@ -755,9 +756,10 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
|
|
||||||
// Wallet Coin
|
// Wallet Coin
|
||||||
this.get('/wallet/:id/coin/:hash/:index', function(req, res, next, send) {
|
this.get('/wallet/:id/coin/:hash/:index', function(req, res, next, send) {
|
||||||
|
var id = req.options.id;
|
||||||
var hash = req.options.hash;
|
var hash = req.options.hash;
|
||||||
var index = req.options.index;
|
var index = req.options.index;
|
||||||
self.walletdb.getCoin(hash, index, function(err, coin) {
|
self.walletdb.getCoin(id, hash, index, function(err, coin) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
@ -780,7 +782,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return send(404);
|
return send(404);
|
||||||
|
|
||||||
utils.forEachSerial(txs, function(tx, next) {
|
utils.forEachSerial(txs, function(tx, next) {
|
||||||
self.walletdb.fillHistory(tx, next);
|
self.walletdb.fillHistory(id, tx, next);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -804,7 +806,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return send(404);
|
return send(404);
|
||||||
|
|
||||||
utils.forEachSerial(txs, function(tx, next) {
|
utils.forEachSerial(txs, function(tx, next) {
|
||||||
self.walletdb.fillHistory(tx, next);
|
self.walletdb.fillHistory(id, tx, next);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -829,7 +831,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return send(404);
|
return send(404);
|
||||||
|
|
||||||
utils.forEachSerial(txs, function(tx, next) {
|
utils.forEachSerial(txs, function(tx, next) {
|
||||||
self.walletdb.fillHistory(tx, next);
|
self.walletdb.fillHistory(id, tx, next);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -854,7 +856,7 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
return send(404);
|
return send(404);
|
||||||
|
|
||||||
utils.forEachSerial(txs, function(tx, next) {
|
utils.forEachSerial(txs, function(tx, next) {
|
||||||
self.walletdb.fillHistory(tx, next);
|
self.walletdb.fillHistory(id, tx, next);
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
@ -868,14 +870,16 @@ HTTPServer.prototype._init = function _init() {
|
|||||||
|
|
||||||
// Wallet TX
|
// Wallet TX
|
||||||
this.get('/wallet/:id/tx/:hash', function(req, res, next, send) {
|
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)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
|
|
||||||
if (!tx)
|
if (!tx)
|
||||||
return send(404);
|
return send(404);
|
||||||
|
|
||||||
self.walletdb.fillHistory(tx, function(err) {
|
self.walletdb.fillHistory(id, tx, function(err) {
|
||||||
if (err)
|
if (err)
|
||||||
return next(err);
|
return next(err);
|
||||||
send(200, tx.toJSON());
|
send(200, tx.toJSON());
|
||||||
|
|||||||
@ -53,24 +53,28 @@ utils.inherits(HTTPWallet, EventEmitter);
|
|||||||
HTTPWallet.prototype._init = function _init() {
|
HTTPWallet.prototype._init = function _init() {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
this.client.on('tx', function(tx, map) {
|
this.client.on('tx', function(details) {
|
||||||
self.emit('tx', tx, map);
|
self.emit('tx', details);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('confirmed', function(tx, map) {
|
this.client.on('confirmed', function(details) {
|
||||||
self.emit('confirmed', tx, map);
|
self.emit('confirmed', details);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('updated', function(tx, map) {
|
this.client.on('unconfirmed', function(tx, details) {
|
||||||
self.emit('updated', tx, map);
|
self.emit('unconfirmed', details);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('balance', function(balance, id) {
|
this.client.on('conflict', function(tx, details) {
|
||||||
self.emit('balance', balance, id);
|
self.emit('conflict', details);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('address', function(receive, change, map) {
|
this.client.on('balance', function(balance) {
|
||||||
self.emit('address', receive, change, map);
|
self.emit('balance', balance);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.client.on('address', function(receive) {
|
||||||
|
self.emit('address', receive);
|
||||||
});
|
});
|
||||||
|
|
||||||
this.client.on('error', function(err) {
|
this.client.on('error', function(err) {
|
||||||
|
|||||||
@ -73,7 +73,7 @@ TXDB.prototype.prefix = function prefix(key) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
TXDB.prototype.emit = function emit(event, tx, info) {
|
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);
|
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) {
|
TXDB.prototype.getBalance = function getBalance(account, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var balance = new Balance();
|
var balance = new Balance(this.wallet.id);
|
||||||
|
|
||||||
if (typeof account === 'function') {
|
if (typeof account === 'function') {
|
||||||
callback = account;
|
callback = account;
|
||||||
@ -1748,7 +1748,7 @@ TXDB.prototype.getBalance = function getBalance(account, callback) {
|
|||||||
|
|
||||||
TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
TXDB.prototype.getAccountBalance = function getBalance(account, callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
var balance = new Balance();
|
var balance = new Balance(this.wallet.id);
|
||||||
var key, coin;
|
var key, coin;
|
||||||
|
|
||||||
function parse(data) {
|
function parse(data) {
|
||||||
@ -1974,7 +1974,8 @@ DetailsMember.prototype.toJSON = function toJSON() {
|
|||||||
* Balance
|
* Balance
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function Balance() {
|
function Balance(id) {
|
||||||
|
this.id = id;
|
||||||
this.unconfirmed = 0;
|
this.unconfirmed = 0;
|
||||||
this.confirmed = 0;
|
this.confirmed = 0;
|
||||||
this.total = 0;
|
this.total = 0;
|
||||||
@ -2008,6 +2009,7 @@ Balance.prototype.unconfirm = function unconfirm(value) {
|
|||||||
|
|
||||||
Balance.prototype.toJSON = function toJSON() {
|
Balance.prototype.toJSON = function toJSON() {
|
||||||
return {
|
return {
|
||||||
|
id: this.id,
|
||||||
unconfirmed: utils.btc(this.unconfirmed),
|
unconfirmed: utils.btc(this.unconfirmed),
|
||||||
confirmed: utils.btc(this.confirmed),
|
confirmed: utils.btc(this.confirmed),
|
||||||
total: utils.btc(this.total)
|
total: utils.btc(this.total)
|
||||||
|
|||||||
@ -51,6 +51,7 @@ function Wallet(db, options) {
|
|||||||
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.network = db.network;
|
this.network = db.network;
|
||||||
|
this.logger = db.logger;
|
||||||
this.workerPool = db.workerPool;
|
this.workerPool = db.workerPool;
|
||||||
this.writeLock = new bcoin.locker(this);
|
this.writeLock = new bcoin.locker(this);
|
||||||
this.fundLock = new bcoin.locker(this);
|
this.fundLock = new bcoin.locker(this);
|
||||||
|
|||||||
@ -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}.
|
* @see {@link TXDB#getTX}.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -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) {
|
it('should fill with funds', function(cb) {
|
||||||
var balance, receive, tx;
|
var balance, receive, details;
|
||||||
|
|
||||||
// Coinbase
|
// Coinbase
|
||||||
var t1 = bcoin.mtx()
|
var t1 = bcoin.mtx()
|
||||||
@ -104,16 +93,16 @@ describe('HTTP', function() {
|
|||||||
|
|
||||||
t1.addInput(dummyInput);
|
t1.addInput(dummyInput);
|
||||||
|
|
||||||
wallet.once('balance', function(b, id) {
|
wallet.once('balance', function(b) {
|
||||||
balance = b;
|
balance = b;
|
||||||
});
|
});
|
||||||
|
|
||||||
wallet.once('address', function(r, c, map) {
|
wallet.once('address', function(r) {
|
||||||
receive = r[0];
|
receive = r[0];
|
||||||
});
|
});
|
||||||
|
|
||||||
wallet.once('tx', function(t, map) {
|
wallet.once('tx', function(d) {
|
||||||
tx = t;
|
details = d;
|
||||||
});
|
});
|
||||||
|
|
||||||
node.walletdb.addTX(t1, function(err) {
|
node.walletdb.addTX(t1, function(err) {
|
||||||
@ -127,8 +116,8 @@ describe('HTTP', function() {
|
|||||||
assert.equal(balance.confirmed, 0);
|
assert.equal(balance.confirmed, 0);
|
||||||
assert.equal(balance.unconfirmed, 201840);
|
assert.equal(balance.unconfirmed, 201840);
|
||||||
assert.equal(balance.total, 201840);
|
assert.equal(balance.total, 201840);
|
||||||
assert(tx);
|
assert(details);
|
||||||
assert.equal(tx.hash('hex'), t1.hash('hex'));
|
assert.equal(details.hash, t1.rhash);
|
||||||
cb();
|
cb();
|
||||||
}, 300);
|
}, 300);
|
||||||
});
|
});
|
||||||
@ -160,10 +149,7 @@ describe('HTTP', function() {
|
|||||||
assert.equal(tx.outputs.length, 2);
|
assert.equal(tx.outputs.length, 2);
|
||||||
assert.equal(tx.getOutputValue(), 48190);
|
assert.equal(tx.getOutputValue(), 48190);
|
||||||
hash = tx.hash('hex');
|
hash = tx.hash('hex');
|
||||||
node.walletdb.addTX(tx, function(err) {
|
cb();
|
||||||
assert.ifError(err);
|
|
||||||
cb();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user