From 8069360c7bb2c4af81f36b8517ee89149e28b739 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Jul 2016 01:25:11 -0700 Subject: [PATCH] refactor mtx. --- lib/bcoin/mtx.js | 110 +++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index 590501f3..1106d605 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -56,55 +56,68 @@ var TX = bcoin.tx; */ function MTX(options) { - var i; - if (!(this instanceof MTX)) return new MTX(options); - if (!options) - options = {}; + TX.call(this); - this.version = options.version || 1; - this.flag = options.flag || 1; - this.inputs = []; - this.outputs = []; - this.locktime = 0; - - this.ts = 0; - this.block = null; - this.index = -1; - this.ps = options.ps != null ? options.ps : utils.now(); - this.height = -1; this.mutable = true; + this.changeIndex = -1; - this._hash = null; - this._whash = null; + if (options) + this.fromOptions(options); +} - this._raw = null; - this._size = -1; - this._witnessSize = -1; - this._lastWitnessSize = 0; +utils.inherits(MTX, TX); - this._outputValue = -1; - this._inputValue = -1; - this._hashPrevouts = null; - this._hashSequence = null; - this._hashOutputs = null; +/** + * Inject properties from options object. + * @private + * @param {Object} options + */ - this.changeIndex = options.changeIndex != null ? options.changeIndex : -1; +MTX.prototype.fromOptions = function fromOptions(options) { + var i; + + if (options.version != null) { + assert(utils.isNumber(options.version)); + this.version = options.version; + } + + if (options.flag != null) { + assert(utils.isNumber(options.flag)); + this.flag = options.flag; + } if (options.inputs) { + assert(Array.isArray(options.inputs)); for (i = 0; i < options.inputs.length; i++) this.addInput(options.inputs[i]); } if (options.outputs) { + assert(Array.isArray(options.outputs)); for (i = 0; i < options.outputs.length; i++) this.addOutput(options.outputs[i]); } -} -utils.inherits(MTX, TX); + if (options.locktime != null) { + assert(utils.isNumber(options.locktime)); + this.locktime = options.locktime; + } + + if (options.ps != null) { + assert(utils.isNumber(options.ps)); + this.ps = options.ps; + } + + if (options.changeIndex != null) { + assert(utils.isNumber(options.changeIndex)); + this.changeIndex = options.changeIndex; + } + + return this; +}; /** * Instantiate MTX from options. @@ -113,7 +126,7 @@ utils.inherits(MTX, TX); */ MTX.fromOptions = function fromOptions(options) { - return new MTX(options); + return new MTX().fromOptions(options); }; /** @@ -122,14 +135,7 @@ MTX.fromOptions = function fromOptions(options) { */ MTX.prototype.clone = function clone() { - var tx = new MTX(this); - tx.locktime = this.locktime; - tx.ts = this.ts; - tx.block = this.block; - tx.index = this.index; - tx.ps = this.ps; - tx.height = this.height; - return tx; + return new MTX(this); }; /** @@ -1290,6 +1296,20 @@ MTX.prototype.setLocktime = function setLocktime(locktime) { this.locktime = locktime; }; +/** + * Mark inputs and outputs as mutable. + * @private + */ + +MTX.prototype._mutable = function _mutable() { + var i; + for (i = 0; i < this.inputs.length; i++) + this.inputs[i].mutable = true; + for (i = 0; i < this.outputs.length; i++) + this.outputs[i].mutable = true; + return this; +}; + /** * @see TX.fromJSON */ @@ -1308,20 +1328,6 @@ MTX.fromRaw = function fromRaw(data, enc) { return new MTX().fromRaw(data)._mutable(); }; -/** - * Mark inputs and outputs as mutable. - * @private - */ - -MTX._mutable = function _mutable() { - var i; - for (i = 0; i < this.inputs.length; i++) - this.inputs[i].mutable = true; - for (i = 0; i < this.outputs.length; i++) - this.outputs[i].mutable = true; - return this; -}; - /** * @see TX.fromExtended */