From e57d02c03cccc9fe2e0276c47b534715e505a2b7 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Thu, 8 Jan 2015 10:57:46 -0300 Subject: [PATCH] Use define immutable helper --- lib/address.js | 17 ++++------------- lib/blockheader.js | 1 + lib/hdprivatekey.js | 6 +++--- lib/hdpublickey.js | 5 ++--- lib/opcode.js | 6 +++--- lib/privatekey.js | 18 +++++------------- lib/publickey.js | 18 ++++-------------- lib/transaction/input/input.js | 1 + lib/transaction/output.js | 2 ++ lib/transaction/transaction.js | 1 + 10 files changed, 26 insertions(+), 49 deletions(-) diff --git a/lib/address.js b/lib/address.js index 835f0ba..4ddcd21 100644 --- a/lib/address.js +++ b/lib/address.js @@ -74,19 +74,10 @@ function Address(data, network, type) { info.network = info.network || Networks.get(network) || Networks.defaultNetwork; info.type = info.type || type || Address.PayToPublicKeyHash; - Object.defineProperty(this, 'hashBuffer', { - configurable: false, - value: info.hashBuffer - }); - - Object.defineProperty(this, 'network', { - configurable: false, - value: info.network - }); - - Object.defineProperty(this, 'type', { - configurable: false, - value: info.type + JSUtil.defineImmutable(this, { + hashBuffer: info.hashBuffer, + network: info.network, + type: info.type }); return this; diff --git a/lib/blockheader.js b/lib/blockheader.js index 06e100f..80ad727 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -215,6 +215,7 @@ BlockHeader.prototype._getHash = function hash() { var idProperty = { configurable: false, writeable: false, + enumerable: true, /** * @returns {string} - The big endian hash buffer of the header */ diff --git a/lib/hdprivatekey.js b/lib/hdprivatekey.js index 3026aa4..8e40411 100644 --- a/lib/hdprivatekey.js +++ b/lib/hdprivatekey.js @@ -358,9 +358,9 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) { /* jshint maxstatements: 20 */ HDPrivateKey._validateBufferArguments(arg); - Object.defineProperty(this, '_buffers', { - configurable: false, - value: arg + + JSUtil.defineImmutable(this, { + _buffers: arg }); var sequence = [ diff --git a/lib/hdpublickey.js b/lib/hdpublickey.js index 323b71c..721b18a 100644 --- a/lib/hdpublickey.js +++ b/lib/hdpublickey.js @@ -297,9 +297,8 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) { HDPublicKey._validateBufferArguments(arg); - Object.defineProperty(this, '_buffers', { - configurable: false, - value: arg + JSUtil.defineImmutable(this, { + _buffers: arg }); var sequence = [ diff --git a/lib/opcode.js b/lib/opcode.js index 62a0c51..9e4cfe3 100644 --- a/lib/opcode.js +++ b/lib/opcode.js @@ -3,6 +3,7 @@ var _ = require('lodash'); var $ = require('./util/preconditions'); var BufferUtil = require('./util/buffer'); +var JSUtil = require('./util/js'); function Opcode(num) { if (!(this instanceof Opcode)) { @@ -19,9 +20,8 @@ function Opcode(num) { throw new TypeError('Unrecognized num type: "' + typeof(num) + '" for Opcode'); } - Object.defineProperty(this, 'num', { - configurable: false, - value: value + JSUtil.defineImmutable(this, { + num: value }); return this; diff --git a/lib/privatekey.js b/lib/privatekey.js index ac49996..72620f9 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -57,23 +57,15 @@ var PrivateKey = function PrivateKey(data, network) { throw new TypeError('Must specify the network ("livenet" or "testnet")'); } - Object.defineProperty(this, 'bn', { - configurable: false, - value: info.bn - }); - - Object.defineProperty(this, 'compressed', { - configurable: false, - value: info.compressed - }); - - Object.defineProperty(this, 'network', { - configurable: false, - value: info.network + JSUtil.defineImmutable(this, { + bn: info.bn, + compressed: info.compressed, + network: info.network }); Object.defineProperty(this, 'publicKey', { configurable: false, + enumerable: true, get: this.toPublicKey.bind(this) }); diff --git a/lib/publickey.js b/lib/publickey.js index 59afd0a..a5d5e23 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -52,23 +52,13 @@ var PublicKey = function PublicKey(data, extra) { // validation info.point.validate(); - Object.defineProperty(this, 'point', { - configurable: false, - value: info.point - }); - - Object.defineProperty(this, 'compressed', { - configurable: false, - value: info.compressed - }); - - Object.defineProperty(this, 'network', { - configurable: false, - value: info.network || Network.defaultNetwork + JSUtil.defineImmutable(this, { + point: info.point, + compressed: info.compressed, + network: info.network || Network.defaultNetwork }); return this; - }; /** diff --git a/lib/transaction/input/input.js b/lib/transaction/input/input.js index 73041f7..915f718 100644 --- a/lib/transaction/input/input.js +++ b/lib/transaction/input/input.js @@ -22,6 +22,7 @@ function Input(params) { Object.defineProperty(Input.prototype, 'script', { configurable: false, writeable: false, + enumerable: true, get: function() { if (!this._script) { this._script = new Script(this._scriptBuffer); diff --git a/lib/transaction/output.js b/lib/transaction/output.js index 9b31f66..0d48c44 100644 --- a/lib/transaction/output.js +++ b/lib/transaction/output.js @@ -20,6 +20,7 @@ function Output(params) { Object.defineProperty(Output.prototype, 'script', { configurable: false, writeable: false, + enumerable: true, get: function() { if (!this._script) { this._script = new Script(this._scriptBuffer); @@ -31,6 +32,7 @@ Object.defineProperty(Output.prototype, 'script', { Object.defineProperty(Output.prototype, 'satoshis', { configurable: false, writeable: true, + enumerable: true, get: function() { return this._satoshis.toNumber(); }, diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 3a1f833..b86ace0 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -81,6 +81,7 @@ Transaction.shallowCopy = function(transaction) { var hashProperty = { configurable: false, writeable: false, + enumerable: true, get: function() { return new BufferReader(this._getHash()).readReverse().toString('hex'); }