From bed6ccaac0c143d1bce897046fe4e15eadb1d5b0 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 25 Jul 2014 13:03:15 -0300 Subject: [PATCH 1/5] start adding script tests --- lib/Script.js | 8 ++++---- test/test.Script.js | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/Script.js b/lib/Script.js index 6f732433f..f611f74b8 100644 --- a/lib/Script.js +++ b/lib/Script.js @@ -115,8 +115,8 @@ Script.prototype.isMultiSig = function() { this.chunks[this.chunks.length - 1] == Opcode.map.OP_CHECKMULTISIG); }; -Script.prototype.isPubkeyHashScript = function() { - // TODO: add more restrictions to chunks +Script.prototype.isPubkeyHashScriptSig = function() { + // TODO: add more restrictions to chunks? return (this.chunks.length == 2 && Buffer.isBuffer(this.chunks[0]) && Buffer.isBuffer(this.chunks[1])); @@ -149,7 +149,7 @@ Script.prototype.countSignatures = function() { ret = l - 2; } // p2pubkeyhash - else if (this.isPubkeyHashScript()) { + else if (this.isPubkeyHashScriptSig()) { ret = 1; } // p2pubkey @@ -176,7 +176,7 @@ Script.prototype.getSignatures = function() { } } // p2pubkeyhash - else if (this.isPubkeyHashScript()) { + else if (this.isPubkeyHashScriptSig()) { ret.push(this.chunks[0]); } // p2pubkey diff --git a/test/test.Script.js b/test/test.Script.js index c3fb878a2..01d4f1479 100644 --- a/test/test.Script.js +++ b/test/test.Script.js @@ -191,5 +191,23 @@ describe('Script', function() { }); }); + describe('#isPubkeyHashScriptSig', function() { + var testPKHSS = function(raw, result) { + var s = new Script(new Buffer(raw, 'hex'); + s.isPubkeyHashScriptSig().should.equal(result); + }; + it('should identify pubkeyhash scriptsig', function() { + testPKHSS(pkhss, true); + }); + it('should not identify pubkey scriptsig', function() { + testPKHSS(pkss, false); + }); + it('should not identify p2sh scriptsig', function() { + testPKHSS(p2shss, false); + }); + it('should not identify multisig scriptsig', function() { + testPKHSS(msss, false); + }); + }); }); From b727cc0d7c707282d2214feb4d77a15c025cc5cc Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 28 Jul 2014 14:03:22 -0300 Subject: [PATCH 2/5] add Script validation tests --- lib/Script.js | 2 +- test/test.Script.js | 92 ++++++++++++++++++++++++++++++--------------- 2 files changed, 63 insertions(+), 31 deletions(-) diff --git a/lib/Script.js b/lib/Script.js index f611f74b8..1b5c77d88 100644 --- a/lib/Script.js +++ b/lib/Script.js @@ -116,7 +116,6 @@ Script.prototype.isMultiSig = function() { }; Script.prototype.isPubkeyHashScriptSig = function() { - // TODO: add more restrictions to chunks? return (this.chunks.length == 2 && Buffer.isBuffer(this.chunks[0]) && Buffer.isBuffer(this.chunks[1])); @@ -423,6 +422,7 @@ Script.prototype.writeOp = function(opcode) { this.buffer = buf; this.chunks.push(opcode); + return this; }; Script.prototype.writeN = function(n) { diff --git a/test/test.Script.js b/test/test.Script.js index 01d4f1479..1de7c5260 100644 --- a/test/test.Script.js +++ b/test/test.Script.js @@ -7,6 +7,7 @@ var should = chai.should(); var ScriptModule = bitcore.Script; var Address = bitcore.Address; +var Opcode = bitcore.Opcode; var networks = bitcore.networks; var Script; var testdata = testdata || require('./testdata'); @@ -116,12 +117,12 @@ describe('Script', function() { '0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea', '02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70', '02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793' - ]; + ]; describe('#_sortKeys', function() { it('should get the pubkeys in properly sorted order', function() { - var pubs = testPubKeysHex.map( function(hex) { - return new Buffer(hex,'hex'); + var pubs = testPubKeysHex.map(function(hex) { + return new Buffer(hex, 'hex'); }); var sorted = Script._sortKeys(pubs); sorted[0].toString('hex').should.equal(testPubKeysHex[2]); @@ -134,17 +135,19 @@ describe('Script', function() { describe('#createMultisig', function() { it('should create ', function() { - var pubs = testPubKeysHex.map( function(hex) { - return new Buffer(hex,'hex'); + var pubs = testPubKeysHex.map(function(hex) { + return new Buffer(hex, 'hex'); }); // 3 of 5 multisig, unsorted // test case generated with: bitcoind createmultisig 3 '["02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0", "02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758", "0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea","02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70", "02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793"]' - var s1 = Script.createMultisig(3,pubs, {noSorting: true}); + var s1 = Script.createMultisig(3, pubs, { + noSorting: true + }); s1.getBuffer().toString('hex').should.equal('532102c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae02102b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758210266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea2102ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e702102c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af79355ae'); // 3 of 5 multisig, sorted // test case generated with: bitcoind createmultisig 3 '["0266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea", "02b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f0758", "02c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae0", "02c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af793", "02ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e70"]' - var s2 = Script.createMultisig(3,pubs); + var s2 = Script.createMultisig(3, pubs); s2.getBuffer().toString('hex').should.equal('53210266dd7664e65958f3cc67bf92ad6243bc495df5ab56691719263977104b635bea2102b937d54b550a3afdc2819772822d25869495f9e588b56a0205617d80514f07582102c525d65d18be8fb36ab50a21bee02ac9fdc2c176fa18791ac664ea4b95572ae02102c8f63ad4822ef360b5c300f08488fa0fa24af2b2bebb6d6b602ca938ee5af7932102ee91377073b04d1d9d19597b81a7be3db6554bd7d16151cb5599a6107a589e7055ae'); }); @@ -156,7 +159,7 @@ describe('Script', function() { it('should count missing signature in empty scripts', function() { var s = new Script(); s.countMissingSignatures().should.equal(1); - }); + }); it('should count missing signatures p2sh 2-3 1 missing', function() { // from https://gist.github.com/matiu/11182987 var b = new Buffer('00483045022100aac57f3ba004e6265097b759d92132c43fb5dcb033c2a3f6e61caa5e05e6b97e02200dae579e54c8e733d222eae5bbbaf557bbcf03271cf76775c91744c24a99916b014c69522103197599f6e209cefef07da2fddc6fe47715a70162c531ffff8e611cef23dfb70d210392dccb2ed470a45984811d6402fdca613c175f8f3e4eb8e2306e8ccd7d0aed032103e085eb6fa1f20b2722c16161144314070a2c316a9cae2489fd52ce5f63fff6e453ae', 'hex'); @@ -177,37 +180,66 @@ describe('Script', function() { describe('#isMultiSig', function() { it('should return true for valid multisig scripts', function() { - var pubs = testPubKeysHex.map( function(hex) { - return new Buffer(hex,'hex'); + var pubs = testPubKeysHex.map(function(hex) { + return new Buffer(hex, 'hex'); + }); + var s1 = Script.createMultisig(3, pubs, { + noSorting: true }); - var s1 = Script.createMultisig(3,pubs, {noSorting: true}); s1.isMultiSig().should.equal(true); }); it('should return false for invalid multisig scripts', function() { - (new Script(new Buffer('000000ae','hex'))).isMultiSig().should.equal(false); - var s = new Script(new Buffer('522103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba42103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba45f6054ae','hex')); + (new Script(new Buffer('000000ae', 'hex'))).isMultiSig().should.equal(false); + var s = new Script(new Buffer('522103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba42103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba45f6054ae', 'hex')); + + (new Script(new Buffer('522103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba42103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba45f6054ae', 'hex'))).isMultiSig().should.equal(false); - (new Script(new Buffer('522103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba42103bb52138972c48a132fc1f637858c5189607dd0f7fe40c4f20f6ad65f2d389ba45f6054ae','hex'))).isMultiSig().should.equal(false); - }); }); - describe('#isPubkeyHashScriptSig', function() { - var testPKHSS = function(raw, result) { - var s = new Script(new Buffer(raw, 'hex'); - s.isPubkeyHashScriptSig().should.equal(result); + describe.only('ScriptSig validations', function() { + var pkhss = '4730440220150eccaec1e5d9104434544bf820b1e24c94e0da7a768d62260b57b9f02877db02204d5d193e833099adb0bf38a610d314936fb70671383d2fa6e09586bc77abe3f9012103146226860c4f62b1ab79bdbb0d3145bf1dc1a0cfa7bf35f2aa30e8432717ac72' + var p2shss = '004930460221008d36f82425396aff3797aed0651954b5bd2bf8768baf358fbeef9994a282d639022100e3967e55972a99b37da210e9a01c580dc3e0e4df8dc9f5a87ba4338c8fc9e5ba0147304402201aafdf74d2dc5d9d78baadd3beb2e565b0ed14489ad2f1434f9b51ad9b4fa7df02204de4400a1e6817c0883cae056baab77986e65a65aac885020d29d4ddafe30960014c69522103909e13a508df9edd35c806b4d0993bca644e69963041aa93dc209105cfd39b282103b3805706833fab77ae3ad3be1117bf797b460bd58c901f5e12721975d89aff8f2103d442f2fe27171b5d1404a9d7ca943e01951fdc103a25bd89089eb88b5a3e743a53ae' + var pkss = '0313220402bc01062f503253482f'; + var msss = '004830450220582cd0d8c0f42113ef036af9b5b26d500447eb47dd737e129b0d1b9f870166fa022100e6794cc9158cb2347ff440cec6c017ab5043bb71f1cff55baf9df4888902e26a0149304602210089c912fa687304f82634fe4e02f86ad721c3f9b8a6e7a2c06a7b0ba7a891ac18022100ff4f47c88c752a9e2e1ad8d450c7d5c06628159ea2e614f260dcf28c1c7333b101483045022100dd0c15876575df2e9973f3cd57c4f5e9e84d94277d2f4d82cebfb10fe2b25d62022060eb86654f538a5e5c55288de828bdd854e5d1434050a6b54d7d5402b59528ae01'; + var createTestF = function(f) { + var testF = function(raw, result) { + var s = new Script(new Buffer(raw, 'hex')); + f.bind(s)().should.equal(result); + }; + return testF; }; - it('should identify pubkeyhash scriptsig', function() { - testPKHSS(pkhss, true); + describe('#isPubkeyHashScriptSig', function() { + var isPubkeyHashScriptSig = new Script().isPubkeyHashScriptSig; + var testPKHSS = createTestF(isPubkeyHashScriptSig); + it('should identify pubkeyhash scriptsig', function() { + testPKHSS(pkhss, true); + }); + it('should not identify pubkey scriptsig', function() { + testPKHSS(pkss, false); + }); + it('should not identify p2sh scriptsig', function() { + testPKHSS(p2shss, false); + }); + it('should not identify multisig scriptsig', function() { + testPKHSS(msss, false); + }); }); - it('should not identify pubkey scriptsig', function() { - testPKHSS(pkss, false); + describe('#isP2shScriptSig', function() { + var isP2shScriptSig = new Script().isP2shScriptSig; + var testP2SHSS = createTestF(isP2shScriptSig); + it('should not identify pubkeyhash scriptsig', function() { + testP2SHSS(pkhss, false); + }); + it('should not identify pubkey scriptsig', function() { + testP2SHSS(pkss, false); + }); + it('should identify p2sh scriptsig', function() { + testP2SHSS(p2shss, true); + }); + it('should not identify multisig scriptsig', function() { + testP2SHSS(msss, false); + }); }); - it('should not identify p2sh scriptsig', function() { - testPKHSS(p2shss, false); - }); - it('should not identify multisig scriptsig', function() { - testPKHSS(msss, false); - }); - }); + }); }); From 80b5fcc614b4df1d15920138f3e22db58d0e19b1 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 28 Jul 2014 14:40:16 -0300 Subject: [PATCH 3/5] add is* tests for Script --- lib/Script.js | 4 ++++ test/test.Script.js | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/lib/Script.js b/lib/Script.js index 1b5c77d88..ea2217ffb 100644 --- a/lib/Script.js +++ b/lib/Script.js @@ -135,6 +135,10 @@ Script.prototype.isMultiSigScriptSig = function() { return false; return !this.isP2shScriptSig(); }; +Script.prototype.isPubkeyScriptSig = function() { + return (this.chunks.length == 1 && + Buffer.isBuffer(this.chunks[0])); +}; Script.prototype.countSignatures = function() { var ret = 0; diff --git a/test/test.Script.js b/test/test.Script.js index 1de7c5260..9ac3ec9b1 100644 --- a/test/test.Script.js +++ b/test/test.Script.js @@ -196,10 +196,10 @@ describe('Script', function() { }); }); - describe.only('ScriptSig validations', function() { + describe('ScriptSig validations', function() { var pkhss = '4730440220150eccaec1e5d9104434544bf820b1e24c94e0da7a768d62260b57b9f02877db02204d5d193e833099adb0bf38a610d314936fb70671383d2fa6e09586bc77abe3f9012103146226860c4f62b1ab79bdbb0d3145bf1dc1a0cfa7bf35f2aa30e8432717ac72' var p2shss = '004930460221008d36f82425396aff3797aed0651954b5bd2bf8768baf358fbeef9994a282d639022100e3967e55972a99b37da210e9a01c580dc3e0e4df8dc9f5a87ba4338c8fc9e5ba0147304402201aafdf74d2dc5d9d78baadd3beb2e565b0ed14489ad2f1434f9b51ad9b4fa7df02204de4400a1e6817c0883cae056baab77986e65a65aac885020d29d4ddafe30960014c69522103909e13a508df9edd35c806b4d0993bca644e69963041aa93dc209105cfd39b282103b3805706833fab77ae3ad3be1117bf797b460bd58c901f5e12721975d89aff8f2103d442f2fe27171b5d1404a9d7ca943e01951fdc103a25bd89089eb88b5a3e743a53ae' - var pkss = '0313220402bc01062f503253482f'; + var pkss = '483045022100afe5d533f9925f987991328b7abbdb5a705113b72488ff6cd5502dcfc2ea1c8b02205cb8a0c686bf13b439b2f4b6e8f69c08ae00df0901a89a4a1313a936b044a43f01'; var msss = '004830450220582cd0d8c0f42113ef036af9b5b26d500447eb47dd737e129b0d1b9f870166fa022100e6794cc9158cb2347ff440cec6c017ab5043bb71f1cff55baf9df4888902e26a0149304602210089c912fa687304f82634fe4e02f86ad721c3f9b8a6e7a2c06a7b0ba7a891ac18022100ff4f47c88c752a9e2e1ad8d450c7d5c06628159ea2e614f260dcf28c1c7333b101483045022100dd0c15876575df2e9973f3cd57c4f5e9e84d94277d2f4d82cebfb10fe2b25d62022060eb86654f538a5e5c55288de828bdd854e5d1434050a6b54d7d5402b59528ae01'; var createTestF = function(f) { var testF = function(raw, result) { @@ -240,6 +240,38 @@ describe('Script', function() { testP2SHSS(msss, false); }); }); + describe('#isMultiSigScriptSig', function() { + var isMultiSigScriptSig = new Script().isMultiSigScriptSig; + var testMSSS = createTestF(isMultiSigScriptSig); + it('should not identify pubkeyhash scriptsig', function() { + testMSSS(pkhss, false); + }); + it('should not identify pubkey scriptsig', function() { + testMSSS(pkss, false); + }); + it('should identify p2sh scriptsig', function() { + testMSSS(p2shss, false); + }); + it('should not identify multisig scriptsig', function() { + testMSSS(msss, true); + }); + }); + describe('#isPubkeyScriptSig', function() { + var isPubkeyScriptSig = new Script().isPubkeyScriptSig; + var testPKSS = createTestF(isPubkeyScriptSig); + it('should not identify pubkeyhash scriptsig', function() { + testPKSS(pkhss, false); + }); + it('should not identify pubkey scriptsig', function() { + testPKSS(pkss, true); + }); + it('should identify p2sh scriptsig', function() { + testPKSS(p2shss, false); + }); + it('should not identify multisig scriptsig', function() { + testPKSS(msss, false); + }); + }); }); }); From fd3f8ca017af4a2d6e94491aec60a3e50098edb9 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Mon, 28 Jul 2014 16:29:58 -0300 Subject: [PATCH 4/5] add getSignatures tests --- test/test.Script.js | 47 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/test/test.Script.js b/test/test.Script.js index 9ac3ec9b1..9bb3ef2cb 100644 --- a/test/test.Script.js +++ b/test/test.Script.js @@ -100,12 +100,8 @@ describe('Script', function() { if (datum.length < 2) throw new Error('Invalid test data'); var human = datum[0] + ' ' + datum[1]; it('should parse script from human readable ' + human, function() { - //console.log('********'); - //console.log(human); var script = Script.fromHumanReadable(human); - //console.log(script); var h2 = script.toHumanReadable(); - //console.log(h2); Script.fromHumanReadable(h2).toHumanReadable().should.equal(h2); }); }); @@ -202,9 +198,10 @@ describe('Script', function() { var pkss = '483045022100afe5d533f9925f987991328b7abbdb5a705113b72488ff6cd5502dcfc2ea1c8b02205cb8a0c686bf13b439b2f4b6e8f69c08ae00df0901a89a4a1313a936b044a43f01'; var msss = '004830450220582cd0d8c0f42113ef036af9b5b26d500447eb47dd737e129b0d1b9f870166fa022100e6794cc9158cb2347ff440cec6c017ab5043bb71f1cff55baf9df4888902e26a0149304602210089c912fa687304f82634fe4e02f86ad721c3f9b8a6e7a2c06a7b0ba7a891ac18022100ff4f47c88c752a9e2e1ad8d450c7d5c06628159ea2e614f260dcf28c1c7333b101483045022100dd0c15876575df2e9973f3cd57c4f5e9e84d94277d2f4d82cebfb10fe2b25d62022060eb86654f538a5e5c55288de828bdd854e5d1434050a6b54d7d5402b59528ae01'; var createTestF = function(f) { - var testF = function(raw, result) { + var testF = function(raw, expected) { var s = new Script(new Buffer(raw, 'hex')); - f.bind(s)().should.equal(result); + var actual = f.bind(s)(); + actual.should.equal(expected); }; return testF; }; @@ -272,6 +269,42 @@ describe('Script', function() { testPKSS(msss, false); }); }); - + describe('#countSignatures', function() { + var testCount = createTestF(new Script().countSignatures); + it('should not identify pubkeyhash scriptsig', function() { + testCount(pkhss, 1); + }); + it('should not identify pubkey scriptsig', function() { + testCount(pkss, 0); + }); + it('should identify p2sh scriptsig', function() { + testCount(p2shss, 2); + }); + it('should not identify multisig scriptsig', function() { + testCount(msss, 3); + }); + }); + describe('#getSignatures', function() { + var testSigs = function(raw, expected) { + var s = new Script(new Buffer(raw, 'hex')); + var actual = s.getSignatures(); + actual.length.should.equal(expected.length); + for (var i=0; i Date: Mon, 28 Jul 2014 17:18:08 -0300 Subject: [PATCH 5/5] add getHashType tests --- test/test.Script.js | 50 +++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/test/test.Script.js b/test/test.Script.js index 9bb3ef2cb..df03ae588 100644 --- a/test/test.Script.js +++ b/test/test.Script.js @@ -5,19 +5,15 @@ var bitcore = bitcore || require('../bitcore'); var should = chai.should(); -var ScriptModule = bitcore.Script; +var Script = bitcore.Script; var Address = bitcore.Address; var Opcode = bitcore.Opcode; +var Transaction = bitcore.Transaction; var networks = bitcore.networks; -var Script; var testdata = testdata || require('./testdata'); describe('Script', function() { - it('should initialze the main object', function() { - should.exist(ScriptModule); - }); it('should be able to create class', function() { - Script = ScriptModule; should.exist(Script); }); it('should be able to create instance', function() { @@ -151,7 +147,6 @@ describe('Script', function() { describe('#countMissingSignatures', function() { - Script = ScriptModule; it('should count missing signature in empty scripts', function() { var s = new Script(); s.countMissingSignatures().should.equal(1); @@ -201,7 +196,12 @@ describe('Script', function() { var testF = function(raw, expected) { var s = new Script(new Buffer(raw, 'hex')); var actual = f.bind(s)(); - actual.should.equal(expected); + if (expected !== null) { + should.exist(actual); + actual.should.equal(expected); + } else { + should.not.exist(actual); + } }; return testF; }; @@ -289,21 +289,41 @@ describe('Script', function() { var s = new Script(new Buffer(raw, 'hex')); var actual = s.getSignatures(); actual.length.should.equal(expected.length); - for (var i=0; i