RPC method compatibility with bitcoin core.

- RPC method param semantics, unfortunately, aren't consistent in bitcoin core, therefore bcoin can't be used as a drop in replacement if bcoin "fixes the glitch".
- To avoid breaking bcoin's current api, validation of bool includes integers 0 and 1 as false and true, respectively.
- The effect of this is to allow getRawTransaction to work with older client code.
This commit is contained in:
Chris Kleeschulte 2017-04-21 13:06:04 -04:00
parent 6acef06cbc
commit dd49ee0761
No known key found for this signature in database
GPG Key ID: 33195D27EF6BDB7F
3 changed files with 20 additions and 0 deletions

View File

@ -407,6 +407,14 @@ Validator.prototype.bool = function bool(key, fallback) {
if (value === null)
return fallback;
// bitcoin core mixes semantics of truthiness amoung rpc methods
// most "verbose" parameters are bools, but getrawtransaction is 1/0
if (value === 1)
return true;
if (value === 0)
return false;
if (typeof value !== 'string') {
if (typeof value !== 'boolean')
throw new ValidationError(fmt(key) + ' must be a boolean.');

View File

@ -154,6 +154,11 @@ describe('HTTP', function() {
assert.equal(info.blocks, 0);
}));
it('should execute an rpc call with bool parameter', co(function* () {
var info = yield wallet.client.rpc.execute('getrawmempool', [true]);
assert.deepStrictEqual(info, {});
}));
it('should create account', co(function* () {
var info = yield wallet.createAccount('foo1');
assert(info);

View File

@ -9,6 +9,7 @@ var crypto = require('../lib/crypto/crypto');
var schnorr = require('../lib/crypto/schnorr');
var Amount = require('../lib/btc/amount');
var consensus = require('../lib/protocol/consensus');
var Validator = require('../lib/utils/validator');
describe('Utils', function() {
var vectors, signed, unsigned;
@ -328,4 +329,10 @@ describe('Utils', function() {
assert(schnorr.verify(msg, sig, pub));
assert.deepEqual(schnorr.recover(sig, msg), pub);
});
it('should validate integers 0 and 1 as booleans', function() {
var validator = new Validator({shouldBeTrue: 1, shouldBeFalse: 0});
assert(validator.bool('shouldBeTrue') === true);
assert(validator.bool('shouldBeFalse') === false);
});
});