Skip to content

Commit 7ca5b94

Browse files
author
Pedro Branco
committed
Support JSON RPC named parameters
1 parent 1e0d75c commit 7ca5b94

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/index.js

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class Client {
6363

6464
this.agentOptions = agentOptions;
6565
this.auth = (password || username) && { pass: password, user: username };
66+
this.hasNamedParametersSupport = false;
6667
this.headers = headers;
6768
this.host = host;
6869
this.password = password;
@@ -77,6 +78,7 @@ class Client {
7778
let unsupported = [];
7879

7980
if (version) {
81+
this.hasNamedParametersSupport = semver.satisfies(version, '>=0.14.0');
8082
unsupported = _.chain(methods)
8183
.pickBy(method => !semver.satisfies(version, method.version))
8284
.keys()
@@ -113,6 +115,10 @@ class Client {
113115
parameters = _.dropRight(parameters);
114116
}
115117

118+
if (this.hasNamedParametersSupport && parameters.length === 1 && _.isPlainObject(parameters[0])) {
119+
parameters = parameters[0];
120+
}
121+
116122
return Promise.try(() => {
117123
if (Array.isArray(input)) {
118124
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(_.defaults({ version: '0.15.0' }, 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(_.defaults({ version: '0.15.0' }, config.bitcoind)).listTransactions({ account: 'test' });
257+
258+
transactions.should.be.an.Array().and.empty();
259+
260+
transactions = await new Client(_.defaults({ version: '0.15.0' }, 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)