From 19584e1f267bde36b423661e58e7ea4ed89ee841 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Jul 2014 01:40:13 -0300 Subject: [PATCH 1/4] add check in constructor --- lib/SIN.js | 3 +++ test/test.SIN.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/SIN.js b/lib/SIN.js index a14cfe5de0d..5143c93d66b 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -9,6 +9,9 @@ function SIN(type, payload) { SIN.super_.call(this, type, payload); return; } + if ( !Buffer.isBuffer(payload) || payload.length != 20) + throw new Error('Payload must be 20 bytes'); + this.data = new Buffer(1 + 1 + payload.length); this.converters = this.encodings['binary'].converters; this._encoding = this.encodings['binary']._encoding; diff --git a/test/test.SIN.js b/test/test.SIN.js index a1c00bea3e7..23d117abf42 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -41,6 +41,13 @@ describe('SIN', function() { var sin = new SIN(SIN.SIN_EPHEM, myhash); should.exist(sin); }); + it('should fail with param version, string', function() { + var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0'; + ( function (){ + var sin = new SIN(SIN.SIN_EPHEM, hash); + }).should.throw(); + }); + }); describe('#fromPubKey', function() { it('should fail to create a new SIN not using a pub key', function() { From 71f992138bc9f88316887150a1558c96e45006a2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Jul 2014 01:40:56 -0300 Subject: [PATCH 2/4] add check in constructor of Address --- lib/Address.js | 2 +- test/test.Address.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Address.js b/lib/Address.js index 0eb383a6663..6dafa0cc44f 100644 --- a/lib/Address.js +++ b/lib/Address.js @@ -38,7 +38,7 @@ var Script = require('./Script'); var util = require('util'); function Address(version, hash) { - if (hash && hash.length && hash.length != 20) + if (hash && hash.length && (!Buffer.isBuffer(hash) || hash.length != 20)) throw new Error('Hash must be 20 bytes'); Address.super_.call(this, version, hash); } diff --git a/test/test.Address.js b/test/test.Address.js index e130b4d9253..5b47929e1b9 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -107,6 +107,21 @@ describe('Address', function() { }); }); + describe('constructor, 2 params', function() { + it('should make an address from a version, hash', function() { + var hash = new Buffer('1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex'); + var addr = new Address(0, hash); + addr.toString().should.equal('13SE7uKmnQwGA8X1A8WcZnX2ceQRDEzsAd'); + }); + it('should fail with param version, string', function() { + var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0'; + ( function (){ + var addr = new Address(0, hash); + }).should.throw(); + }); + }); + + describe('#fromPubKey', function() { it('should make pubkeyhash address from an uncompressed public key', function() { var pubkey = new Buffer('04fa05ce8b25010cb6e17a30e0b66668bf083c40687547748ec330ee77adf53a42abd3d26148cbacfcf79c907ddefeb2c37f8bebc0a695ba79d634449d871de218', 'hex'); From 3d828df063d3bd1200888ec7030847dc38a3d10d Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Jul 2014 01:52:17 -0300 Subject: [PATCH 3/4] add test to SIN --- test/test.SIN.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/test.SIN.js b/test/test.SIN.js index 23d117abf42..eee75e0cd58 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -37,7 +37,7 @@ describe('SIN', function() { describe('#SIN', function() { it('should be able to create a new SIN with a version byte', function() { - var myhash = bitcore.util.sha256ripe160('test'); + var myhash = new Buffer('1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex'); var sin = new SIN(SIN.SIN_EPHEM, myhash); should.exist(sin); }); @@ -48,6 +48,13 @@ describe('SIN', function() { }).should.throw(); }); + it('should fail with wrong sized hash', function() { + var myhash = new Buffer('111ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex'); + ( function (){ + var sin = new SIN(SIN.SIN_EPHEM, myhash); + }).should.throw(); + }); + }); describe('#fromPubKey', function() { it('should fail to create a new SIN not using a pub key', function() { From 2f973908494c01953464d4404581c06b76260fcb Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 24 Jul 2014 16:47:50 -0300 Subject: [PATCH 4/4] formatted with js-beautify --- lib/SIN.js | 2 +- test/test.Address.js | 79 +++++++++++++++++++++++--------------------- test/test.SIN.js | 21 ++++++------ 3 files changed, 53 insertions(+), 49 deletions(-) diff --git a/lib/SIN.js b/lib/SIN.js index 5143c93d66b..d8e143b278e 100644 --- a/lib/SIN.js +++ b/lib/SIN.js @@ -9,7 +9,7 @@ function SIN(type, payload) { SIN.super_.call(this, type, payload); return; } - if ( !Buffer.isBuffer(payload) || payload.length != 20) + if (!Buffer.isBuffer(payload) || payload.length != 20) throw new Error('Payload must be 20 bytes'); this.data = new Buffer(1 + 1 + payload.length); diff --git a/test/test.Address.js b/test/test.Address.js index 5b47929e1b9..ab7e4c7e1a9 100644 --- a/test/test.Address.js +++ b/test/test.Address.js @@ -29,18 +29,18 @@ describe('Address', function() { ['1Q1pE5vPGEEMqRcVRMbtBK842Y6Pzo6nK9', true], ['1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb', false], //bad checksum ... thanks @wtogami ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', true], - ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum - ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW620', false], // bad checksum - ['1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // data changed, original checksum. - ['1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // invalid chars - ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j', false], // checksums don't match. - ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!', false], // bad char (!) + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW600', false], // bad checksum + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW620', false], // bad checksum + ['1ANNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // data changed, original checksum. + ['1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i', false], // invalid chars + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62j', false], // checksums don't match. + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62!', false], // bad char (!) ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62iz', false], // too long Bitcoin address - ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz', false],// too long Bitcoin address - ['2cFupjhnEsSn59qHXstmK2ffpLv2', false], // valid base58 invalid data - ['dB7cwYdcPSgiyAwKWL3JwCVwSk6epU2txw', false], // valid base58, valid length, invalid network - ['2MnmgiRH4eGLyLc9eAqStzk7dFgBjFtUCtu', false], // valid base58, valid length, invalid network - ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true], // valid base58, valid length, valid network + ['1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62izz', false], // too long Bitcoin address + ['2cFupjhnEsSn59qHXstmK2ffpLv2', false], // valid base58 invalid data + ['dB7cwYdcPSgiyAwKWL3JwCVwSk6epU2txw', false], // valid base58, valid length, invalid network + ['2MnmgiRH4eGLyLc9eAqStzk7dFgBjFtUCtu', false], // valid base58, valid length, invalid network + ['32QBdjycLwbDTuGafUwaU5p5GxzSLPYoF6', true], // valid base58, valid length, valid network ]; data.forEach(function(datum) { var address = datum[0]; @@ -102,7 +102,9 @@ describe('Address', function() { var key = new bitcore.Key(); key.private = privkey; key.regenerateSync(); - var f = function() {new bitcore.Address(0, key.public);}; + var f = function() { + new bitcore.Address(0, key.public); + }; expect(f).to.throw(Error); }); }); @@ -115,12 +117,12 @@ describe('Address', function() { }); it('should fail with param version, string', function() { var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0'; - ( function (){ + (function() { var addr = new Address(0, hash); }).should.throw(); }); }); - + describe('#fromPubKey', function() { it('should make pubkeyhash address from an uncompressed public key', function() { @@ -149,7 +151,9 @@ describe('Address', function() { var pubkey3 = new Buffer('032c0d2e394541e2efdc7ac3500e16e7e69df541f38670402e95aa477202fa06bb', 'hex'); var sortedPubKeys = [pubkey3, pubkey2, pubkey1]; var mReq = 2; - var script = bitcore.Script.createMultisig(mReq, sortedPubKeys, {noSorting: true}); + var script = bitcore.Script.createMultisig(mReq, sortedPubKeys, { + noSorting: true + }); var hash = bitcore.util.sha256ripe160(script.getBuffer()); var version = bitcore.networks['livenet'].P2SHVersion; var addr = new Address(version, hash); @@ -175,7 +179,7 @@ describe('Address', function() { var addrB = Address.fromScript(scriptHex); var addr2B = Address.fromPubKeys(mReq, pubKeys); addrB.toString().should.equal(addr2B.toString()); - + }); it('it should make this hand-crafted address', function() { @@ -186,7 +190,7 @@ describe('Address', function() { var mReq = 2; var script = bitcore.Script.createMultisig(mReq, pubKeys); var addr = Address.fromScript(script); - + var hash = bitcore.util.sha256ripe160(script.getBuffer()); var version = bitcore.networks['livenet'].P2SHVersion; var addr2 = new Address(version, hash); @@ -198,16 +202,16 @@ describe('Address', function() { describe('#getScriptPubKey', function() { var data = [ ['76a91423b7530a00dd7951e11791c529389421c0b8d83b88ac', 'mimoZNLcP2rrMRgdeX5PSnR7AjCqQveZZ4'], - ['a9147049be48e74a660157da3ed64569981592f7fa0587','2N3Ux1YTnt1ixofYvJfaabqZSj2MBF3jsmv'], + ['a9147049be48e74a660157da3ed64569981592f7fa0587', '2N3Ux1YTnt1ixofYvJfaabqZSj2MBF3jsmv'], ['76a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac', 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE'], ['76a914b00127584485a7cff0949ef0f6bc5575f06ce00d88ac', 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'] ]; it('validate scriptPubKey for a given address', function() { - for(var i in data) { - var d = data[i]; - var b = new Address(d[1]).getScriptPubKey().getBuffer(); - b.toString('hex').should.equal(d[0]); + for (var i in data) { + var d = data[i]; + var b = new Address(d[1]).getScriptPubKey().getBuffer(); + b.toString('hex').should.equal(d[0]); } }); }); @@ -217,29 +221,28 @@ describe('Address', function() { // All examples checked againt bitcoind decodescript var cases = [ ['76a91423b7530a00dd7951e11791c529389421c0b8d83b88ac', 'mimoZNLcP2rrMRgdeX5PSnR7AjCqQveZZ4'], - ['a9147049be48e74a660157da3ed64569981592f7fa0587','2N3Ux1YTnt1ixofYvJfaabqZSj2MBF3jsmv'], + ['a9147049be48e74a660157da3ed64569981592f7fa0587', '2N3Ux1YTnt1ixofYvJfaabqZSj2MBF3jsmv'], ['76a914774e603bafb717bd3f070e68bbcccfd907c77d1388ac', 'mrPnbY1yKDBsdgbHbS7kJ8GVm8F66hWHLE'], ['76a914b00127584485a7cff0949ef0f6bc5575f06ce00d88ac', 'mwZabyZXg8JzUtFX1pkGygsMJjnuqiNhgd'], ['532103bf025eb410407aec5a67c975ce222e363bb88c69bb1acce45d20d85602df2ec52103d76dd6d99127f4b733e772f0c0a09c573ac7e4d69b8bf50272292da2e093de2c2103dd9acd8dd1816c825d6b0739339c171ae2cb10efb53699680537865b07086e9b2102371cabbaf466c3a536034b4bda64ad515807bffd87488f44f93c2373d4d189c9210264cd444358f8d57f8637a7309f9736806f4883aebc4fe7da4bad1e4b37f2d12c55ae', [ - "n4JAZc4cJimQbky5wxZUEDeAFZtGaZrjWK", - "msge5muNmBSRDn5nsaRcHCU6dg2zimA8wQ", - "mvz9MjocpyXdgXqRcZYazsdE8iThdvjdhk", - "miQGZ2gybQe7UvUQDBYsgcctUteij5pTpm", - "mu9kmhGrzREKsWaXUEUrsRLLMG4UMPy1LF" + "n4JAZc4cJimQbky5wxZUEDeAFZtGaZrjWK", + "msge5muNmBSRDn5nsaRcHCU6dg2zimA8wQ", + "mvz9MjocpyXdgXqRcZYazsdE8iThdvjdhk", + "miQGZ2gybQe7UvUQDBYsgcctUteij5pTpm", + "mu9kmhGrzREKsWaXUEUrsRLLMG4UMPy1LF" ]] ]; - for(var i in cases){ - var c=cases[i]; - it('it should generate the right address', function(){ + for (var i in cases) { + var c = cases[i]; + it('it should generate the right address', function() { if (typeof c[1] === 'string') { - (new Address.fromScriptPubKey(c[0],'testnet')).toString().should.equal(c[1]); - var s = new bitcore.Script(new Buffer(c[0],'hex')); - (new Address.fromScriptPubKey(s,'testnet')).toString().should.equal(c[1]); - } - else { - var as=new Address.fromScriptPubKey(c[0],'testnet'); - for(var j in as){ + (new Address.fromScriptPubKey(c[0], 'testnet')).toString().should.equal(c[1]); + var s = new bitcore.Script(new Buffer(c[0], 'hex')); + (new Address.fromScriptPubKey(s, 'testnet')).toString().should.equal(c[1]); + } else { + var as = new Address.fromScriptPubKey(c[0], 'testnet'); + for (var j in as) { as[j].toString().should.equal(c[1][j]); } } diff --git a/test/test.SIN.js b/test/test.SIN.js index eee75e0cd58..cdb7672e20a 100644 --- a/test/test.SIN.js +++ b/test/test.SIN.js @@ -43,25 +43,29 @@ describe('SIN', function() { }); it('should fail with param version, string', function() { var hash = '1ab59a0fd1d5fc446d38746ee033c8af57ed6bc0'; - ( function (){ + (function() { var sin = new SIN(SIN.SIN_EPHEM, hash); }).should.throw(); }); - + it('should fail with wrong sized hash', function() { var myhash = new Buffer('111ab59a0fd1d5fc446d38746ee033c8af57ed6bc0', 'hex'); - ( function (){ + (function() { var sin = new SIN(SIN.SIN_EPHEM, myhash); }).should.throw(); }); - + }); describe('#fromPubKey', function() { it('should fail to create a new SIN not using a pub key', function() { - (function() { SIN.fromPubKey('1234')}).should.throw(); + (function() { + SIN.fromPubKey('1234') + }).should.throw(); }); it('should fail to create a new SIN not using a pub key case 2', function() { - (function() { SIN.fromPubKey('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27')}).should.throw(); + (function() { + SIN.fromPubKey('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27') + }).should.throw(); }); it('should be able to create a new SIN using a pub key', function() { var pubkey1 = new Buffer('03e0973263b4e0d5f5f56d25d430e777ab3838ff644db972c0bf32c31da5686c27', 'hex'); @@ -69,9 +73,6 @@ describe('SIN', function() { should.exist(sin); sin.toString().should.equal('FrCfKjSFN1Ubp3x6AD6au8M5LTaNAEN8b'); }); - + }); }); - - -