From 79eea76cc4a567be517143530d8afee5bd0bdeb2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 12 Jan 2017 16:13:32 -0800 Subject: [PATCH] output: remove output mutability and addr caching. --- lib/primitives/input.js | 6 +++--- lib/primitives/mtx.js | 26 +++----------------------- lib/primitives/outpoint.js | 13 +++++++++++++ lib/primitives/output.js | 16 +++------------- lib/wallet/wallet.js | 1 - 5 files changed, 22 insertions(+), 40 deletions(-) diff --git a/lib/primitives/input.js b/lib/primitives/input.js index 70396a49..2e7cab14 100644 --- a/lib/primitives/input.js +++ b/lib/primitives/input.js @@ -476,9 +476,9 @@ Input.fromTX = function fromTX(tx, index) { Input.isInput = function isInput(obj) { return obj - && obj.prevout !== undefined - && obj.script !== undefined - && obj.witness !== undefined + && typeof obj.prevout === 'object' + && typeof obj.script === 'object' + && typeof obj.witness === 'object' && typeof obj.getAddress === 'function'; }; diff --git a/lib/primitives/mtx.js b/lib/primitives/mtx.js index 2083c629..aeb1e356 100644 --- a/lib/primitives/mtx.js +++ b/lib/primitives/mtx.js @@ -232,7 +232,6 @@ MTX.prototype.addOutput = function addOutput(options, value) { options = Script.fromAddress(options); output = new Output(); - output.mutable = true; if (options instanceof Script) { assert(util.isUInt53(value), 'Value must be a uint53.'); @@ -1228,7 +1227,6 @@ MTX.prototype.fund = co(function* fund(coins, options) { change = new Output(); change.value = select.change; change.script.fromAddress(select.changeAddress); - change.mutable = true; if (change.isDust(policy.MIN_RELAY)) { // Do nothing. Change is added to fee. @@ -1329,23 +1327,6 @@ MTX.prototype.setSequence = function setSequence(index, locktime, seconds) { input.sequence = locktime; }; -/** - * Mark outputs as mutable. - * @private - * @param {Boolean} flag - */ - -MTX.prototype._mutable = function _mutable(flag) { - var i, output; - - for (i = 0; i < this.outputs.length; i++) { - output = this.outputs[i]; - output.mutable = flag; - } - - return this; -}; - /** * Inspect the transaction. * @returns {Object} @@ -1391,7 +1372,7 @@ MTX.prototype.getJSON = function getJSON(network) { */ MTX.fromJSON = function fromJSON(json) { - return new MTX().fromJSON(JSON)._mutable(true); + return new MTX().fromJSON(JSON); }; /** @@ -1401,7 +1382,7 @@ MTX.fromJSON = function fromJSON(json) { */ MTX.fromReader = function fromReader(br) { - return new MTX().fromReader(br)._mutable(true); + return new MTX().fromReader(br); }; /** @@ -1414,7 +1395,7 @@ MTX.fromReader = function fromReader(br) { MTX.fromRaw = function fromRaw(data, enc) { if (typeof data === 'string') data = new Buffer(data, enc); - return new MTX().fromRaw(data)._mutable(true); + return new MTX().fromRaw(data); }; /** @@ -1768,7 +1749,6 @@ CoinSelector.prototype.selectEstimate = co(function* selectEstimate() { // Add dummy output for change. change = new Output(); - change.mutable = true; if (this.changeAddress) { change.script.fromAddress(this.changeAddress); diff --git a/lib/primitives/outpoint.js b/lib/primitives/outpoint.js index cf958509..17959f4b 100644 --- a/lib/primitives/outpoint.js +++ b/lib/primitives/outpoint.js @@ -266,6 +266,19 @@ Outpoint.prototype.inspect = function inspect() { return ''; }; +/** + * Test an object to see if it is an outpoint. + * @param {Object} obj + * @returns {Boolean} + */ + +Outpoint.isOutpoint = function isOutpoint(obj) { + return obj + && typeof obj.hash === 'string' + && typeof obj.index === 'number' + && typeof obj.toKey === 'function'; +}; + /* * Expose */ diff --git a/lib/primitives/output.js b/lib/primitives/output.js index b065924e..e234cbb5 100644 --- a/lib/primitives/output.js +++ b/lib/primitives/output.js @@ -32,8 +32,6 @@ function Output(options) { this.value = 0; this.script = new Script(); - this.mutable = false; - this._address = null; if (options) this.fromOptions(options); @@ -87,15 +85,7 @@ Output.prototype.getType = function getType() { */ Output.prototype.getAddress = function getAddress() { - var address = this._address; - - if (!address) { - address = this.script.getAddress(); - if (!this.mutable) - this._address = address; - } - - return address; + return this.script.getAddress(); }; /** @@ -300,8 +290,8 @@ Output.fromRaw = function fromRaw(data, enc) { Output.isOutput = function isOutput(obj) { return obj - && obj.value !== undefined - && obj.script !== undefined + && typeof obj.value === 'number' + && typeof obj.script === 'object' && typeof obj.getAddress === 'function'; }; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index d25ec457..97b50701 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -1561,7 +1561,6 @@ Wallet.prototype.createTX = co(function* createTX(options, force) { // Add the outputs for (i = 0; i < outputs.length; i++) { output = new Output(outputs[i]); - output.mutable = true; if (output.isDust()) throw new Error('Output is dust.');