-
Notifications
You must be signed in to change notification settings - Fork 2.2k
TransactionBuilder P2SH Multisig order of signatures (alternative implementation) #349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,7 +178,7 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { | |
| var scriptSig | ||
| var scriptType = input.scriptType | ||
|
|
||
| var signatures = input.signatures.map(function(signature) { | ||
| var signatures = input.signatures.filter(function(signature) { return !!signature }).map(function(signature) { | ||
| return signature.toScriptSignature(input.hashType) | ||
| }) | ||
|
|
||
|
|
@@ -223,7 +223,7 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT | |
| var prevOutScript = this.prevOutScripts[index] | ||
| var prevOutType = this.prevOutTypes[index] | ||
|
|
||
| var scriptType, hash | ||
| var scriptType, hash, pubKeys | ||
| if (redeemScript) { | ||
| prevOutScript = prevOutScript || scripts.scriptHashOutput(redeemScript.getHash()) | ||
| prevOutType = prevOutType || 'scripthash' | ||
|
|
@@ -235,6 +235,13 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT | |
| assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH') | ||
| assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)') | ||
|
|
||
| // for multisig we need to get the pubKeys from the redeemScript since their order is important | ||
| if (scriptType === 'multisig') { | ||
| pubKeys = redeemScript.chunks.slice(1, -2).map(function(pubKey) { | ||
| return ECPubKey.fromBuffer(pubKey) | ||
| }) | ||
| } | ||
|
|
||
| hash = this.tx.hashForSignature(index, redeemScript, hashType) | ||
|
|
||
| } else { | ||
|
|
@@ -268,8 +275,25 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT | |
| assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') | ||
|
|
||
| var signature = privKey.sign(hash) | ||
| input.pubKeys.push(privKey.pub) | ||
| input.signatures.push(signature) | ||
|
|
||
| if (pubKeys) { | ||
| var multisigSignatures = {}; | ||
| input.signatures.filter(function(signature) { return !!signature }).forEach(function(signature) { | ||
| pubKeys.forEach(function(pubKey, i) { | ||
| if (!multisigSignatures[i] && pubKey.verify(hash, signature)) { | ||
| multisigSignatures[i] = signature | ||
| } | ||
| }) | ||
| }) | ||
| input.signatures = pubKeys.map(function(pubKey, i) { return multisigSignatures[i] || null }) | ||
| var signatureIndex = pubKeys.map(function(pubKey) { return pubKey.toBuffer().toString('hex') }).indexOf(privKey.pub.toBuffer().toString('hex')) | ||
| assert.notEqual(signatureIndex, -1) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it's 'correct' to fail here if we're esentially signing with a privKey for which there is no pubKey?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a tonne of fun to deal with |
||
| input.signatures[signatureIndex] = signature; | ||
| input.pubKeys = pubKeys | ||
| } else { | ||
| input.signatures.push(signature) | ||
| input.pubKeys.push(privKey.pub) | ||
| } | ||
| } | ||
|
|
||
| module.exports = TransactionBuilder | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a better way to do
.indexOffor the pubKey?