TxBuilder: fix out-of-order multisignature signing

This commit is contained in:
Daniel Cousens 2015-01-06 15:50:13 +11:00
parent 4c9fd6010e
commit 23a37fb771
2 changed files with 22 additions and 16 deletions

View file

@ -49,13 +49,14 @@ function extractInput(txIn) {
break break
case 'multisig': case 'multisig':
parsed = scriptSig.chunks.slice(1).filter(function(chunk) { signatures = scriptSig.chunks.slice(1).map(function(chunk) {
return chunk !== ops.OP_0 if (chunk === ops.OP_0) return chunk
}).map(ECSignature.parseScriptSignature)
hashType = parsed[0].hashType var parsed = ECSignature.parseScriptSignature(chunk)
signatures = parsed.map(function(p) { return p.signature }) hashType = parsed.hashType
initialized = true
return parsed.signature
})
if (redeemScript) { if (redeemScript) {
pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer)
@ -124,10 +125,8 @@ TransactionBuilder.fromTransaction = function(transaction) {
// Extract/add signatures // Extract/add signatures
txb.inputs = transaction.ins.map(function(txIn) { txb.inputs = transaction.ins.map(function(txIn) {
// Coinbase inputs not supported // TODO: remove me after testcase added
assert(!Array.prototype.every.call(txIn.hash, function(x) { assert(!isCoinbase(txIn.hash), 'coinbase inputs not supported')
return x === 0
}), 'coinbase inputs not supported')
// Ignore empty scripts // Ignore empty scripts
if (txIn.script.buffer.length === 0) return if (txIn.script.buffer.length === 0) return
@ -236,9 +235,17 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) {
case 'multisig': case 'multisig':
assert(input.signatures, 'Transaction is missing signatures') assert(input.signatures, 'Transaction is missing signatures')
var signatures = input.signatures.map(function(signature) { // Array.prototype.map is sparse-compatible
var msSignatures = input.signatures.map(function(signature) {
return signature.toScriptSignature(input.hashType) return signature.toScriptSignature(input.hashType)
}).filter(function(signature) { return !!signature }) })
// fill in blanks with OP_0
for (var i = 0; i < msSignatures.length; ++i) {
if (msSignatures[i]) continue
msSignatures[i] = ops.OP_0
}
var redeemScript = allowIncomplete ? undefined : input.redeemScript var redeemScript = allowIncomplete ? undefined : input.redeemScript
scriptSig = scripts.multisigInput(signatures, redeemScript) scriptSig = scripts.multisigInput(signatures, redeemScript)

View file

@ -276,11 +276,10 @@ describe('TransactionBuilder', function() {
}) })
}) })
// TODO: test for reverse order signing it('works for the out-of-order P2SH multisig case', function() {
it('works for the P2SH multisig case', function() {
var privKeys = [ var privKeys = [
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT",
"91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx"
].map(ECKey.fromWIF) ].map(ECKey.fromWIF)
var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG") var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG")