From dd49ee076196d2353783e3044185165dbac4aeb9 Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Fri, 21 Apr 2017 13:06:04 -0400 Subject: [PATCH] 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. --- lib/utils/validator.js | 8 ++++++++ test/http-test.js | 5 +++++ test/utils-test.js | 7 +++++++ 3 files changed, 20 insertions(+) diff --git a/lib/utils/validator.js b/lib/utils/validator.js index 4b524df4..8003d9ab 100644 --- a/lib/utils/validator.js +++ b/lib/utils/validator.js @@ -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.'); diff --git a/test/http-test.js b/test/http-test.js index d0c1573d..c44a29cb 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -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); diff --git a/test/utils-test.js b/test/utils-test.js index db687305..65fb23a9 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -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); + }); });