wallet fixes.
This commit is contained in:
parent
800d17278e
commit
2d83ce9579
@ -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);
|
||||
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
|
||||
@ -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);
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user