Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JSON RPC named parameters #33

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Client {

this.agentOptions = agentOptions;
this.auth = (password || username) && { pass: password, user: username };
this.hasNamedParametersSupport = false;
this.headers = headers;
this.host = host;
this.password = password;
Expand All @@ -77,6 +78,14 @@ class Client {
let unsupported = [];

if (version) {
// Convert to semver (removing pathes).
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

patches

if (!/[0-9]+\.[0-9]+\.[0-9]+/.test(version)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to run this test as exec gives us both informations (the test and the capture which we need below).

throw new Error(`Invalid version "${version}"`, { version });
}

[version] = /[0-9]+\.[0-9]+\.[0-9]+/.exec(version);

this.hasNamedParametersSupport = semver.satisfies(version, '>=0.14.0');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If version is null, the result will be false, so everything will be ok here.

unsupported = _.chain(methods)
.pickBy(method => !semver.satisfies(version, method.version))
.keys()
Expand Down Expand Up @@ -113,6 +122,10 @@ class Client {
parameters = _.dropRight(parameters);
}

if (this.hasNamedParametersSupport && parameters.length === 1 && _.isPlainObject(parameters[0])) {
parameters = parameters[0];
}

return Promise.try(() => {
if (Array.isArray(input)) {
body = input.map((method, index) => this.requester.prepare({ method: method.method, parameters: method.parameters, suffix: index }));
Expand Down
35 changes: 35 additions & 0 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ describe('Client', () => {

balance.should.be.a.Number();
});

it('should support named parameters', async () => {
const balance = await new Client(_.defaults({ version: '0.15.0' }, config.bitcoind)).getBalance({
account: '*',
minconf: 0
});

balance.should.be.a.Number();
});
});

describe('getDifficulty()', () => {
Expand Down Expand Up @@ -242,6 +251,16 @@ describe('Client', () => {

transactions.should.be.an.Array().and.empty();
});

it('should support named parameters', async () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more generic test we can add that doesn't require adding a should support named parameters type of test for each and every method?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since each method has its own named arguments, we cannot add a generic test (or at least without adding all method definitions).

let transactions = await new Client(_.defaults({ version: '0.15.0' }, config.bitcoind)).listTransactions({ account: 'test' });

transactions.should.be.an.Array().and.empty();

transactions = await new Client(_.defaults({ version: '0.15.0' }, config.bitcoind)).listTransactions({ account: 'test', count: 15 });

transactions.should.be.an.Array().and.empty();
});
});
});

Expand Down Expand Up @@ -322,6 +341,22 @@ describe('Client', () => {
}
});

it('should throw an error if version is invalid', async () => {
try {
await new Client({ version: '0.12' }).getHashesPerSec();

should.fail();
} catch (e) {
e.should.be.an.instanceOf(Error);
e.message.should.equal('Invalid version "0.12"');
}
});

it('should not throw an error if version is valid', async () => {
await new Client(_.defaults({ version: '0.15.0.1' }, config.bitcoind)).getInfo();
await new Client(_.defaults({ version: '0.15.0' }, config.bitcoind)).getInfo();
});

it('should throw an error if version does not support a given method', async () => {
try {
await new Client({ version: '0.12.0' }).getHashesPerSec();
Expand Down