From 906accdc0f8321071d9f334e1fddf01eba711f80 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 12 Dec 2014 12:48:04 +1100 Subject: [PATCH 01/30] TxBuilder: extract extractSignatures to free function --- src/transaction_builder.js | 131 ++++++++++++++++++++----------------- 1 file changed, 70 insertions(+), 61 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 2cf785c..21e5c86 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -16,6 +16,71 @@ function TransactionBuilder() { this.tx = new Transaction() } +function extractSignature(txIn) { + assert(!Array.prototype.every.call(txIn.hash, function(x) { + return x === 0 + }), 'coinbase inputs not supported') + + var redeemScript + var scriptSig = txIn.script + var scriptType = scripts.classifyInput(scriptSig, true) + + // Re-classify if P2SH + if (scriptType === 'scripthash') { + redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0]) + scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1)) + + scriptType = scripts.classifyInput(scriptSig, true) + assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') + } + + // Extract hashType, pubKeys and signatures + var hashType, parsed, pubKeys, signatures + + switch (scriptType) { + case 'pubkeyhash': + parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1]) + + hashType = parsed.hashType + pubKeys = [pubKey] + signatures = [parsed.signature] + + break + + case 'multisig': + parsed = scriptSig.chunks.slice(1).filter(function(chunk) { + return chunk !== ops.OP_0 + }).map(ECSignature.parseScriptSignature) + + hashType = parsed[0].hashType + pubKeys = [] + signatures = parsed.map(function(p) { return p.signature }) + + break + + case 'pubkey': + parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + + hashType = parsed.hashType + pubKeys = [] + signatures = [parsed.signature] + + break + + default: + assert(false, scriptType + ' inputs not supported') + } + + return { + hashType: hashType, + pubKeys: pubKeys, + redeemScript: redeemScript, + scriptType: scriptType, + signatures: signatures + } +} + // Static constructors TransactionBuilder.fromTransaction = function(transaction) { var txb = new TransactionBuilder() @@ -35,72 +100,16 @@ TransactionBuilder.fromTransaction = function(transaction) { }) // Extract/add signatures - transaction.ins.forEach(function(txIn, i) { - // Ignore empty scripts - if (txIn.script.buffer.length === 0) return - + txb.signatures = transaction.ins.map(function(txIn) { + // Coinbase inputs not supported assert(!Array.prototype.every.call(txIn.hash, function(x) { return x === 0 }), 'coinbase inputs not supported') - var redeemScript - var scriptSig = txIn.script - var scriptType = scripts.classifyInput(scriptSig, true) + // Ignore empty scripts + if (txIn.script.buffer.length === 0) return - // Re-classify if P2SH - if (scriptType === 'scripthash') { - redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0]) - scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1)) - - scriptType = scripts.classifyInput(scriptSig, true) - assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') - } - - // Extract hashType, pubKeys and signatures - var hashType, parsed, pubKeys, signatures - - switch (scriptType) { - case 'pubkeyhash': - parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) - var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1]) - - hashType = parsed.hashType - pubKeys = [pubKey] - signatures = [parsed.signature] - - break - - case 'multisig': - parsed = scriptSig.chunks.slice(1).filter(function(chunk) { - return chunk !== ops.OP_0 - }).map(ECSignature.parseScriptSignature) - - hashType = parsed[0].hashType - pubKeys = [] - signatures = parsed.map(function(p) { return p.signature }) - - break - - case 'pubkey': - parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) - - hashType = parsed.hashType - pubKeys = [] - signatures = [parsed.signature] - - break - - default: - assert(false, scriptType + ' inputs not supported') - } - - txb.signatures[i] = { - hashType: hashType, - pubKeys: pubKeys, - redeemScript: redeemScript, - scriptType: scriptType, - signatures: signatures - } + return extractSignature(txIn) }) return txb From 46db11e04a60b89fb193a95244090d6dac5bafc0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 6 Jan 2015 16:05:38 +1100 Subject: [PATCH 02/30] TxBuilder: extract isCoinbaseHash function --- src/transaction_builder.js | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 21e5c86..3148386 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -7,20 +7,13 @@ var ECSignature = require('./ecsignature') var Script = require('./script') var Transaction = require('./transaction') -function TransactionBuilder() { - this.prevOutMap = {} - this.prevOutScripts = {} - this.prevOutTypes = {} - - this.signatures = [] - this.tx = new Transaction() +function isCoinbase(txHash) { + return Array.prototype.every.call(txHash, function(x) { + return x === 0 + }) } function extractSignature(txIn) { - assert(!Array.prototype.every.call(txIn.hash, function(x) { - return x === 0 - }), 'coinbase inputs not supported') - var redeemScript var scriptSig = txIn.script var scriptType = scripts.classifyInput(scriptSig, true) @@ -81,7 +74,15 @@ function extractSignature(txIn) { } } -// Static constructors +function TransactionBuilder() { + this.prevOutMap = {} + this.prevOutScripts = {} + this.prevOutTypes = {} + + this.signatures = [] + this.tx = new Transaction() +} + TransactionBuilder.fromTransaction = function(transaction) { var txb = new TransactionBuilder() @@ -101,10 +102,8 @@ TransactionBuilder.fromTransaction = function(transaction) { // Extract/add signatures txb.signatures = transaction.ins.map(function(txIn) { - // Coinbase inputs not supported - assert(!Array.prototype.every.call(txIn.hash, function(x) { - return x === 0 - }), 'coinbase inputs not supported') + // TODO: remove me after testcase added + assert(!isCoinbase(txIn.hash), 'coinbase inputs not supported') // Ignore empty scripts if (txIn.script.buffer.length === 0) return @@ -115,7 +114,6 @@ TransactionBuilder.fromTransaction = function(transaction) { return txb } -// Operations TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) { var prevOutHash From ebe34db8df55ba078d7c56a372dd44b9303b1628 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 5 Jan 2015 14:50:54 +1100 Subject: [PATCH 03/30] TxBuilder: avoid var redeclaration due to hoisting --- src/transaction_builder.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 3148386..2f110fc 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -191,9 +191,8 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { switch (scriptType) { case 'pubkeyhash': - var signature = signatures[0] var pubKey = input.pubKeys[0] - scriptSig = scripts.pubKeyHashInput(signature, pubKey) + scriptSig = scripts.pubKeyHashInput(signatures[0], pubKey) break @@ -204,8 +203,7 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { break case 'pubkey': - var signature = signatures[0] - scriptSig = scripts.pubKeyInput(signature) + scriptSig = scripts.pubKeyInput(signatures[0]) break From ba97b5ee3422df331def82670680febd961704b5 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 12 Dec 2014 14:48:31 +1100 Subject: [PATCH 04/30] TxBuilder: re-order to avoid mutation in case of failure --- src/transaction_builder.js | 19 +++++++++++-------- test/transaction_builder.js | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 2f110fc..3581726 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -253,25 +253,28 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT hash = this.tx.hashForSignature(index, prevOutScript, hashType) } - this.prevOutScripts[index] = prevOutScript - this.prevOutTypes[index] = prevOutType - - if (!(index in this.signatures)) { - this.signatures[index] = { + var input = this.signatures[index] + if (!input) { + input = { hashType: hashType, pubKeys: [], redeemScript: redeemScript, scriptType: scriptType, signatures: [] } + + this.signatures[index] = input + } else { assert.equal(scriptType, 'multisig', scriptType + ' doesn\'t support multiple signatures') + assert.equal(input.hashType, hashType, 'Inconsistent hashType') + assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') } - var input = this.signatures[index] - assert.equal(input.hashType, hashType, 'Inconsistent hashType') - assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') + this.prevOutScripts[index] = prevOutScript + this.prevOutTypes[index] = prevOutType + // TODO: order signatures for multisig, enforce m < n var signature = privKey.sign(hash) input.pubKeys.push(privKey.pub) input.signatures.push(signature) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 8e2b528..cde634b 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -153,6 +153,29 @@ describe('TransactionBuilder', function() { }, /RedeemScript can\'t be P2SH/) }) + it('throws if hashType is inconsistent', function() { + var redeemScript = scripts.multisigOutput(1, [privKey.pub]) + + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey, redeemScript, 83) + + assert.throws(function() { + txb.sign(0, privKey, redeemScript, 82) + }, /Inconsistent hashType/) + }) + + it('throws if redeemScript is inconsistent', function() { + var firstScript = scripts.multisigOutput(1, [privKey.pub]) + var otherScript = scripts.multisigOutput(2, [privKey.pub, privKey.pub]) + + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey, firstScript) + + assert.throws(function() { + txb.sign(0, privKey, otherScript) + }, /Inconsistent redeemScript/) + }) + it('throws if redeemScript not supported', function() { txb.addInput(prevTxHash, 0) From c29b233744438e10dcd991749991f975142cfe48 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 13:30:35 +1100 Subject: [PATCH 05/30] TxBuilder: build convenience functions don't need extra line breaks --- src/transaction_builder.js | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 3581726..ede0283 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -162,14 +162,8 @@ TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { return this.tx.addOutput(scriptPubKey, value) } -TransactionBuilder.prototype.build = function() { - return this.__build(false) -} - -TransactionBuilder.prototype.buildIncomplete = function() { - return this.__build(true) -} - +TransactionBuilder.prototype.build = function() { return this.__build(false) } +TransactionBuilder.prototype.buildIncomplete = function() { return this.__build(true) } TransactionBuilder.prototype.__build = function(allowIncomplete) { if (!allowIncomplete) { assert(this.tx.ins.length > 0, 'Transaction has no inputs') From dfe74fa0d23b2a0c4ba3753fabfbd27405b257b3 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 12 Dec 2014 15:19:03 +1100 Subject: [PATCH 06/30] TxBuilder: sign now signs inputs in known publicKey order --- src/transaction_builder.js | 40 +++++++++++++++++++++++++++---------- test/transaction_builder.js | 21 +++++++++++++++++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index ede0283..d05ee5b 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -33,10 +33,9 @@ function extractSignature(txIn) { switch (scriptType) { case 'pubkeyhash': parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) - var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1]) - hashType = parsed.hashType - pubKeys = [pubKey] + + pubKeys = [ECPubKey.fromBuffer(scriptSig.chunks[1])] signatures = [parsed.signature] break @@ -50,6 +49,10 @@ function extractSignature(txIn) { pubKeys = [] signatures = parsed.map(function(p) { return p.signature }) + if (redeemScript) { + pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) + } + break case 'pubkey': @@ -59,6 +62,10 @@ function extractSignature(txIn) { pubKeys = [] signatures = [parsed.signature] + if (redeemScript) { + pubKeys = [ECPubKey.fromBuffer(redeemScript.chunks[0])] + } + break default: @@ -249,15 +256,26 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT var input = this.signatures[index] if (!input) { + var pubKeys = [] + + if (redeemScript && scriptType === 'multisig') { + pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) + + } else { + pubKeys.push(privKey.pub) + } + input = { hashType: hashType, - pubKeys: [], + pubKeys: pubKeys, redeemScript: redeemScript, scriptType: scriptType, signatures: [] } this.signatures[index] = input + this.prevOutScripts[index] = prevOutScript + this.prevOutTypes[index] = prevOutType } else { assert.equal(scriptType, 'multisig', scriptType + ' doesn\'t support multiple signatures') @@ -265,13 +283,15 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') } - this.prevOutScripts[index] = prevOutScript - this.prevOutTypes[index] = prevOutType + // enforce signing in order of public keys + assert(input.pubKeys.some(function(pubKey, i) { + if (!privKey.pub.Q.equals(pubKey.Q)) return false // FIXME: could be better? - // TODO: order signatures for multisig, enforce m < n - var signature = privKey.sign(hash) - input.pubKeys.push(privKey.pub) - input.signatures.push(signature) + assert(!input.signatures[i], 'Signature already exists') + input.signatures[i] = privKey.sign(hash) + + return true + }), 'privateKey cannot sign for this input') } module.exports = TransactionBuilder diff --git a/test/transaction_builder.js b/test/transaction_builder.js index cde634b..a53cdaf 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -184,6 +184,27 @@ describe('TransactionBuilder', function() { }, /RedeemScript not supported \(nonstandard\)/) }) }) + + it('throws if signature already exists', function() { + var redeemScript = scripts.multisigOutput(1, [privKey.pub]) + + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey, redeemScript) + + assert.throws(function() { + txb.sign(0, privKey, redeemScript) + }, /Signature already exists/) + }) + + it('throws if private key is unable to sign for that input', function() { + var redeemScript = scripts.multisigOutput(1, [privKey.pub]) + + txb.addInput(prevTxHash, 0) + + assert.throws(function() { + txb.sign(0, ECKey.makeRandom(), redeemScript) + }, /privateKey cannot sign for this input/) + }) }) describe('build', function() { From b629a03c98521daf62f6b8b89aa82147d370ac26 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 5 Jan 2015 15:30:04 +1100 Subject: [PATCH 07/30] TxBuilder: rename prevOutMap to prevTxMap --- src/transaction_builder.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index d05ee5b..ddddb1a 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -82,7 +82,7 @@ function extractSignature(txIn) { } function TransactionBuilder() { - this.prevOutMap = {} + this.prevTxMap = {} this.prevOutScripts = {} this.prevOutTypes = {} @@ -151,10 +151,10 @@ TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOu }), 'No, this would invalidate signatures') var prevOut = prevOutHash.toString('hex') + ':' + index - assert(!(prevOut in this.prevOutMap), 'Transaction is already an input') + assert(!(prevOut in this.prevTxMap), 'Transaction is already an input') var vout = this.tx.addInput(prevOutHash, index, sequence) - this.prevOutMap[prevOut] = true + this.prevTxMap[prevOut] = true this.prevOutScripts[vout] = prevOutScript this.prevOutTypes[vout] = prevOutType From 35fa86c1f941835547f37a6ce4d04e580ae7f771 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 5 Jan 2015 14:55:52 +1100 Subject: [PATCH 08/30] tests: add [failing] raw multisig fixture for TxBuilder --- test/fixtures/transaction_builder.json | 25 +++++++++++++++++++++---- test/transaction_builder.js | 1 - 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 5b0bb32..5964311 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -3,7 +3,6 @@ "build": [ { "description": "pubKeyHash->pubKeyHash 1:1 transaction", - "txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e", "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { @@ -23,7 +22,6 @@ }, { "description": "pubKey->pubKeyHash 1:1 transaction", - "txid": "a900dea133a3c51e9fe55d82bf4a4f50a4c3ac6e380c841f93651a076573320c", "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { @@ -44,7 +42,6 @@ }, { "description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction", - "txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24", "txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", "inputs": [ { @@ -64,9 +61,29 @@ } ] }, + { + "description": "2-of-2 raw multisig -> pubKeyHash 1:1 Transaction", + "txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f07149000000009100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d54801ffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", + "inputs": [ + { + "index": 0, + "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "prevTxScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", + "privKeys": [ + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + ] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 faf1d99bf040ea9c7f8cc9f14ac6733ad75ce246 OP_EQUALVERIFY OP_CHECKSIG", + "value": 10000 + } + ] + }, { "description": "Transaction w/ non-zero vin inputs", - "txid": "7d9b699f26765fdfdd598223a952a6e129f8c159e2e05e911af822ee743fa745", "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205c80bbb5125b35d5e5a8324b1336832d29a6fc004859c8a9ff6bef47ba7fc348022018612216e57a521b2c4543f1f4fd738a76814c37c074e88adfe12464fff31cf901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { diff --git a/test/transaction_builder.js b/test/transaction_builder.js index a53cdaf..f743300 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -246,7 +246,6 @@ describe('TransactionBuilder', function() { var tx = txb.build() - assert.equal(tx.getId(), f.txid) assert.equal(tx.toHex(), f.txhex) }) }) From 396e4d4235c2e29a7558457803c81df3a36be5ef Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 6 Jan 2015 11:00:49 +1100 Subject: [PATCH 09/30] tests: add [failing] test for nulldata signing --- test/fixtures/transaction_builder.json | 5 +++- test/transaction_builder.js | 41 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 5964311..86ae18b 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -187,7 +187,9 @@ "value": 1000 } ] - }, + } + ], + "sign": [ { "exception": "nulldata not supported", "inputs": [ @@ -195,6 +197,7 @@ "index": 0, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", + "throws": true, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" ] diff --git a/test/transaction_builder.js b/test/transaction_builder.js index f743300..47f5d3e 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -205,6 +205,47 @@ describe('TransactionBuilder', function() { txb.sign(0, ECKey.makeRandom(), redeemScript) }, /privateKey cannot sign for this input/) }) + + fixtures.invalid.sign.forEach(function(f) { + it('throws on ' + f.exception, function() { + f.inputs.forEach(function(input) { + var prevTxScript + + if (input.prevTxScript) { + prevTxScript = Script.fromASM(input.prevTxScript) + } + + txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript) + }) + + f.outputs.forEach(function(output) { + var script = Script.fromASM(output.script) + + txb.addOutput(script, output.value) + }) + + f.inputs.forEach(function(input, index) { + var redeemScript + + if (input.redeemScript) { + redeemScript = Script.fromASM(input.redeemScript) + } + + input.privKeys.forEach(function(wif) { + var privKey = ECKey.fromWIF(wif) + + if (!input.throws) { + txb.sign(index, privKey, redeemScript) + + } else { + assert.throws(function() { + txb.sign(index, privKey, redeemScript) + }, new RegExp(f.exception)) + } + }) + }) + }) + }) }) describe('build', function() { From b048627a59a7bfd4637956fc33ccef43ebf8b719 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 6 Jan 2015 11:18:04 +1100 Subject: [PATCH 10/30] tests: move TxBuilder.sign tests to fixtures --- src/transaction_builder.js | 1 + test/fixtures/transaction_builder.json | 146 ++++++++++++++++++++++++- test/transaction_builder.js | 72 +----------- 3 files changed, 147 insertions(+), 72 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index ddddb1a..4180c5e 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -239,6 +239,7 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT scriptType = scripts.classifyOutput(redeemScript) assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH') + assert.notEqual(scriptType, 'nulldata', 'RedeemScript not supported (nulldata)') assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)') hash = this.tx.hashForSignature(index, redeemScript, hashType) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 86ae18b..c184ef0 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -190,6 +190,148 @@ } ], "sign": [ + { + "exception": "pubkeyhash doesn\\'t support multiple signatures", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ], + "throws": 1 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "RedeemScript not supported \\(nulldata\\)", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ], + "redeemScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", + "throws": 0 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "PrevOutScript is P2SH, missing redeemScript", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "prevTxScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL", + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ], + "throws": 0 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "RedeemScript can\\'t be P2SH", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ], + "redeemScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL", + "throws": 0 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "PrevOutScript must be P2SH", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ], + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", + "throws": 0 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "Signature already exists", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ], + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", + "throws": 1 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "privateKey cannot sign for this input", + "inputs": [ + { + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [ + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx" + ], + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", + "throws": 0 + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, { "exception": "nulldata not supported", "inputs": [ @@ -197,10 +339,10 @@ "index": 0, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", - "throws": true, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ] + ], + "throws": 0 } ], "outputs": [ diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 47f5d3e..6d304f0 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -106,27 +106,6 @@ describe('TransactionBuilder', function() { }) }) - it('throws if scriptType doesn\'t support multiple signatures', function() { - txb.addInput(prevTxHash, 0) - txb.sign(0, privKey) - - assert.throws(function() { - txb.sign(0, privKey) - }, /pubkeyhash doesn\'t support multiple signatures/) - }) - - describe('when redeemScript is undefined', function() { - it('throws if prevOutScript is P2SH', function() { - var privScriptP2SH = scripts.scriptHashOutput(privScript.getHash()) - - txb.addInput(prevTxHash, 0, undefined, privScriptP2SH) - - assert.throws(function() { - txb.sign(0, privKey) - }, /PrevOutScript is P2SH, missing redeemScript/) - }) - }) - describe('when redeemScript is defined', function() { it('assumes scriptHash', function() { txb.addInput(prevTxHash, 0) @@ -135,24 +114,6 @@ describe('TransactionBuilder', function() { assert.equal(txb.signatures[0].redeemScript, privScript) }) - it('throws if prevOutScript is not P2SH', function() { - txb.addInput(prevTx, 0) - - assert.throws(function() { - txb.sign(0, privKey, privScript) - }, /PrevOutScript must be P2SH/) - }) - - it('throws if redeemScript is P2SH', function() { - txb.addInput(prevTxHash, 0) - - var privScriptP2SH = scripts.scriptHashOutput(privScript.getHash()) - - assert.throws(function() { - txb.sign(0, privKey, privScriptP2SH) - }, /RedeemScript can\'t be P2SH/) - }) - it('throws if hashType is inconsistent', function() { var redeemScript = scripts.multisigOutput(1, [privKey.pub]) @@ -175,35 +136,6 @@ describe('TransactionBuilder', function() { txb.sign(0, privKey, otherScript) }, /Inconsistent redeemScript/) }) - - it('throws if redeemScript not supported', function() { - txb.addInput(prevTxHash, 0) - - assert.throws(function() { - txb.sign(0, privKey, Script.EMPTY) - }, /RedeemScript not supported \(nonstandard\)/) - }) - }) - - it('throws if signature already exists', function() { - var redeemScript = scripts.multisigOutput(1, [privKey.pub]) - - txb.addInput(prevTxHash, 0) - txb.sign(0, privKey, redeemScript) - - assert.throws(function() { - txb.sign(0, privKey, redeemScript) - }, /Signature already exists/) - }) - - it('throws if private key is unable to sign for that input', function() { - var redeemScript = scripts.multisigOutput(1, [privKey.pub]) - - txb.addInput(prevTxHash, 0) - - assert.throws(function() { - txb.sign(0, ECKey.makeRandom(), redeemScript) - }, /privateKey cannot sign for this input/) }) fixtures.invalid.sign.forEach(function(f) { @@ -231,10 +163,10 @@ describe('TransactionBuilder', function() { redeemScript = Script.fromASM(input.redeemScript) } - input.privKeys.forEach(function(wif) { + input.privKeys.forEach(function(wif, i) { var privKey = ECKey.fromWIF(wif) - if (!input.throws) { + if (input.throws !== i) { txb.sign(index, privKey, redeemScript) } else { From 4c9fd6010eeb5bf364bb101466c8781e7d312bd8 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 6 Jan 2015 12:33:49 +1100 Subject: [PATCH 11/30] TxBuilder: fix failing test for non-standard/multisig inputs Instead of failing in `fromTransaction`, TxBuilder will now only fail in `sign` if you attempt to sign a non-standard input. Transactions with non-standard inputs can only be built with buildIncomplete() (for now). --- src/transaction_builder.js | 263 +++++++++++++++++-------- test/fixtures/transaction_builder.json | 18 +- test/transaction_builder.js | 27 ++- 3 files changed, 196 insertions(+), 112 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 4180c5e..d73555b 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -13,31 +13,39 @@ function isCoinbase(txHash) { }) } -function extractSignature(txIn) { +function extractInput(txIn) { var redeemScript var scriptSig = txIn.script - var scriptType = scripts.classifyInput(scriptSig, true) + var prevOutScript + var prevOutType = scripts.classifyInput(scriptSig, true) + var scriptType // Re-classify if P2SH - if (scriptType === 'scripthash') { + if (prevOutType === 'scripthash') { redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0]) - scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1)) + prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) + scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1)) scriptType = scripts.classifyInput(scriptSig, true) assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') + + } else { + scriptType = prevOutType } // Extract hashType, pubKeys and signatures - var hashType, parsed, pubKeys, signatures + var hashType, initialized, parsed, pubKeys, signatures switch (scriptType) { case 'pubkeyhash': parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) hashType = parsed.hashType - pubKeys = [ECPubKey.fromBuffer(scriptSig.chunks[1])] signatures = [parsed.signature] + initialized = true + prevOutScript = pubKeys[0].getAddress().toOutputScript() + break case 'multisig': @@ -46,8 +54,8 @@ function extractSignature(txIn) { }).map(ECSignature.parseScriptSignature) hashType = parsed[0].hashType - pubKeys = [] signatures = parsed.map(function(p) { return p.signature }) + initialized = true if (redeemScript) { pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) @@ -57,11 +65,11 @@ function extractSignature(txIn) { case 'pubkey': parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) - hashType = parsed.hashType - pubKeys = [] signatures = [parsed.signature] + initialized = true + if (redeemScript) { pubKeys = [ECPubKey.fromBuffer(redeemScript.chunks[0])] } @@ -69,11 +77,18 @@ function extractSignature(txIn) { break default: - assert(false, scriptType + ' inputs not supported') + if (redeemScript) { + initialized = true + } + + break } return { hashType: hashType, + initialized: initialized, + prevOutScript: prevOutScript, + prevOutType: prevOutType, pubKeys: pubKeys, redeemScript: redeemScript, scriptType: scriptType, @@ -86,7 +101,7 @@ function TransactionBuilder() { this.prevOutScripts = {} this.prevOutTypes = {} - this.signatures = [] + this.inputs = [] this.tx = new Transaction() } @@ -108,14 +123,16 @@ TransactionBuilder.fromTransaction = function(transaction) { }) // Extract/add signatures - txb.signatures = transaction.ins.map(function(txIn) { - // TODO: remove me after testcase added - assert(!isCoinbase(txIn.hash), 'coinbase inputs not supported') + txb.inputs = transaction.ins.map(function(txIn) { + // Coinbase inputs not supported + assert(!Array.prototype.every.call(txIn.hash, function(x) { + return x === 0 + }), 'coinbase inputs not supported') // Ignore empty scripts if (txIn.script.buffer.length === 0) return - return extractSignature(txIn) + return extractInput(txIn) }) return txb @@ -139,31 +156,51 @@ TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOu } - var prevOutType - if (prevOutScript !== undefined) { - prevOutType = scripts.classifyOutput(prevOutScript) + var input = {} + if (prevOutScript) { + var prevOutType = scripts.classifyOutput(prevOutScript) - assert.notEqual(prevOutType, 'nonstandard', 'PrevOutScript not supported (nonstandard)') + // if we can, extract pubKey information + switch (prevOutType) { + case 'multisig': + input.pubKeys = prevOutScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) + break + + case 'pubkey': + input.pubKeys = prevOutScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) + break + } + + if (prevOutType !== 'scripthash') { + input.scriptType = prevOutType + } + + input.prevOutScript = prevOutScript + input.prevOutType = prevOutType } - assert(this.signatures.every(function(input) { - return input.hashType & Transaction.SIGHASH_ANYONECANPAY + assert(this.inputs.every(function(input2) { + if (input2.hashType === undefined) return true + + return input2.hashType & Transaction.SIGHASH_ANYONECANPAY }), 'No, this would invalidate signatures') var prevOut = prevOutHash.toString('hex') + ':' + index assert(!(prevOut in this.prevTxMap), 'Transaction is already an input') var vout = this.tx.addInput(prevOutHash, index, sequence) + this.prevTxMap[prevOut] = true - this.prevOutScripts[vout] = prevOutScript - this.prevOutTypes[vout] = prevOutType + this.inputs[vout] = input return vout } TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { - assert(this.signatures.every(function(signature) { - return (signature.hashType & 0x1f) === Transaction.SIGHASH_SINGLE + assert(this.inputs.every(function(input) { + if (input.hashType === undefined) return true + + return (input.hashType & 0x1f) === Transaction.SIGHASH_SINGLE }), 'No, this would invalidate signatures') return this.tx.addOutput(scriptPubKey, value) @@ -175,41 +212,50 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { if (!allowIncomplete) { assert(this.tx.ins.length > 0, 'Transaction has no inputs') assert(this.tx.outs.length > 0, 'Transaction has no outputs') - assert(this.signatures.length > 0, 'Transaction has no signatures') - assert.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures') } var tx = this.tx.clone() // Create script signatures from signature meta-data - this.signatures.forEach(function(input, index) { + this.inputs.forEach(function(input, index) { + if (!allowIncomplete) { + assert(input.initialized, 'Transaction is not complete') + } + var scriptSig - var scriptType = input.scriptType - var signatures = input.signatures.map(function(signature) { - return signature.toScriptSignature(input.hashType) - }) - - switch (scriptType) { + switch (input.scriptType) { case 'pubkeyhash': - var pubKey = input.pubKeys[0] - scriptSig = scripts.pubKeyHashInput(signatures[0], pubKey) + assert(input.signatures, 'Transaction is missing signatures') + assert.equal(input.signatures.length, 1, 'Transaction is missing signatures') + var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) + scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) break case 'multisig': + assert(input.signatures, 'Transaction is missing signatures') + + var signatures = input.signatures.map(function(signature) { + return signature.toScriptSignature(input.hashType) + }).filter(function(signature) { return !!signature }) + var redeemScript = allowIncomplete ? undefined : input.redeemScript scriptSig = scripts.multisigInput(signatures, redeemScript) - break case 'pubkey': - scriptSig = scripts.pubKeyInput(signatures[0]) + assert(input.signatures, 'Transaction is missing signatures') + assert.equal(input.signatures.length, 1, 'Transaction is missing signatures') + var pkSignature = input.signatures[0].toScriptSignature(input.hashType) + scriptSig = scripts.pubKeyInput(pkSignature) break default: - assert(false, scriptType + ' not supported') + if (allowIncomplete) return + + assert(false, input.scriptType + ' not supported') } if (input.redeemScript) { @@ -223,73 +269,116 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { } TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { - assert(this.tx.ins.length >= index, 'No input at index: ' + index) + assert(index in this.inputs, 'No input at index: ' + index) hashType = hashType || Transaction.SIGHASH_ALL - var prevOutScript = this.prevOutScripts[index] - var prevOutType = this.prevOutTypes[index] + var input = this.inputs[index] - var scriptType, hash - if (redeemScript) { - prevOutScript = prevOutScript || scripts.scriptHashOutput(redeemScript.getHash()) - prevOutType = prevOutType || 'scripthash' - - assert.equal(prevOutType, 'scripthash', 'PrevOutScript must be P2SH') - - scriptType = scripts.classifyOutput(redeemScript) - - assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH') - assert.notEqual(scriptType, 'nulldata', 'RedeemScript not supported (nulldata)') - assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)') - - hash = this.tx.hashForSignature(index, redeemScript, hashType) - - } else { - prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript() - prevOutType = prevOutType || 'pubkeyhash' - - assert.notEqual(prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript') - - scriptType = prevOutType - - hash = this.tx.hashForSignature(index, prevOutScript, hashType) + if (input.hashType !== undefined) { + assert.equal(input.hashType, hashType, 'Inconsistent hashType') } - var input = this.signatures[index] - if (!input) { - var pubKeys = [] - - if (redeemScript && scriptType === 'multisig') { - pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) + // are we already initialized? + if (input.initialized) { + if (input.prevOutType === 'scripthash') { + assert(input.redeemScript, 'PrevOutScript is P2SH, missing redeemScript') } else { - pubKeys.push(privKey.pub) + assert(!input.redeemScript, 'PrevOutScript must be P2SH') } - input = { - hashType: hashType, - pubKeys: pubKeys, - redeemScript: redeemScript, - scriptType: scriptType, - signatures: [] + // redeemScript only needed to initialize, but if provided again, enforce consistency + if (redeemScript) { + assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') } - this.signatures[index] = input - this.prevOutScripts[index] = prevOutScript - this.prevOutTypes[index] = prevOutType + // if signatures already exist, enforce multisig scriptType + if (input.signatures.length > 0) { + assert.equal(input.scriptType, 'multisig', input.scriptType + ' doesn\'t support multiple signatures') + } + + // initialize it + } else { + if (redeemScript) { + if (input.prevOutScript) { + assert.equal(input.prevOutType, 'scripthash', 'PrevOutScript must be P2SH') + + var scriptHash = input.prevOutScript.chunks[1] + assert.deepEqual(scriptHash, redeemScript.getHash(), 'RedeemScript does not match ' + scriptHash.toString('hex')) + + } else { + input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) + input.prevOutType = 'scripthash' + } + + var scriptType = scripts.classifyOutput(redeemScript) + + switch (scriptType) { + case 'multisig': + input.pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) + break + + case 'pubkeyhash': + var pkh1 = redeemScript.chunks[2] + var pkh2 = privKey.pub.getAddress().hash + + assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input') + input.pubKeys = [privKey.pub] + break + + case 'pubkey': + input.pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) + break + + default: + assert(false, 'RedeemScript not supported (' + scriptType + ')') + } + + input.redeemScript = redeemScript + input.scriptType = scriptType + + } else { + assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript') + + if (!input.scriptType) { + input.prevOutScript = privKey.pub.getAddress().toOutputScript() + input.prevOutType = 'pubkeyhash' + input.pubKeys = [privKey.pub] + input.scriptType = input.prevOutType + } + } + + input.hashType = hashType + input.initialized = true + input.signatures = input.signatures || [] + } + + switch (input.scriptType) { + case 'pubkeyhash': + case 'multisig': + case 'pubkey': + break + + default: + assert(false, input.scriptType + ' not supported') + } + + var signatureHash + if (input.redeemScript) { + signatureHash = this.tx.hashForSignature(index, input.redeemScript, hashType) } else { - assert.equal(scriptType, 'multisig', scriptType + ' doesn\'t support multiple signatures') - assert.equal(input.hashType, hashType, 'Inconsistent hashType') - assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') + signatureHash = this.tx.hashForSignature(index, input.prevOutScript, hashType) } + var signature = privKey.sign(signatureHash) + // enforce signing in order of public keys assert(input.pubKeys.some(function(pubKey, i) { - if (!privKey.pub.Q.equals(pubKey.Q)) return false // FIXME: could be better? + if (!privKey.pub.Q.equals(pubKey.Q)) return false assert(!input.signatures[i], 'Signature already exists') - input.signatures[i] = privKey.sign(hash) + input.signatures[i] = signature return true }), 'privateKey cannot sign for this input') diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index c184ef0..1faebdf 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -150,7 +150,8 @@ "outputs": [] }, { - "exception": "Transaction has no signatures", + "description": "Incomplete transaction, nothing assumed", + "exception": "Transaction is not complete", "inputs": [ { "index": 0, @@ -166,7 +167,8 @@ ] }, { - "exception": "Transaction is missing signatures", + "description": "Incomplete transaction w/ prevTxScript defined", + "exception": "Transaction is not complete", "inputs": [ { "index": 0, @@ -178,6 +180,7 @@ { "index": 1, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "privKeys": [] } ], @@ -251,7 +254,7 @@ ] }, { - "exception": "RedeemScript can\\'t be P2SH", + "exception": "RedeemScript not supported \\(scripthash\\)", "inputs": [ { "index": 1, @@ -313,13 +316,14 @@ ] }, { + "description": "Wrong private key for multisig redeemScript", "exception": "privateKey cannot sign for this input", "inputs": [ { "index": 1, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "privKeys": [ - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx" + "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx" ], "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", "throws": 0 @@ -356,11 +360,7 @@ "fromTransaction": [ { "exception": "coinbase inputs not supported", - "hex": "01000000010000000000000000000000000000000000000000000000000000000000000000000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000" - }, - { - "exception": "nonstandard inputs not supported", - "hex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000023aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000" + "txHex": "01000000010000000000000000000000000000000000000000000000000000000000000000000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000" } ] } diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 6d304f0..ceed7e2 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -37,7 +37,7 @@ describe('TransactionBuilder', function() { assert.equal(txin.hash, prevTxHash) assert.equal(txin.index, 1) assert.equal(txin.sequence, 54) - assert.equal(txb.prevOutScripts[0], undefined) + assert.equal(txb.inputs[0].prevOutScript, undefined) }) it('accepts a txHash, index [, sequence number and scriptPubKey]', function() { @@ -48,7 +48,7 @@ describe('TransactionBuilder', function() { assert.equal(txin.hash, prevTxHash) assert.equal(txin.index, 1) assert.equal(txin.sequence, 54) - assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) + assert.equal(txb.inputs[0].prevOutScript, prevTx.outs[1].script) }) it('accepts a prevTx, index [and sequence number]', function() { @@ -59,7 +59,7 @@ describe('TransactionBuilder', function() { assert.deepEqual(txin.hash, prevTxHash) assert.equal(txin.index, 1) assert.equal(txin.sequence, 54) - assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) + assert.equal(txb.inputs[0].prevOutScript, prevTx.outs[1].script) }) it('returns the input index', function() { @@ -67,12 +67,6 @@ describe('TransactionBuilder', function() { assert.equal(txb.addInput(prevTxHash, 1), 1) }) - it('throws if prevOutScript is not supported', function() { - assert.throws(function() { - txb.addInput(prevTxHash, 0, undefined, Script.EMPTY) - }, /PrevOutScript not supported \(nonstandard\)/) - }) - it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() { txb.addInput(prevTxHash, 0) txb.sign(0, privKey) @@ -101,8 +95,8 @@ describe('TransactionBuilder', function() { txb.addInput(prevTxHash, 0) txb.sign(0, privKey) - assert.strictEqual(txb.signatures[0].redeemScript, undefined) - assert.equal(txb.signatures[0].scriptType, 'pubkeyhash') + assert.equal(txb.inputs[0].scriptType, 'pubkeyhash') + assert.equal(txb.inputs[0].redeemScript, undefined) }) }) @@ -111,7 +105,8 @@ describe('TransactionBuilder', function() { txb.addInput(prevTxHash, 0) txb.sign(0, privKey, privScript) - assert.equal(txb.signatures[0].redeemScript, privScript) + assert.equal(txb.inputs[0].prevOutType, 'scripthash') + assert.equal(txb.inputs[0].redeemScript, privScript) }) it('throws if hashType is inconsistent', function() { @@ -139,7 +134,7 @@ describe('TransactionBuilder', function() { }) fixtures.invalid.sign.forEach(function(f) { - it('throws on ' + f.exception, function() { + it('throws on ' + f.exception + ' (' + f.description + ')', function() { f.inputs.forEach(function(input) { var prevTxScript @@ -218,13 +213,12 @@ describe('TransactionBuilder', function() { if (f.locktime !== undefined) txb.tx.locktime = f.locktime var tx = txb.build() - assert.equal(tx.toHex(), f.txhex) }) }) fixtures.invalid.build.forEach(function(f) { - it('throws on ' + f.exception, function() { + it('throws on ' + f.exception + ' (' + f.description + ')', function() { f.inputs.forEach(function(input) { var prevTxScript @@ -274,7 +268,7 @@ describe('TransactionBuilder', function() { fixtures.invalid.fromTransaction.forEach(function(f) { it('throws on ' + f.exception, function() { - var tx = Transaction.fromHex(f.hex) + var tx = Transaction.fromHex(f.txHex) assert.throws(function() { TransactionBuilder.fromTransaction(tx) @@ -282,6 +276,7 @@ describe('TransactionBuilder', function() { }) }) + // TODO: test for reverse order signing it('works for the P2SH multisig case', function() { var privKeys = [ "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", From 23a37fb771df5e01bab7c4e616ae99bac85473a8 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 6 Jan 2015 15:50:13 +1100 Subject: [PATCH 12/30] TxBuilder: fix out-of-order multisignature signing --- src/transaction_builder.js | 31 +++++++++++++++++++------------ test/transaction_builder.js | 7 +++---- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index d73555b..e481fde 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -49,13 +49,14 @@ function extractInput(txIn) { break case 'multisig': - parsed = scriptSig.chunks.slice(1).filter(function(chunk) { - return chunk !== ops.OP_0 - }).map(ECSignature.parseScriptSignature) + signatures = scriptSig.chunks.slice(1).map(function(chunk) { + if (chunk === ops.OP_0) return chunk - hashType = parsed[0].hashType - signatures = parsed.map(function(p) { return p.signature }) - initialized = true + var parsed = ECSignature.parseScriptSignature(chunk) + hashType = parsed.hashType + + return parsed.signature + }) if (redeemScript) { pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) @@ -124,10 +125,8 @@ TransactionBuilder.fromTransaction = function(transaction) { // Extract/add signatures txb.inputs = transaction.ins.map(function(txIn) { - // Coinbase inputs not supported - assert(!Array.prototype.every.call(txIn.hash, function(x) { - return x === 0 - }), 'coinbase inputs not supported') + // TODO: remove me after testcase added + assert(!isCoinbase(txIn.hash), 'coinbase inputs not supported') // Ignore empty scripts if (txIn.script.buffer.length === 0) return @@ -236,9 +235,17 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { case 'multisig': 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) - }).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 scriptSig = scripts.multisigInput(signatures, redeemScript) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index ceed7e2..66f89ac 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -276,11 +276,10 @@ describe('TransactionBuilder', function() { }) }) - // TODO: test for reverse order signing - it('works for the P2SH multisig case', function() { + it('works for the out-of-order P2SH multisig case', function() { var privKeys = [ - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT", + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx" ].map(ECKey.fromWIF) var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG") From 95911c5dde9313464a87aa09178de9872f0eddbf Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 6 Jan 2015 16:13:15 +1100 Subject: [PATCH 13/30] tests: consistent test data names --- test/fixtures/transaction_builder.json | 93 +++++++++++++------------- test/transaction_builder.js | 38 +++++------ 2 files changed, 65 insertions(+), 66 deletions(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 1faebdf..9a356ce 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -2,12 +2,12 @@ "valid": { "build": [ { - "description": "pubKeyHash->pubKeyHash 1:1 transaction", - "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", + "description": "Transaction w/ pubKeyHash -> pubKeyHash", + "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { - "index": 0, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" ] @@ -21,12 +21,12 @@ ] }, { - "description": "pubKey->pubKeyHash 1:1 transaction", - "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", + "description": "Transaction w/ pubKey -> pubKeyHash", + "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { - "index": 0, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, "prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG", "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" @@ -41,12 +41,12 @@ ] }, { - "description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction", - "txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", + "description": "Transaction w/ scriptHash(multisig 2-of-2) -> pubKeyHash", + "txHex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", "inputs": [ { - "index": 0, - "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "vout": 0, "privKeys": [ "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" @@ -62,12 +62,12 @@ ] }, { - "description": "2-of-2 raw multisig -> pubKeyHash 1:1 Transaction", - "txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f07149000000009100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d54801ffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", + "description": "Transaction w/ multisig 2-of-2 -> pubKeyHash", + "txHex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f07149000000009100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d54801ffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", "inputs": [ { - "index": 0, - "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "vout": 0, "prevTxScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", "privKeys": [ "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", @@ -84,11 +84,11 @@ }, { "description": "Transaction w/ non-zero vin inputs", - "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205c80bbb5125b35d5e5a8324b1336832d29a6fc004859c8a9ff6bef47ba7fc348022018612216e57a521b2c4543f1f4fd738a76814c37c074e88adfe12464fff31cf901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", + "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205c80bbb5125b35d5e5a8324b1336832d29a6fc004859c8a9ff6bef47ba7fc348022018612216e57a521b2c4543f1f4fd738a76814c37c074e88adfe12464fff31cf901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" ] @@ -103,14 +103,13 @@ }, { "description": "Transaction w/ non-default input sequence numbers, version and locktime", - "txid": "4503038f144af7b0c11fad6e09cf8deb4ef04645d203e1c90b86f25b7b243fe8", - "txhex": "0400000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff020000006b483045022100c5bcd521df085481e2dcc2c0f14173043f0fa2001dca582b45186a95d248d28002204c571eabcec1410bd53a7da29b9da6b4c858c3fdabbfdb110a030c507ff5bc0501210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b9c220000110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac09990400", + "txHex": "0400000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff020000006b483045022100c5bcd521df085481e2dcc2c0f14173043f0fa2001dca582b45186a95d248d28002204c571eabcec1410bd53a7da29b9da6b4c858c3fdabbfdb110a030c507ff5bc0501210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798b9c220000110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac09990400", "version": 4, "locktime": 301321, "inputs": [ { - "index": 2, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 2, "sequence": 2147001, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" @@ -142,8 +141,8 @@ "exception": "Transaction has no outputs", "inputs": [ { - "index": 0, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, "privKeys": [] } ], @@ -154,8 +153,8 @@ "exception": "Transaction is not complete", "inputs": [ { - "index": 0, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, "privKeys": [] } ], @@ -171,15 +170,15 @@ "exception": "Transaction is not complete", "inputs": [ { - "index": 0, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" ] }, { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "privKeys": [] } @@ -197,8 +196,8 @@ "exception": "pubkeyhash doesn\\'t support multiple signatures", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" @@ -217,8 +216,8 @@ "exception": "RedeemScript not supported \\(nulldata\\)", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" ], @@ -237,8 +236,8 @@ "exception": "PrevOutScript is P2SH, missing redeemScript", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "prevTxScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL", "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" @@ -257,8 +256,8 @@ "exception": "RedeemScript not supported \\(scripthash\\)", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" ], @@ -277,8 +276,8 @@ "exception": "PrevOutScript must be P2SH", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" @@ -298,8 +297,8 @@ "exception": "Signature already exists", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" @@ -320,8 +319,8 @@ "exception": "privateKey cannot sign for this input", "inputs": [ { - "index": 1, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 1, "privKeys": [ "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx" ], @@ -340,8 +339,8 @@ "exception": "nulldata not supported", "inputs": [ { - "index": 0, - "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", "privKeys": [ "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 66f89ac..9356e3d 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -33,10 +33,10 @@ describe('TransactionBuilder', function() { var vin = txb.addInput(prevTxHash, 1, 54) assert.equal(vin, 0) - var txin = txb.tx.ins[0] - assert.equal(txin.hash, prevTxHash) - assert.equal(txin.index, 1) - assert.equal(txin.sequence, 54) + var txIn = txb.tx.ins[0] + assert.equal(txIn.hash, prevTxHash) + assert.equal(txIn.index, 1) + assert.equal(txIn.sequence, 54) assert.equal(txb.inputs[0].prevOutScript, undefined) }) @@ -44,10 +44,10 @@ describe('TransactionBuilder', function() { var vin = txb.addInput(prevTxHash, 1, 54, prevTx.outs[1].script) assert.equal(vin, 0) - var txin = txb.tx.ins[0] - assert.equal(txin.hash, prevTxHash) - assert.equal(txin.index, 1) - assert.equal(txin.sequence, 54) + var txIn = txb.tx.ins[0] + assert.equal(txIn.hash, prevTxHash) + assert.equal(txIn.index, 1) + assert.equal(txIn.sequence, 54) assert.equal(txb.inputs[0].prevOutScript, prevTx.outs[1].script) }) @@ -55,10 +55,10 @@ describe('TransactionBuilder', function() { var vin = txb.addInput(prevTx, 1, 54) assert.equal(vin, 0) - var txin = txb.tx.ins[0] - assert.deepEqual(txin.hash, prevTxHash) - assert.equal(txin.index, 1) - assert.equal(txin.sequence, 54) + var txIn = txb.tx.ins[0] + assert.deepEqual(txIn.hash, prevTxHash) + assert.equal(txIn.index, 1) + assert.equal(txIn.sequence, 54) assert.equal(txb.inputs[0].prevOutScript, prevTx.outs[1].script) }) @@ -142,7 +142,7 @@ describe('TransactionBuilder', function() { prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript) + txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function(output) { @@ -177,7 +177,7 @@ describe('TransactionBuilder', function() { describe('build', function() { fixtures.valid.build.forEach(function(f) { - it('builds the correct transaction', function() { + it('builds \"' + f.description + '\"', function() { f.inputs.forEach(function(input) { var prevTxScript @@ -185,7 +185,7 @@ describe('TransactionBuilder', function() { prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript) + txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function(output) { @@ -213,7 +213,7 @@ describe('TransactionBuilder', function() { if (f.locktime !== undefined) txb.tx.locktime = f.locktime var tx = txb.build() - assert.equal(tx.toHex(), f.txhex) + assert.equal(tx.toHex(), f.txHex) }) }) @@ -226,7 +226,7 @@ describe('TransactionBuilder', function() { prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript) + txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) f.outputs.forEach(function(output) { @@ -259,10 +259,10 @@ describe('TransactionBuilder', function() { describe('fromTransaction', function() { fixtures.valid.build.forEach(function(f) { it('builds the correct TransactionBuilder for ' + f.description, function() { - var tx = Transaction.fromHex(f.txhex) + var tx = Transaction.fromHex(f.txHex) var txb = TransactionBuilder.fromTransaction(tx) - assert.equal(txb.build().toHex(), f.txhex) + assert.equal(txb.build().toHex(), f.txHex) }) }) From 3f53b528a86c15de7ef0c5add0818bc535d1a207 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 28 Jan 2015 17:31:06 +1100 Subject: [PATCH 14/30] tests: reduce setup-code duplication --- test/fixtures/transaction_builder.json | 1 + test/transaction_builder.js | 129 +++++++++++-------------- 2 files changed, 55 insertions(+), 75 deletions(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 9a356ce..8122304 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -168,6 +168,7 @@ { "description": "Incomplete transaction w/ prevTxScript defined", "exception": "Transaction is not complete", + "alwaysThrows": true, "inputs": [ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 9356e3d..6c5ba21 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -9,6 +9,44 @@ var TransactionBuilder = require('../src/transaction_builder') var fixtures = require('./fixtures/transaction_builder') +function construct(txb, f, sign) { + f.inputs.forEach(function(input) { + var prevTxScript + + if (input.prevTxScript) { + prevTxScript = Script.fromASM(input.prevTxScript) + } + + txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) + }) + + f.outputs.forEach(function(output) { + var script = Script.fromASM(output.script) + + txb.addOutput(script, output.value) + }) + + if (sign === undefined || sign) { + f.inputs.forEach(function(input, index) { + var redeemScript + + if (input.redeemScript) { + redeemScript = Script.fromASM(input.redeemScript) + } + + input.privKeys.forEach(function(wif) { + var privKey = ECKey.fromWIF(wif) + + txb.sign(index, privKey, redeemScript) + }) + }) + } + + // FIXME: add support for locktime/version in TransactionBuilder API + if (f.version !== undefined) txb.tx.version = f.version + if (f.locktime !== undefined) txb.tx.locktime = f.locktime +} + describe('TransactionBuilder', function() { var privAddress, privScript var prevTx, prevTxHash @@ -135,21 +173,7 @@ describe('TransactionBuilder', function() { fixtures.invalid.sign.forEach(function(f) { it('throws on ' + f.exception + ' (' + f.description + ')', function() { - f.inputs.forEach(function(input) { - var prevTxScript - - if (input.prevTxScript) { - prevTxScript = Script.fromASM(input.prevTxScript) - } - - txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) - }) - - f.outputs.forEach(function(output) { - var script = Script.fromASM(output.script) - - txb.addOutput(script, output.value) - }) + construct(txb, f, false) f.inputs.forEach(function(input, index) { var redeemScript @@ -178,39 +202,7 @@ describe('TransactionBuilder', function() { describe('build', function() { fixtures.valid.build.forEach(function(f) { it('builds \"' + f.description + '\"', function() { - f.inputs.forEach(function(input) { - var prevTxScript - - if (input.prevTxScript) { - prevTxScript = Script.fromASM(input.prevTxScript) - } - - txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) - }) - - f.outputs.forEach(function(output) { - var script = Script.fromASM(output.script) - - txb.addOutput(script, output.value) - }) - - f.inputs.forEach(function(input, index) { - var redeemScript - - if (input.redeemScript) { - redeemScript = Script.fromASM(input.redeemScript) - } - - input.privKeys.forEach(function(wif) { - var privKey = ECKey.fromWIF(wif) - - txb.sign(index, privKey, redeemScript) - }) - }) - - // FIXME: add support for locktime/version in TransactionBuilder API - if (f.version !== undefined) txb.tx.version = f.version - if (f.locktime !== undefined) txb.tx.locktime = f.locktime + construct(txb, f) var tx = txb.build() assert.equal(tx.toHex(), f.txHex) @@ -218,40 +210,27 @@ describe('TransactionBuilder', function() { }) fixtures.invalid.build.forEach(function(f) { - it('throws on ' + f.exception + ' (' + f.description + ')', function() { - f.inputs.forEach(function(input) { - var prevTxScript + describe('for ' + f.description, function() { + beforeEach(function() { + if (f.txHex) { + var tx = Transaction.fromHex(f.txHex) + txb = TransactionBuilder.fromTransaction(tx) - if (input.prevTxScript) { - prevTxScript = Script.fromASM(input.prevTxScript) + } else { + construct(txb, f) } - - txb.addInput(input.txId, input.vout, input.sequence, prevTxScript) }) - f.outputs.forEach(function(output) { - var script = Script.fromASM(output.script) - - txb.addOutput(script, output.value) + it('throws on ' + f.exception, function() { + assert.throws(function() { + txb.build() + }, new RegExp(f.exception)) }) - f.inputs.forEach(function(input, index) { - var redeemScript - - if (input.redeemScript) { - redeemScript = Script.fromASM(input.redeemScript) - } - - input.privKeys.forEach(function(wif) { - var privKey = ECKey.fromWIF(wif) - - txb.sign(index, privKey, redeemScript) - }) + if (f.alwaysThrows) return + it('doesn\'t throw if building incomplete', function() { + txb.buildIncomplete() }) - - assert.throws(function() { - txb.build() - }, new RegExp(f.exception)) }) }) }) From f0c4a76325790048fbef2cadc24b7579f50b06ea Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 28 Jan 2015 17:04:27 +1100 Subject: [PATCH 15/30] tests: add scriptHash(pubKey) test fixture --- test/fixtures/transaction_builder.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 8122304..432c530 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -82,6 +82,27 @@ } ] }, + { + "description": "Transaction w/ scriptHash(pubKey) -> pubKeyHash", + "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006c47304402201115644b134932c8a7a8e925769d130a801288d477130e2bf6fadda20b33754d02202ecefbf63844d7cb2d5868539c39f973fe019f72e5c31a707836c0d61ef317db012321033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70facffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", + "inputs": [ + { + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, + "prevTxScript": "OP_HASH160 e89677d91455e541630d62c63718bef738b478b1 OP_EQUAL", + "privKeys": [ + "KxLDMPtVM7sLSu2v5n1LybDibw6P9FFbL4pUwJ51UDm7rp5AmXWW" + ], + "redeemScript": "033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70f OP_CHECKSIG" + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 2500000000 + } + ] + }, { "description": "Transaction w/ non-zero vin inputs", "txHex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff010000006a47304402205c80bbb5125b35d5e5a8324b1336832d29a6fc004859c8a9ff6bef47ba7fc348022018612216e57a521b2c4543f1f4fd738a76814c37c074e88adfe12464fff31cf901210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", From ebbe1278a06e4bb1ce0557e5a0bd4865c97276e5 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 28 Jan 2015 17:31:17 +1100 Subject: [PATCH 16/30] tests: add non-standard input fixture --- test/fixtures/transaction_builder.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 432c530..2b8929b 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -211,6 +211,11 @@ "value": 1000 } ] + }, + { + "description": "Complete transaction w/ non-standard inputs", + "exception": "nonstandard not supported", + "txHex": "010000000100000000171a0000e028f2000000000050178500000000000d0000000e000000000000002009f691b2263260e71f363d1db51ff3100d285956a40cc0e4f8c8c2c4a80559b1ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000" } ], "sign": [ From 1fde0a401d0515460f5cd2c499808b9a8356a65d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 20:22:11 +1100 Subject: [PATCH 17/30] tests: move inconsistent hashType/redeemScript test --- test/fixtures/transaction_builder.json | 222 ++++++++++++++++++------- test/transaction_builder.js | 78 ++------- 2 files changed, 174 insertions(+), 126 deletions(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 2b8929b..160701e 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -8,8 +8,10 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + } ] } ], @@ -28,8 +30,10 @@ "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, "prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG", - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + } ] } ], @@ -47,11 +51,15 @@ { "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", "vout": 0, - "privKeys": [ - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" - ], - "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG" + "signs": [ + { + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", + "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG" + }, + { + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + } + ] } ], "outputs": [ @@ -69,9 +77,13 @@ "txId": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", "vout": 0, "prevTxScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", - "privKeys": [ - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", - "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + "signs": [ + { + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx" + }, + { + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + } ] } ], @@ -90,10 +102,12 @@ "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, "prevTxScript": "OP_HASH160 e89677d91455e541630d62c63718bef738b478b1 OP_EQUAL", - "privKeys": [ - "KxLDMPtVM7sLSu2v5n1LybDibw6P9FFbL4pUwJ51UDm7rp5AmXWW" - ], - "redeemScript": "033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70f OP_CHECKSIG" + "signs": [ + { + "privKey": "KxLDMPtVM7sLSu2v5n1LybDibw6P9FFbL4pUwJ51UDm7rp5AmXWW", + "redeemScript": "033e29aea1168a835d5e386c292082db7b7807172a10ec634ad34226f36d79e70f OP_CHECKSIG" + } + ] } ], "outputs": [ @@ -110,8 +124,10 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + } ] } ], @@ -132,8 +148,10 @@ "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 2, "sequence": 2147001, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + } ] } ], @@ -164,7 +182,7 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, - "privKeys": [] + "signs": [] } ], "outputs": [] @@ -176,7 +194,7 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, - "privKeys": [] + "signs": [] } ], "outputs": [ @@ -194,15 +212,17 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + } ] }, { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, "prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", - "privKeys": [] + "signs": [] } ], "outputs": [ @@ -225,11 +245,15 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "throws": 1 + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + }, + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "throws": true + } + ] } ], "outputs": [ @@ -245,11 +269,13 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "redeemScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", - "throws": 0 + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "redeemScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", + "throws": true + } + ] } ], "outputs": [ @@ -266,10 +292,65 @@ "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, "prevTxScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL", - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "throws": 0 + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "throws": true + } + ] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "Inconsistent redeemScript", + "inputs": [ + { + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, + "signs": [ + { + "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + }, + { + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "throws": true + } + ] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] + }, + { + "exception": "Inconsistent hashType", + "inputs": [ + { + "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "vout": 0, + "signs": [ + { + "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "hashType": 4 + }, + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "hashType": 2, + "throws": true + } + ] } ], "outputs": [ @@ -285,11 +366,13 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "redeemScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL", - "throws": 0 + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "redeemScript": "OP_HASH160 7f67f0521934a57d3039f77f9f32cf313f3ac74b OP_EQUAL", + "throws": true + } + ] } ], "outputs": [ @@ -306,11 +389,13 @@ "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, "prevTxScript": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", - "throws": 0 + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", + "throws": true + } + ] } ], "outputs": [ @@ -326,12 +411,17 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", - "throws": 1 + "signs": [ + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG" + }, + { + "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", + "throws": true + } + ] } ], "outputs": [ @@ -348,11 +438,13 @@ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 1, - "privKeys": [ - "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx" - ], - "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", - "throws": 0 + "signs": [ + { + "privKey": "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx", + "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", + "throws": true + } + ] } ], "outputs": [ @@ -369,10 +461,12 @@ "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "vout": 0, "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", - "privKeys": [ - "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" - ], - "throws": 0 + "signs": [ + { + "privKey": "KzrA86mCVMGWnLGBQu9yzQa32qbxb5dvSK4XhyjjGAWSBKYX4rHx", + "throws": true + } + ] } ], "outputs": [ diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 6c5ba21..83ee604 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -1,5 +1,4 @@ var assert = require('assert') -var scripts = require('../src/scripts') var BigInteger = require('bigi') var ECKey = require('../src/eckey') @@ -28,16 +27,15 @@ function construct(txb, f, sign) { if (sign === undefined || sign) { f.inputs.forEach(function(input, index) { - var redeemScript + input.signs.forEach(function(sign) { + var privKey = ECKey.fromWIF(sign.privKey) + var redeemScript - if (input.redeemScript) { - redeemScript = Script.fromASM(input.redeemScript) - } + if (sign.redeemScript) { + redeemScript = Script.fromASM(sign.redeemScript) + } - input.privKeys.forEach(function(wif) { - var privKey = ECKey.fromWIF(wif) - - txb.sign(index, privKey, redeemScript) + txb.sign(index, privKey, redeemScript, sign.hashType) }) }) } @@ -128,69 +126,25 @@ describe('TransactionBuilder', function() { }) describe('sign', function() { - describe('when prevOutScript is undefined', function() { - it('assumes pubKeyHash', function() { - txb.addInput(prevTxHash, 0) - txb.sign(0, privKey) - - assert.equal(txb.inputs[0].scriptType, 'pubkeyhash') - assert.equal(txb.inputs[0].redeemScript, undefined) - }) - }) - - describe('when redeemScript is defined', function() { - it('assumes scriptHash', function() { - txb.addInput(prevTxHash, 0) - txb.sign(0, privKey, privScript) - - assert.equal(txb.inputs[0].prevOutType, 'scripthash') - assert.equal(txb.inputs[0].redeemScript, privScript) - }) - - it('throws if hashType is inconsistent', function() { - var redeemScript = scripts.multisigOutput(1, [privKey.pub]) - - txb.addInput(prevTxHash, 0) - txb.sign(0, privKey, redeemScript, 83) - - assert.throws(function() { - txb.sign(0, privKey, redeemScript, 82) - }, /Inconsistent hashType/) - }) - - it('throws if redeemScript is inconsistent', function() { - var firstScript = scripts.multisigOutput(1, [privKey.pub]) - var otherScript = scripts.multisigOutput(2, [privKey.pub, privKey.pub]) - - txb.addInput(prevTxHash, 0) - txb.sign(0, privKey, firstScript) - - assert.throws(function() { - txb.sign(0, privKey, otherScript) - }, /Inconsistent redeemScript/) - }) - }) - fixtures.invalid.sign.forEach(function(f) { it('throws on ' + f.exception + ' (' + f.description + ')', function() { construct(txb, f, false) f.inputs.forEach(function(input, index) { - var redeemScript + input.signs.forEach(function(sign) { + var privKey = ECKey.fromWIF(sign.privKey) + var redeemScript - if (input.redeemScript) { - redeemScript = Script.fromASM(input.redeemScript) - } + if (sign.redeemScript) { + redeemScript = Script.fromASM(sign.redeemScript) + } - input.privKeys.forEach(function(wif, i) { - var privKey = ECKey.fromWIF(wif) - - if (input.throws !== i) { - txb.sign(index, privKey, redeemScript) + if (!sign.throws) { + txb.sign(index, privKey, redeemScript, sign.hashType) } else { assert.throws(function() { - txb.sign(index, privKey, redeemScript) + txb.sign(index, privKey, redeemScript, sign.hashType) }, new RegExp(f.exception)) } }) From 5f761113cc3bf268d395b37a9271b18a673c7482 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 20:29:58 +1100 Subject: [PATCH 18/30] tests: if description undefined, use exception --- src/scripts.js | 2 +- test/transaction_builder.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scripts.js b/src/scripts.js index e58ce99..52824cd 100644 --- a/src/scripts.js +++ b/src/scripts.js @@ -65,8 +65,8 @@ function isPubKeyOutput(script) { function isScriptHashInput(script, allowIncomplete) { if (script.chunks.length < 2) return false - var lastChunk = script.chunks[script.chunks.length - 1] + var lastChunk = script.chunks[script.chunks.length - 1] if (!Buffer.isBuffer(lastChunk)) return false var scriptSig = Script.fromChunks(script.chunks.slice(0, -1)) diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 83ee604..cf14df6 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -164,7 +164,7 @@ describe('TransactionBuilder', function() { }) fixtures.invalid.build.forEach(function(f) { - describe('for ' + f.description, function() { + describe('for ' + (f.description || f.exception), function() { beforeEach(function() { if (f.txHex) { var tx = Transaction.fromHex(f.txHex) @@ -175,7 +175,7 @@ describe('TransactionBuilder', function() { } }) - it('throws on ' + f.exception, function() { + it('throws', function() { assert.throws(function() { txb.build() }, new RegExp(f.exception)) From d3af28e37e392818f46cb92dea05c1413dea929c Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 16:14:20 +1100 Subject: [PATCH 19/30] tests: fix TxBuilder fixtures to be consistent w/ compression --- test/fixtures/transaction_builder.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 160701e..b6342fd 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -316,11 +316,11 @@ "signs": [ { "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", - "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx" }, { "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", - "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT", "throws": true } ] @@ -342,11 +342,11 @@ "signs": [ { "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG", - "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "hashType": 4 }, { - "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", + "privKey": "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT", "hashType": 2, "throws": true } From 085b813958dc4d17af497e148c72cb52b8086bc9 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 19:36:21 +1100 Subject: [PATCH 20/30] TxBuilder: replace switch lookup with object lookup --- src/transaction_builder.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index e481fde..3b9c6a5 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -275,6 +275,8 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { return tx } +var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true } + TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { assert(index in this.inputs, 'No input at index: ' + index) hashType = hashType || Transaction.SIGHASH_ALL @@ -360,15 +362,8 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT input.signatures = input.signatures || [] } - switch (input.scriptType) { - case 'pubkeyhash': - case 'multisig': - case 'pubkey': - break - - default: - assert(false, input.scriptType + ' not supported') - } + // do we know how to sign this? + assert(input.scriptType in canSignTypes, input.scriptType + ' not supported') var signatureHash if (input.redeemScript) { From 8a8f40e903196ac79c82deb395adaea4c80c1326 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 20:02:10 +1100 Subject: [PATCH 21/30] TxBuilder: defer mutation as long as possible --- src/transaction_builder.js | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 3b9c6a5..76cd271 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -314,17 +314,15 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT var scriptHash = input.prevOutScript.chunks[1] assert.deepEqual(scriptHash, redeemScript.getHash(), 'RedeemScript does not match ' + scriptHash.toString('hex')) - - } else { - input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) - input.prevOutType = 'scripthash' } var scriptType = scripts.classifyOutput(redeemScript) + assert(scriptType in canSignTypes, 'RedeemScript not supported (' + scriptType + ')') + var pubKeys = [] switch (scriptType) { case 'multisig': - input.pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) + pubKeys = redeemScript.chunks.slice(1, -2).map(ECPubKey.fromBuffer) break case 'pubkeyhash': @@ -332,17 +330,20 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT var pkh2 = privKey.pub.getAddress().hash assert.deepEqual(pkh1, pkh2, 'privateKey cannot sign for this input') - input.pubKeys = [privKey.pub] + pubKeys = [privKey.pub] break case 'pubkey': - input.pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) + pubKeys = redeemScript.chunks.slice(0, 1).map(ECPubKey.fromBuffer) break - - default: - assert(false, 'RedeemScript not supported (' + scriptType + ')') } + if (!input.prevOutScript) { + input.prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) + input.prevOutType = 'scripthash' + } + + input.pubKeys = pubKeys input.redeemScript = redeemScript input.scriptType = scriptType @@ -365,14 +366,8 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT // do we know how to sign this? assert(input.scriptType in canSignTypes, input.scriptType + ' not supported') - var signatureHash - if (input.redeemScript) { - signatureHash = this.tx.hashForSignature(index, input.redeemScript, hashType) - - } else { - signatureHash = this.tx.hashForSignature(index, input.prevOutScript, hashType) - } - + var signatureScript = input.redeemScript || input.prevOutScript + var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType) var signature = privKey.sign(signatureHash) // enforce signing in order of public keys From b55e3a0245323677c09b0465bd746e04863c6a96 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 20:06:21 +1100 Subject: [PATCH 22/30] TxBuilder: missing redeemScript not an issue if already added If the input is initialized, we already have the redeemScript, no need to pass it in again --- src/transaction_builder.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 76cd271..b8616dd 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -289,13 +289,6 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT // are we already initialized? if (input.initialized) { - if (input.prevOutType === 'scripthash') { - assert(input.redeemScript, 'PrevOutScript is P2SH, missing redeemScript') - - } else { - assert(!input.redeemScript, 'PrevOutScript must be P2SH') - } - // redeemScript only needed to initialize, but if provided again, enforce consistency if (redeemScript) { assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') From e33a6409d3a527c43841fd360b6501faf1b3112f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 21:10:33 +1100 Subject: [PATCH 23/30] TxBuilder: add explanation for prevOutScript branch --- src/transaction_builder.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index b8616dd..a33f786 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -302,6 +302,7 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT // initialize it } else { if (redeemScript) { + // if we have a prevOutScript, enforce scriptHash equality to the redeemScript if (input.prevOutScript) { assert.equal(input.prevOutType, 'scripthash', 'PrevOutScript must be P2SH') From c7c58307f058a28f9bbb61de1d3879c995058ee9 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 13:29:28 +1100 Subject: [PATCH 24/30] TxBuilder: avoid unnecessary assertion, already done in classification --- src/transaction_builder.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index a33f786..5f6571f 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -20,14 +20,13 @@ function extractInput(txIn) { var prevOutType = scripts.classifyInput(scriptSig, true) var scriptType - // Re-classify if P2SH + // Re-classify if scriptHash if (prevOutType === 'scripthash') { redeemScript = Script.fromBuffer(scriptSig.chunks.slice(-1)[0]) prevOutScript = scripts.scriptHashOutput(redeemScript.getHash()) scriptSig = Script.fromChunks(scriptSig.chunks.slice(0, -1)) scriptType = scripts.classifyInput(scriptSig, true) - assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') } else { scriptType = prevOutType From 986e9d4710af0ead46fc3324eae82c85e404ef40 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 13:29:59 +1100 Subject: [PATCH 25/30] TxBuilder: vout is actually vin for addInput --- src/transaction_builder.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 5f6571f..d33bf71 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -186,12 +186,11 @@ TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOu var prevOut = prevOutHash.toString('hex') + ':' + index assert(!(prevOut in this.prevTxMap), 'Transaction is already an input') - var vout = this.tx.addInput(prevOutHash, index, sequence) + var vin = this.tx.addInput(prevOutHash, index, sequence) + this.inputs[vin] = input + this.prevTxMap[prevOut] = vin - this.prevTxMap[prevOut] = true - this.inputs[vout] = input - - return vout + return vin } TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { From fd2311bda42c843bbb5cfd429ac1ba9cd6dd2cd6 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 13:57:21 +1100 Subject: [PATCH 26/30] TxBuilder: remove impossible/untestable assertions --- src/transaction_builder.js | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index d33bf71..7d44f57 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -205,6 +205,9 @@ TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { TransactionBuilder.prototype.build = function() { return this.__build(false) } TransactionBuilder.prototype.buildIncomplete = function() { return this.__build(true) } + +var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true } + TransactionBuilder.prototype.__build = function(allowIncomplete) { if (!allowIncomplete) { assert(this.tx.ins.length > 0, 'Transaction has no inputs') @@ -215,24 +218,22 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { // Create script signatures from signature meta-data this.inputs.forEach(function(input, index) { - if (!allowIncomplete) { - assert(input.initialized, 'Transaction is not complete') - } - + var scriptType = input.scriptType var scriptSig - switch (input.scriptType) { - case 'pubkeyhash': - assert(input.signatures, 'Transaction is missing signatures') - assert.equal(input.signatures.length, 1, 'Transaction is missing signatures') + if (!allowIncomplete) { + assert(input.initialized, 'Transaction is not complete') + assert(scriptType in canSignTypes, scriptType + ' not supported') + assert(input.signatures, 'Transaction is missing signatures') + } + switch (scriptType) { + case 'pubkeyhash': var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) break case 'multisig': - assert(input.signatures, 'Transaction is missing signatures') - // Array.prototype.map is sparse-compatible var msSignatures = input.signatures.map(function(signature) { return signature.toScriptSignature(input.hashType) @@ -250,20 +251,13 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { break case 'pubkey': - assert(input.signatures, 'Transaction is missing signatures') - assert.equal(input.signatures.length, 1, 'Transaction is missing signatures') - var pkSignature = input.signatures[0].toScriptSignature(input.hashType) scriptSig = scripts.pubKeyInput(pkSignature) break - - default: - if (allowIncomplete) return - - assert(false, input.scriptType + ' not supported') } - if (input.redeemScript) { + // if we built a scriptSig, wrap as scriptHash if necessary + if (scriptSig && input.prevOutType === 'scripthash') { scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) } @@ -273,8 +267,6 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { return tx } -var canSignTypes = { 'pubkeyhash': true, 'multisig': true, 'pubkey': true } - TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { assert(index in this.inputs, 'No input at index: ' + index) hashType = hashType || Transaction.SIGHASH_ALL @@ -342,6 +334,7 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT } else { assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript') + // if nothing known, assume pubKeyHash if (!input.scriptType) { input.prevOutScript = privKey.pub.getAddress().toOutputScript() input.prevOutType = 'pubkeyhash' From 3a371fccecf60d5ff180b15ab8efbc839ce46e12 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 14:13:27 +1100 Subject: [PATCH 27/30] TxBuilder: remove initialized field from signature inputs --- src/transaction_builder.js | 116 ++++++++++++------------- test/fixtures/transaction_builder.json | 7 +- 2 files changed, 58 insertions(+), 65 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 7d44f57..fb394ee 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -33,21 +33,32 @@ function extractInput(txIn) { } // Extract hashType, pubKeys and signatures - var hashType, initialized, parsed, pubKeys, signatures + var hashType, parsed, pubKeys, signatures switch (scriptType) { - case 'pubkeyhash': + case 'pubkeyhash': { parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) hashType = parsed.hashType pubKeys = [ECPubKey.fromBuffer(scriptSig.chunks[1])] signatures = [parsed.signature] - - initialized = true prevOutScript = pubKeys[0].getAddress().toOutputScript() break + } - case 'multisig': + case 'pubkey': { + parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + hashType = parsed.hashType + signatures = [parsed.signature] + + if (redeemScript) { + pubKeys = [ECPubKey.fromBuffer(redeemScript.chunks[0])] + } + + break + } + + case 'multisig': { signatures = scriptSig.chunks.slice(1).map(function(chunk) { if (chunk === ops.OP_0) return chunk @@ -62,31 +73,11 @@ function extractInput(txIn) { } break - - case 'pubkey': - parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) - hashType = parsed.hashType - signatures = [parsed.signature] - - initialized = true - - if (redeemScript) { - pubKeys = [ECPubKey.fromBuffer(redeemScript.chunks[0])] - } - - break - - default: - if (redeemScript) { - initialized = true - } - - break + } } return { hashType: hashType, - initialized: initialized, prevOutScript: prevOutScript, prevOutType: prevOutType, pubKeys: pubKeys, @@ -222,38 +213,40 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { var scriptSig if (!allowIncomplete) { - assert(input.initialized, 'Transaction is not complete') + assert(!!scriptType, 'Transaction is not complete') assert(scriptType in canSignTypes, scriptType + ' not supported') assert(input.signatures, 'Transaction is missing signatures') } - switch (scriptType) { - case 'pubkeyhash': - var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) - scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) - break + if (input.signatures) { + switch (scriptType) { + case 'pubkeyhash': + var pkhSignature = input.signatures[0].toScriptSignature(input.hashType) + scriptSig = scripts.pubKeyHashInput(pkhSignature, input.pubKeys[0]) + break - case 'multisig': - // Array.prototype.map is sparse-compatible - var msSignatures = input.signatures.map(function(signature) { - return signature.toScriptSignature(input.hashType) - }) + case 'multisig': + // Array.prototype.map is sparse-compatible + var msSignatures = input.signatures.map(function(signature) { + return signature.toScriptSignature(input.hashType) + }) - // fill in blanks with OP_0 - for (var i = 0; i < msSignatures.length; ++i) { - if (msSignatures[i]) continue + // fill in blanks with OP_0 + for (var i = 0; i < msSignatures.length; ++i) { + if (msSignatures[i]) continue - msSignatures[i] = ops.OP_0 - } + msSignatures[i] = ops.OP_0 + } - var redeemScript = allowIncomplete ? undefined : input.redeemScript - scriptSig = scripts.multisigInput(signatures, redeemScript) - break + var redeemScript = allowIncomplete ? undefined : input.redeemScript + scriptSig = scripts.multisigInput(msSignatures, redeemScript) + break - case 'pubkey': - var pkSignature = input.signatures[0].toScriptSignature(input.hashType) - scriptSig = scripts.pubKeyInput(pkSignature) - break + case 'pubkey': + var pkSignature = input.signatures[0].toScriptSignature(input.hashType) + scriptSig = scripts.pubKeyInput(pkSignature) + break + } } // if we built a scriptSig, wrap as scriptHash if necessary @@ -277,18 +270,19 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT assert.equal(input.hashType, hashType, 'Inconsistent hashType') } + var initialized = input.prevOutScript && + input.prevOutType && + input.hashType && + input.pubKeys && + input.signatures + // are we already initialized? - if (input.initialized) { + if (initialized) { // redeemScript only needed to initialize, but if provided again, enforce consistency if (redeemScript) { assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') } - // if signatures already exist, enforce multisig scriptType - if (input.signatures.length > 0) { - assert.equal(input.scriptType, 'multisig', input.scriptType + ' doesn\'t support multiple signatures') - } - // initialize it } else { if (redeemScript) { @@ -344,26 +338,24 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT } input.hashType = hashType - input.initialized = true input.signatures = input.signatures || [] } // do we know how to sign this? assert(input.scriptType in canSignTypes, input.scriptType + ' not supported') - var signatureScript = input.redeemScript || input.prevOutScript - var signatureHash = this.tx.hashForSignature(index, signatureScript, hashType) - var signature = privKey.sign(signatureHash) - - // enforce signing in order of public keys + // 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 return true - }), 'privateKey cannot sign for this input') + }, this), 'privateKey cannot sign for this input') } module.exports = TransactionBuilder diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index b6342fd..265ad2a 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -206,7 +206,7 @@ }, { "description": "Incomplete transaction w/ prevTxScript defined", - "exception": "Transaction is not complete", + "exception": "Transaction is missing signatures", "alwaysThrows": true, "inputs": [ { @@ -240,7 +240,8 @@ ], "sign": [ { - "exception": "pubkeyhash doesn\\'t support multiple signatures", + "description": "Too many signatures - pubKeyHash", + "exception": "Signature already exists", "inputs": [ { "txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", @@ -406,6 +407,7 @@ ] }, { + "description": "Too many signatures - scriptHash(multisig 1-of-1)", "exception": "Signature already exists", "inputs": [ { @@ -418,7 +420,6 @@ }, { "privKey": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn", - "redeemScript": "OP_1 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 OP_1 OP_CHECKMULTISIG", "throws": true } ] From a7882149213a4d4903ddb5a45292ab9b479d695a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 14:33:31 +1100 Subject: [PATCH 28/30] TxBuilder: fix undefined scriptSig Fixed and typeForce used to enforce this wont happen again in future. --- src/transaction.js | 3 +++ src/transaction_builder.js | 13 ++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index a952dbb..31c8657 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -308,6 +308,9 @@ Transaction.prototype.toHex = function() { } Transaction.prototype.setInputScript = function(index, script) { + typeForce('Number', index) + typeForce('Script', script) + this.ins[index].script = script } diff --git a/src/transaction_builder.js b/src/transaction_builder.js index fb394ee..14c3451 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -249,12 +249,15 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { } } - // if we built a scriptSig, wrap as scriptHash if necessary - if (scriptSig && input.prevOutType === 'scripthash') { - scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) - } + // did we build a scriptSig? + if (scriptSig) { + // wrap as scriptHash if necessary + if (input.prevOutType === 'scripthash') { + scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) + } - tx.setInputScript(index, scriptSig) + tx.setInputScript(index, scriptSig) + } }) return tx From 4ef2c19d71fde6ecfa7beb85000458790dbf826c Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 14:45:12 +1100 Subject: [PATCH 29/30] TxBuilder: defer mutation further, but still catch non-standards --- src/transaction_builder.js | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 14c3451..e1ede83 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -273,10 +273,11 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT assert.equal(input.hashType, hashType, 'Inconsistent hashType') } - var initialized = input.prevOutScript && + var initialized = input.hashType && + input.prevOutScript && input.prevOutType && - input.hashType && input.pubKeys && + input.scriptType && input.signatures // are we already initialized? @@ -331,12 +332,17 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT } else { assert.notEqual(input.prevOutType, 'scripthash', 'PrevOutScript is P2SH, missing redeemScript') - // if nothing known, assume pubKeyHash - if (!input.scriptType) { + // can we sign this? + if (input.scriptType) { + assert(input.pubKeys, input.scriptType + ' not supported') + + // we know nothin' Jon Snow, assume pubKeyHash + } else { input.prevOutScript = privKey.pub.getAddress().toOutputScript() input.prevOutType = 'pubkeyhash' input.pubKeys = [privKey.pub] input.scriptType = input.prevOutType + } } @@ -344,9 +350,6 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT input.signatures = input.signatures || [] } - // do we know how to sign this? - assert(input.scriptType in canSignTypes, input.scriptType + ' not supported') - // enforce in order signing of public keys assert(input.pubKeys.some(function(pubKey, i) { if (!privKey.pub.Q.equals(pubKey.Q)) return false From 73bf8a42ea3e2b070e03baba4237305f05f4c002 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Thu, 5 Feb 2015 14:48:29 +1100 Subject: [PATCH 30/30] TxBuilder: hashtype only relevant to things we can sign --- src/transaction_builder.js | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index e1ede83..40a5546 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -268,26 +268,23 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT hashType = hashType || Transaction.SIGHASH_ALL var input = this.inputs[index] + var canSign = input.hashType && + input.prevOutScript && + input.prevOutType && + input.pubKeys && + input.scriptType && + input.signatures - if (input.hashType !== undefined) { - assert.equal(input.hashType, hashType, 'Inconsistent hashType') - } - - var initialized = input.hashType && - input.prevOutScript && - input.prevOutType && - input.pubKeys && - input.scriptType && - input.signatures - - // are we already initialized? - if (initialized) { - // redeemScript only needed to initialize, but if provided again, enforce consistency + // are we almost ready to sign? + if (canSign) { + // if redeemScript was provided, enforce consistency if (redeemScript) { assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') } - // initialize it + assert.equal(input.hashType, hashType, 'Inconsistent hashType') + + // no? prepare } else { if (redeemScript) { // if we have a prevOutScript, enforce scriptHash equality to the redeemScript