|
|
@ -11,6 +11,8 @@ var $ = require('../../util/preconditions'); |
|
|
|
var Script = require('../../script'); |
|
|
|
var Signature = require('../../crypto/signature'); |
|
|
|
var Sighash = require('../sighash'); |
|
|
|
var SighashWitness = require('../sighashwitness'); |
|
|
|
var BufferWriter = require('../../encoding/bufferwriter'); |
|
|
|
var BufferUtil = require('../../util/buffer'); |
|
|
|
var TransactionSignature = require('../signature'); |
|
|
|
|
|
|
@ -74,6 +76,24 @@ MultiSigScriptHashInput.prototype._serializeSignatures = function() { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
MultiSigScriptHashInput.prototype.getScriptCode = function() { |
|
|
|
var writer = new BufferWriter(); |
|
|
|
if (!this.script.hasCodeseparators()) { |
|
|
|
writer.writeVarintNum(this._scriptBuffer.length); |
|
|
|
writer.write(this._scriptBuffer); |
|
|
|
} else { |
|
|
|
throw new Error('@TODO'); |
|
|
|
} |
|
|
|
return writer.toBuffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
MultiSigScriptHashInput.prototype.getSatoshisBuffer = function() { |
|
|
|
$.checkState(this.output instanceof Output); |
|
|
|
$.checkState(this.output._satoshisBN); |
|
|
|
var buffer = new BufferWriter().writeUInt64LEBN(this.output._satoshisBN).toBuffer(); |
|
|
|
return buffer; |
|
|
|
}; |
|
|
|
|
|
|
|
MultiSigScriptHashInput.prototype.getSignatures = function(transaction, privateKey, index, sigtype) { |
|
|
|
$.checkState(this.output instanceof Output); |
|
|
|
sigtype = sigtype || Signature.SIGHASH_ALL; |
|
|
@ -82,12 +102,20 @@ MultiSigScriptHashInput.prototype.getSignatures = function(transaction, privateK |
|
|
|
var results = []; |
|
|
|
_.each(this.publicKeys, function(publicKey) { |
|
|
|
if (publicKey.toString() === privateKey.publicKey.toString()) { |
|
|
|
var signature; |
|
|
|
if (self.nestedWitness) { |
|
|
|
var scriptCode = self.getScriptCode(); |
|
|
|
var satoshisBuffer = self.getSatoshisBuffer(); |
|
|
|
signature = SighashWitness.sign(transaction, privateKey, sigtype, index, scriptCode, satoshisBuffer); |
|
|
|
} else { |
|
|
|
signature = Sighash.sign(transaction, privateKey, sigtype, index, self.redeemScript); |
|
|
|
} |
|
|
|
results.push(new TransactionSignature({ |
|
|
|
publicKey: privateKey.publicKey, |
|
|
|
prevTxId: self.prevTxId, |
|
|
|
outputIndex: self.outputIndex, |
|
|
|
inputIndex: index, |
|
|
|
signature: Sighash.sign(transaction, privateKey, sigtype, index, self.redeemScript), |
|
|
|
signature: signature, |
|
|
|
sigtype: sigtype |
|
|
|
})); |
|
|
|
} |
|
|
@ -159,15 +187,29 @@ MultiSigScriptHashInput.prototype.publicKeysWithoutSignature = function() { |
|
|
|
}; |
|
|
|
|
|
|
|
MultiSigScriptHashInput.prototype.isValidSignature = function(transaction, signature) { |
|
|
|
// FIXME: Refactor signature so this is not necessary
|
|
|
|
signature.signature.nhashtype = signature.sigtype; |
|
|
|
return Sighash.verify( |
|
|
|
if (this.nestedWitness) { |
|
|
|
signature.signature.nhashtype = signature.sigtype; |
|
|
|
var scriptCode = this.getScriptCode(); |
|
|
|
var satoshisBuffer = this.getSatoshisBuffer(); |
|
|
|
return SighashWitness.verify( |
|
|
|
transaction, |
|
|
|
signature.signature, |
|
|
|
signature.publicKey, |
|
|
|
signature.inputIndex, |
|
|
|
scriptCode, |
|
|
|
satoshisBuffer |
|
|
|
); |
|
|
|
} else { |
|
|
|
// FIXME: Refactor signature so this is not necessary
|
|
|
|
signature.signature.nhashtype = signature.sigtype; |
|
|
|
return Sighash.verify( |
|
|
|
transaction, |
|
|
|
signature.signature, |
|
|
|
signature.publicKey, |
|
|
|
signature.inputIndex, |
|
|
|
this.redeemScript |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
MultiSigScriptHashInput.OPCODES_SIZE = 7; // serialized size (<=3) + 0 .. N .. M OP_CHECKMULTISIG
|
|
|
|