Skip to content

Commit

Permalink
Release 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobranco committed Sep 27, 2019
1 parent 151bd61 commit 504afda
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 101 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Changelog

## [2.3.0](https://github.com/ruimarinho/bitcoin-core/tree/2.3.0) (2019-09-27)
## [3.0.0](https://github.com/ruimarinho/bitcoin-core/tree/3.0.0) (2019-09-27)

[Full Changelog](https://github.com/ruimarinho/bitcoin-core/compare/v2.2.1...2.3.0)
[Full Changelog](https://github.com/ruimarinho/bitcoin-core/compare/v2.3.0...3.0.0)

**Merged pull requests:**

- Remove bluebird dependency and callbacks support [\#87](https://github.com/ruimarinho/bitcoin-core/pull/87) ([pedrobranco](https://github.com/pedrobranco))

## [v2.3.0](https://github.com/ruimarinho/bitcoin-core/tree/v2.3.0) (2019-09-27)

[Full Changelog](https://github.com/ruimarinho/bitcoin-core/compare/v2.2.1...v2.3.0)

**Merged pull requests:**

Expand Down
151 changes: 65 additions & 86 deletions dist/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ exports.default = void 0;

var _parser = _interopRequireDefault(require("./parser"));

var _bluebird = _interopRequireDefault(require("bluebird"));

var _requester = _interopRequireDefault(require("./requester"));

var _lodash = _interopRequireDefault(require("lodash"));
Expand All @@ -27,35 +25,33 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
* Module dependencies.
*/

/**
* Source arguments to find out if a callback has been passed.
*/
function source(...args) {
const last = _lodash.default.last(args);

let callback;

if (_lodash.default.isFunction(last)) {
callback = last;
args = _lodash.default.dropRight(args);
}

return [args, callback];
}
/**
* List of networks and their default port mapping.
*/


const networks = {
mainnet: 8332,
regtest: 18332,
testnet: 18332
};
/**
* Promisify helper.
*/

const promisify = fn => (...args) => new Promise((resolve, reject) => {
fn(...args, (error, value) => {
if (error) {
reject(error);
return;
}

resolve(value);
});
});
/**
* Constructor.
*/


class Client {
constructor({
agentOptions,
Expand Down Expand Up @@ -121,14 +117,14 @@ class Client {
};
}, {});
const request = (0, _requestLogger.default)(logger);
this.request = _bluebird.default.promisifyAll(request.defaults({
this.request = request.defaults({
agentOptions: this.agentOptions,
baseUrl: `${this.ssl.enabled ? 'https' : 'http'}://${this.host}:${this.port}`,
strictSSL: this.ssl.strict,
timeout: this.timeout
}), {
multiArgs: true
});
this.request.getAsync = promisify(this.request.get);
this.request.postAsync = promisify(this.request.post);
this.requester = new _requester.default({
methods: this.methods,
version
Expand All @@ -142,21 +138,13 @@ class Client {
*/


command(...args) {
async command(...args) {
let body;
let callback;
let multiwallet;
let [input, ...parameters] = args; // eslint-disable-line prefer-const

const lastArg = _lodash.default.last(parameters);

const isBatch = Array.isArray(input);

if (_lodash.default.isFunction(lastArg)) {
callback = lastArg;
parameters = _lodash.default.dropRight(parameters);
}

if (isBatch) {
multiwallet = _lodash.default.some(input, command => {
return _lodash.default.get(this.methods[command.method], 'features.multiwallet.supported', false) === true;
Expand All @@ -178,29 +166,24 @@ class Client {
});
}

return _bluebird.default.try(() => {
return this.request.postAsync({
auth: _lodash.default.pickBy(this.auth, _lodash.default.identity),
body: JSON.stringify(body),
uri: `${multiwallet && this.wallet ? `/wallet/${this.wallet}` : '/'}`
}).bind(this).then(this.parser.rpc);
}).asCallback(callback);
return this.parser.rpc((await this.request.postAsync({
auth: _lodash.default.pickBy(this.auth, _lodash.default.identity),
body: JSON.stringify(body),
uri: `${multiwallet && this.wallet ? `/wallet/${this.wallet}` : '/'}`
})));
}
/**
* Given a transaction hash, returns a transaction in binary, hex-encoded binary, or JSON formats.
*/


getTransactionByHash(...args) {
const [[hash, {
extension = 'json'
} = {}], callback] = source(...args);
return _bluebird.default.try(() => {
return this.request.getAsync({
encoding: extension === 'bin' ? null : undefined,
url: `/rest/tx/${hash}.${extension}`
}).bind(this).then(_lodash.default.partial(this.parser.rest, extension));
}).asCallback(callback);
async getTransactionByHash(hash, {
extension = 'json'
} = {}) {
return this.parser.rest(extension, (await this.request.getAsync({
encoding: extension === 'bin' ? null : undefined,
url: `/rest/tx/${hash}.${extension}`
})));
}
/**
* Given a block hash, returns a block, in binary, hex-encoded binary or JSON formats.
Expand All @@ -209,43 +192,40 @@ class Client {
*/


getBlockByHash(...args) {
const [[hash, {
summary = false,
extension = 'json'
} = {}], callback] = source(...args);
return _bluebird.default.try(() => {
return this.request.getAsync({
encoding: extension === 'bin' ? null : undefined,
url: `/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}`
}).bind(this).then(_lodash.default.partial(this.parser.rest, extension));
}).asCallback(callback);
async getBlockByHash(hash, {
summary = false,
extension = 'json'
} = {}) {
const encoding = extension === 'bin' ? null : undefined;
const url = `/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}`;
return this.parser.rest(extension, (await this.request.getAsync({
encoding,
url
})));
}
/**
* Given a block hash, returns amount of blockheaders in upward direction.
*/


getBlockHeadersByHash(...args) {
const [[hash, count, {
extension = 'json'
} = {}], callback] = source(...args);
return _bluebird.default.try(() => {
return this.request.getAsync({
encoding: extension === 'bin' ? null : undefined,
url: `/rest/headers/${count}/${hash}.${extension}`
}).bind(this).then(_lodash.default.partial(this.parser.rest, extension));
}).asCallback(callback);
async getBlockHeadersByHash(hash, count, {
extension = 'json'
} = {}) {
const encoding = extension === 'bin' ? null : undefined;
const url = `/rest/headers/${count}/${hash}.${extension}`;
return this.parser.rest(extension, (await this.request.getAsync({
encoding,
url
})));
}
/**
* Returns various state info regarding block chain processing.
* Only supports JSON as output format.
*/


getBlockchainInformation(...args) {
const [, callback] = source(...args);
return this.request.getAsync(`/rest/chaininfo.json`).bind(this).then(_lodash.default.partial(this.parser.rest, 'json')).asCallback(callback);
async getBlockchainInformation() {
return this.parser.rest('json', (await this.request.getAsync(`/rest/chaininfo.json`)));
}
/**
* Query unspent transaction outputs for a given set of outpoints.
Expand All @@ -254,29 +234,29 @@ class Client {
*/


getUnspentTransactionOutputs(...args) {
const [[outpoints, {
extension = 'json'
} = {}], callback] = source(...args);
async getUnspentTransactionOutputs(outpoints, {
extension = 'json'
} = {}) {
const encoding = extension === 'bin' ? null : undefined;

const sets = _lodash.default.flatten([outpoints]).map(outpoint => {
return `${outpoint.id}-${outpoint.index}`;
}).join('/');

return this.request.getAsync({
encoding: extension === 'bin' ? null : undefined,
url: `/rest/getutxos/checkmempool/${sets}.${extension}`
}).bind(this).then(_lodash.default.partial(this.parser.rest, extension)).asCallback(callback);
const url = `/rest/getutxos/checkmempool/${sets}.${extension}`;
return this.parser.rest(extension, (await this.request.getAsync({
encoding,
url
})));
}
/**
* Returns transactions in the transaction memory pool.
* Only supports JSON as output format.
*/


getMemoryPoolContent(...args) {
const [, callback] = source(...args);
return this.request.getAsync('/rest/mempool/contents.json').bind(this).then(_lodash.default.partial(this.parser.rest, 'json')).asCallback(callback);
async getMemoryPoolContent() {
return this.parser.rest('json', (await this.request.getAsync('/rest/mempool/contents.json')));
}
/**
* Returns various information about the transaction memory pool.
Expand All @@ -288,9 +268,8 @@ class Client {
*/


getMemoryPoolInformation(...args) {
const [, callback] = source(...args);
return this.request.getAsync('/rest/mempool/info.json').bind(this).then(_lodash.default.partial(this.parser.rest, 'json')).asCallback(callback);
async getMemoryPoolInformation() {
return this.parser.rest('json', (await this.request.getAsync('/rest/mempool/info.json')));
}

}
Expand Down
24 changes: 12 additions & 12 deletions dist/src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ class Parser {
*/


rpc([response, body]) {
rpc(response) {
// The RPC api returns a `text/html; charset=ISO-8859-1` encoded response with an empty string as the body
// when an error occurs.
if (typeof body === 'string' && response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) {
if (typeof response.body === 'string' && response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) {
throw new _rpcError.default(response.statusCode, response.statusMessage, {
body
body: response.body
});
} // Parsing the body with custom parser to support BigNumbers.


body = parse(body);
const body = parse(response.body);

if (!Array.isArray(body)) {
return getRpcResult(body, {
Expand Down Expand Up @@ -104,31 +104,31 @@ class Parser {
return batch;
}

rest(extension, [response, body]) {
rest(extension, response) {
// The REST api returns a `text/plain` encoded response with the error line and the control
// characters \r\n. For readability and debuggability, the error message is set to this content.
// When requesting a binary response, the body will be returned as a Buffer representation of
// this error string.
if (response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) {
if (body instanceof Buffer) {
body = body.toString('utf-8');
if (response.body instanceof Buffer) {
response.body = response.body.toString('utf-8');
}

throw new _rpcError.default(response.statusCode, body.replace('\r\n', ''), {
body
throw new _rpcError.default(response.statusCode, response.body.replace('\r\n', ''), {
body: response.body
});
} // Parsing the body with custom parser to support BigNumbers.


if (extension === 'json') {
body = parse(body);
response.body = parse(response.body);
}

if (this.headers) {
return [body, response.headers];
return [response.body, response.headers];
}

return body;
return response.body;
}

}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bitcoin-core",
"version": "2.3.0",
"version": "3.0.0",
"description": "A modern Bitcoin Core REST and RPC client.",
"keywords": [
"bitcoin",
Expand Down

0 comments on commit 504afda

Please sign in to comment.