From 58da4be8fab938514e1f39335423ca22c933a3ad Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 15 Jan 2017 13:18:35 -0800 Subject: [PATCH] primitives: more aggressive asserting. --- lib/primitives/coin.js | 40 ++++++++++++++++++++++++++++------------ lib/primitives/input.js | 6 ++++-- lib/primitives/output.js | 2 +- lib/primitives/tx.js | 6 +++--- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index 25074add..fb8635f2 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -60,23 +60,39 @@ util.inherits(Coin, Output); Coin.prototype.fromOptions = function fromOptions(options) { assert(options, 'Coin data is required.'); - assert(util.isNumber(options.version)); - assert(util.isNumber(options.height)); - assert(util.isNumber(options.value)); - assert(typeof options.coinbase === 'boolean'); - assert(typeof options.hash === 'string'); - assert(util.isNumber(options.index)); - this.version = options.version; - this.height = options.height; - this.value = options.value; + if (options.version != null) { + assert(util.isUInt32(options.version)); + this.version = options.version; + } + + if (options.height != null) { + assert(options.height === -1 || util.isUInt32(options.height)); + this.height = options.height; + } + + if (options.value != null) { + assert(util.isUInt53(options.value)); + this.value = options.value; + } if (options.script) this.script.fromOptions(options.script); - this.coinbase = options.coinbase; - this.hash = options.hash; - this.index = options.index; + if (options.coinbase != null) { + assert(typeof options.coinbase === 'boolean'); + this.coinbase = options.coinbase; + } + + if (options.hash != null) { + assert(typeof options.hash === 'string'); + this.hash = options.hash; + } + + if (options.index != null) { + assert(util.isUInt32(options.index)); + this.index = options.index; + } return this; }; diff --git a/lib/primitives/input.js b/lib/primitives/input.js index 2e7cab14..4705f10a 100644 --- a/lib/primitives/input.js +++ b/lib/primitives/input.js @@ -56,7 +56,7 @@ Input.prototype.fromOptions = function fromOptions(options) { this.script.fromOptions(options.script); if (options.sequence != null) { - assert(util.isNumber(options.sequence)); + assert(util.isUInt32(options.sequence)); this.sequence = options.sequence; } @@ -300,7 +300,7 @@ Input.prototype.getJSON = function getJSON(network, coin) { Input.prototype.fromJSON = function fromJSON(json) { assert(json, 'Input data is required.'); - assert(util.isNumber(json.sequence)); + assert(util.isUInt32(json.sequence)); this.prevout.fromJSON(json.prevout); this.script.fromJSON(json.script); this.witness.fromJSON(json.witness); @@ -451,6 +451,8 @@ Input.fromCoin = function fromCoin(coin) { */ Input.prototype.fromTX = function fromTX(tx, index) { + assert(tx); + assert(typeof index === 'number'); assert(index < tx.outputs.length); this.prevout.hash = tx.hash('hex'); this.prevout.index = index; diff --git a/lib/primitives/output.js b/lib/primitives/output.js index e234cbb5..76e2cf4c 100644 --- a/lib/primitives/output.js +++ b/lib/primitives/output.js @@ -47,7 +47,7 @@ Output.prototype.fromOptions = function fromOptions(options) { assert(options, 'Output data is required.'); if (options.value) { - assert(util.isNumber(options.value)); + assert(util.isInt53(options.value)); this.value = options.value; } diff --git a/lib/primitives/tx.js b/lib/primitives/tx.js index 84838934..dd874d5b 100644 --- a/lib/primitives/tx.js +++ b/lib/primitives/tx.js @@ -2250,11 +2250,11 @@ TX.prototype.fromJSON = function fromJSON(json) { var i, input, output; assert(json, 'TX data is required.'); - assert(util.isNumber(json.version)); - assert(util.isNumber(json.flag)); + assert(util.isUInt32(json.version)); + assert(util.isUInt8(json.flag)); assert(Array.isArray(json.inputs)); assert(Array.isArray(json.outputs)); - assert(util.isNumber(json.locktime)); + assert(util.isUInt32(json.locktime)); this.version = json.version;