convenience: new Privkey(bn)

This commit is contained in:
Ryan X. Charles 2014-09-01 21:13:44 -07:00
parent ef3a89f254
commit 6f56c8d1fc
2 changed files with 12 additions and 2 deletions

View File

@ -4,11 +4,15 @@ var constants = require('./constants');
var base58check = require('./base58check');
var Random = require('./random');
var Privkey = function Privkey(obj) {
var Privkey = function Privkey(bn) {
if (!(this instanceof Privkey))
return new Privkey(obj);
if (obj)
if (bn instanceof BN)
this.bn = bn;
else if (bn) {
var obj = bn;
this.set(obj);
}
};
Privkey.prototype.set = function(obj) {

View File

@ -17,6 +17,12 @@ describe('Privkey', function() {
should.exist(privkey);
});
it('should create a 0 private key with this convenience method', function() {
var bn = BN(0);
var privkey = new Privkey(bn);
privkey.bn.toString().should.equal(bn.toString());
});
it('should create a mainnet private key', function() {
var privkey = new Privkey({bn: BN.fromBuffer(buf), networkstr: 'mainnet', compressed: true});
privkey.toString().should.equal(encmainnet);