From cace8316e4433d61c32a5d262f54cf1b6eff91f4 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Fri, 9 May 2014 12:56:35 +1000 Subject: [PATCH] Script: extract test data to fixture --- test/fixtures/script.js | 54 ++++++++++++++++++++++++ test/script.js | 93 ++++++++++++----------------------------- 2 files changed, 81 insertions(+), 66 deletions(-) create mode 100644 test/fixtures/script.js diff --git a/test/fixtures/script.js b/test/fixtures/script.js new file mode 100644 index 0000000..8fb5d21 --- /dev/null +++ b/test/fixtures/script.js @@ -0,0 +1,54 @@ +module.exports = { + valid: [ + { + description: 'P2SH ScriptPubKey', + hex: 'a914e8c300c87986efa84c37c0519929019ef86eb5b487', + type: 'scripthash', + hash: '0ba47b56a573bab4b430ad6ed3ec79270e04b066', + scriptPubKey: true + }, + { + description: 'PubKeyHash ScriptPubKey', + hex: '76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac', + type: 'pubkeyhash', + hash: 'a5313f33d5c7b81674b35f7f3febc3522ef234db', + scriptPubKey: true + }, + { + description: 'pubKeyHash scriptSig', + hex: '48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8', + type: 'pubkeyhash', + hash: 'b9bac2a5c5c29bb27c382d41fa3d179c646c78fd', + scriptPubKey: false + }, + { + description: 'Valid multisig script', + hex: '5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae', + type: 'multisig', + hash: 'f1c98f0b74ecabcf78ae20dfa224bb6666051fbe', + scriptPubKey: true + }, + { + description: 'OP_RETURN script', + hex:'6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474', + type: 'nulldata', + hash: 'ec88f016655477663455fe6a8e83508c348ea145', + scriptPubKey: true + }, + { + description: 'Non standard script', + hex: 'aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087', + type: 'nonstandard', + hash: '3823382e70d1930989813d3459988e0d7c2861d8', + scriptPubKey: true + }, + { + description: 'Invalid multisig script', + asm: '0 0 0 OP_CHECKmulTISIG', + hex: '000000ae', + type: 'nonstandard', + hash: '62ede8963f9387544935f168745262f703dab1fb', + scriptPubKey: true + } + ] +} diff --git a/test/script.js b/test/script.js index 83b35ae..1da5fc1 100644 --- a/test/script.js +++ b/test/script.js @@ -5,30 +5,12 @@ var networks = require('..').networks var Address = require('../src/address.js') var Script = require('../src/script.js') +var fixtures = require('./fixtures/script') + function b2h(b) { return new Buffer(b).toString('hex') } function h2b(h) { return new Buffer(h, 'hex') } describe('Script', function() { - var p2shScriptPubKey, pubKeyScriptPubKey, addressScriptSig - - beforeEach(function(){ - p2shScriptPubKey = "a914e8c300c87986efa84c37c0519929019ef86eb5b487" - pubKeyScriptPubKey = "76a9145a3acbc7bbcc97c5ff16f5909c9d7d3fadb293a888ac" - addressScriptSig = "48304502206becda98cecf7a545d1a640221438ff8912d9b505ede67e0138485111099f696022100ccd616072501310acba10feb97cecc918e21c8e92760cd35144efec7622938f30141040cd2d2ce17a1e9b2b3b2cb294d40eecf305a25b7e7bfdafae6bb2639f4ee399b3637706c3d377ec4ab781355add443ae864b134c5e523001c442186ea60f0eb8" - - // txid: 09dd94f2c85262173da87a745a459007bb1eed6eeb6bfa238a0cd91a16cf7790 - validMultisigScript = '5121032487c2a32f7c8d57d2a93906a6457afd00697925b0e6e145d89af6d3bca330162102308673d16987eaa010e540901cc6fe3695e758c19f46ce604e174dac315e685a52ae' - - // txid: 5e9be7fb36ee49ce84bee4c8ef38ad0efc0608b78dae1c2c99075297ef527890 - opreturnScript = '6a2606deadbeef03f895a2ad89fb6d696497af486cb7c644a27aa568c7a18dd06113401115185474' - - // asm: "0 0 0 OP_CHECKMULTISIG" - invalidMultisigScript = '000000ae' - - // txid: a4bfa8ab6435ae5f25dae9d89e4eb67dfa94283ca751f393c1ddc5a837bbc31b - nonStandardScript = 'aa206fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d619000000000087' - }) - describe('constructor', function() { it('works for a byte array', function() { assert.ok(new Script([])) @@ -44,57 +26,34 @@ describe('Script', function() { }) describe('fromHex/toHex', function() { - it('matches the test data', function() { - [ - p2shScriptPubKey, - pubKeyScriptPubKey, - addressScriptSig, - validMultisigScript, - opreturnScript, - nonStandardScript, - invalidMultisigScript - ].forEach(function(hex) { - assert.equal(Script.fromHex(hex).toHex(), hex) + it('matches the test vectors', function() { + fixtures.valid.forEach(function(f) { + assert.equal(Script.fromHex(f.hex).toHex(), f.hex) }) }) }) - describe('getOutType', function() { - it('supports p2sh', function() { - var script = Script.fromHex(p2shScriptPubKey) - assert.equal(script.getOutType(), 'scripthash') - }) + describe('getInType', function() { + fixtures.valid.forEach(function(f) { + if (!f.scriptPubKey) { + it('supports ' + f.description, function() { + var script = Script.fromHex(f.hex) - it('supports pubkeyhash', function() { - var script = Script.fromHex(pubKeyScriptPubKey) - assert.equal(script.getOutType(), 'pubkeyhash') - }) - - it('supports multisig', function() { - var script = Script.fromHex(validMultisigScript) - assert.equal(script.getOutType(), 'multisig') - }) - - it('supports null_data', function() { - var script = Script.fromHex(opreturnScript) - assert.equal(script.getOutType(), 'nulldata') - }) - - it('supports nonstandard script', function() { - var script = Script.fromHex(nonStandardScript) - assert.equal(script.getOutType(), 'nonstandard') - }) - - it('identifies invalid multisig script as nonstandard', function() { - var script = Script.fromHex(invalidMultisigScript) - assert.equal(script.getOutType(), 'nonstandard') + assert.equal(script.getInType(), f.type) + }) + } }) }) - describe('getInType', function() { - it('works for address', function() { - var script = Script.fromHex(addressScriptSig) - assert.equal(script.getInType(), 'pubkeyhash') + describe('getOutType', function() { + fixtures.valid.forEach(function(f) { + if (f.scriptPubKey) { + it('supports ' + f.description, function() { + var script = Script.fromHex(f.hex) + + assert.equal(script.getOutType(), f.type) + }) + } }) }) @@ -103,7 +62,8 @@ describe('Script', function() { var address = Address.fromBase58Check('19E6FV3m3kEPoJD5Jz6dGKdKwTVvjsWUvu') var script = Script.createPubKeyHashScriptPubKey(address.hash) - assert.equal(script.toHex(), pubKeyScriptPubKey) + // FIXME: not good TDD + assert.equal(script.toHex(), fixtures.valid[1].hex) }) }) @@ -112,11 +72,12 @@ describe('Script', function() { var hash = new Buffer('e8c300c87986efa84c37c0519929019ef86eb5b4', 'hex') var script = Script.createP2SHScriptPubKey(hash) - assert.equal(script.toHex(), p2shScriptPubKey) + // FIXME: not good TDD + assert.equal(script.toHex(), fixtures.valid[0].hex) }) }) - describe('2-of-3 Multi-Signature', function() { + describe('2-of-3 Multi-Signature scriptPubKey', function() { var pubKeys beforeEach(function() {