You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1192 lines
30 KiB
1192 lines
30 KiB
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>JSDoc: Source: bcoin/http/client.js</title>
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
<!--[if lt IE 9]>
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
<![endif]-->
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="main">
|
|
|
|
<h1 class="page-title">Source: bcoin/http/client.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
<article>
|
|
<pre class="prettyprint source linenums"><code>/*!
|
|
* client.js - http client for wallets
|
|
* Copyright (c) 2014-2015, Fedor Indutny (MIT License)
|
|
* Copyright (c) 2014-2016, Christopher Jeffrey (MIT License).
|
|
* https://github.com/indutny/bcoin
|
|
*/
|
|
|
|
var bcoin = require('../env');
|
|
var EventEmitter = require('events').EventEmitter;
|
|
var utils = require('../utils');
|
|
var assert = utils.assert;
|
|
var request = require('./request');
|
|
|
|
/**
|
|
* BCoin HTTP client.
|
|
* @exports HTTPClient
|
|
* @constructor
|
|
* @param {String} uri
|
|
* @param {Object?} options
|
|
*/
|
|
|
|
function HTTPClient(options) {
|
|
if (!(this instanceof HTTPClient))
|
|
return new HTTPClient(options);
|
|
|
|
if (!options)
|
|
options = {};
|
|
|
|
if (typeof options === 'string')
|
|
options = { uri: options };
|
|
|
|
EventEmitter.call(this);
|
|
|
|
this.uri = options.uri;
|
|
this.loaded = false;
|
|
this.id = null;
|
|
this.options = options;
|
|
this.network = bcoin.network.get(options.network);
|
|
this._init();
|
|
}
|
|
|
|
utils.inherits(HTTPClient, EventEmitter);
|
|
|
|
HTTPClient.prototype._init = function _init() {
|
|
var self = this;
|
|
var IOClient;
|
|
|
|
try {
|
|
IOClient = require('socket.io-client');
|
|
} catch (e) {
|
|
;
|
|
}
|
|
|
|
if (!IOClient)
|
|
return;
|
|
|
|
this.socket = new IOClient(this.uri);
|
|
|
|
this.socket.on('error', function(err) {
|
|
self.emit('error', err);
|
|
});
|
|
|
|
this.socket.on('connect', function() {
|
|
self.socket.on('version', function(info) {
|
|
bcoin.debug('Connected to bcoin server: %s (%s)',
|
|
info.version, info.network);
|
|
assert(info.network === self.network.type, 'Wrong network.');
|
|
});
|
|
|
|
self.socket.on('tx', function(tx, map) {
|
|
try {
|
|
tx = bcoin.tx.fromJSON(tx);
|
|
} catch (e) {
|
|
return self.emit('error', e);
|
|
}
|
|
self.emit('tx', tx, map);
|
|
});
|
|
|
|
self.socket.on('confirmed', function(tx, map) {
|
|
try {
|
|
tx = bcoin.tx.fromJSON(tx);
|
|
} catch (e) {
|
|
return self.emit('error', e);
|
|
}
|
|
self.emit('confirmed', tx, map);
|
|
});
|
|
|
|
self.socket.on('updated', function(tx, map) {
|
|
try {
|
|
tx = bcoin.tx.fromJSON(tx);
|
|
} catch (e) {
|
|
return self.emit('error', e);
|
|
}
|
|
self.emit('updated', tx, map);
|
|
});
|
|
|
|
self.socket.on('balance', function(balance, id) {
|
|
self.emit('balance', {
|
|
confirmed: utils.satoshi(balance.confirmed),
|
|
unconfirmed: utils.satoshi(balance.unconfirmed),
|
|
total: utils.satoshi(balance.total)
|
|
}, id);
|
|
});
|
|
|
|
self.socket.on('address', function(receive, change, map) {
|
|
receive = receive.map(function(address) {
|
|
return bcoin.keyring.fromJSON(address);
|
|
});
|
|
change = change.map(function(address) {
|
|
return bcoin.keyring.fromJSON(address);
|
|
});
|
|
self.emit('address', receive, change, map);
|
|
});
|
|
|
|
self.socket.on('balances', function(json) {
|
|
var balances = {};
|
|
Object.keys(json).forEach(function(id) {
|
|
balances[id] = {
|
|
confirmed: utils.satoshi(json[id].confirmed),
|
|
unconfirmed: utils.satoshi(json[id].unconfirmed),
|
|
total: utils.satoshi(json[id].total)
|
|
};
|
|
});
|
|
self.emit('balances', balances);
|
|
});
|
|
|
|
self.loaded = true;
|
|
self.emit('open');
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Open the client, wait for the socket to load.
|
|
* @param {Function} callback
|
|
*/
|
|
|
|
HTTPClient.prototype.open = function open(callback) {
|
|
if (this.loaded)
|
|
return utils.nextTick(callback);
|
|
|
|
this.once('open', callback);
|
|
};
|
|
|
|
/**
|
|
* Listen for events on wallet id.
|
|
* @param {WalletID} id
|
|
*/
|
|
|
|
HTTPClient.prototype.join = function join(id) {
|
|
if (!this.socket)
|
|
return;
|
|
|
|
this.socket.emit('join', id);
|
|
};
|
|
|
|
/**
|
|
* Unlisten for events on wallet id.
|
|
* @param {WalletID} id
|
|
*/
|
|
|
|
HTTPClient.prototype.leave = function leave(id) {
|
|
if (!this.socket)
|
|
return;
|
|
|
|
this.socket.emit('leave', id);
|
|
};
|
|
|
|
/**
|
|
* Listen for events on all wallets.
|
|
*/
|
|
|
|
HTTPClient.prototype.all = function all() {
|
|
this.join('!all');
|
|
};
|
|
|
|
/**
|
|
* Unlisten for events on all wallets.
|
|
*/
|
|
|
|
HTTPClient.prototype.none = function none() {
|
|
this.leave('!all');
|
|
};
|
|
|
|
/**
|
|
* Close the client, wait for the socket to close.
|
|
* @method
|
|
* @param {Function} callback
|
|
*/
|
|
|
|
HTTPClient.prototype.close =
|
|
HTTPClient.prototype.destroy = function destroy(callback) {
|
|
callback = utils.ensure(callback);
|
|
|
|
if (!this.socket)
|
|
return utils.nextTick(callback);
|
|
|
|
this.socket.destroy();
|
|
this.socket = null;
|
|
|
|
return utils.nextTick(callback);
|
|
};
|
|
|
|
/**
|
|
* Make an http request to endpoint.
|
|
* @private
|
|
* @param {String} method
|
|
* @param {String} endpoint - Path.
|
|
* @param {Object} json - Body or query depending on method.
|
|
* @param {Function} callback - Returns [Error, Object?].
|
|
*/
|
|
|
|
HTTPClient.prototype._request = function _request(method, endpoint, json, callback) {
|
|
var self = this;
|
|
var query;
|
|
var networkType;
|
|
|
|
if (!callback) {
|
|
callback = json;
|
|
json = null;
|
|
}
|
|
|
|
if (json && method === 'get') {
|
|
query = json;
|
|
json = true;
|
|
}
|
|
|
|
request({
|
|
method: method,
|
|
uri: this.uri + endpoint,
|
|
query: query,
|
|
json: json,
|
|
expect: 'json'
|
|
}, function(err, res, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
networkType = res.headers['x-bcoin-network'];
|
|
assert(networkType === self.network.type, 'Wrong network.');
|
|
self.network.updateHeight(+res.headers['x-bcoin-height']);
|
|
|
|
if (res.statusCode === 404)
|
|
return callback();
|
|
|
|
if (!body)
|
|
return callback(new Error('No body.'));
|
|
|
|
if (res.statusCode !== 200) {
|
|
if (body.error)
|
|
return callback(new Error(body.error));
|
|
return callback(new Error('Status code: ' + res.statusCode));
|
|
}
|
|
|
|
try {
|
|
return callback(null, body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Make a GET http request to endpoint.
|
|
* @private
|
|
* @param {String} endpoint - Path.
|
|
* @param {Object} json - Querystring.
|
|
* @param {Function} callback - Returns [Error, Object?].
|
|
*/
|
|
|
|
HTTPClient.prototype._get = function _get(endpoint, json, callback) {
|
|
return this._request('get', endpoint, json, callback);
|
|
};
|
|
|
|
/**
|
|
* Make a POST http request to endpoint.
|
|
* @private
|
|
* @param {String} endpoint - Path.
|
|
* @param {Object} json - Body.
|
|
* @param {Function} callback - Returns [Error, Object?].
|
|
*/
|
|
|
|
HTTPClient.prototype._post = function _post(endpoint, json, callback) {
|
|
return this._request('post', endpoint, json, callback);
|
|
};
|
|
|
|
/**
|
|
* Make a PUT http request to endpoint.
|
|
* @private
|
|
* @param {String} endpoint - Path.
|
|
* @param {Object} json - Body.
|
|
* @param {Function} callback - Returns [Error, Object?].
|
|
*/
|
|
|
|
HTTPClient.prototype._put = function _put(endpoint, json, callback) {
|
|
return this._request('put', endpoint, json, callback);
|
|
};
|
|
|
|
/**
|
|
* Make a DELETE http request to endpoint.
|
|
* @private
|
|
* @param {String} endpoint - Path.
|
|
* @param {Object} json - Body.
|
|
* @param {Function} callback - Returns [Error, Object?].
|
|
*/
|
|
|
|
HTTPClient.prototype._del = function _del(endpoint, json, callback) {
|
|
return this._request('delete', endpoint, json, callback);
|
|
};
|
|
|
|
/**
|
|
* Request the raw wallet JSON (will create wallet if it does not exist).
|
|
* @private
|
|
* @param {Object} options - See {@link Wallet}.
|
|
* @param {Function} callback - Returns [Error, Object].
|
|
*/
|
|
|
|
HTTPClient.prototype.createWallet = function createWallet(options, callback) {
|
|
return this._post('/wallet', options, callback);
|
|
};
|
|
|
|
/**
|
|
* Get the raw wallet JSON.
|
|
* @private
|
|
* @param {WalletID} id
|
|
* @param {Function} callback - Returns [Error, Object].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWallet = function getWallet(id, callback) {
|
|
return this._get('/wallet/' + id, callback);
|
|
};
|
|
|
|
/**
|
|
* Get wallet transaction history.
|
|
* @param {WalletID} id
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletHistory = function getWalletHistory(id, account, callback) {
|
|
var options;
|
|
|
|
if (typeof account === 'function') {
|
|
callback = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account };
|
|
|
|
return this._get('/wallet/' + id + '/tx/history', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.tx.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get wallet coins.
|
|
* @param {WalletID} id
|
|
* @param {Function} callback - Returns [Error, {@link Coin}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletCoins = function getWalletCoins(id, account, callback) {
|
|
var options;
|
|
|
|
if (typeof account === 'function') {
|
|
callback = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account };
|
|
|
|
return this._get('/wallet/' + id + '/coin', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.coin.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get all unconfirmed transactions.
|
|
* @param {WalletID} id
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletUnconfirmed = function getUnconfirmed(id, account, callback) {
|
|
var options;
|
|
|
|
if (typeof account === 'function') {
|
|
callback = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account };
|
|
|
|
return this._get('/wallet/' + id + '/tx/unconfirmed', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.tx.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Calculate wallet balance.
|
|
* @param {WalletID} id
|
|
* @param {Function} callback - Returns [Error, {@link Balance}].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletBalance = function getBalance(id, account, callback) {
|
|
var options;
|
|
|
|
if (typeof account === 'function') {
|
|
callback = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account };
|
|
|
|
return this._get('/wallet/' + id + '/balance', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(new Error('Not found.'));
|
|
|
|
return callback(null, {
|
|
confirmed: utils.satoshi(body.confirmed),
|
|
unconfirmed: utils.satoshi(body.unconfirmed),
|
|
total: utils.satoshi(body.total)
|
|
});
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get last N wallet transactions.
|
|
* @param {WalletID} id
|
|
* @param {Number} limit - Max number of transactions.
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletLast = function getWalletLast(id, account, limit, callback) {
|
|
var options;
|
|
|
|
if (typeof account === 'function') {
|
|
callback = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account, limit: limit };
|
|
|
|
return this._get('/wallet/' + id + '/tx/last', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.tx.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get wallet transactions by timestamp range.
|
|
* @param {WalletID} id
|
|
* @param {Object} options
|
|
* @param {Number} options.start - Start time.
|
|
* @param {Number} options.end - End time.
|
|
* @param {Number?} options.limit - Max number of records.
|
|
* @param {Boolean?} options.reverse - Reverse order.
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletRange = function getWalletRange(id, account, options, callback) {
|
|
if (typeof options === 'function') {
|
|
callback = options;
|
|
options = account;
|
|
account = null;
|
|
}
|
|
|
|
options = {
|
|
account: account || options.account,
|
|
start: options.start,
|
|
end: options.end ,
|
|
limit: options.limit,
|
|
reverse: options.reverse
|
|
};
|
|
|
|
return this._get('/wallet/' + id + '/tx/range', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.tx.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get transaction (only possible if the transaction
|
|
* is available in the wallet history).
|
|
* @param {WalletID} id
|
|
* @param {Hash} hash
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletTX = function getTX(id, account, hash, callback) {
|
|
var options;
|
|
|
|
if (typeof hash === 'function') {
|
|
callback = hash;
|
|
hash = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account };
|
|
|
|
hash = utils.revHex(hash);
|
|
|
|
return this._get('/wallet/' + id + '/tx/' + hash, options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback();
|
|
|
|
try {
|
|
body = bcoin.tx.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get unspent coin (only possible if the transaction
|
|
* is available in the wallet history).
|
|
* @param {WalletID} id
|
|
* @param {Hash} hash
|
|
* @param {Number} index
|
|
* @param {Function} callback - Returns [Error, {@link Coin}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletCoin = function getCoin(id, account, hash, index, callback) {
|
|
var options, path;
|
|
|
|
if (typeof hash === 'function') {
|
|
callback = index;
|
|
index = hash;
|
|
hash = account;
|
|
account = null;
|
|
}
|
|
|
|
options = { account: account };
|
|
|
|
hash = utils.revHex(hash);
|
|
path = '/wallet/' + id + '/coin/' + hash + '/' + index;
|
|
|
|
return this._get(path, options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback();
|
|
|
|
try {
|
|
body = bcoin.coin.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get coins that pertain to an address from the mempool or chain database.
|
|
* Takes into account spent coins in the mempool.
|
|
* @param {Base58Address|Base58Address[]} addresses
|
|
* @param {Function} callback - Returns [Error, {@link Coin}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getCoinsByAddress = function getCoinsByAddress(address, callback) {
|
|
var body = { addresses: address };
|
|
|
|
return this._post('/coin/address', body, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.coin.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Retrieve a coin from the mempool or chain database.
|
|
* Takes into account spent coins in the mempool.
|
|
* @param {Hash} hash
|
|
* @param {Number} index
|
|
* @param {Function} callback - Returns [Error, {@link Coin}].
|
|
*/
|
|
|
|
HTTPClient.prototype.getCoin = function getCoin(hash, index, callback) {
|
|
hash = utils.revHex(hash);
|
|
|
|
return this._get('/coin/' + hash + '/' + index, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback();
|
|
|
|
try {
|
|
body = bcoin.coin.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Retrieve transactions pertaining to an
|
|
* address from the mempool or chain database.
|
|
* @param {Base58Address|Base58Address[]} addresses
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getTXByAddress = function getTXByAddress(address, callback) {
|
|
var body = { addresses: address };
|
|
|
|
return this._post('/tx/address', body, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.tx.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Retrieve a transaction from the mempool or chain database.
|
|
* @param {Hash} hash
|
|
* @param {Function} callback - Returns [Error, {@link TX}].
|
|
*/
|
|
|
|
HTTPClient.prototype.getTX = function getTX(hash, callback) {
|
|
hash = utils.revHex(hash);
|
|
|
|
return this._get('/tx/' + hash, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback();
|
|
|
|
try {
|
|
body = bcoin.tx.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Retrieve a block from the chain database.
|
|
* @param {Hash} hash
|
|
* @param {Function} callback - Returns [Error, {@link Block}].
|
|
*/
|
|
|
|
HTTPClient.prototype.getBlock = function getBlock(hash, callback) {
|
|
if (typeof hash !== 'number')
|
|
hash = utils.revHex(hash);
|
|
|
|
return this._get('/block/' + hash, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback();
|
|
|
|
try {
|
|
body = bcoin.block.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Add a transaction to the mempool and broadcast it.
|
|
* @param {TX} tx
|
|
* @param {Function} callback
|
|
*/
|
|
|
|
HTTPClient.prototype.broadcast = function broadcast(tx, callback) {
|
|
var body = { tx: tx.toRaw('hex') };
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._post('/broadcast', body, function(err) {
|
|
if (err)
|
|
return callback(err);
|
|
return callback();
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Create a transaction, fill, sign, and broadcast.
|
|
* @param {WalletID} id
|
|
* @param {Object} options
|
|
* @param {Base58Address} options.address
|
|
* @param {Amount} options.value
|
|
* @param {Function} callback - Returns [Error, {@link TX}].
|
|
*/
|
|
|
|
HTTPClient.prototype.walletSend = function walletSend(id, options, outputs, callback) {
|
|
if (typeof outputs === 'function') {
|
|
callback = outputs;
|
|
outputs = null;
|
|
}
|
|
|
|
options = utils.merge({}, options);
|
|
options.outputs = outputs || options.outputs || [];
|
|
|
|
if (!Array.isArray(options.outputs))
|
|
options.outputs = [options.outputs];
|
|
|
|
options.outputs = options.outputs.map(function(output) {
|
|
return {
|
|
value: utils.btc(output.value),
|
|
address: output.address && output.address.toBase58
|
|
? output.address.toBase58()
|
|
: output.address,
|
|
script: output.script ? output.script.toRaw('hex') : null
|
|
};
|
|
});
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._post('/wallet/' + id + '/send', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
try {
|
|
body = bcoin.tx.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
options = utils.merge({}, options);
|
|
options.outputs = outputs || options.outputs || [];
|
|
|
|
if (!Array.isArray(options.outputs))
|
|
options.outputs = [options.outputs];
|
|
|
|
options.outputs = options.outputs.map(function(output) {
|
|
return {
|
|
value: utils.btc(output.value),
|
|
address: output.address && output.address.toBase58
|
|
? output.address.toBase58()
|
|
: output.address,
|
|
script: output.script ? output.script.toRaw('hex') : null
|
|
};
|
|
});
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._post('/wallet/' + id + '/create', options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
try {
|
|
body = bcoin.tx.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Sign a transaction.
|
|
* @param {WalletID} id
|
|
* @param {TX} tx
|
|
* @param {Object} options
|
|
* @param {Function} callback - Returns [Error, {@link TX}].
|
|
*/
|
|
|
|
HTTPClient.prototype.walletSign = function walletCreate(id, tx, options, callback) {
|
|
var body;
|
|
|
|
if (typeof options === 'function') {
|
|
callback = options;
|
|
options = null;
|
|
}
|
|
|
|
body = utils.merge({}, options || {}, {
|
|
tx: tx.toRaw('hex')
|
|
});
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._post('/wallet/' + id + '/sign', body, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
try {
|
|
body = bcoin.tx.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Fill a transaction with coins.
|
|
* @param {TX} tx
|
|
* @param {Function} callback - Returns [Error, {@link TX}].
|
|
*/
|
|
|
|
HTTPClient.prototype.walletFill = function walletFill(tx, callback) {
|
|
var body = { tx: tx.toRaw('hex') };
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._post('/wallet/_/fill', body, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
try {
|
|
body = bcoin.tx.fromJSON(body);
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* @param {WalletID} id
|
|
* @param {Number} now - Current time.
|
|
* @param {Number} age - Age delta (delete transactions older than `now - age`).
|
|
* @param {Function} callback
|
|
*/
|
|
|
|
HTTPClient.prototype.walletZap = function walletZap(id, account, age, callback) {
|
|
var body;
|
|
|
|
if (typeof age === 'function') {
|
|
callback = age;
|
|
age = account;
|
|
account = null;
|
|
}
|
|
|
|
body = {
|
|
account: account,
|
|
age: age
|
|
};
|
|
|
|
assert(utils.isNumber(age));
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._post('/wallet/' + id + '/zap', body, function(err) {
|
|
if (err)
|
|
return callback(err);
|
|
return callback();
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Add a public account/purpose key to the wallet for multisig.
|
|
* @param {WalletID} id
|
|
* @param {HDPublicKey[]|Base58String[]} keys - Account (bip44) or
|
|
* Purpose (bip45) key (can be in base58 form).
|
|
* @param {Function} callback
|
|
*/
|
|
|
|
HTTPClient.prototype.addKey = function addKey(id, account, keys, callback) {
|
|
var options;
|
|
|
|
if (typeof keys === 'function') {
|
|
callback = keys;
|
|
keys = account;
|
|
account = null;
|
|
}
|
|
|
|
if (!Array.isArray(keys))
|
|
keys = [keys];
|
|
|
|
keys = keys.map(function(key) {
|
|
return key.xpubkey || key;
|
|
});
|
|
|
|
options = { account: account, keys: keys };
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._put('/wallet/' + id + '/key', keys, function(err) {
|
|
if (err)
|
|
return callback(err);
|
|
return callback();
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Remove a public account/purpose key to the wallet for multisig.
|
|
* @param {WalletID} id
|
|
* @param {HDPublicKey[]|Base58String[]} keys - Account (bip44) or Purpose
|
|
* (bip45) key (can be in base58 form).
|
|
* @param {Function} callback
|
|
*/
|
|
|
|
HTTPClient.prototype.removeKey = function removeKey(id, account, keys, callback) {
|
|
var options;
|
|
|
|
if (typeof keys === 'function') {
|
|
callback = keys;
|
|
keys = account;
|
|
account = null;
|
|
}
|
|
|
|
if (!Array.isArray(keys))
|
|
keys = [keys];
|
|
|
|
keys = keys.map(function(key) {
|
|
return key.xpubkey || key;
|
|
});
|
|
|
|
options = { account: account, keys: keys };
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
return this._del('/wallet/' + id + '/key', keys, function(err) {
|
|
if (err)
|
|
return callback(err);
|
|
return callback();
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get wallet accounts.
|
|
* @param {WalletID} id
|
|
* @param {Function} callback - Returns [Error, Array].
|
|
*/
|
|
|
|
HTTPClient.prototype.getWalletAccounts = function getWalletAccounts(id, callback) {
|
|
var path = '/wallet/' + id + '/account';
|
|
return this._get(path, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Create account.
|
|
* @param {WalletID} id
|
|
* @param {Object} options
|
|
* @param {Function} callback - Returns [Error, Array].
|
|
*/
|
|
|
|
HTTPClient.prototype.createWalletAccount = function createWalletAccount(id, options, callback) {
|
|
var path;
|
|
|
|
if (typeof options === 'function') {
|
|
callback = options;
|
|
options = null;
|
|
}
|
|
|
|
if (!options)
|
|
options = {};
|
|
|
|
if (typeof options === 'string')
|
|
options = { account: options };
|
|
|
|
path = '/wallet/' + id + '/account';
|
|
|
|
return this._post(path, options, function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
|
|
/**
|
|
* Get a mempool snapshot.
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
*/
|
|
|
|
HTTPClient.prototype.getMempool = function getMempool(callback) {
|
|
return this._get('/mempool', function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(null, []);
|
|
|
|
try {
|
|
body = body.map(function(data) {
|
|
return bcoin.tx.fromJSON(data);
|
|
});
|
|
} catch (e) {
|
|
return callback(e);
|
|
}
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Get some info about the server (network and version).
|
|
* @param {Function} callback - Returns [Error, Object].
|
|
*/
|
|
|
|
HTTPClient.prototype.getInfo = function getInfo(callback) {
|
|
return this._get('/', function(err, body) {
|
|
if (err)
|
|
return callback(err);
|
|
|
|
if (!body)
|
|
return callback(new Error('Info not available.'));
|
|
|
|
return callback(null, body);
|
|
});
|
|
};
|
|
|
|
/*
|
|
* Expose
|
|
*/
|
|
|
|
module.exports = HTTPClient;
|
|
</code></pre>
|
|
</article>
|
|
</section>
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
<nav>
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bcoin.html">bcoin</a></li><li><a href="module-constants.html">constants</a></li><li><a href="module-ec.html">ec</a></li><li><a href="module-ldb.html">ldb</a></li><li><a href="module-network.html">network</a></li><li><a href="module-profiler.html">profiler</a></li><li><a href="module-request.html">request</a></li><li><a href="module-uri.html">uri</a></li><li><a href="module-utils.html">utils</a></li></ul><h3>Classes</h3><ul><li><a href="AbstractBlock.html">AbstractBlock</a></li><li><a href="Account.html">Account</a></li><li><a href="Address.html">Address</a></li><li><a href="AES.html">AES</a></li><li><a href="Block.html">Block</a></li><li><a href="Bloom.html">Bloom</a></li><li><a href="BroadcastItem.html">BroadcastItem</a></li><li><a href="BST.html">BST</a></li><li><a href="BufferReader.html">BufferReader</a></li><li><a href="BufferWriter.html">BufferWriter</a></li><li><a href="Chain.html">Chain</a></li><li><a href="ChainDB.html">ChainDB</a></li><li><a href="ChainEntry.html">ChainEntry</a></li><li><a href="Coin.html">Coin</a></li><li><a href="Coins.html">Coins</a></li><li><a href="CoinView.html">CoinView</a></li><li><a href="CompactBlock.html">CompactBlock</a></li><li><a href="Environment.html">Environment</a></li><li><a href="Framer.html">Framer</a></li><li><a href="Fullnode.html">Fullnode</a></li><li><a href="HD.html">HD</a></li><li><a href="HDPrivateKey.html">HDPrivateKey</a></li><li><a href="HDPublicKey.html">HDPublicKey</a></li><li><a href="Headers.html">Headers</a></li><li><a href="HTTPBase.html">HTTPBase</a></li><li><a href="HTTPClient.html">HTTPClient</a></li><li><a href="HTTPServer.html">HTTPServer</a></li><li><a href="HTTPWallet.html">HTTPWallet</a></li><li><a href="Input.html">Input</a></li><li><a href="KeyPair.html">KeyPair</a></li><li><a href="KeyRing.html">KeyRing</a></li><li><a href="LoadRequest.html">LoadRequest</a></li><li><a href="Locker.html">Locker</a></li><li><a href="LowlevelUp.html">LowlevelUp</a></li><li><a href="LRU.html">LRU</a></li><li><a href="Master.html">Master</a></li><li><a href="Mempool.html">Mempool</a></li><li><a href="MempoolEntry.html">MempoolEntry</a></li><li><a href="MerkleBlock.html">MerkleBlock</a></li><li><a href="Miner.html">Miner</a></li><li><a href="MinerBlock.html">MinerBlock</a></li><li><a href="Mnemonic.html">Mnemonic</a></li><li><a href="MTX.html">MTX</a></li><li><a href="Network.html">Network</a></li><li><a href="NetworkAddress.html">NetworkAddress</a></li><li><a href="Node.html">Node</a></li><li><a href="NullCache.html">NullCache</a></li><li><a href="Output.html">Output</a></li><li><a href="Parser.html">Parser</a></li><li><a href="Peer.html">Peer</a></li><li><a href="Pool.html">Pool</a></li><li><a href="Profile.html">Profile</a></li><li><a href="RollingFilter.html">RollingFilter</a></li><li><a href="Script.html">Script</a></li><li><a href="ScriptError.html">ScriptError</a></li><li><a href="Snapshot.html">Snapshot</a></li><li><a href="SPVNode.html">SPVNode</a></li><li><a href="Stack.html">Stack</a></li><li><a href="TimeData.html">TimeData</a></li><li><a href="TX.html">TX</a></li><li><a href="TXDB.html">TXDB</a></li><li><a href="VerifyError.html">VerifyError</a></li><li><a href="Wallet.html">Wallet</a></li><li><a href="WalletDB.html">WalletDB</a></li><li><a href="Witness.html">Witness</a></li><li><a href="Worker.html">Worker</a></li><li><a href="Workers.html">Workers</a></li></ul><h3>Global</h3><ul><li><a href="global.html#hostname">hostname</a></li><li><a href="global.html#isMapped">isMapped</a></li><li><a href="global.html#mkdirp">mkdirp</a></li><li><a href="global.html#normalize">normalize</a></li><li><a href="global.html#parseHost">parseHost</a></li><li><a href="global.html#scrypt">scrypt</a></li><li><a href="global.html#toBuffer">toBuffer</a></li><li><a href="global.html#toString">toString</a></li><li><a href="global.html#version">version</a></li></ul>
|
|
</nav>
|
|
|
|
<br class="clear">
|
|
|
|
<footer>
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun Jun 05 2016 20:46:56 GMT-0700 (PDT)
|
|
</footer>
|
|
|
|
<script> prettyPrint(); </script>
|
|
<script src="scripts/linenumber.js"> </script>
|
|
</body>
|
|
</html>
|
|
|