wallet: add wallet.send.

This commit is contained in:
Christopher Jeffrey 2016-08-05 13:38:27 -07:00
parent 8fe79fc7e7
commit ce7f300260
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
6 changed files with 73 additions and 33 deletions

View File

@ -241,6 +241,13 @@ Fullnode.prototype._init = function _init() {
this.miner.on('block', function(block) { this.miner.on('block', function(block) {
self.pool.broadcast(block.toInv()); self.pool.broadcast(block.toInv());
}); });
this.walletdb.on('send', function(tx) {
self.sendTX(tx, function(err) {
if (err)
self.emit('error', err);
});
});
}; };
/** /**

View File

@ -2560,21 +2560,7 @@ RPC.prototype._send = function _send(account, address, amount, subtractFee, call
}] }]
}; };
this.wallet.createTX(options, function(err, tx) { this.wallet.send(options, callback);
if (err)
return callback(err);
self.wallet.sign(tx, function(err) {
if (err)
return callback(err);
self.node.sendTX(tx, function(err) {
if (err)
return callback(err);
callback(null, tx);
});
});
});
}; };
RPC.prototype.sendfrom = function sendfrom(args, callback) { RPC.prototype.sendfrom = function sendfrom(args, callback) {

View File

@ -597,21 +597,11 @@ HTTPServer.prototype._init = function _init() {
var id = req.options.id; var id = req.options.id;
var options = req.options; var options = req.options;
self.walletdb.createTX(id, options, function(err, tx) { self.walletdb.send(id, options, function(err, tx) {
if (err) if (err)
return next(err); return next(err);
self.walletdb.sign(id, tx, options, function(err) { send(200, tx.toJSON());
if (err)
return next(err);
self.node.sendTX(tx, function(err) {
if (err)
return next(err);
send(200, tx.toJSON());
});
});
}); });
}); });

View File

@ -143,6 +143,13 @@ SPVNode.prototype._init = function _init() {
this.walletdb.on('save address', function(address) { this.walletdb.on('save address', function(address) {
self.pool.watch(address.getHash()); self.pool.watch(address.getHash());
}); });
this.walletdb.on('send', function(tx) {
self.sendTX(tx, function(err) {
if (err)
self.emit('error', err);
});
});
}; };
/** /**

View File

@ -52,7 +52,7 @@ function Wallet(db, options) {
this.network = db.network; this.network = db.network;
this.workerPool = db.workerPool; this.workerPool = db.workerPool;
this.writeLock = new bcoin.locker(this); this.writeLock = new bcoin.locker(this);
this.fillLock = new bcoin.locker(this); this.fundLock = new bcoin.locker(this);
this.id = null; this.id = null;
this.master = null; this.master = null;
@ -687,7 +687,7 @@ Wallet.prototype.getPath = function getPath(address, callback) {
* fee from existing outputs rather than adding more inputs. * fee from existing outputs rather than adding more inputs.
*/ */
Wallet.prototype.fund = function fund(tx, options, callback) { Wallet.prototype.fund = function fund(tx, options, callback, force) {
var self = this; var self = this;
var unlock, rate; var unlock, rate;
@ -701,7 +701,7 @@ Wallet.prototype.fund = function fund(tx, options, callback) {
// We use a lock here to ensure we // We use a lock here to ensure we
// don't end up double spending coins. // don't end up double spending coins.
unlock = this.fillLock.lock(fund, [tx, options, callback]); unlock = this.fundLock.lock(fund, [tx, options, callback], force);
if (!unlock) if (!unlock)
return; return;
@ -764,13 +764,13 @@ Wallet.prototype.fund = function fund(tx, options, callback) {
/** /**
* Build a transaction, fill it with outputs and inputs, * Build a transaction, fill it with outputs and inputs,
* sort the members according to BIP69, set locktime, * sort the members according to BIP69, set locktime,
* and sign it (accesses db). * and template it.
* @param {Object} options - See {@link Wallet#fund options}. * @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}. * @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @param {Function} callback - Returns [Error, {@link MTX}]. * @param {Function} callback - Returns [Error, {@link MTX}].
*/ */
Wallet.prototype.createTX = function createTX(options, callback) { Wallet.prototype.createTX = function createTX(options, callback, force) {
var self = this; var self = this;
var outputs = options.outputs; var outputs = options.outputs;
var i, tx; var i, tx;
@ -821,7 +821,51 @@ Wallet.prototype.createTX = function createTX(options, callback) {
return callback(null, tx); return callback(null, tx);
}); });
}); }, force);
};
/**
* Build a transaction, fill it with outputs and inputs,
* sort the members according to BIP69, set locktime,
* sign and broadcast. Doing this all in one go prevents
* coins from being double spent.
* @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @param {Function} callback - Returns [Error, {@link TX}].
*/
Wallet.prototype.send = function send(options, callback) {
var self = this;
var unlock = this.fundLock.lock(send, [options, callback]);
if (!unlock)
return;
callback = utils.wrap(callback, unlock);
this.createTX(options, function(err, tx) {
if (err)
return callback(err);
self.sign(tx, function(err) {
if (err)
return callback(err);
if (!tx.isSigned())
return callback(new Error('TX could not be fully signed.'));
tx = tx.toTX();
self.addTX(tx, function(err) {
if (err)
return callback(err);
self.db.emit('send', tx);
return callback(null, tx);
});
});
}, true);
}; };
/** /**

View File

@ -1407,6 +1407,12 @@ WalletDB.prototype.createTX = function createTX(id, options, callback) {
}); });
}; };
WalletDB.prototype.send = function send(id, options, callback) {
this.fetchWallet(id, callback, function(wallet, callback) {
wallet.send(options, callback);
});
};
WalletDB.prototype.addKey = function addKey(id, name, key, callback) { WalletDB.prototype.addKey = function addKey(id, name, key, callback) {
this.fetchWallet(id, callback, function(wallet, callback) { this.fetchWallet(id, callback, function(wallet, callback) {
wallet.addKey(name, key, callback); wallet.addKey(name, key, callback);