From 884fd542fec61fc23794b204280416558883d55f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 25 Jul 2014 16:11:45 +1000 Subject: [PATCH 01/16] Transaction: deprecate Tx signing methods --- src/transaction.js | 38 ++++++++++++++++++++++++++------------ test/bitcoin.core.js | 2 +- test/transaction.js | 2 +- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 1bfff5d..d8a0fc4 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -162,7 +162,17 @@ Transaction.prototype.toHex = function() { * hashType, serializes and finally hashes the result. This hash can then be * used to sign the transaction input in question. */ -Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashType) { +Transaction.prototype.hashForSignature = function(inIndex, prevOutScript, hashType) { + // FIXME: remove in 2.x.y + if (arguments[0] instanceof Script) { + console.warn('hashForSignature(prevOutScript, inIndex, ...) has been deprecated. Use hashForSignature(inIndex, prevOutScript, ...)') + + // swap the arguments (must be stored in tmp, arguments is special) + var tmp = arguments[0] + inIndex = arguments[1] + prevOutScript = tmp + } + assert(inIndex >= 0, 'Invalid vin index') assert(inIndex < this.ins.length, 'Invalid vin index') assert(prevOutScript instanceof Script, 'Invalid Script object') @@ -296,35 +306,39 @@ Transaction.fromHex = function(hex) { return Transaction.fromBuffer(new Buffer(hex, 'hex')) } -/** - * Signs a pubKeyHash output at some index with the given key - */ +Transaction.prototype.setInputScript = function(index, script) { + this.ins[index].script = script +} + +// FIXME: remove in 2.x.y Transaction.prototype.sign = function(index, privKey, hashType) { + console.warn("Transaction.prototype.sign is deprecated. Use TransactionBuilder instead.") + var prevOutScript = privKey.pub.getAddress().toOutputScript() var signature = this.signInput(index, prevOutScript, privKey, hashType) - // FIXME: Assumed prior TX was pay-to-pubkey-hash var scriptSig = scripts.pubKeyHashInput(signature, privKey.pub) this.setInputScript(index, scriptSig) } +// FIXME: remove in 2.x.y Transaction.prototype.signInput = function(index, prevOutScript, privKey, hashType) { + console.warn("Transaction.prototype.signInput is deprecated. Use TransactionBuilder instead.") + hashType = hashType || Transaction.SIGHASH_ALL - var hash = this.hashForSignature(prevOutScript, index, hashType) + var hash = this.hashForSignature(index, prevOutScript, hashType) var signature = privKey.sign(hash) return signature.toScriptSignature(hashType) } -Transaction.prototype.setInputScript = function(index, script) { - this.ins[index].script = script -} - -// FIXME: could be validateInput(index, prevTxOut, pub) +// FIXME: remove in 2.x.y Transaction.prototype.validateInput = function(index, prevOutScript, pubKey, buffer) { + console.warn("Transaction.prototype.validateInput is deprecated. Use TransactionBuilder instead.") + var parsed = ECSignature.parseScriptSignature(buffer) - var hash = this.hashForSignature(prevOutScript, index, parsed.hashType) + var hash = this.hashForSignature(index, prevOutScript, parsed.hashType) return pubKey.verify(hash, parsed.signature) } diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 71bd7e6..5cf004b 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -183,7 +183,7 @@ describe('Bitcoin-core', function() { var actualHash try { - actualHash = transaction.hashForSignature(script, inIndex, hashType) + actualHash = transaction.hashForSignature(inIndex, script, hashType) } catch (e) { // don't fail if we don't support it yet, TODO if (!e.message.match(/not yet supported/)) throw e diff --git a/test/transaction.js b/test/transaction.js index a8aa1d7..fc2711d 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -221,7 +221,7 @@ describe('Transaction', function() { // TODO: // hashForSignature: [Function], - // FIXME: could be better + // FIXME: remove in 2.x.y describe('signInput/validateInput', function() { it('works for multi-sig redeem script', function() { var tx = new Transaction() From bcbcd58964a8124bae20f504ab1975bf6d490e52 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 16 Jun 2014 16:05:31 +1000 Subject: [PATCH 02/16] TxBuilder: Initial commit and tests --- src/transaction_builder.js | 159 +++++++++++++++++ test/fixtures/transaction_builder.json | 113 ++++++++++++ test/transaction_builder.js | 228 +++++++++++++++++++++++++ 3 files changed, 500 insertions(+) create mode 100644 src/transaction_builder.js create mode 100644 test/fixtures/transaction_builder.json create mode 100644 test/transaction_builder.js diff --git a/src/transaction_builder.js b/src/transaction_builder.js new file mode 100644 index 0000000..e284d63 --- /dev/null +++ b/src/transaction_builder.js @@ -0,0 +1,159 @@ +var assert = require('assert') +var scripts = require('./scripts') + +var ECKey = require('./eckey') +var Transaction = require('./transaction') +var Script = require('./script') + +function TransactionBuilder() { + this.prevOutMap = {} + this.prevOutScripts = {} + this.prevOutTypes = {} + + this.signatures = [] + this.tx = new Transaction() +} + +TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript) { + var prevOutHash + + if (typeof prevTx === 'string') { + prevOutHash = new Buffer(prevTx, 'hex') + + // TxId hex is big-endian, we want little-endian hash + Array.prototype.reverse.call(prevOutHash) + + } else if (prevTx instanceof Transaction) { + assert(prevOutScript === undefined, 'Unnecessary Script provided') + + prevOutHash = prevTx.getHash() + prevOutScript = prevTx.outs[index].script + + } else { + prevOutHash = prevTx + + } + + var prevOutType + if (prevOutScript !== undefined) { + prevOutType = scripts.classifyOutput(prevOutScript) + + assert.notEqual(prevOutType, 'nonstandard', 'PrevOutScript not supported (nonstandard)') + } + + assert(this.signatures.every(function(input) { + return input.hashType & Transaction.SIGHASH_ANYONECANPAY + }), 'No, this would invalidate signatures') + + var prevOut = prevOutHash.toString('hex') + ':' + index + assert(!(prevOut in this.prevOutMap), 'Transaction is already an input') + + var vout = this.tx.addInput(prevOutHash, index) + this.prevOutMap[prevOut] = true + this.prevOutScripts[vout] = prevOutScript + this.prevOutTypes[vout] = prevOutType + + return vout +} + +TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { + assert(this.signatures.every(function(signature) { + return (signature.hashType & 0x1f) === Transaction.SIGHASH_SINGLE + }), 'No, this would invalidate signatures') + + return this.tx.addOutput(scriptPubKey, value) +} + +TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { + assert(this.tx.ins.length >= index, 'No input at index: ' + index) + hashType = hashType || Transaction.SIGHASH_ALL + + var prevOutScript = this.prevOutScripts[index] + var prevOutType = this.prevOutTypes[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, 'nonstandard', 'RedeemScript not supported (nonstandard)') + + hash = this.tx.hashForSignature(index, redeemScript, hashType) + + } else { + prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript() + scriptType = prevOutType || 'pubkeyhash' + + assert.notEqual(scriptType, 'scripthash', 'PrevOutScript requires redeemScript') + + hash = this.tx.hashForSignature(index, prevOutScript, hashType) + } + + var signature = privKey.sign(hash) + + if (!(index in this.signatures)) { + this.signatures[index] = { + hashType: hashType, + pubKeys: [], + redeemScript: redeemScript, + scriptType: scriptType, + signatures: [] + } + } + + var input = this.signatures[index] + input.pubKeys.push(privKey.pub) + input.signatures.push(signature) + + assert.equal(input.hashType, hashType, 'Inconsistent hashType') + assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') +} + +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.equal(this.signatures.length, this.tx.ins.length, 'Transaction is missing signatures') + } + + var tx = this.tx.clone() + + this.signatures.forEach(function(input, index) { + var scriptSig + + if (input.scriptType === 'pubkeyhash') { + var signature = input.signatures[0].toScriptSignature(input.hashType) + var publicKey = input.pubKeys[0] + scriptSig = scripts.pubKeyHashInput(signature, publicKey) + + } else if (input.scriptType === 'multisig') { + var redeemScript = allowIncomplete ? undefined : input.redeemScript + var signatures = input.signatures.map(function(signature) { + return signature.toScriptSignature(input.hashType) + }) + scriptSig = scripts.multisigInput(signatures, redeemScript) + + } else if (input.scriptType === 'pubkey') { + var signature = input.signatures[0] + scriptSig = scripts.pubKeyInput(signature) + + } else { + assert(false, input.scriptType + ' not supported') + } + + if (input.redeemScript) { + scriptSig = scripts.scriptHashInput(scriptSig, input.redeemScript) + } + + tx.setInputScript(index, scriptSig) + }) + + return tx +} + +module.exports = TransactionBuilder diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json new file mode 100644 index 0000000..69c1fea --- /dev/null +++ b/test/fixtures/transaction_builder.json @@ -0,0 +1,113 @@ +{ + "valid": { + "build": [ + { + "description": "pubKeyHash 1:1 transaction", + "txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e", + "inputs": [ + { + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 10000 + } + ] + }, + { + "description": "2-of-2 P2SH multisig Transaction", + "txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24", + "inputs": [ + { + "index": 0, + "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", + "privKeys": ["91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"], + "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG" + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 faf1d99bf040ea9c7f8cc9f14ac6733ad75ce246 OP_EQUALVERIFY OP_CHECKSIG", + "value": 10000 + } + ] + } + ] + }, + "invalid": { + "build": [ + { + "exception": "Transaction has no inputs", + "hex": "", + "outputs": [ + { + "script": "", + "value": 1000 + } + ] + }, + { + "exception": "Transaction has no outputs", + "hex": "", + "inputs": [ + { + "hash": "", + "index": 0 + } + ] + }, + { + "exception": "Transaction has no signatures", + "hex": "", + "inputs": [ + { + "hash": "", + "index": 0 + } + ], + "outputs": [ + { + "script": "", + "value": 1000 + } + ] + }, + { + "exception": "Transaction is missing signatures", + "hex": "", + "inputs": [ + { + "hash": "", + "index": 0 + }, + { + "hash": "", + "index": 1 + } + ], + "outputs": [ + { + "script": "", + "value": 1000 + } + ], + "signatures": [ + { + "wif": "", + "index": 0 + } + ] + } + ], + "fromTransaction": [ + { + "exception": "Transaction contains unsupported script types", + "hex": "" + } + ] + } +} diff --git a/test/transaction_builder.js b/test/transaction_builder.js new file mode 100644 index 0000000..4a7f0ea --- /dev/null +++ b/test/transaction_builder.js @@ -0,0 +1,228 @@ +var assert = require('assert') +var ecdsa = require('../src/ecdsa') +var scripts = require('../src/scripts') + +var Address = require('../src/address') +var BigInteger = require('bigi') +var ECKey = require('../src/eckey') +var Script = require('../src/script') +var Transaction = require('../src/transaction') +var TransactionBuilder = require('../src/transaction_builder') + +var fixtures = require('./fixtures/transaction_builder') + +describe('TransactionBuilder', function() { + var privAddress, privScript + var prevTx, prevTxHash + var privKey + var txb + + beforeEach(function() { + txb = new TransactionBuilder() + + prevTx = new Transaction() + prevTx.addOutput('1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH', 0) + prevTx.addOutput('1cMh228HTCiwS8ZsaakH8A8wze1JR5ZsP', 1) + prevTxHash = prevTx.getHash() + prevTxId = prevTx.getId() + + privKey = new ECKey(BigInteger.ONE, false) + privAddress = privKey.pub.getAddress() + privScript = privAddress.toOutputScript() + value = 10000 + }) + + describe('addInput', function() { + it('accepts a txHash and index', function() { + var vin = txb.addInput(prevTxHash, 1) + assert.equal(vin, 0) + + var txin = txb.tx.ins[0] + assert.equal(txin.hash, prevTxHash) + assert.equal(txin.index, 1) + assert.equal(txb.prevOutScripts[0], undefined) + }) + + it('accepts a txHash, index and scriptPubKey', function() { + var vin = txb.addInput(prevTxHash, 1, 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(txb.prevOutScripts[0], prevTx.outs[1].script) + }) + + it('accepts a prevTx and index', function() { + var vin = txb.addInput(prevTx, 1) + assert.equal(vin, 0) + + var txin = txb.tx.ins[0] + assert.deepEqual(txin.hash, prevTxHash) + assert.equal(txin.index, 1) + assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) + }) + + it('returns the input index', function() { + assert.equal(txb.addInput(prevTxHash, 0), 0) + assert.equal(txb.addInput(prevTxHash, 1), 1) + }) + + it('throws if a Tx and prevOutScript is given', function() { + assert.throws(function() { + txb.addInput(prevTx, 0, privScript) + }, /Unnecessary Script provided/) + }) + + it('throws if prevOutScript is not supported', function() { + assert.throws(function() { + txb.addInput(prevTxHash, 0, 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) + + assert.throws(function() { + txb.addInput(prevTxHash, 0) + }, /No, this would invalidate signatures/) + }) + }) + + describe('addOutput', function() { + it('throws if SIGHASH_ALL has been used to sign any existing scriptSigs', function() { + txb.addInput(prevTxHash, 0) + txb.addOutput(privScript, value) + txb.sign(0, privKey) + + assert.throws(function() { + txb.addOutput(privScript, 9000) + }, /No, this would invalidate signatures/) + }) + }) + + describe('sign', function() { + describe('when prevOutScript is undefined', function() { + it('assumes pubKeyHash', function() { + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey) + + assert.strictEqual(txb.signatures[0].redeemScript, undefined) + assert.equal(txb.signatures[0].scriptType, 'pubkeyhash') + }) + }) + + describe('when redeemScript is defined', function() { + it('assumes scriptHash', function() { + txb.addInput(prevTxHash, 0) + txb.sign(0, privKey, privScript) + + 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 redeemScript not supported', function() { + txb.addInput(prevTxHash, 0) + + assert.throws(function() { + txb.sign(0, privKey, Script.EMPTY) + }, /RedeemScript not supported \(nonstandard\)/) + }) + }) + }) + + describe('build', function() { + fixtures.valid.build.forEach(function(f) { + it('builds the correct transaction', function() { + f.inputs.forEach(function(input) { + var prevTx + if (input.prevTx.length === 64) { + prevTx = input.prevTx + } else { + prevTx = Transaction.fromHex(input.prevTx) + } + + txb.addInput(prevTx, input.index) + }) + + 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) + }) + }) + + var tx = txb.build() + + assert.equal(tx.getId(), f.txid) + }) + }) + + it('throws if Transaction has no inputs', function() { + txb.addOutput(privScript, value) + + assert.throws(function() { + txb.build() + }, /Transaction has no inputs/) + }) + + it('throws if Transaction has no outputs', function() { + txb.addInput(prevTxHash, 0) + + assert.throws(function() { + txb.build() + }, /Transaction has no outputs/) + }) + + it('throws if Transaction has no signatures', function() { + txb.addInput(prevTxHash, 0) + txb.addOutput(privScript, value) + + assert.throws(function() { + txb.build() + }, /Transaction is missing signatures/) + }) + + it('throws if Transaction has not enough signatures', function() { + txb.addInput(prevTxHash, 0) + txb.addInput(prevTxHash, 1) + txb.addOutput(privScript, value) + txb.sign(0, privKey) + + assert.throws(function() { + txb.build() + }, /Transaction is missing signatures/) + }) + }) +}) From 36b225a3df9a0f58774bbf2090828ded9dd1be72 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 16 Jul 2014 22:24:10 +1000 Subject: [PATCH 03/16] TxBuilder: use data fixtures for invalid tests --- src/transaction_builder.js | 1 + test/fixtures/transaction_builder.json | 46 ++++++++------------- test/transaction_builder.js | 57 ++++++++++++++------------ 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index e284d63..6611bca 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -118,6 +118,7 @@ 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') } diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 69c1fea..8946805 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -42,72 +42,62 @@ "build": [ { "exception": "Transaction has no inputs", - "hex": "", + "inputs": [], "outputs": [ { - "script": "", + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "value": 1000 } ] }, { "exception": "Transaction has no outputs", - "hex": "", "inputs": [ { - "hash": "", - "index": 0 + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [] } - ] + ], + "outputs": [] }, { "exception": "Transaction has no signatures", - "hex": "", "inputs": [ { - "hash": "", - "index": 0 + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [] } ], "outputs": [ { - "script": "", + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "value": 1000 } ] }, { "exception": "Transaction is missing signatures", - "hex": "", "inputs": [ { - "hash": "", - "index": 0 + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] }, { - "hash": "", - "index": 1 + "index": 1, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "privKeys": [] } ], "outputs": [ { - "script": "", + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", "value": 1000 } - ], - "signatures": [ - { - "wif": "", - "index": 0 - } ] } - ], - "fromTransaction": [ - { - "exception": "Transaction contains unsupported script types", - "hex": "" - } ] } } diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 4a7f0ea..36ad20c 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -189,40 +189,43 @@ describe('TransactionBuilder', function() { }) }) - it('throws if Transaction has no inputs', function() { - txb.addOutput(privScript, value) + fixtures.invalid.build.forEach(function(f) { + it('throws on ' + f.exception, function() { + f.inputs.forEach(function(input) { + var prevTx + if (input.prevTx.length === 64) { + prevTx = input.prevTx + } else { + prevTx = Transaction.fromHex(input.prevTx) + } - assert.throws(function() { - txb.build() - }, /Transaction has no inputs/) - }) + txb.addInput(prevTx, input.index) + }) - it('throws if Transaction has no outputs', function() { - txb.addInput(prevTxHash, 0) + f.outputs.forEach(function(output) { + var script = Script.fromASM(output.script) - assert.throws(function() { - txb.build() - }, /Transaction has no outputs/) - }) + txb.addOutput(script, output.value) + }) - it('throws if Transaction has no signatures', function() { - txb.addInput(prevTxHash, 0) - txb.addOutput(privScript, value) + f.inputs.forEach(function(input, index) { + var redeemScript - assert.throws(function() { - txb.build() - }, /Transaction is missing signatures/) - }) + if (input.redeemScript) { + redeemScript = Script.fromASM(input.redeemScript) + } - it('throws if Transaction has not enough signatures', function() { - txb.addInput(prevTxHash, 0) - txb.addInput(prevTxHash, 1) - txb.addOutput(privScript, value) - txb.sign(0, privKey) + input.privKeys.forEach(function(wif) { + var privKey = ECKey.fromWIF(wif) - assert.throws(function() { - txb.build() - }, /Transaction is missing signatures/) + txb.sign(index, privKey, redeemScript) + }) + }) + + assert.throws(function() { + txb.build() + }, new RegExp(f.exception)) + }) }) }) }) From 377b815417011d4839381ff7967cb1cf3e416b55 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 16 Jul 2014 22:37:28 +1000 Subject: [PATCH 04/16] TxBuilder: transform all signatures once --- src/transaction_builder.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 6611bca..07fce4b 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -127,24 +127,26 @@ TransactionBuilder.prototype.build = function(allowIncomplete) { this.signatures.forEach(function(input, index) { var scriptSig + var signatures = input.signatures.map(function(signature) { + return signature.toScriptSignature(input.hashType) + }) + if (input.scriptType === 'pubkeyhash') { - var signature = input.signatures[0].toScriptSignature(input.hashType) + var signature = signatures[0] var publicKey = input.pubKeys[0] scriptSig = scripts.pubKeyHashInput(signature, publicKey) } else if (input.scriptType === 'multisig') { var redeemScript = allowIncomplete ? undefined : input.redeemScript - var signatures = input.signatures.map(function(signature) { - return signature.toScriptSignature(input.hashType) - }) scriptSig = scripts.multisigInput(signatures, redeemScript) } else if (input.scriptType === 'pubkey') { - var signature = input.signatures[0] + var signature = signatures[0] scriptSig = scripts.pubKeyInput(signature) } else { assert(false, input.scriptType + ' not supported') + } if (input.redeemScript) { From 4e3a6c9557b75a37f256648c5dc695d93799fb86 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 15 Aug 2014 13:13:36 +1000 Subject: [PATCH 05/16] TxBuilder: use build/buildIncomplete over boolean --- src/transaction_builder.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 07fce4b..b2e0396 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -114,7 +114,15 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') } -TransactionBuilder.prototype.build = function(allowIncomplete) { +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') assert(this.tx.outs.length > 0, 'Transaction has no outputs') From 26b028adcf564030db4f5aeeefb1427f968b8071 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 16 Jul 2014 22:43:40 +1000 Subject: [PATCH 06/16] Wallet: use TxBuilder instead --- src/wallet.js | 25 +++++++++++-------------- test/wallet.js | 9 +++++---- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 29f124b..af28be0 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -4,7 +4,7 @@ var networks = require('./networks') var Address = require('./address') var HDNode = require('./hdnode') -var Transaction = require('./transaction') +var TransactionBuilder = require('./transaction_builder') var Script = require('./script') function Wallet(seed, network, unspents) { @@ -57,17 +57,17 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) { var subTotal = value var addresses = [] - var tx = new Transaction() - tx.addOutput(to, value) + var txb = new TransactionBuilder() + txb.addOutput(to, value) for (var i = 0; i < utxos.length; ++i) { var utxo = utxos[i] addresses.push(utxo.address) var outpoint = utxo.from.split(':') - tx.addInput(outpoint[0], parseInt(outpoint[1])) + txb.addInput(outpoint[0], parseInt(outpoint[1])) - var fee = fixedFee === undefined ? estimatePaddedFee(tx, this.network) : fixedFee + var fee = fixedFee === undefined ? estimatePaddedFee(txb.buildIncomplete(), this.network) : fixedFee accum += utxo.value subTotal = value + fee @@ -75,7 +75,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) { var change = accum - subTotal if (change > this.network.dustThreshold) { - tx.addOutput(changeAddress || this.getChangeAddress(), change) + txb.addOutput(changeAddress || this.getChangeAddress(), change) } break @@ -84,8 +84,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) { assert(accum >= subTotal, 'Not enough funds (incl. fee): ' + accum + ' < ' + subTotal) - this.signWith(tx, addresses) - return tx + return this.signWith(txb, addresses).build() } Wallet.prototype.processPendingTx = function(tx){ @@ -219,16 +218,14 @@ Wallet.prototype.setUnspentOutputs = function(utxo) { this.outputs = processUnspentOutputs(utxo) } -Wallet.prototype.signWith = function(tx, addresses) { - assert.equal(tx.ins.length, addresses.length, 'Number of addresses must match number of transaction inputs') - +Wallet.prototype.signWith = function(txb, addresses) { addresses.forEach(function(address, i) { - var key = this.getPrivateKeyForAddress(address) + var privKey = this.getPrivateKeyForAddress(address) - tx.sign(i, key) + txb.sign(i, privKey) }, this) - return tx + return txb } function outputToUnspentOutput(output){ diff --git a/test/wallet.js b/test/wallet.js index c08ae2f..87d0ced 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -7,6 +7,7 @@ var scripts = require('../src/scripts') var Address = require('../src/address') var HDNode = require('../src/hdnode') var Transaction = require('../src/transaction') +var TransactionBuilder = require('../src/transaction_builder') var Wallet = require('../src/wallet') var fixtureTxes = require('./fixtures/mainnet_tx') @@ -623,17 +624,17 @@ describe('Wallet', function() { describe('signing', function(){ afterEach(function(){ - Transaction.prototype.sign.restore() + TransactionBuilder.prototype.sign.restore() }) it('signes the inputs with respective keys', function(){ var fee = 30000 - sinon.stub(Transaction.prototype, "sign") + sinon.spy(TransactionBuilder.prototype, "sign") var tx = wallet.createTx(to, value, fee) - assert(Transaction.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2))) - assert(Transaction.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1))) + assert(TransactionBuilder.prototype.sign.calledWith(0, wallet.getPrivateKeyForAddress(address2))) + assert(TransactionBuilder.prototype.sign.calledWith(1, wallet.getPrivateKeyForAddress(address1))) }) }) From d0ac9b405a5eb9bba58dfd8c37ba6207f0ba906d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 16 Jul 2014 23:05:49 +1000 Subject: [PATCH 07/16] tests: add TxBuilder pubKey test fixture --- test/fixtures/transaction_builder.json | 22 ++++++++++++++++++++-- test/transaction_builder.js | 22 ++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 8946805..3fbc794 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -2,7 +2,7 @@ "valid": { "build": [ { - "description": "pubKeyHash 1:1 transaction", + "description": "pubKeyHash->pubKeyHash 1:1 transaction", "txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e", "inputs": [ { @@ -19,7 +19,25 @@ ] }, { - "description": "2-of-2 P2SH multisig Transaction", + "description": "pubKey->pubKeyHash 1:1 transaction", + "txid": "a900dea133a3c51e9fe55d82bf4a4f50a4c3ac6e380c841f93651a076573320c", + "inputs": [ + { + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG", + "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 2500000000 + } + ] + }, + { + "description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction", "txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24", "inputs": [ { diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 36ad20c..0d2abdf 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -153,14 +153,13 @@ describe('TransactionBuilder', function() { fixtures.valid.build.forEach(function(f) { it('builds the correct transaction', function() { f.inputs.forEach(function(input) { - var prevTx - if (input.prevTx.length === 64) { - prevTx = input.prevTx - } else { - prevTx = Transaction.fromHex(input.prevTx) + var prevTxScript + + if (input.prevTxScript) { + prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(prevTx, input.index) + txb.addInput(input.prevTx, input.index, prevTxScript) }) f.outputs.forEach(function(output) { @@ -192,14 +191,13 @@ describe('TransactionBuilder', function() { fixtures.invalid.build.forEach(function(f) { it('throws on ' + f.exception, function() { f.inputs.forEach(function(input) { - var prevTx - if (input.prevTx.length === 64) { - prevTx = input.prevTx - } else { - prevTx = Transaction.fromHex(input.prevTx) + var prevTxScript + + if (input.prevTxScript) { + prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(prevTx, input.index) + txb.addInput(input.prevTx, input.index, prevTxScript) }) f.outputs.forEach(function(output) { From 31ea956e8e0cc00ad1590565b3475e031df0d0a0 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 16 Jul 2014 23:39:20 +1000 Subject: [PATCH 08/16] TxBuilder: add invalid nulldata case --- test/fixtures/transaction_builder.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 3fbc794..4301cda 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -115,6 +115,23 @@ "value": 1000 } ] + }, + { + "exception": "nulldata not supported", + "inputs": [ + { + "index": 0, + "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", + "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + } + ], + "outputs": [ + { + "script": "OP_DUP OP_HASH160 aa4d7985c57e011a8b3dd8e0e5a73aaef41629c5 OP_EQUALVERIFY OP_CHECKSIG", + "value": 1000 + } + ] } ] } From 14211b5f3e8eb1f854b005fd0811250026bfa91a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 18 Jul 2014 02:50:36 +1000 Subject: [PATCH 09/16] TxBuilder: sign after error checking --- src/transaction_builder.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index b2e0396..f0863d3 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -94,8 +94,6 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT hash = this.tx.hashForSignature(index, prevOutScript, hashType) } - var signature = privKey.sign(hash) - if (!(index in this.signatures)) { this.signatures[index] = { hashType: hashType, @@ -107,11 +105,13 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT } var input = this.signatures[index] - input.pubKeys.push(privKey.pub) - input.signatures.push(signature) assert.equal(input.hashType, hashType, 'Inconsistent hashType') assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') + + var signature = privKey.sign(hash) + input.pubKeys.push(privKey.pub) + input.signatures.push(signature) } TransactionBuilder.prototype.build = function() { From 1e3e003120fbc182e5e4e2fdd45aa8684dc55126 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 25 Jul 2014 15:49:42 +1000 Subject: [PATCH 10/16] TxBuilder: remove unnecessary assert --- src/transaction_builder.js | 2 -- test/transaction_builder.js | 6 ------ 2 files changed, 8 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index f0863d3..5a86ffb 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -24,8 +24,6 @@ TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript) { Array.prototype.reverse.call(prevOutHash) } else if (prevTx instanceof Transaction) { - assert(prevOutScript === undefined, 'Unnecessary Script provided') - prevOutHash = prevTx.getHash() prevOutScript = prevTx.outs[index].script diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 0d2abdf..5bc972a 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -68,12 +68,6 @@ describe('TransactionBuilder', function() { assert.equal(txb.addInput(prevTxHash, 1), 1) }) - it('throws if a Tx and prevOutScript is given', function() { - assert.throws(function() { - txb.addInput(prevTx, 0, privScript) - }, /Unnecessary Script provided/) - }) - it('throws if prevOutScript is not supported', function() { assert.throws(function() { txb.addInput(prevTxHash, 0, Script.EMPTY) From 418a56cbdcc7791dec465dbe39d5cbbdf5b59efd Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 25 Jul 2014 17:16:30 +1000 Subject: [PATCH 11/16] index: add TransactionBuilder --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index 0b2897e..73f3c1e 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ module.exports = { Script: require('./script'), scripts: require('./scripts'), Transaction: require('./transaction'), + TransactionBuilder: require('./transaction_builder'), networks: require('./networks'), Wallet: require('./wallet') } From f3199b6fcef68ea543a8c33200c20c37a3e29c8d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 25 Jul 2014 16:18:13 +1000 Subject: [PATCH 12/16] tests: integration test to use TxBuilder --- test/integration/p2sh.js | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/test/integration/p2sh.js b/test/integration/p2sh.js index ff7946c..3d47406 100644 --- a/test/integration/p2sh.js +++ b/test/integration/p2sh.js @@ -1,14 +1,12 @@ var assert = require('assert') var bitcoin = require('../../') -var crypto = bitcoin.crypto var networks = bitcoin.networks var scripts = bitcoin.scripts var Address = bitcoin.Address var ECKey = bitcoin.ECKey -var Transaction = bitcoin.Transaction -var Script = bitcoin.Script +var TransactionBuilder = bitcoin.TransactionBuilder var helloblock = require('helloblock-js')({ network: 'testnet' @@ -43,35 +41,31 @@ describe('Bitcoin-js', function() { var targetAddress = ECKey.makeRandom().pub.getAddress(networks.testnet).toString() // get latest unspents from the multisigAddress - helloblock.addresses.getUnspents(multisigAddress, function(err, resp, resource) { + helloblock.addresses.getUnspents(multisigAddress, function(err, res, unspents) { if (err) return done(err) // use the oldest unspent - var unspent = resource[resource.length - 1] + var unspent = unspents[unspents.length - 1] var spendAmount = Math.min(unspent.value, outputAmount) - var tx = new Transaction() - tx.addInput(unspent.txHash, unspent.index) - tx.addOutput(targetAddress, spendAmount) + var txb = new TransactionBuilder() + txb.addInput(unspent.txHash, unspent.index) + txb.addOutput(targetAddress, spendAmount) - var signatures = privKeys.map(function(privKey) { - return tx.signInput(0, redeemScript, privKey) + privKeys.forEach(function(privKey) { + txb.sign(0, privKey, redeemScript) }) - var redeemScriptSig = scripts.multisigInput(signatures) - var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) - tx.setInputScript(0, scriptSig) - // broadcast our transaction - helloblock.transactions.propagate(tx.toHex(), function(err, resp, resource) { + helloblock.transactions.propagate(txb.build().toHex(), function(err, res) { // no err means that the transaction has been successfully propagated if (err) return done(err) // Check that the funds (spendAmount Satoshis) indeed arrived at the intended address - helloblock.addresses.get(targetAddress, function(err, resp, resource) { + helloblock.addresses.get(targetAddress, function(err, res, addrInfo) { if (err) return done(err) - assert.equal(resource.balance, spendAmount) + assert.equal(addrInfo.balance, spendAmount) done() }) }) From 7f62069d82735e16b8d725e8d5c8645e347efe8b Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 25 Jul 2014 18:30:39 +1000 Subject: [PATCH 13/16] TxBuilder: add sequence number passthrough --- src/transaction_builder.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 5a86ffb..c3d3507 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -14,7 +14,7 @@ function TransactionBuilder() { this.tx = new Transaction() } -TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript) { +TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript, sequence) { var prevOutHash if (typeof prevTx === 'string') { @@ -46,7 +46,7 @@ TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript) { var prevOut = prevOutHash.toString('hex') + ':' + index assert(!(prevOut in this.prevOutMap), 'Transaction is already an input') - var vout = this.tx.addInput(prevOutHash, index) + var vout = this.tx.addInput(prevOutHash, index, sequence) this.prevOutMap[prevOut] = true this.prevOutScripts[vout] = prevOutScript this.prevOutTypes[vout] = prevOutType From f9fed3c8155ef622bc29a3382881444b75cc3fef Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 28 Jul 2014 14:28:44 +1000 Subject: [PATCH 14/16] TxBuilder: adds fromTransaction impl. and basic tests --- src/transaction_builder.js | 124 +++++++++++++++++++++---- test/fixtures/transaction_builder.json | 24 ++++- test/transaction_builder.js | 32 +++++-- 3 files changed, 146 insertions(+), 34 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index c3d3507..ad8c704 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -1,9 +1,10 @@ var assert = require('assert') var scripts = require('./scripts') -var ECKey = require('./eckey') -var Transaction = require('./transaction') +var ECPubKey = require('./ecpubkey') +var ECSignature = require('./ecsignature') var Script = require('./script') +var Transaction = require('./transaction') function TransactionBuilder() { this.prevOutMap = {} @@ -14,7 +15,7 @@ function TransactionBuilder() { this.tx = new Transaction() } -TransactionBuilder.prototype.addInput = function(prevTx, index, prevOutScript, sequence) { +TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) { var prevOutHash if (typeof prevTx === 'string') { @@ -103,7 +104,6 @@ TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashT } var input = this.signatures[index] - assert.equal(input.hashType, hashType, 'Inconsistent hashType') assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') @@ -130,29 +130,34 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { var tx = this.tx.clone() + // Create script signatures from signature meta-data this.signatures.forEach(function(input, index) { var scriptSig + var scriptType = input.scriptType var signatures = input.signatures.map(function(signature) { return signature.toScriptSignature(input.hashType) }) - if (input.scriptType === 'pubkeyhash') { - var signature = signatures[0] - var publicKey = input.pubKeys[0] - scriptSig = scripts.pubKeyHashInput(signature, publicKey) - - } else if (input.scriptType === 'multisig') { - var redeemScript = allowIncomplete ? undefined : input.redeemScript - scriptSig = scripts.multisigInput(signatures, redeemScript) - - } else if (input.scriptType === 'pubkey') { - var signature = signatures[0] - scriptSig = scripts.pubKeyInput(signature) - - } else { - assert(false, input.scriptType + ' not supported') - + switch (scriptType) { + case 'pubkeyhash': + var signature = signatures[0] + var pubKey = input.pubKeys[0] + scriptSig = scripts.pubKeyHashInput(signature, pubKey) + + break + case 'multisig': + var redeemScript = allowIncomplete ? undefined : input.redeemScript + scriptSig = scripts.multisigInput(signatures, redeemScript) + + break + case 'pubkey': + var signature = signatures[0] + scriptSig = scripts.pubKeyInput(signature) + + break + default: + assert(false, scriptType + ' not supported') } if (input.redeemScript) { @@ -165,4 +170,83 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { return tx } +TransactionBuilder.fromTransaction = function(transaction) { + var txb = new TransactionBuilder() + + // Extract/add inputs + transaction.ins.forEach(function(txin) { + txb.addInput(txin.hash, txin.index, txin.sequence) + }) + + // Extract/add outputs + transaction.outs.forEach(function(txout) { + txb.addOutput(txout.script, txout.value) + }) + + // Extract/add signatures + transaction.ins.forEach(function(txin) { + // Ignore empty scripts + if (txin.script.buffer.length === 0) return + + var redeemScript + var scriptSig = txin.script + var scriptType = scripts.classifyInput(scriptSig) + + // 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) + assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') + } + + // Extract hashType, pubKeys and signatures + var hashType, pubKeys, signatures + + switch (scriptType) { + case 'pubkeyhash': + var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1]) + + hashType = parsed.hashType + pubKeys = [pubKey] + signatures = [parsed.signature] + + break + case 'multisig': + var scriptSigs = scriptSig.chunks.slice(1) // ignore OP_0 + var parsed = scriptSigs.map(function(scriptSig) { + return ECSignature.parseScriptSignature(scriptSig) + }) + + hashType = parsed[0].hashType + pubKeys = [] + signatures = parsed.map(function(p) { return p.signature }) + + break + case 'pubkey': + var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + + hashType = parsed.hashType + pubKeys = [] + signatures = [parsed.signature] + + break + default: + assert(false, scriptType + ' not supported') + } + + txb.signatures[txin.index] = { + hashType: hashType, + pubKeys: pubKeys, + redeemScript: redeemScript, + scriptType: scriptType, + signatures: signatures + } + }) + + return txb +} + module.exports = TransactionBuilder diff --git a/test/fixtures/transaction_builder.json b/test/fixtures/transaction_builder.json index 4301cda..c2444ea 100644 --- a/test/fixtures/transaction_builder.json +++ b/test/fixtures/transaction_builder.json @@ -4,11 +4,14 @@ { "description": "pubKeyHash->pubKeyHash 1:1 transaction", "txid": "bd641f4b0aa8bd70189ab45e935c4762f0e1c49f294b4779d79887937b7cf42e", + "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000006b483045022100a3b254e1c10b5d039f36c05f323995d6e5a367d98dd78a13d5bbc3991b35720e022022fccea3897d594de0689601fbd486588d5bfa6915be2386db0397ee9a6e80b601210279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798ffffffff0110270000000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { "index": 0, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ] } ], "outputs": [ @@ -21,12 +24,15 @@ { "description": "pubKey->pubKeyHash 1:1 transaction", "txid": "a900dea133a3c51e9fe55d82bf4a4f50a4c3ac6e380c841f93651a076573320c", + "txhex": "0100000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000494830450221009833abb3ab49d7004c06bcc79eafd6905ada3eee91f3376ad388548034acd9a702202e84dda6ef2678c82256afcfc459aaa68e179b2bb0e6b2dc3f1410e132c5e6c301ffffffff0100f90295000000001976a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac00000000", "inputs": [ { "index": 0, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTxScript": "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798 OP_CHECKSIG", - "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ] } ], "outputs": [ @@ -39,11 +45,15 @@ { "description": "2-of-2 P2SH multisig -> pubKeyHash 1:1 Transaction", "txid": "8c500ce6eef6c78a10de923b68394cf31120151bdc4600e4b12de865defa9d24", + "txhex": "0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1a0100473044022040039a3d0a806d6c2c0ac8a62f2467c979c897c945f3f11905b9c5ea76b4a88002200976f187f852f7d186e8e8aa39332092aa8a504b63a7ae3d0eca09ebea1497fd0147304402205522d1949d13347054bd5ea86cdcad2344f49628a935faaee8f5e744bd3ef87e022063a28ab077817222ccd7d5a70e77ed7274840b9ba8db5dd93a33bdd41813d548014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914faf1d99bf040ea9c7f8cc9f14ac6733ad75ce24688ac00000000", "inputs": [ { "index": 0, "prevTx": "4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", - "privKeys": ["91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT"], + "privKeys": [ + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + ], "redeemScript": "OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG" } ], @@ -101,7 +111,9 @@ { "index": 0, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ] }, { "index": 1, @@ -123,7 +135,9 @@ "index": 0, "prevTx": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "prevTxScript": "OP_RETURN 06deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474", - "privKeys": ["KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn"] + "privKeys": [ + "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sVHnoWn" + ] } ], "outputs": [ diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 5bc972a..563e242 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -33,33 +33,36 @@ describe('TransactionBuilder', function() { }) describe('addInput', function() { - it('accepts a txHash and index', function() { - var vin = txb.addInput(prevTxHash, 1) + it('accepts a txHash, index [and sequence number]', 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) assert.equal(txb.prevOutScripts[0], undefined) }) - it('accepts a txHash, index and scriptPubKey', function() { - var vin = txb.addInput(prevTxHash, 1, prevTx.outs[1].script) + it('accepts a txHash, index [, sequence number and scriptPubKey]', 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) assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) }) - it('accepts a prevTx and index', function() { - var vin = txb.addInput(prevTx, 1) + it('accepts a prevTx, index [and sequence number]', 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) assert.equal(txb.prevOutScripts[0], prevTx.outs[1].script) }) @@ -70,7 +73,7 @@ describe('TransactionBuilder', function() { it('throws if prevOutScript is not supported', function() { assert.throws(function() { - txb.addInput(prevTxHash, 0, Script.EMPTY) + txb.addInput(prevTxHash, 0, undefined, Script.EMPTY) }, /PrevOutScript not supported \(nonstandard\)/) }) @@ -153,7 +156,7 @@ describe('TransactionBuilder', function() { prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(input.prevTx, input.index, prevTxScript) + txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript) }) f.outputs.forEach(function(output) { @@ -191,7 +194,7 @@ describe('TransactionBuilder', function() { prevTxScript = Script.fromASM(input.prevTxScript) } - txb.addInput(input.prevTx, input.index, prevTxScript) + txb.addInput(input.prevTx, input.index, input.sequence, prevTxScript) }) f.outputs.forEach(function(output) { @@ -220,4 +223,15 @@ 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 txb = TransactionBuilder.fromTransaction(tx) + + assert.equal(txb.build().toHex(), f.txhex) + }) + }) + }) }) From 4f88980dfb0a729841f4222d47033005a704724e Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 28 Jul 2014 15:40:07 +1000 Subject: [PATCH 15/16] tests: add P2SH multisig example case --- src/transaction_builder.js | 6 ++++++ test/transaction_builder.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index ad8c704..023591a 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -146,16 +146,19 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { scriptSig = scripts.pubKeyHashInput(signature, pubKey) break + case 'multisig': var redeemScript = allowIncomplete ? undefined : input.redeemScript scriptSig = scripts.multisigInput(signatures, redeemScript) break + case 'pubkey': var signature = signatures[0] scriptSig = scripts.pubKeyInput(signature) break + default: assert(false, scriptType + ' not supported') } @@ -214,6 +217,7 @@ TransactionBuilder.fromTransaction = function(transaction) { signatures = [parsed.signature] break + case 'multisig': var scriptSigs = scriptSig.chunks.slice(1) // ignore OP_0 var parsed = scriptSigs.map(function(scriptSig) { @@ -225,6 +229,7 @@ TransactionBuilder.fromTransaction = function(transaction) { signatures = parsed.map(function(p) { return p.signature }) break + case 'pubkey': var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) @@ -233,6 +238,7 @@ TransactionBuilder.fromTransaction = function(transaction) { signatures = [parsed.signature] break + default: assert(false, scriptType + ' not supported') } diff --git a/test/transaction_builder.js b/test/transaction_builder.js index 563e242..d6ab2f0 100644 --- a/test/transaction_builder.js +++ b/test/transaction_builder.js @@ -233,5 +233,30 @@ describe('TransactionBuilder', function() { assert.equal(txb.build().toHex(), f.txhex) }) }) + + it('works for the P2SH multisig case', function() { + var privKeys = [ + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgwmaKkrx", + "91avARGdfge8E4tZfYLoxeJ5sGBdNJQH4kvjJoQFacbgww7vXtT" + ].map(function(wif) { return ECKey.fromWIF(wif) }) + var redeemScript = Script.fromASM("OP_2 0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8 04c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a OP_2 OP_CHECKMULTISIG") + + txb.addInput("4971f016798a167331bcbc67248313fbc444c6e92e4416efd06964425588f5cf", 0) + txb.addOutput("1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH", 10000) + txb.sign(0, privKeys[0], redeemScript) + + var tx = txb.buildIncomplete() + + // in another galaxy... + // ... far, far away + var txb2 = TransactionBuilder.fromTransaction(tx) + + // [you should] verify that Transaction is what you want... + // ... then sign it + txb2.sign(0, privKeys[1], redeemScript) + var tx2 = txb2.build() + + assert.equal(tx2.toHex(), '0100000001cff58855426469d0ef16442ee9c644c4fb13832467bcbc3173168a7916f0714900000000fd1c01004830450221009c92c1ae1767ac04e424da7f6db045d979b08cde86b1ddba48621d59a109d818022004f5bb21ad72255177270abaeb2d7940ac18f1e5ca1f53db4f3fd1045647a8a8014830450221009418caa5bc18da87b188a180125c0cf06dce6092f75b2d3c01a29493466800fd02206ead65e7ca6e0f17eefe6f78457c084eab59af7c9882be1437de2e7116358eb9014c8752410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b84104c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee51ae168fea63dc339a3c58419466ceaeef7f632653266d0e1236431a950cfe52a52aeffffffff0110270000000000001976a914751e76e8199196d454941c45d1b3a323f1433bd688ac00000000') + }) }) }) From 22f8c8aa4aa21ea5f41ed86d8b0efe075cd1e791 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 18 Aug 2014 08:59:26 +1000 Subject: [PATCH 16/16] TxBuilder: re-order functions to project standard --- src/transaction_builder.js | 230 +++++++++++++++++++------------------ 1 file changed, 116 insertions(+), 114 deletions(-) diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 023591a..05c0591 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -15,6 +15,90 @@ function TransactionBuilder() { this.tx = new Transaction() } +// Static constructors +TransactionBuilder.fromTransaction = function(transaction) { + var txb = new TransactionBuilder() + + // Extract/add inputs + transaction.ins.forEach(function(txin) { + txb.addInput(txin.hash, txin.index, txin.sequence) + }) + + // Extract/add outputs + transaction.outs.forEach(function(txout) { + txb.addOutput(txout.script, txout.value) + }) + + // Extract/add signatures + transaction.ins.forEach(function(txin) { + // Ignore empty scripts + if (txin.script.buffer.length === 0) return + + var redeemScript + var scriptSig = txin.script + var scriptType = scripts.classifyInput(scriptSig) + + // 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) + assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') + } + + // Extract hashType, pubKeys and signatures + var hashType, pubKeys, signatures + + switch (scriptType) { + case 'pubkeyhash': + var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1]) + + hashType = parsed.hashType + pubKeys = [pubKey] + signatures = [parsed.signature] + + break + + case 'multisig': + var scriptSigs = scriptSig.chunks.slice(1) // ignore OP_0 + var parsed = scriptSigs.map(function(scriptSig) { + return ECSignature.parseScriptSignature(scriptSig) + }) + + hashType = parsed[0].hashType + pubKeys = [] + signatures = parsed.map(function(p) { return p.signature }) + + break + + case 'pubkey': + var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + + hashType = parsed.hashType + pubKeys = [] + signatures = [parsed.signature] + + break + + default: + assert(false, scriptType + ' not supported') + } + + txb.signatures[txin.index] = { + hashType: hashType, + pubKeys: pubKeys, + redeemScript: redeemScript, + scriptType: scriptType, + signatures: signatures + } + }) + + return txb +} + +// Operations TransactionBuilder.prototype.addInput = function(prevTx, index, sequence, prevOutScript) { var prevOutHash @@ -63,55 +147,6 @@ TransactionBuilder.prototype.addOutput = function(scriptPubKey, value) { return this.tx.addOutput(scriptPubKey, value) } -TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { - assert(this.tx.ins.length >= index, 'No input at index: ' + index) - hashType = hashType || Transaction.SIGHASH_ALL - - var prevOutScript = this.prevOutScripts[index] - var prevOutType = this.prevOutTypes[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, 'nonstandard', 'RedeemScript not supported (nonstandard)') - - hash = this.tx.hashForSignature(index, redeemScript, hashType) - - } else { - prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript() - scriptType = prevOutType || 'pubkeyhash' - - assert.notEqual(scriptType, 'scripthash', 'PrevOutScript requires redeemScript') - - hash = this.tx.hashForSignature(index, prevOutScript, hashType) - } - - if (!(index in this.signatures)) { - this.signatures[index] = { - hashType: hashType, - pubKeys: [], - redeemScript: redeemScript, - scriptType: scriptType, - signatures: [] - } - } - - var input = this.signatures[index] - assert.equal(input.hashType, hashType, 'Inconsistent hashType') - assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') - - var signature = privKey.sign(hash) - input.pubKeys.push(privKey.pub) - input.signatures.push(signature) -} - TransactionBuilder.prototype.build = function() { return this.__build(false) } @@ -173,86 +208,53 @@ TransactionBuilder.prototype.__build = function(allowIncomplete) { return tx } -TransactionBuilder.fromTransaction = function(transaction) { - var txb = new TransactionBuilder() - - // Extract/add inputs - transaction.ins.forEach(function(txin) { - txb.addInput(txin.hash, txin.index, txin.sequence) - }) - - // Extract/add outputs - transaction.outs.forEach(function(txout) { - txb.addOutput(txout.script, txout.value) - }) - - // Extract/add signatures - transaction.ins.forEach(function(txin) { - // Ignore empty scripts - if (txin.script.buffer.length === 0) return - - var redeemScript - var scriptSig = txin.script - var scriptType = scripts.classifyInput(scriptSig) - - // 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) - assert.equal(scripts.classifyOutput(redeemScript), scriptType, 'Non-matching scriptSig and scriptPubKey in input') - } - - // Extract hashType, pubKeys and signatures - var hashType, pubKeys, signatures - - switch (scriptType) { - case 'pubkeyhash': - var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) - var pubKey = ECPubKey.fromBuffer(scriptSig.chunks[1]) +TransactionBuilder.prototype.sign = function(index, privKey, redeemScript, hashType) { + assert(this.tx.ins.length >= index, 'No input at index: ' + index) + hashType = hashType || Transaction.SIGHASH_ALL - hashType = parsed.hashType - pubKeys = [pubKey] - signatures = [parsed.signature] + var prevOutScript = this.prevOutScripts[index] + var prevOutType = this.prevOutTypes[index] - break + var scriptType, hash + if (redeemScript) { + prevOutScript = prevOutScript || scripts.scriptHashOutput(redeemScript.getHash()) + prevOutType = prevOutType || 'scripthash' - case 'multisig': - var scriptSigs = scriptSig.chunks.slice(1) // ignore OP_0 - var parsed = scriptSigs.map(function(scriptSig) { - return ECSignature.parseScriptSignature(scriptSig) - }) + assert.equal(prevOutType, 'scripthash', 'PrevOutScript must be P2SH') - hashType = parsed[0].hashType - pubKeys = [] - signatures = parsed.map(function(p) { return p.signature }) + scriptType = scripts.classifyOutput(redeemScript) - break + assert.notEqual(scriptType, 'scripthash', 'RedeemScript can\'t be P2SH') + assert.notEqual(scriptType, 'nonstandard', 'RedeemScript not supported (nonstandard)') - case 'pubkey': - var parsed = ECSignature.parseScriptSignature(scriptSig.chunks[0]) + hash = this.tx.hashForSignature(index, redeemScript, hashType) - hashType = parsed.hashType - pubKeys = [] - signatures = [parsed.signature] + } else { + prevOutScript = prevOutScript || privKey.pub.getAddress().toOutputScript() + scriptType = prevOutType || 'pubkeyhash' - break + assert.notEqual(scriptType, 'scripthash', 'PrevOutScript requires redeemScript') - default: - assert(false, scriptType + ' not supported') - } + hash = this.tx.hashForSignature(index, prevOutScript, hashType) + } - txb.signatures[txin.index] = { + if (!(index in this.signatures)) { + this.signatures[index] = { hashType: hashType, - pubKeys: pubKeys, + pubKeys: [], redeemScript: redeemScript, scriptType: scriptType, - signatures: signatures + signatures: [] } - }) + } - return txb + var input = this.signatures[index] + assert.equal(input.hashType, hashType, 'Inconsistent hashType') + assert.deepEqual(input.redeemScript, redeemScript, 'Inconsistent redeemScript') + + var signature = privKey.sign(hash) + input.pubKeys.push(privKey.pub) + input.signatures.push(signature) } module.exports = TransactionBuilder