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) {
var p = new BufferWriter();
var i, hashPrevouts, hashSequence, hashOutputs;
var i, p, input, output, prevouts, sequences, outputs;
if (!(type & constants.hashType.ANYONECANPAY)) {
if (this._hashPrevouts) {
hashPrevouts = this._hashPrevouts;
prevouts = this._hashPrevouts;
} else {
hashPrevouts = new BufferWriter();
for (i = 0; i < this.inputs.length; i++)
this.inputs[i].prevout.toRaw(hashPrevouts);
hashPrevouts = crypto.hash256(hashPrevouts.render());
p = new BufferWriter();
for (i = 0; i < this.inputs.length; i++) {
input = this.inputs[i];
input.prevout.toRaw(p);
}
prevouts = crypto.hash256(p.render());
if (!this.mutable)
this._hashPrevouts = hashPrevouts;
this._hashPrevouts = prevouts;
}
} else {
hashPrevouts = utils.copy(constants.ZERO_HASH);
prevouts = utils.copy(constants.ZERO_HASH);
}
if (!(type & constants.hashType.ANYONECANPAY)
&& (type & 0x1f) !== constants.hashType.SINGLE
&& (type & 0x1f) !== constants.hashType.NONE) {
if (this._hashSequence) {
hashSequence = this._hashSequence;
sequences = this._hashSequence;
} else {
hashSequence = new BufferWriter();
for (i = 0; i < this.inputs.length; i++)
hashSequence.writeU32(this.inputs[i].sequence);
hashSequence = crypto.hash256(hashSequence.render());
p = new BufferWriter();
for (i = 0; i < this.inputs.length; i++) {
input = this.inputs[i];
p.writeU32(input.sequence);
}
sequences = crypto.hash256(p.render());
if (!this.mutable)
this._hashSequence = hashSequence;
this._hashSequence = sequences;
}
} else {
hashSequence = utils.copy(constants.ZERO_HASH);
sequences = utils.copy(constants.ZERO_HASH);
}
if ((type & 0x1f) !== constants.hashType.SINGLE
&& (type & 0x1f) !== constants.hashType.NONE) {
if (this._hashOutputs) {
hashOutputs = this._hashOutputs;
outputs = this._hashOutputs;
} else {
hashOutputs = new BufferWriter();
for (i = 0; i < this.outputs.length; i++)
this.outputs[i].toRaw(hashOutputs);
hashOutputs = crypto.hash256(hashOutputs.render());
p = new BufferWriter();
for (i = 0; i < this.outputs.length; i++) {
output = this.outputs[i];
output.toRaw(p);
}
outputs = crypto.hash256(p.render());
if (!this.mutable)
this._hashOutputs = hashOutputs;
this._hashOutputs = outputs;
}
} else if ((type & 0x1f) === constants.hashType.SINGLE && index < this.outputs.length) {
hashOutputs = this.outputs[index].toRaw();
hashOutputs = crypto.hash256(hashOutputs);
output = this.outputs[index];
outputs = crypto.hash256(output.toRaw());
} 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.writeBytes(hashPrevouts);
p.writeBytes(hashSequence);
p.writeHash(this.inputs[index].prevout.hash);
p.writeU32(this.inputs[index].prevout.index);
p.writeBytes(prevouts);
p.writeBytes(sequences);
p.writeHash(input.prevout.hash);
p.writeU32(input.prevout.index);
p.writeVarBytes(prev.toRaw());
p.write64(this.inputs[index].coin.value);
p.writeU32(this.inputs[index].sequence);
p.writeBytes(hashOutputs);
p.write64(input.coin.value);
p.writeU32(input.sequence);
p.writeBytes(outputs);
p.writeU32(this.locktime);
p.writeU32(type);