tx: refactor witness sighash.

This commit is contained in:
Christopher Jeffrey 2016-10-12 20:20:10 -07:00
parent ed6ec1e56c
commit 469474ae0a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -584,69 +584,87 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
*/ */
TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) { TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
var p = new BufferWriter(); var i, p, input, output, prevouts, sequences, outputs;
var i, hashPrevouts, hashSequence, hashOutputs;
if (!(type & constants.hashType.ANYONECANPAY)) { if (!(type & constants.hashType.ANYONECANPAY)) {
if (this._hashPrevouts) { if (this._hashPrevouts) {
hashPrevouts = this._hashPrevouts; prevouts = this._hashPrevouts;
} else { } else {
hashPrevouts = new BufferWriter(); p = new BufferWriter();
for (i = 0; i < this.inputs.length; i++)
this.inputs[i].prevout.toRaw(hashPrevouts); for (i = 0; i < this.inputs.length; i++) {
hashPrevouts = crypto.hash256(hashPrevouts.render()); input = this.inputs[i];
input.prevout.toRaw(p);
}
prevouts = crypto.hash256(p.render());
if (!this.mutable) if (!this.mutable)
this._hashPrevouts = hashPrevouts; this._hashPrevouts = prevouts;
} }
} else { } else {
hashPrevouts = utils.copy(constants.ZERO_HASH); prevouts = utils.copy(constants.ZERO_HASH);
} }
if (!(type & constants.hashType.ANYONECANPAY) if (!(type & constants.hashType.ANYONECANPAY)
&& (type & 0x1f) !== constants.hashType.SINGLE && (type & 0x1f) !== constants.hashType.SINGLE
&& (type & 0x1f) !== constants.hashType.NONE) { && (type & 0x1f) !== constants.hashType.NONE) {
if (this._hashSequence) { if (this._hashSequence) {
hashSequence = this._hashSequence; sequences = this._hashSequence;
} else { } else {
hashSequence = new BufferWriter(); p = new BufferWriter();
for (i = 0; i < this.inputs.length; i++)
hashSequence.writeU32(this.inputs[i].sequence); for (i = 0; i < this.inputs.length; i++) {
hashSequence = crypto.hash256(hashSequence.render()); input = this.inputs[i];
p.writeU32(input.sequence);
}
sequences = crypto.hash256(p.render());
if (!this.mutable) if (!this.mutable)
this._hashSequence = hashSequence; this._hashSequence = sequences;
} }
} else { } else {
hashSequence = utils.copy(constants.ZERO_HASH); sequences = utils.copy(constants.ZERO_HASH);
} }
if ((type & 0x1f) !== constants.hashType.SINGLE if ((type & 0x1f) !== constants.hashType.SINGLE
&& (type & 0x1f) !== constants.hashType.NONE) { && (type & 0x1f) !== constants.hashType.NONE) {
if (this._hashOutputs) { if (this._hashOutputs) {
hashOutputs = this._hashOutputs; outputs = this._hashOutputs;
} else { } else {
hashOutputs = new BufferWriter(); p = new BufferWriter();
for (i = 0; i < this.outputs.length; i++)
this.outputs[i].toRaw(hashOutputs); for (i = 0; i < this.outputs.length; i++) {
hashOutputs = crypto.hash256(hashOutputs.render()); output = this.outputs[i];
output.toRaw(p);
}
outputs = crypto.hash256(p.render());
if (!this.mutable) if (!this.mutable)
this._hashOutputs = hashOutputs; this._hashOutputs = outputs;
} }
} else if ((type & 0x1f) === constants.hashType.SINGLE && index < this.outputs.length) { } else if ((type & 0x1f) === constants.hashType.SINGLE && index < this.outputs.length) {
hashOutputs = this.outputs[index].toRaw(); output = this.outputs[index];
hashOutputs = crypto.hash256(hashOutputs); outputs = crypto.hash256(output.toRaw());
} else { } else {
hashOutputs = utils.copy(constants.ZERO_HASH); outputs = utils.copy(constants.ZERO_HASH);
} }
input = this.inputs[index];
p = new BufferWriter();
p.writeU32(this.version); p.writeU32(this.version);
p.writeBytes(hashPrevouts); p.writeBytes(prevouts);
p.writeBytes(hashSequence); p.writeBytes(sequences);
p.writeHash(this.inputs[index].prevout.hash); p.writeHash(input.prevout.hash);
p.writeU32(this.inputs[index].prevout.index); p.writeU32(input.prevout.index);
p.writeVarBytes(prev.toRaw()); p.writeVarBytes(prev.toRaw());
p.write64(this.inputs[index].coin.value); p.write64(input.coin.value);
p.writeU32(this.inputs[index].sequence); p.writeU32(input.sequence);
p.writeBytes(hashOutputs); p.writeBytes(outputs);
p.writeU32(this.locktime); p.writeU32(this.locktime);
p.writeU32(type); p.writeU32(type);