From 150a9434473160139654c75fa1d2ca5f61aa223d Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Wed, 19 Mar 2014 19:00:45 -0300 Subject: [PATCH] fix private key validation and base58 invalid tests --- PrivateKey.js | 1 + WalletKey.js | 1 + test/test.misc.js | 22 +++++++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/PrivateKey.js b/PrivateKey.js index 9e2eac612..32adb6729 100644 --- a/PrivateKey.js +++ b/PrivateKey.js @@ -19,6 +19,7 @@ PrivateKey.prototype.validate = function() { if (this.data.length < 32 || (this.data.length > 1+32 && !this.compressed()) || (this.data.length==1+32+1 && this.data[1+32+1-1]!=1) || this.data.length>1+32+1) throw new Error('invalid data length'); }); + if (typeof this.network() === 'undefined') throw new Error('invalid network'); }; // get or set the payload data (as a Buffer object) diff --git a/WalletKey.js b/WalletKey.js index 2c9c6e31c..3ff6214eb 100644 --- a/WalletKey.js +++ b/WalletKey.js @@ -42,6 +42,7 @@ WalletKey.prototype.fromObj = function(obj) { this.privKey.compressed = typeof obj.compressed === 'undefined'? true: obj.compressed; } else { var priv = new PrivateKey(obj.priv); + priv.validate(); this.privKey.private = new Buffer(priv.payload()); this.privKey.compressed = priv.compressed(); } diff --git a/test/test.misc.js b/test/test.misc.js index 0a4b36830..1172b03e3 100644 --- a/test/test.misc.js +++ b/test/test.misc.js @@ -131,11 +131,31 @@ describe('Miscelaneous stuff', function() { }); testdata.dataBase58KeysInvalid.forEach(function(datum) { var b58 = datum[0]; - it('shouldnt be able to create Address nor WalletKey with ' + b58, function() { + it('shouldnt be able to create Address with ' + b58, function() { var a = new Address(b58); var invalidAddress = (!a.isValid()); invalidAddress.should.equal(true); }); + it('shouldnt be able to create WalletKey with ' + b58, function() { + var kl = new WalletKey({ + network: networks.livenet + }); + var kt = new WalletKey({ + network: networks.livenet + }); + var createLivenet = function() { + kl.fromObj({ + priv: b58 + }); + }; + var createTestnet = function() { + kt.fromObj({ + priv: b58 + }); + }; + createLivenet.should.throw(); + createTestnet.should.throw(); + }); }); });