Skip to content

Commit

Permalink
fix private key validation and base58 invalid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Mar 19, 2014
1 parent 3cd4e31 commit 150a943
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions PrivateKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions WalletKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
22 changes: 21 additions & 1 deletion test/test.misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

});

0 comments on commit 150a943

Please sign in to comment.