From 576d5dfa3fe53ddf83a73c7277b9ef357d296627 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sat, 14 Jun 2014 23:30:34 +1000 Subject: [PATCH] Scripts: move test data to fixtures --- test/fixtures/scripts.json | 80 ++++++++++++++++ test/scripts.js | 185 ++++++++++++++++++++----------------- 2 files changed, 182 insertions(+), 83 deletions(-) create mode 100644 test/fixtures/scripts.json diff --git a/test/fixtures/scripts.json b/test/fixtures/scripts.json new file mode 100644 index 0000000..58eb32e --- /dev/null +++ b/test/fixtures/scripts.json @@ -0,0 +1,80 @@ +{ + "valid": { + "pubKey": [ + { + "pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "scriptPubKey": "2102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1ac", + "scriptSig": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801" + } + ], + "pubKeyHash": [ + { + "pubKey": "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "signature": "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "scriptPubKey": "76a914aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac", + "scriptSig": "47304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca28012102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1" + } + ], + "multisig": [ + { + "pubKeys": [ + "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a" + ], + "signatures": [ + "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801", + "3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501" + ], + "scriptPubKey": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae", + "scriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501" + }, + { + "pubKeys": [ + "02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f", + "02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f", + "036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19" + ], + "signatures": [], + "scriptPubKey": "532102ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f2102fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f21036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e1953ae", + "scriptSig": "" + } + ], + "scripthash": [ + { + "redeemScript": "522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae", + "redeemScriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501", + "scriptSig": "0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae", + "scriptPubKey": "a914722ff0bc2c3f47b35c20df646c395594da24e90e87" + } + ] + }, + "invalid": { + "multisig": [ + { + "exception": "Not enough pubKeys provided", + "m": 4, + "pubKeys": [ + "02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f", + "02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f", + "036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19" + ], + "signatures": [ + "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801" + ], + "scriptPubKey": true + }, + { + "exception": "Not enough signatures provided", + "pubKeys": [ + "02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1", + "0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a" + ], + "signatures": [ + "304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801" + ], + "scriptPubKey": false + } + ] + } +} diff --git a/test/scripts.js b/test/scripts.js index 19222e5..b6b07e5 100644 --- a/test/scripts.js +++ b/test/scripts.js @@ -8,9 +8,7 @@ var ECPubKey = require('../src/ecpubkey') var Script = require('../src/script') var fixtures = require('./fixtures/script.json') - -function b2h(b) { return new Buffer(b).toString('hex') } -function h2b(h) { return new Buffer(h, 'hex') } +var fixtures2 = require('./fixtures/scripts.json') describe('Scripts', function() { describe('classifyInput', function() { @@ -39,106 +37,127 @@ describe('Scripts', function() { }) }) - // FIXME: bad - describe('pay-to-pubKeyHash', function() { - it('matches the test data', function() { - var f = fixtures.valid[2] - var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu') - var script = scripts.pubKeyHashOutput(address.hash) - - assert.equal(script.toHex(), f.hex) - }) - }) - - // FIXME: bad - describe('pay-to-pubkey', function() { - describe('input', function() { - it('matches the test data', function() { - var f = fixtures.valid[4] - var signature = new Buffer(f.signature, 'hex') - var script = scripts.pubKeyInput(signature) + describe('pubKey', function() { + fixtures2.valid.pubKey.forEach(function(f) { + describe('input script', function() { + it('is generated correctly for ' + f.pubKey, function() { + var signature = new Buffer(f.signature, 'hex') - assert.equal(script.toHex(), f.hex) + var scriptSig = scripts.pubKeyInput(signature) + assert.equal(scriptSig.toHex(), f.scriptSig) + }) }) - }) - describe('output', function() { - it('matches the test data', function() { - var f = fixtures.valid[0] - var pubKey = ECPubKey.fromHex(f.pubKey) - var script = scripts.pubKeyOutput(pubKey) + describe('output script', function() { + it('is generated correctly for ' + f.pubKey, function() { + var pubKey = ECPubKey.fromHex(f.pubKey) - assert.equal(script.toHex(), f.hex) + var scriptPubKey = scripts.pubKeyOutput(pubKey) + assert.equal(scriptPubKey.toHex(), f.scriptPubKey) + }) }) }) }) - // FIXME: bad - describe('pay-to-scriptHash', function() { - it('matches the test data', function() { - var f = fixtures.valid[1] - var address = Address.fromBase58Check('3NukJ6fYZJ5Kk8bPjycAnruZkE5Q7UW7i8') - var script = scripts.scriptHashOutput(address.hash) + describe('pubKeyHash', function() { + fixtures2.valid.pubKeyHash.forEach(function(f) { + var pubKey = ECPubKey.fromHex(f.pubKey) + var address = pubKey.getAddress() - assert.equal(script.toHex(), f.hex) - }) - }) + describe('input script', function() { + it('is generated correctly for ' + address, function() { + var signature = new Buffer(f.signature, 'hex') - // FIXME: bad - describe('2-of-3 Multi-Signature scriptPubKey', function() { - var pubKeys + var scriptSig = scripts.pubKeyHashInput(signature, pubKey) + assert.equal(scriptSig.toHex(), f.scriptSig) + }) + }) - beforeEach(function() { - pubKeys = [ - '02ea1297665dd733d444f31ec2581020004892cdaaf3dd6c0107c615afb839785f', - '02fab2dea1458990793f56f42e4a47dbf35a12a351f26fa5d7e0cc7447eaafa21f', - '036c6802ce7e8113723dd92cdb852e492ebb157a871ca532c3cb9ed08248ff0e19' - ].map(ECPubKey.fromHex) + describe('output script', function() { + it('is generated correctly for ' + address, function() { + var scriptPubKey = scripts.pubKeyHashOutput(address.hash) + assert.equal(scriptPubKey.toHex(), f.scriptPubKey) + }) + }) }) + }) - it('should create valid redeemScript', function() { - var redeemScript = scripts.multisigOutput(2, pubKeys) - - var hash160 = crypto.hash160(new Buffer(redeemScript.buffer)) - var multisigAddress = new Address(hash160, networks.bitcoin.scripthash) - - assert.equal(multisigAddress.toString(), '32vYjxBb7pHJJyXgNk8UoK3BdRDxBzny2v') + describe('multisig', function() { + fixtures2.valid.multisig.forEach(function(f) { + var pubKeys = f.pubKeys.map(ECPubKey.fromHex) + var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) + + // FIXME: some missing test data for now + if (f.scriptSig) { + describe('input script', function() { + it('is generated correctly for ' + scriptPubKey.toHex(), function() { + var signatures = f.signatures.map(function(signature) { + return new Buffer(signature, 'hex') + }) + + var scriptSig = scripts.multisigInput(signatures) + assert.equal(scriptSig.toHex(), f.scriptSig) + }) + }) + } + + describe('output script', function() { + it('is generated correctly for ' + scriptPubKey.toHex(), function() { + assert.equal(scriptPubKey.toHex(), f.scriptPubKey) + }) + }) }) - it('should throw on not enough pubKeys provided', function() { - assert.throws(function() { - scripts.multisigOutput(4, pubKeys) - }, /Not enough pubKeys provided/) + fixtures2.invalid.multisig.forEach(function(f) { + var pubKeys = f.pubKeys.map(ECPubKey.fromHex) + var scriptPubKey = scripts.multisigOutput(pubKeys.length, pubKeys) + + if (f.scriptPubKey) { + describe('output script', function() { + it('throws on ' + f.exception, function() { + assert.throws(function() { + scripts.multisigOutput(f.m, pubKeys) + }, new RegExp(f.exception)) + }) + }) + } else { + describe('input script', function() { + it('throws on ' + f.exception, function() { + var signatures = f.signatures.map(function(signature) { + return new Buffer(signature, 'hex') + }) + + assert.throws(function() { + scripts.multisigInput(signatures, scriptPubKey) + }, new RegExp(f.exception)) + }) + }) + } }) }) - // FIXME: bad - describe('2-of-2 Multisig scriptSig', function() { - var pubKeys = [ - '02359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1', - '0395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a' - ].map(ECPubKey.fromHex) - var signatures = [ - '304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801', - '3045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d140501' - ].map(h2b) - var expected = '0047304402207515cf147d201f411092e6be5a64a6006f9308fad7b2a8fdaab22cd86ce764c202200974b8aca7bf51dbf54150d3884e1ae04f675637b926ec33bf75939446f6ca2801483045022100ef253c1faa39e65115872519e5f0a33bbecf430c0f35cf562beabbad4da24d8d02201742be8ee49812a73adea3007c9641ce6725c32cd44ddb8e3a3af460015d14050147522102359c6e3f04cefbf089cf1d6670dc47c3fb4df68e2bad1fa5a369f9ce4b42bbd1210395a9d84d47d524548f79f435758c01faec5da2b7e551d3b8c995b7e06326ae4a52ae' - - it('should create a valid P2SH multisig scriptSig', function() { - var redeemScript = scripts.multisigOutput(2, pubKeys) - var redeemInput = scripts.multisigInput(signatures) - - var scriptSig = scripts.scriptHashInput(redeemInput, redeemScript) - - assert.equal(b2h(scriptSig.buffer), expected) - }) + describe('scripthash', function() { + fixtures2.valid.scripthash.forEach(function(f) { + var redeemScript = Script.fromHex(f.redeemScript) + var redeemScriptSig = Script.fromHex(f.redeemScriptSig) - it('should throw on not enough signatures', function() { - var redeemScript = scripts.multisigOutput(2, pubKeys) + var address = Address.fromOutputScript(Script.fromHex(f.scriptPubKey)) - assert.throws(function() { - scripts.multisigInput(signatures.slice(1), redeemScript) - }, /Not enough signatures provided/) + describe('input script', function() { + it('is generated correctly for ' + address, function() { + var scriptSig = scripts.scriptHashInput(redeemScriptSig, redeemScript) + + assert.equal(scriptSig.toHex(), f.scriptSig) + }) + }) + + describe('output script', function() { + it('is generated correctly for ' + address, function() { + var scriptPubKey = scripts.scriptHashOutput(redeemScript.getHash()) + + assert.equal(scriptPubKey.toHex(), f.scriptPubKey) + }) + }) }) }) })