Merge pull request #379 from bitcoinjs/373opti

TransactionBuilder.sign signature re-ordering and verification optimization
This commit is contained in:
Daniel Cousens 2015-03-15 17:16:55 +11:00
commit 9e631ceebf

View file

@ -371,22 +371,26 @@ TransactionBuilder.prototype.sign = function (index, privKey, redeemScript, hash
var signatureScript = input.redeemScript || input.prevOutScript var signatureScript = input.redeemScript || input.prevOutScript
var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType) var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType)
// enforce signature order matches public keys
if (input.scriptType === 'multisig' && input.redeemScript && input.signatures.length !== input.pubKeys.length) { if (input.scriptType === 'multisig' && input.redeemScript && input.signatures.length !== input.pubKeys.length) {
// store signatures locally // maintain a local copy of unmatched signatures
var _signatures = input.signatures.slice() var unmatched = input.signatures.slice()
// loop over pubKeys to set their respective signature or set it to OP_0
input.signatures = input.pubKeys.map(function (pubKey) { input.signatures = input.pubKeys.map(function (pubKey) {
var signature = null var match
_signatures.forEach(function (_signature, _sigIdx) {
// check if the signature is not null / false / OP_0 and verify if it belongs to the pubKey // check for any matching signatures
if (!signature && _signature && pubKey.verify(signatureHash, _signature)) { unmatched.some(function (signature, i) {
// use .splice to remove the signature from the list, so we won't verify it again if (!pubKey.verify(signatureHash, signature)) return false
signature = _signatures.splice(_sigIdx, 1)[0] match = signature
}
// remove matched signature from unmatched
unmatched.splice(i, 1)
return true
}) })
return signature || ops.OP_0 return match || undefined
}) })
} }