Merge pull request #911 from yemel/fix/enumerate-properties

Use defineImmutable helper
This commit is contained in:
Esteban Ordano 2015-01-08 14:29:21 -05:00
commit c98f4dd8e5
10 changed files with 26 additions and 49 deletions

View File

@ -74,19 +74,10 @@ function Address(data, network, type) {
info.network = info.network || Networks.get(network) || Networks.defaultNetwork; info.network = info.network || Networks.get(network) || Networks.defaultNetwork;
info.type = info.type || type || Address.PayToPublicKeyHash; info.type = info.type || type || Address.PayToPublicKeyHash;
Object.defineProperty(this, 'hashBuffer', { JSUtil.defineImmutable(this, {
configurable: false, hashBuffer: info.hashBuffer,
value: info.hashBuffer network: info.network,
}); type: info.type
Object.defineProperty(this, 'network', {
configurable: false,
value: info.network
});
Object.defineProperty(this, 'type', {
configurable: false,
value: info.type
}); });
return this; return this;

View File

@ -215,6 +215,7 @@ BlockHeader.prototype._getHash = function hash() {
var idProperty = { var idProperty = {
configurable: false, configurable: false,
writeable: false, writeable: false,
enumerable: true,
/** /**
* @returns {string} - The big endian hash buffer of the header * @returns {string} - The big endian hash buffer of the header
*/ */

View File

@ -358,9 +358,9 @@ HDPrivateKey.prototype._buildFromBuffers = function(arg) {
/* jshint maxstatements: 20 */ /* jshint maxstatements: 20 */
HDPrivateKey._validateBufferArguments(arg); HDPrivateKey._validateBufferArguments(arg);
Object.defineProperty(this, '_buffers', {
configurable: false, JSUtil.defineImmutable(this, {
value: arg _buffers: arg
}); });
var sequence = [ var sequence = [

View File

@ -297,9 +297,8 @@ HDPublicKey.prototype._buildFromBuffers = function (arg) {
HDPublicKey._validateBufferArguments(arg); HDPublicKey._validateBufferArguments(arg);
Object.defineProperty(this, '_buffers', { JSUtil.defineImmutable(this, {
configurable: false, _buffers: arg
value: arg
}); });
var sequence = [ var sequence = [

View File

@ -3,6 +3,7 @@
var _ = require('lodash'); var _ = require('lodash');
var $ = require('./util/preconditions'); var $ = require('./util/preconditions');
var BufferUtil = require('./util/buffer'); var BufferUtil = require('./util/buffer');
var JSUtil = require('./util/js');
function Opcode(num) { function Opcode(num) {
if (!(this instanceof Opcode)) { if (!(this instanceof Opcode)) {
@ -19,9 +20,8 @@ function Opcode(num) {
throw new TypeError('Unrecognized num type: "' + typeof(num) + '" for Opcode'); throw new TypeError('Unrecognized num type: "' + typeof(num) + '" for Opcode');
} }
Object.defineProperty(this, 'num', { JSUtil.defineImmutable(this, {
configurable: false, num: value
value: value
}); });
return this; return this;

View File

@ -57,23 +57,15 @@ var PrivateKey = function PrivateKey(data, network) {
throw new TypeError('Must specify the network ("livenet" or "testnet")'); throw new TypeError('Must specify the network ("livenet" or "testnet")');
} }
Object.defineProperty(this, 'bn', { JSUtil.defineImmutable(this, {
configurable: false, bn: info.bn,
value: info.bn compressed: info.compressed,
}); network: info.network
Object.defineProperty(this, 'compressed', {
configurable: false,
value: info.compressed
});
Object.defineProperty(this, 'network', {
configurable: false,
value: info.network
}); });
Object.defineProperty(this, 'publicKey', { Object.defineProperty(this, 'publicKey', {
configurable: false, configurable: false,
enumerable: true,
get: this.toPublicKey.bind(this) get: this.toPublicKey.bind(this)
}); });

View File

@ -52,23 +52,13 @@ var PublicKey = function PublicKey(data, extra) {
// validation // validation
info.point.validate(); info.point.validate();
Object.defineProperty(this, 'point', { JSUtil.defineImmutable(this, {
configurable: false, point: info.point,
value: info.point compressed: info.compressed,
}); network: info.network || Network.defaultNetwork
Object.defineProperty(this, 'compressed', {
configurable: false,
value: info.compressed
});
Object.defineProperty(this, 'network', {
configurable: false,
value: info.network || Network.defaultNetwork
}); });
return this; return this;
}; };
/** /**

View File

@ -22,6 +22,7 @@ function Input(params) {
Object.defineProperty(Input.prototype, 'script', { Object.defineProperty(Input.prototype, 'script', {
configurable: false, configurable: false,
writeable: false, writeable: false,
enumerable: true,
get: function() { get: function() {
if (!this._script) { if (!this._script) {
this._script = new Script(this._scriptBuffer); this._script = new Script(this._scriptBuffer);

View File

@ -20,6 +20,7 @@ function Output(params) {
Object.defineProperty(Output.prototype, 'script', { Object.defineProperty(Output.prototype, 'script', {
configurable: false, configurable: false,
writeable: false, writeable: false,
enumerable: true,
get: function() { get: function() {
if (!this._script) { if (!this._script) {
this._script = new Script(this._scriptBuffer); this._script = new Script(this._scriptBuffer);
@ -31,6 +32,7 @@ Object.defineProperty(Output.prototype, 'script', {
Object.defineProperty(Output.prototype, 'satoshis', { Object.defineProperty(Output.prototype, 'satoshis', {
configurable: false, configurable: false,
writeable: true, writeable: true,
enumerable: true,
get: function() { get: function() {
return this._satoshis.toNumber(); return this._satoshis.toNumber();
}, },

View File

@ -81,6 +81,7 @@ Transaction.shallowCopy = function(transaction) {
var hashProperty = { var hashProperty = {
configurable: false, configurable: false,
writeable: false, writeable: false,
enumerable: true,
get: function() { get: function() {
return new BufferReader(this._getHash()).readReverse().toString('hex'); return new BufferReader(this._getHash()).readReverse().toString('hex');
} }