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

View File

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

View File

@ -88,34 +88,20 @@ HTTPWallet.prototype._init = function _init() {
HTTPWallet.prototype.open = function open(options, callback) { HTTPWallet.prototype.open = function open(options, callback) {
var self = this; var self = this;
if (options.token) { if (Buffer.isBuffer(options.token))
if (typeof options.token === 'string') { options.token = options.token.toString('hex');
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') };
}
this.client.open(function(err) { this.client.open(function(err) {
if (err) if (err)
return callback(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) { self.client.createWallet(options, function(err, wallet) {
if (err) if (err)
return callback(err); return callback(err);
self.id = wallet.id; self.id = wallet.id;
self.client.auth = { username: 'x', password: wallet.token }; self.client.auth = { username: 'x', password: wallet.token };
self.token = new Buffer(wallet.token, 'hex'); 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) if (err)
return callback(new Error(err.error)); return callback(new Error(err.error));
callback(null, wallet); callback(null, wallet);
@ -266,8 +252,8 @@ HTTPWallet.prototype.createAccount = function createAccount(options, callback) {
* @see Wallet#setPassphrase * @see Wallet#setPassphrase
*/ */
HTTPWallet.prototype.setPassphrase = function setPassphrase(old, _new, callback) { HTTPWallet.prototype.setPassphrase = function setPassphrase(old, new_, callback) {
return this.client.walletSetPassphrase(this.id, 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, * 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 sign it (accesses db).
* @param {Object} options - See {@link Wallet#fill options}. * @param {Object} options - See {@link Wallet#fund options}.
* @param {Object[]} options.outputs - See {@link Script.createOutputScript}. * @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) {
var self = this; var self = this;
var outputs = options.outputs || []; var outputs = options.outputs;
var i, tx; var i, tx;
if (!Array.isArray(outputs) || outputs.length === 0)
return callback(new Error('No outputs.'));
// Create mutable tx // Create mutable tx
tx = bcoin.mtx(); tx = bcoin.mtx();
if (outputs.length === 0)
outputs.push(options);
// Add the outputs // Add the outputs
for (i = 0; i < outputs.length; i++) { for (i = 0; i < outputs.length; i++) {
try { try {