From e36f78f6bfc9c7a0953a3ff167a1c6f34489838a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 12 Jan 2017 12:41:04 -0800 Subject: [PATCH] coin: add rhash method. --- lib/primitives/coin.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/lib/primitives/coin.js b/lib/primitives/coin.js index 480f54c9..25074add 100644 --- a/lib/primitives/coin.js +++ b/lib/primitives/coin.js @@ -64,8 +64,8 @@ Coin.prototype.fromOptions = function fromOptions(options) { assert(util.isNumber(options.height)); assert(util.isNumber(options.value)); assert(typeof options.coinbase === 'boolean'); - assert(options.hash == null || typeof options.hash === 'string'); - assert(options.index == null || util.isNumber(options.index)); + assert(typeof options.hash === 'string'); + assert(util.isNumber(options.index)); this.version = options.version; this.height = options.height; @@ -147,6 +147,15 @@ Coin.fromKey = function fromKey(key) { return new Coin().fromKey(key); }; +/** + * Get little-endian hash. + * @returns {Hash} + */ + +Coin.prototype.rhash = function rhash() { + return util.revHex(this.hash); +}; + /** * Convert the coin to a more user-friendly object. * @returns {Object} @@ -201,9 +210,7 @@ Coin.prototype.getJSON = function getJSON(network, minimal) { script: this.script.toJSON(), address: address, coinbase: this.coinbase, - hash: !minimal - ? (this.hash ? util.revHex(this.hash) : null) - : undefined, + hash: !minimal ? this.rhash() : undefined, index: !minimal ? this.index : undefined }; }; @@ -228,8 +235,8 @@ Coin.prototype.fromJSON = function fromJSON(json) { this.value = Amount.value(json.value); this.script.fromJSON(json.script); this.coinbase = json.coinbase; - this.hash = json.hash ? util.revHex(json.hash) : null; - this.index = json.index != null ? json.index : -1; + this.hash = json.hash ? util.revHex(json.hash) : encoding.NULL_HASH; + this.index = json.index != null ? json.index : 0; return this; }; @@ -374,8 +381,8 @@ Coin.fromTX = function fromTX(tx, index, height) { Coin.isCoin = function isCoin(obj) { return obj - && obj.version !== undefined - && obj.script !== undefined + && typeof obj.version === 'number' + && typeof obj.script === 'object' && typeof obj.getDepth === 'function'; };