Privkey().fromRandom()
This commit is contained in:
parent
2ef5e2f003
commit
c39acbcca3
@ -1,8 +1,7 @@
|
|||||||
var Address = require('../lib/address');
|
var Address = require('../lib/address');
|
||||||
var Privkey = require('./privkey');
|
var Privkey = require('./privkey');
|
||||||
var Pubkey = require('./pubkey');
|
var Pubkey = require('./pubkey');
|
||||||
var Random = require('./random');
|
var BN = require('./bn');
|
||||||
var Bn = require('./bn');
|
|
||||||
var point = require('./point');
|
var point = require('./point');
|
||||||
|
|
||||||
var Key = function Key(obj) {
|
var Key = function Key(obj) {
|
||||||
@ -19,11 +18,7 @@ Key.prototype.set = function(obj) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Key.prototype.fromRandom = function() {
|
Key.prototype.fromRandom = function() {
|
||||||
do {
|
this.privkey = Privkey().fromRandom();
|
||||||
var privbuf = Random.getRandomBuffer(32);
|
|
||||||
this.privkey = new Privkey({bn: Bn(privbuf)});
|
|
||||||
var condition = this.privkey.bn.lt(point.getN());
|
|
||||||
} while (!condition);
|
|
||||||
this.privkey2pubkey();
|
this.privkey2pubkey();
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,7 +1,8 @@
|
|||||||
var Bn = require('./bn');
|
var BN = require('./bn');
|
||||||
var point = require('./point');
|
var point = require('./point');
|
||||||
var constants = require('./constants');
|
var constants = require('./constants');
|
||||||
var base58check = require('./base58check');
|
var base58check = require('./base58check');
|
||||||
|
var Random = require('./random');
|
||||||
|
|
||||||
var Privkey = function Privkey(obj) {
|
var Privkey = function Privkey(obj) {
|
||||||
if (!(this instanceof Privkey))
|
if (!(this instanceof Privkey))
|
||||||
@ -17,6 +18,16 @@ Privkey.prototype.set = function(obj) {
|
|||||||
return this;
|
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() {
|
Privkey.prototype.validate = function() {
|
||||||
if (!this.bn.lt(point.getN()))
|
if (!this.bn.lt(point.getN()))
|
||||||
throw new Error('Number must be less than N');
|
throw new Error('Number must be less than N');
|
||||||
@ -62,7 +73,7 @@ Privkey.prototype.fromWIF = function(str) {
|
|||||||
else
|
else
|
||||||
throw new Error('Invalid networkstr');
|
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() {
|
Privkey.prototype.toString = function() {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
var Privkey = require('../lib/privkey');
|
var Privkey = require('../lib/privkey');
|
||||||
var base58check = require('../lib/base58check');
|
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();
|
var should = require('chai').should();
|
||||||
|
|
||||||
describe('Privkey', function() {
|
describe('Privkey', function() {
|
||||||
@ -17,24 +18,34 @@ describe('Privkey', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should create a mainnet private key', 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);
|
privkey.toString().should.equal(encmainnet);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create an uncompressed testnet private key', function() {
|
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);
|
privkey.toString().should.equal(enctu);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should create an uncompressed mainnet private key', function() {
|
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);
|
privkey.toString().should.equal(encmu);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#set', function() {
|
describe('#set', function() {
|
||||||
|
|
||||||
it('should set bn', 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…
Reference in New Issue
Block a user