Skip to content

Commit 9fe550d

Browse files
author
Pedro Branco
committed
Support JSON RPC named parameters
1 parent 7bd0c4e commit 9fe550d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class Client {
8484
.value();
8585
}
8686

87+
this.hasNamedParametersSupport = semver.satisfies(version || '0.14.0', '>=0.14.0');
88+
8789
const request = requestLogger(logger);
8890

8991
this.request = Promise.promisifyAll(request.defaults({
@@ -113,6 +115,11 @@ class Client {
113115
parameters = _.dropRight(parameters);
114116
}
115117

118+
// Support named parameters (Bitcoin Core >=0.14.0 only).
119+
if (this.hasNamedParametersSupport && parameters.length === 1 && _.isPlainObject(parameters[0])) {
120+
parameters = parameters[0];
121+
}
122+
116123
return Promise.try(() => {
117124
if (Array.isArray(input)) {
118125
body = input.map((method, index) => this.requester.prepare({ method: method.method, parameters: method.parameters, suffix: index }));

test/index_test.js

+19
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,15 @@ describe('Client', () => {
193193

194194
balance.should.be.a.Number();
195195
});
196+
197+
it('should support named parameters', async () => {
198+
const balance = await new Client(config.bitcoind).getBalance({
199+
account: '*',
200+
minconf: 0
201+
});
202+
203+
balance.should.be.a.Number();
204+
});
196205
});
197206

198207
describe('getDifficulty()', () => {
@@ -242,6 +251,16 @@ describe('Client', () => {
242251

243252
transactions.should.be.an.Array().and.empty();
244253
});
254+
255+
it('should support named parameters', async () => {
256+
let transactions = await new Client(config.bitcoind).listTransactions({ account: 'test' });
257+
258+
transactions.should.be.an.Array().and.empty();
259+
260+
transactions = await new Client(config.bitcoind).listTransactions({ account: 'test', count: 15 });
261+
262+
transactions.should.be.an.Array().and.empty();
263+
});
245264
});
246265
});
247266

0 commit comments

Comments
 (0)