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