From 1fde0a401d0515460f5cd2c499808b9a8356a65d Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Wed, 4 Feb 2015 20:22:11 +1100 Subject: [PATCH] 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) - } - - input.privKeys.forEach(function(wif, i) { - var privKey = ECKey.fromWIF(wif) + if (sign.redeemScript) { + redeemScript = Script.fromASM(sign.redeemScript) + } - 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)) } })