From 2d83ce95795c50860356440c5fe723fc6cee9600 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 9 Apr 2016 04:17:33 -0700 Subject: [PATCH] wallet fixes. --- lib/bcoin/http/server.js | 37 ++++++++++++++++++++++++++++++------- lib/bcoin/spvnode.js | 6 ++++++ lib/bcoin/wallet.js | 25 +++++++++++++++---------- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/lib/bcoin/http/server.js b/lib/bcoin/http/server.js index c342749d..49c65bb4 100644 --- a/lib/bcoin/http/server.js +++ b/lib/bcoin/http/server.js @@ -108,8 +108,21 @@ NodeServer.prototype._init = function _init() { options.address = params.address; } - if (params.value) - options.value = utils.satoshi(params.value); + if (Array.isArray(params.outputs)) { + options.outputs = params.outputs.map(function(output) { + return { + address: output.address, + script: decodeScript(output.script), + value: utils.satoshi(output.value) + }; + }); + } else if (options.value) { + options.outputs = [{ + address: params.address, + script: decodeScript(params.script), + value: utils.satoshi(params.value) + }]; + } if (params.addresses) { if (typeof params.addresses === 'string') @@ -153,6 +166,14 @@ NodeServer.prototype._init = function _init() { next(); }); + function decodeScript(script) { + if (!script) + return; + if (typeof script === 'string') + return new bcoin.script(new Buffer(script, 'hex')); + return new bcoin.script(script); + } + this.get('/', function(req, res, next, send) { send(200, { version: constants.userAgent, @@ -295,6 +316,11 @@ NodeServer.prototype._init = function _init() { this.post('/wallet/:id/send', function(req, res, next, send) { var id = req.options.id; var passphrase = req.options.passphrase; + var outputs = req.options.outputs; + + if (!Array.isArray(outputs)) + return send(400); + self.walletdb.get(id, passphrase, function(err, wallet) { if (err) return next(err); @@ -302,10 +328,7 @@ NodeServer.prototype._init = function _init() { if (!wallet) return send(404); - wallet.createTX({ - address: req.options.address, - value: req.options.value - }, function(err, tx) { + wallet.createTX(outputs, function(err, tx) { if (err) { wallet.destroy(); return next(err); @@ -313,7 +336,7 @@ NodeServer.prototype._init = function _init() { wallet.destroy(); - self.node.sendTX(tx, false, function(err) { + self.node.sendTX(tx, function(err) { if (err) return next(err); diff --git a/lib/bcoin/spvnode.js b/lib/bcoin/spvnode.js index 1d553c07..eb0ab94b 100644 --- a/lib/bcoin/spvnode.js +++ b/lib/bcoin/spvnode.js @@ -151,6 +151,12 @@ SPVNode.prototype.sendTX = function sendTX(item, wait, callback) { callback = wait; wait = null; } + + if (!wait) { + this.pool.sendTX(item); + return utils.nextTick(callback); + } + return this.pool.sendTX(item, callback); }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 960b0871..0ef2bb57 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -599,8 +599,13 @@ Wallet.prototype.createTX = function createTX(options, outputs, callback) { tx = bcoin.mtx(); // Add the outputs - for (i = 0; i < outputs.length; i++) - tx.addOutput(outputs[i]); + for (i = 0; i < outputs.length; i++) { + try { + tx.addOutput(outputs[i]); + } catch (e) { + return utils.asyncify(callback)(e); + } + } // Fill the inputs with unspents this.fill(tx, options, function(err) { @@ -916,56 +921,56 @@ Wallet.prototype.sign = function sign(tx, index, type) { Wallet.prototype.addTX = function addTX(tx, callback) { if (!this.provider || !this.provider.addTX) - return callback(new Error('No transaction pool available.')); + return utils.asyncify(callback)(new Error('No transaction pool available.')); return this.provider.addTX(tx, callback); }; Wallet.prototype.getAll = function getAll(callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getAll(callback); }; Wallet.prototype.getCoins = function getCoins(callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getCoins(callback); }; Wallet.prototype.getPending = function getPending(callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getPending(callback); }; Wallet.prototype.getBalance = function getBalance(callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getBalance(callback); }; Wallet.prototype.getLastTime = function getLastTime(callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getLastTime(callback); }; Wallet.prototype.getLast = function getLast(limit, callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getLast(limit, callback); }; Wallet.prototype.getTimeRange = function getTimeRange(options, callback) { if (!this.provider) - return callback(new Error('No wallet provider available.')); + return utils.asyncify(callback)(new Error('No wallet provider available.')); return this.provider.getTimeRange(options, callback); };