-
Notifications
You must be signed in to change notification settings - Fork 188
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -77,6 +78,14 @@ class Client { | |
let unsupported = []; | ||
|
||
if (version) { | ||
// Convert to semver (removing pathes). | ||
if (!/[0-9]+\.[0-9]+\.[0-9]+/.test(version)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
unsupported = _.chain(methods) | ||
.pickBy(method => !semver.satisfies(version, method.version)) | ||
.keys() | ||
|
@@ -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 })); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()', () => { | ||
|
@@ -242,6 +251,16 @@ describe('Client', () => { | |
|
||
transactions.should.be.an.Array().and.empty(); | ||
}); | ||
|
||
it('should support named parameters', async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
}); | ||
}); | ||
}); | ||
|
||
|
@@ -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(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
patches