Merge pull request #198 from kleetus/compat/bool-validation

RPC method compatibility with bitcoin core.
This commit is contained in:
Christopher Jeffrey (JJ) 2017-05-12 13:33:36 -07:00 committed by GitHub
commit 3fda5bedab
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);
});
});