Browse Source

Privkey().fromRandom()

patch-2
Ryan X. Charles 11 years ago
parent
commit
c39acbcca3
  1. 9
      lib/key.js
  2. 15
      lib/privkey.js
  3. 21
      test/test.privkey.js

9
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;
};

15
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() {

21
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);
});
});

Loading…
Cancel
Save