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.height));
assert(util.isNumber(options.value)); assert(util.isNumber(options.value));
assert(typeof options.coinbase === 'boolean'); assert(typeof options.coinbase === 'boolean');
assert(options.hash == null || typeof options.hash === 'string'); assert(typeof options.hash === 'string');
assert(options.index == null || util.isNumber(options.index)); assert(util.isNumber(options.index));
this.version = options.version; this.version = options.version;
this.height = options.height; this.height = options.height;
@ -147,6 +147,15 @@ Coin.fromKey = function fromKey(key) {
return new Coin().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. * Convert the coin to a more user-friendly object.
* @returns {Object} * @returns {Object}
@ -201,9 +210,7 @@ Coin.prototype.getJSON = function getJSON(network, minimal) {
script: this.script.toJSON(), script: this.script.toJSON(),
address: address, address: address,
coinbase: this.coinbase, coinbase: this.coinbase,
hash: !minimal hash: !minimal ? this.rhash() : undefined,
? (this.hash ? util.revHex(this.hash) : null)
: undefined,
index: !minimal ? this.index : undefined index: !minimal ? this.index : undefined
}; };
}; };
@ -228,8 +235,8 @@ Coin.prototype.fromJSON = function fromJSON(json) {
this.value = Amount.value(json.value); this.value = Amount.value(json.value);
this.script.fromJSON(json.script); this.script.fromJSON(json.script);
this.coinbase = json.coinbase; this.coinbase = json.coinbase;
this.hash = json.hash ? util.revHex(json.hash) : null; this.hash = json.hash ? util.revHex(json.hash) : encoding.NULL_HASH;
this.index = json.index != null ? json.index : -1; this.index = json.index != null ? json.index : 0;
return this; return this;
}; };
@ -374,8 +381,8 @@ Coin.fromTX = function fromTX(tx, index, height) {
Coin.isCoin = function isCoin(obj) { Coin.isCoin = function isCoin(obj) {
return obj return obj
&& obj.version !== undefined && typeof obj.version === 'number'
&& obj.script !== undefined && typeof obj.script === 'object'
&& typeof obj.getDepth === 'function'; && typeof obj.getDepth === 'function';
}; };