diff --git a/lib/key.js b/lib/key.js index bec7295..a73bae2 100644 --- a/lib/key.js +++ b/lib/key.js @@ -1,8 +1,7 @@ var Address = require('../lib/address'); var Privkey = require('./privkey'); var Pubkey = require('./pubkey'); -var Random = require('./random'); -var Bn = require('./bn'); +var BN = require('./bn'); var point = require('./point'); var Key = function Key(obj) { @@ -19,11 +18,7 @@ Key.prototype.set = function(obj) { }; Key.prototype.fromRandom = function() { - do { - var privbuf = Random.getRandomBuffer(32); - this.privkey = new Privkey({bn: Bn(privbuf)}); - var condition = this.privkey.bn.lt(point.getN()); - } while (!condition); + this.privkey = Privkey().fromRandom(); this.privkey2pubkey(); return this; }; diff --git a/lib/privkey.js b/lib/privkey.js index d012fa8..d04972a 100644 --- a/lib/privkey.js +++ b/lib/privkey.js @@ -1,7 +1,8 @@ -var Bn = require('./bn'); +var BN = require('./bn'); var point = require('./point'); var constants = require('./constants'); var base58check = require('./base58check'); +var Random = require('./random'); var Privkey = function Privkey(obj) { if (!(this instanceof Privkey)) @@ -17,6 +18,16 @@ Privkey.prototype.set = function(obj) { return this; }; +Privkey.prototype.fromRandom = function() { + do { + var privbuf = Random.getRandomBuffer(32); + var bn = BN().fromBuffer(privbuf); + var condition = bn.lt(point.getN()); + } while (!condition); + this.bn = bn; + return this; +}; + Privkey.prototype.validate = function() { if (!this.bn.lt(point.getN())) throw new Error('Number must be less than N'); @@ -62,7 +73,7 @@ Privkey.prototype.fromWIF = function(str) { else throw new Error('Invalid networkstr'); - this.bn = Bn.fromBuffer(buf.slice(1, 32 + 1)); + this.bn = BN.fromBuffer(buf.slice(1, 32 + 1)); }; Privkey.prototype.toString = function() { diff --git a/test/test.privkey.js b/test/test.privkey.js index ed0a4ce..6cc0afa 100644 --- a/test/test.privkey.js +++ b/test/test.privkey.js @@ -1,6 +1,7 @@ var Privkey = require('../lib/privkey'); var base58check = require('../lib/base58check'); -var Bn = require('../lib/bn'); +var BN = require('../lib/bn'); +var Point = require('../lib/point'); var should = require('chai').should(); describe('Privkey', function() { @@ -17,24 +18,34 @@ describe('Privkey', function() { }); it('should create a mainnet private key', function() { - var privkey = new Privkey({bn: Bn.fromBuffer(buf), networkstr: 'mainnet', compressed: true}); + var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'mainnet', compressed: true}); privkey.toString().should.equal(encmainnet); }); it('should create an uncompressed testnet private key', function() { - var privkey = new Privkey({bn: Bn.fromBuffer(buf), networkstr: 'testnet', compressed: false}); + var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'testnet', compressed: false}); privkey.toString().should.equal(enctu); }); it('should create an uncompressed mainnet private key', function() { - var privkey = new Privkey({bn: Bn.fromBuffer(buf), networkstr: 'mainnet', compressed: false}); + var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'mainnet', compressed: false}); privkey.toString().should.equal(encmu); }); describe('#set', function() { it('should set bn', function() { - should.exist(Privkey().set({bn: Bn.fromBuffer(buf)}).bn); + should.exist(Privkey().set({bn: BN.fromBuffer(buf)}).bn); + }); + + }); + + describe('#fromRandom', function() { + + it('should set bn gt 0 and lt n', function() { + var privkey = Privkey().fromRandom(); + privkey.bn.gt(BN(0)).should.equal(true); + privkey.bn.lt(Point.getN()).should.equal(true); }); });