coin: add rhash method.

This commit is contained in:
Christopher Jeffrey 2017-01-12 12:41:04 -08:00
parent f3a393668e
commit e36f78f6bf
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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';
};