O(n) sighashing.

This commit is contained in:
Christopher Jeffrey 2016-04-27 20:14:36 -07:00
parent fbeefe03c7
commit 62bab2bf32
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -468,13 +468,19 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
assert(prev instanceof Script);
if (!(type & constants.hashType.ANYONECANPAY)) {
hashPrevouts = new BufferWriter();
for (i = 0; i < this.inputs.length; i++) {
prevout = this.inputs[i].prevout;
hashPrevouts.writeHash(prevout.hash);
hashPrevouts.writeU32(prevout.index);
if (this._hashPrevouts) {
hashPrevouts = this._hashPrevouts;
} else {
hashPrevouts = new BufferWriter();
for (i = 0; i < this.inputs.length; i++) {
prevout = this.inputs[i].prevout;
hashPrevouts.writeHash(prevout.hash);
hashPrevouts.writeU32(prevout.index);
}
hashPrevouts = utils.dsha256(hashPrevouts.render());
if (!this.mutable)
this._hashPrevouts = hashPrevouts;
}
hashPrevouts = utils.dsha256(hashPrevouts.render());
} else {
hashPrevouts = utils.slice(constants.ZERO_HASH);
}
@ -482,20 +488,32 @@ TX.prototype.signatureHashV1 = function signatureHashV1(index, prev, type) {
if (!(type & constants.hashType.ANYONECANPAY)
&& (type & 0x1f) !== constants.hashType.SINGLE
&& (type & 0x1f) !== constants.hashType.NONE) {
hashSequence = new BufferWriter();
for (i = 0; i < this.inputs.length; i++)
hashSequence.writeU32(this.inputs[i].sequence);
hashSequence = utils.dsha256(hashSequence.render());
if (this._hashSequence) {
hashSequence = this._hashSequence;
} else {
hashSequence = new BufferWriter();
for (i = 0; i < this.inputs.length; i++)
hashSequence.writeU32(this.inputs[i].sequence);
hashSequence = utils.dsha256(hashSequence.render());
if (!this.mutable)
this._hashSequence = hashSequence;
}
} else {
hashSequence = utils.slice(constants.ZERO_HASH);
}
if ((type & 0x1f) !== constants.hashType.SINGLE
&& (type & 0x1f) !== constants.hashType.NONE) {
hashOutputs = new BufferWriter();
for (i = 0; i < this.outputs.length; i++)
bcoin.protocol.framer.output(this.outputs[i], hashOutputs);
hashOutputs = utils.dsha256(hashOutputs.render());
if (this._hashOutputs) {
hashOutputs = this._hashOutputs;
} else {
hashOutputs = new BufferWriter();
for (i = 0; i < this.outputs.length; i++)
bcoin.protocol.framer.output(this.outputs[i], hashOutputs);
hashOutputs = utils.dsha256(hashOutputs.render());
if (!this.mutable)
this._hashOutputs = hashOutputs;
}
} else if ((type & 0x1f) === constants.hashType.SINGLE && index < this.outputs.length) {
hashOutputs = bcoin.protocol.framer.output(this.outputs[index]);
hashOutputs = utils.dsha256(hashOutputs);