http client.

This commit is contained in:
Christopher Jeffrey 2016-07-16 15:31:48 -07:00
parent 2d8274b46e
commit a70b615fd2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 44 additions and 78 deletions

View File

@ -176,7 +176,7 @@ HTTPClient.prototype._close = function close(callback) {
* @param {WalletID} id
*/
HTTPClient.prototype.join = function join(id, token, callback) {
HTTPClient.prototype.joinWallet = function joinWallet(id, token, callback) {
if (!this.socket)
return callback();
@ -188,9 +188,9 @@ HTTPClient.prototype.join = function join(id, token, callback) {
* @param {WalletID} id
*/
HTTPClient.prototype.leave = function leave(id, callback) {
HTTPClient.prototype.leaveWallet = function leaveWallet(id, callback) {
if (!this.socket)
return;
return callback();
this.socket.emit('wallet leave', id, callback);
};
@ -199,16 +199,16 @@ HTTPClient.prototype.leave = function leave(id, callback) {
* Listen for events on all wallets.
*/
HTTPClient.prototype.all = function all(token) {
this.join('!all', token);
HTTPClient.prototype.all = function all(token, callback) {
this.joinWallet('!all', token, callback);
};
/**
* Unlisten for events on all wallets.
*/
HTTPClient.prototype.none = function none() {
this.leave('!all');
HTTPClient.prototype.none = function none(callback) {
this.leaveWallet('!all', callback);
};
/**
@ -821,9 +821,6 @@ HTTPClient.prototype.walletSend = function walletSend(id, options, callback) {
options = utils.merge({}, options);
options.outputs = options.outputs || [];
if (options.outputs.length === 0)
options.outputs.push(options);
if (options.rate)
options.rate = utils.btc(options.rate);
@ -891,22 +888,15 @@ HTTPClient.prototype.walletSetPassphrase = function walletSetPassphrase(id, old,
* Create a transaction, fill.
* @param {WalletID} id
* @param {Object} options
* @param {Base58Address} options.address
* @param {Amount} options.value
* @param {Function} callback - Returns [Error, {@link TX}].
*/
HTTPClient.prototype.walletCreate = function walletCreate(id, options, outputs, callback) {
if (typeof outputs === 'function') {
callback = outputs;
outputs = null;
}
HTTPClient.prototype.walletCreate = function walletCreate(id, options, callback) {
options = utils.merge({}, options);
options.outputs = outputs || options.outputs || [];
options.outputs = options.outputs || [];
if (!Array.isArray(options.outputs))
options.outputs = [options.outputs];
if (options.rate)
options.rate = utils.btc(options.rate);
options.outputs = options.outputs.map(function(output) {
return {
@ -1030,32 +1020,27 @@ HTTPClient.prototype.walletZap = function walletZap(id, account, age, callback)
/**
* Add a public account/purpose key to the wallet for multisig.
* @param {WalletID} id
* @param {HDPublicKey[]|Base58String[]} keys - Account (bip44) or
* @param {(String|Number)?} account
* @param {HDPublicKey|Base58String} key - Account (bip44) or
* Purpose (bip45) key (can be in base58 form).
* @param {Function} callback
*/
HTTPClient.prototype.addKey = function addKey(id, account, keys, callback) {
HTTPClient.prototype.addKey = function addKey(id, account, key, callback) {
var options;
if (typeof keys === 'function') {
callback = keys;
keys = account;
if (typeof key === 'function') {
callback = key;
key = account;
account = null;
}
if (!Array.isArray(keys))
keys = [keys];
keys = keys.map(function(key) {
return key.xpubkey || key;
});
options = { account: account, keys: keys };
key = key.xpubkey || key;
options = { account: account, key: key };
callback = utils.ensure(callback);
return this._put('/wallet/' + id + '/key', keys, function(err) {
return this._put('/wallet/' + id + '/key', options, function(err) {
if (err)
return callback(err);
return callback();
@ -1065,32 +1050,27 @@ HTTPClient.prototype.addKey = function addKey(id, account, keys, callback) {
/**
* Remove a public account/purpose key to the wallet for multisig.
* @param {WalletID} id
* @param {HDPublicKey[]|Base58String[]} keys - Account (bip44) or Purpose
* @param {(String|Number)?} account
* @param {HDPublicKey|Base58String} key - Account (bip44) or Purpose
* (bip45) key (can be in base58 form).
* @param {Function} callback
*/
HTTPClient.prototype.removeKey = function removeKey(id, account, keys, callback) {
HTTPClient.prototype.removeKey = function removeKey(id, account, key, callback) {
var options;
if (typeof keys === 'function') {
callback = keys;
keys = account;
if (typeof key === 'function') {
callback = key;
key = account;
account = null;
}
if (!Array.isArray(keys))
keys = [keys];
keys = keys.map(function(key) {
return key.xpubkey || key;
});
options = { account: account, keys: keys };
key = key.xpubkey || key;
options = { account: account, key: key };
callback = utils.ensure(callback);
return this._del('/wallet/' + id + '/key', keys, function(err) {
return this._del('/wallet/' + id + '/key', options, function(err) {
if (err)
return callback(err);
return callback();

View File

@ -613,8 +613,8 @@ HTTPServer.prototype._init = function _init() {
this.put('/wallet/:id/key', function(req, res, next, send) {
var id = req.options.id;
var account = req.options.account;
var keys = req.options.keys;
self.walletdb.addKey(id, account, keys, function(err) {
var key = req.options.key;
self.walletdb.addKey(id, account, key, function(err) {
if (err)
return next(err);
@ -626,8 +626,8 @@ HTTPServer.prototype._init = function _init() {
this.del('/wallet/:id/key', function(req, res, next, send) {
var id = req.options.id;
var account = req.options.account;
var keys = req.options.keys;
self.walletdb.removeKey(id, account, keys, function(err) {
var key = req.options.key;
self.walletdb.removeKey(id, account, key, function(err) {
if (err)
return next(err);

View File

@ -88,34 +88,20 @@ HTTPWallet.prototype._init = function _init() {
HTTPWallet.prototype.open = function open(options, callback) {
var self = this;
if (options.token) {
if (typeof options.token === 'string') {
assert(utils.isHex(options.token), 'API key must be a hex string.');
options.token = new Buffer(options.token, 'hex');
}
assert(Buffer.isBuffer(options.token));
assert(options.token.length === 32, 'API key must be 32 bytes.');
this.id = options.id;
this.client.auth = { username: 'x', password: options.token.toString('hex') };
}
if (Buffer.isBuffer(options.token))
options.token = options.token.toString('hex');
this.client.open(function(err) {
if (err)
return callback(err);
if (options.token) {
self.token = options.token;
self.client.join(options.id, options.token.toString('hex'));
return callback();
}
self.client.createWallet(options, function(err, wallet) {
if (err)
return callback(err);
self.id = wallet.id;
self.client.auth = { username: 'x', password: wallet.token };
self.token = new Buffer(wallet.token, 'hex');
self.client.join(self.id, wallet.token, function(err) {
self.client.joinWallet(self.id, wallet.token, function(err) {
if (err)
return callback(new Error(err.error));
callback(null, wallet);
@ -266,8 +252,8 @@ HTTPWallet.prototype.createAccount = function createAccount(options, callback) {
* @see Wallet#setPassphrase
*/
HTTPWallet.prototype.setPassphrase = function setPassphrase(old, _new, callback) {
return this.client.walletSetPassphrase(this.id, old, _new, callback);
HTTPWallet.prototype.setPassphrase = function setPassphrase(old, new_, callback) {
return this.client.walletSetPassphrase(this.id, old, new_, callback);
};
/**

View File

@ -765,22 +765,22 @@ Wallet.prototype.fund = function fund(tx, options, callback) {
* Build a transaction, fill it with outputs and inputs,
* sort the members according to BIP69, set locktime,
* and sign it (accesses db).
* @param {Object} options - See {@link Wallet#fill options}.
* @param {Object[]} options.outputs - See {@link Script.createOutputScript}.
* @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link MTX#addOutput}.
* @param {Function} callback - Returns [Error, {@link MTX}].
*/
Wallet.prototype.createTX = function createTX(options, callback) {
var self = this;
var outputs = options.outputs || [];
var outputs = options.outputs;
var i, tx;
if (!Array.isArray(outputs) || outputs.length === 0)
return callback(new Error('No outputs.'));
// Create mutable tx
tx = bcoin.mtx();
if (outputs.length === 0)
outputs.push(options);
// Add the outputs
for (i = 0; i < outputs.length; i++) {
try {