|
|
|
<!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
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = function(bcoin) {
|
|
|
|
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var network = bcoin.protocol.network;
|
|
|
|
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(uri, options) {
|
|
|
|
if (!(this instanceof HTTPClient))
|
|
|
|
return new HTTPClient(uri, options);
|
|
|
|
|
|
|
|
if (!options)
|
|
|
|
options = {};
|
|
|
|
|
|
|
|
EventEmitter.call(this);
|
|
|
|
|
|
|
|
this.uri = uri;
|
|
|
|
this.loaded = false;
|
|
|
|
this.id = null;
|
|
|
|
this.options = options;
|
|
|
|
this._init();
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.inherits(HTTPClient, EventEmitter);
|
|
|
|
|
|
|
|
HTTPClient.prototype._init = function _init() {
|
|
|
|
var self = this;
|
|
|
|
var io;
|
|
|
|
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
io = require('socket.io');
|
|
|
|
} catch (e) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!io)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.socket = new io.Socket(this.uri);
|
|
|
|
|
|
|
|
this.socket.on('error', function(err) {
|
|
|
|
self.emit('error', err);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on('open', function() {
|
|
|
|
self.socket.on('version', function(info) {
|
|
|
|
bcoin.debug('Connected to bcoin server: %s (%s)',
|
|
|
|
info.version, info.network);
|
|
|
|
assert(info.network === 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('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.listenWallet = function listenWallet(id) {
|
|
|
|
if (!this.socket)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.socket.join(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlisten for events on wallet id.
|
|
|
|
* @param {WalletID} id
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.unlistenWallet = function unlistenWallet(id) {
|
|
|
|
if (!this.socket)
|
|
|
|
return;
|
|
|
|
|
|
|
|
this.socket.leave(id);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for events on all wallets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.listenAll = function listenAll() {
|
|
|
|
this.listenWallet('!all');
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlisten for events on all wallets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.unlistenAll = function unlistenAll() {
|
|
|
|
this.unlistenWallet('!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 === network.type, 'Wrong network.');
|
|
|
|
|
|
|
|
if (res.statusCode === 404)
|
|
|
|
return callback();
|
|
|
|
|
|
|
|
if (!body)
|
|
|
|
return callback(new Error('No body.'));
|
|
|
|
|
|
|
|
if (res.statusCode !== 200)
|
|
|
|
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 and setup http provider (note that the
|
|
|
|
* passphrase is _not_ sent over the wire).
|
|
|
|
* @param {WalletID} id
|
|
|
|
* @param {String?} passphrase
|
|
|
|
* @param {Function} callback - Returns [Error, {@link Wallet}].
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.getWallet = function getWallet(id, passphrase, callback) {
|
|
|
|
var self = this;
|
|
|
|
var provider;
|
|
|
|
|
|
|
|
return this._getWallet(id, function(err, json) {
|
|
|
|
if (err)
|
|
|
|
return callback(err);
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = bcoin.wallet._fromJSON(json, passphrase);
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
json.provider = new bcoin.http.provider(self.uri);
|
|
|
|
|
|
|
|
return callback(null, new bcoin.wallet(json));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get wallet and setup http provider (note that the
|
|
|
|
* passphrase is _not_ sent over the wire).
|
|
|
|
* @param {WalletID} id
|
|
|
|
* @param {String?} passphrase
|
|
|
|
* @param {Function} callback - Returns [Error, {@link Wallet}].
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.createWallet = function createWallet(options, callback) {
|
|
|
|
var self = this;
|
|
|
|
return this._createWallet(options, function(err, json) {
|
|
|
|
if (err)
|
|
|
|
return callback(err);
|
|
|
|
|
|
|
|
try {
|
|
|
|
json = bcoin.wallet._fromJSON(json, options.passphrase);
|
|
|
|
} catch (e) {
|
|
|
|
return callback(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
json.provider = new bcoin.http.provider(self.uri);
|
|
|
|
|
|
|
|
return callback(null, new bcoin.wallet(json));
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get wallet transaction history.
|
|
|
|
* @param {WalletID} id
|
|
|
|
* @param {Function} callback - Returns [Error, {@link TX}[]].
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.getWalletHistory = function getWalletHistory(id, callback) {
|
|
|
|
return this._get('/wallet/' + id + '/tx/history', 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, callback) {
|
|
|
|
return this._get('/wallet/' + id + '/coin', 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, callback) {
|
|
|
|
return this._get('/wallet/' + id + '/tx/unconfirmed', 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, callback) {
|
|
|
|
return this._get('/wallet/' + id + '/balance', 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, limit, callback) {
|
|
|
|
var options = { 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 height.
|
|
|
|
* @param {Number} options.end - End height.
|
|
|
|
* @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, options, callback) {
|
|
|
|
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, hash, callback) {
|
|
|
|
hash = utils.revHex(hash);
|
|
|
|
|
|
|
|
return this._get('/wallet/' + id + '/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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, hash, index, callback) {
|
|
|
|
var path;
|
|
|
|
|
|
|
|
hash = utils.revHex(hash);
|
|
|
|
path = '/wallet/' + id + '/coin/' + hash + '/' + index;
|
|
|
|
|
|
|
|
return this._get(path, 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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync wallet receive and change depth with the server.
|
|
|
|
* @param {WalletID} id
|
|
|
|
* @param {Object} options
|
|
|
|
* @param {Number} options.receiveDepth
|
|
|
|
* @param {Number} options.changeDepth
|
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.syncWallet = function syncWallet(id, options, callback) {
|
|
|
|
var body = {
|
|
|
|
receiveDepth: options.receiveDepth,
|
|
|
|
changeDepth: options.changeDepth
|
|
|
|
};
|
|
|
|
|
|
|
|
return this._put('/wallet/' + id, body, function(err) {
|
|
|
|
if (err)
|
|
|
|
return callback(err);
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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: utils.toHex(tx.toRaw()) };
|
|
|
|
|
|
|
|
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 {BN} options.value
|
|
|
|
* @param {Function} callback - Returns [Error, {@link TX}].
|
|
|
|
*/
|
|
|
|
|
|
|
|
HTTPClient.prototype.walletSend = function walletSend(id, options, callback) {
|
|
|
|
var body = {
|
|
|
|
address: options.address,
|
|
|
|
value: utils.btc(options.value)
|
|
|
|
};
|
|
|
|
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
|
|
|
|
return this._post('/wallet/' + id + '/send', 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.zapWallet = function zapWallet(id, now, age, callback) {
|
|
|
|
var body = {
|
|
|
|
now: now,
|
|
|
|
age: age
|
|
|
|
};
|
|
|
|
|
|
|
|
assert(utils.isFinite(now));
|
|
|
|
assert(utils.isFinite(age));
|
|
|
|
assert(now >= 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, keys, callback) {
|
|
|
|
if (!Array.isArray(keys))
|
|
|
|
keys = [keys];
|
|
|
|
|
|
|
|
keys = keys.map(function(key) {
|
|
|
|
return key || key.xpubkey;
|
|
|
|
});
|
|
|
|
|
|
|
|
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, keys, callback) {
|
|
|
|
if (!Array.isArray(keys))
|
|
|
|
keys = [keys];
|
|
|
|
|
|
|
|
keys = keys.map(function(key) {
|
|
|
|
return key || key.xpubkey;
|
|
|
|
});
|
|
|
|
|
|
|
|
callback = utils.ensure(callback);
|
|
|
|
|
|
|
|
return this._del('/wallet/' + id + '/key', keys, function(err) {
|
|
|
|
if (err)
|
|
|
|
return callback(err);
|
|
|
|
return callback();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return 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-utils.html">utils</a></li><li><a href="module-workers.html">workers</a></li></ul><h3>Classes</h3><ul><li><a href="AbstractBlock.html">AbstractBlock</a></li><li><a href="Address.html">Address</a></li><li><a href="Block.html">Block</a></li><li><a href="Bloom.html">Bloom</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="ChainBlock.html">ChainBlock</a></li><li><a href="ChainDB.html">ChainDB</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="HDSeed.html">HDSeed</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="HTTPProvider.html">HTTPProvider</a></li><li><a href="HTTPServer.html">HTTPServer</a></li><li><a href="Input.html">Input</a></li><li><a href="KeyPair.html">KeyPair</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="Mempool.html">Mempool</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="MTX.html">MTX</a></li><li><a href="Node.html">Node</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="Provider.html">Provider</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="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></ul><h3><a href="global.html">Global</a></h3>
|
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
|
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sun Apr 17 2016 22:33:43 GMT-0700 (PDT)
|
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|