primitives: more aggressive asserting.

This commit is contained in:
Christopher Jeffrey 2017-01-15 13:18:35 -08:00
parent 6d1c918793
commit 58da4be8fa
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 36 additions and 18 deletions

View File

@ -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;
};

View File

@ -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;

View File

@ -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;
}

View File

@ -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;