Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ TransactionBuilder.prototype.__build = function (allowIncomplete) {
case 'multisig': {
// Array.prototype.map is sparse-compatible
var msSignatures = input.signatures.map(function (signature) {
return signature.toScriptSignature(input.hashType)
return signature && signature.toScriptSignature(input.hashType)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only qualm was that I don't think this is necessary. However I know it is necessary given the current solution of pre-emptively always putting in the OP_0's. I might change that and submit a PR.

})

// fill in blanks with OP_0
Expand Down Expand Up @@ -368,13 +368,33 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
input.signatures = input.signatures || []
}

var signatureScript = input.redeemScript || input.prevOutScript
var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType)

if (input.scriptType === 'multisig' && input.redeemScript && input.signatures.length !== input.pubKeys.length) {
// store signatures locally
var _signatures = input.signatures.slice()

// loop over pubKeys to set their respective signature or set it to OP_0
input.signatures = input.pubKeys.map(function (pubKey) {
var signature = null
_signatures.forEach(function (_signature, _sigIdx) {
// check if the signature is not null / false / OP_0 and verify if it belongs to the pubKey
if (!signature && _signature && pubKey.verify(signatureHash, _signature)) {
// use .splice to remove the signature from the list, so we won't verify it again
signature = _signatures.splice(_sigIdx, 1)[0]
}
})

return signature || ops.OP_0
})
}

// enforce in order signing of public keys
assert(input.pubKeys.some(function (pubKey, i) {
if (!privKey.pub.Q.equals(pubKey.Q)) return false

assert(!input.signatures[i], 'Signature already exists')
var signatureScript = input.redeemScript || input.prevOutScript
var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType)
var signature = privKey.sign(signatureHash)
input.signatures[i] = signature

Expand Down
Loading