simplify tbsHash.

This commit is contained in:
Christopher Jeffrey 2016-01-14 01:01:23 -08:00
parent 8f037ea430
commit ef59dae1b5

View File

@ -72,8 +72,8 @@ TX.prototype.clone = function clone() {
return new TX(this); return new TX(this);
}; };
TX.prototype.hash = function hash(enc) { TX.prototype.hash = function hash(enc, force) {
var h = utils.dsha256(this.render()); var h = utils.dsha256(this.render(force));
return enc === 'hex' ? utils.toHex(h) : h; return enc === 'hex' ? utils.toHex(h) : h;
}; };
@ -653,57 +653,20 @@ TX.prototype.signatureHash = function signatureHash(index, s, type) {
return hash; return hash;
}; };
TX.prototype.tbsHash = function tbsHash(enc) { TX.prototype.tbsHash = function tbsHash(enc, force) {
var copy = this.clone(); var copy = this.clone();
var i, j, input, s, raw, redeem, m; var i;
if (this.isCoinbase()) if (this.isCoinbase())
return this.hash(enc); return this.hash(enc);
if (this._tbsHash) { if (!this._tbsHash || force) {
return enc === 'hex' for (i = 0; i < copy.inputs.length; i++)
? utils.toHex(this._tbsHash) copy.inputs[i].script = [];
: this._tbsHash.slice();
this._tbsHash = utils.dsha256(copy.render(true));
} }
for (i = 0; i < copy.inputs.length; i++) {
input = copy.inputs[i];
input.out.tx = this.inputs[i].out.tx;
assert(input.out.tx);
s = input.script;
redeem = input.out.tx.getSubscript(input.out.index);
if (bcoin.script.isScripthash(redeem)) {
raw = s[s.length - 1];
assert(Array.isArray(raw) && raw.length);
redeem = bcoin.script.subscript(bcoin.script.decode(raw));
} else {
raw = null;
}
if (bcoin.script.isPubkey(redeem)) {
input.script = [[]];
} else if (bcoin.script.isPubkeyhash(redeem)) {
assert(Array.isArray(s[1]) && s[1].length);
input.script = [[], s[1]];
} else if (bcoin.script.isMultisig(redeem)) {
m = s[0];
if (Array.isArray(m))
m = m[0];
input.script = [[]];
for (i = 0; i < m; i++)
input.script.push([]);
} else {
input.script = [];
}
if (raw)
input.script.push(raw);
}
this._tbsHash = utils.dsha256(copy.render(true));
return enc === 'hex' return enc === 'hex'
? utils.toHex(this._tbsHash) ? utils.toHex(this._tbsHash)
: this._tbsHash.slice(); : this._tbsHash.slice();