sighashing.

This commit is contained in:
Christopher Jeffrey 2016-06-17 04:15:34 -07:00
parent 54d3b350e9
commit 32e26446b1
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 51 additions and 47 deletions

View File

@ -86,6 +86,7 @@ function MTX(options) {
this._hashPrevouts = null;
this._hashSequence = null;
this._hashOutputs = null;
this._lastWitnessSize = 0;
if (options.inputs) {
for (i = 0; i < options.inputs.length; i++)
@ -100,6 +101,10 @@ function MTX(options) {
utils.inherits(MTX, bcoin.tx);
MTX.fromOptions = function fromOptions(options) {
return new MTX(options);
};
/**
* Clone the transaction.
* @returns {MTX}

View File

@ -124,41 +124,39 @@ TX.fromOptions = function fromOptions(data) {
*/
TX.prototype.clone = function clone() {
var copy, i;
var copy = new TX();
var i, input, output;
copy = {
version: this.version,
flag: this.flag,
inputs: [],
outputs: [],
locktime: this.locktime,
ts: this.ts,
block: this.block,
index: this.index,
height: this.height
};
copy.ts = this.ts;
copy.block = this.block;
copy.index = this.index;
copy.height = this.height;
copy.version = this.version;
copy.flag = this.flag;
for (i = 0; i < this.inputs.length; i++) {
copy.inputs.push({
prevout: {
hash: this.inputs[i].prevout.hash,
index: this.inputs[i].prevout.index
},
coin: this.inputs[i].coin,
script: this.inputs[i].script,
witness: this.inputs[i].witness,
sequence: this.inputs[i].sequence
});
input = new bcoin.input();
input.prevout = new bcoin.outpoint();
input.prevout.hash = this.inputs[i].prevout.hash;
input.prevout.index = this.inputs[i].prevout.index;
input.coin = this.inputs[i].coin;
input.script = this.inputs[i].script;
input.witness = this.inputs[i].witness;
input.sequence = this.inputs[i].sequence;
copy.inputs.push(input);
}
for (i = 0; i < this.outputs.length; i++) {
copy.outputs.push({
value: this.outputs[i].value,
script: this.outputs[i].script
});
output = new bcoin.output();
output.value = this.outputs[i].value;
output.script = this.outputs[i].script;
copy.outputs.push(output);
}
return new TX(copy);
copy.locktime = this.locktime;
return copy;
};
/**
@ -437,7 +435,9 @@ TX.prototype.signatureHash = function signatureHash(index, prev, type, version)
TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
var p = new BufferWriter();
var i, copy;
var empty = new Script();
var witness = new bcoin.witness();
var i, copy, input, output;
if (typeof index !== 'number')
index = this.inputs.indexOf(index);
@ -449,32 +449,31 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
assert(prev instanceof Script);
// Clone the transaction.
copy = {
version: this.version,
flag: 1,
inputs: [],
outputs: [],
locktime: this.locktime
};
copy = new TX();
copy.version = this.version;
for (i = 0; i < this.inputs.length; i++) {
copy.inputs.push({
prevout: this.inputs[i].prevout,
script: this.inputs[i].script,
sequence: this.inputs[i].sequence
});
input = new bcoin.input();
input.prevout = this.inputs[i].prevout;
input.script = this.inputs[i].script;
input.sequence = this.inputs[i].sequence;
input.witness = witness;
copy.inputs.push(input);
}
for (i = 0; i < this.outputs.length; i++) {
copy.outputs.push({
value: this.outputs[i].value,
script: this.outputs[i].script
});
output = new bcoin.output();
output.value = this.outputs[i].value;
output.script = this.outputs[i].script;
copy.outputs.push(output);
}
copy.locktime = this.locktime;
// Remove all signatures.
for (i = 0; i < copy.inputs.length; i++)
copy.inputs[i].script = new Script();
copy.inputs[i].script = empty;
// Remove all code separators.
prev = prev.removeSeparators();
@ -503,7 +502,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
// Null outputs that are not the at current input index.
for (i = 0; i < copy.outputs.length; i++) {
if (i !== index) {
copy.outputs[i].script = new Script();
copy.outputs[i].script = empty;
copy.outputs[i].value = -1;
}
}
@ -522,7 +521,7 @@ TX.prototype.signatureHashV0 = function signatureHashV0(index, prev, type) {
}
// Render the copy and append the hashtype.
TX(copy).toRaw(p);
copy.toRaw(p);
p.writeU32(type);
return utils.dsha256(p.render());