support string input in constructor

This commit is contained in:
Ryan X. Charles 2014-09-17 15:02:11 -07:00
parent c41419b6ef
commit 0641184e84
2 changed files with 28 additions and 24 deletions

View File

@ -8,9 +8,13 @@ var Random = require('./random');
var bn = require('./bn');
var constants = require('./constants');
var BIP32 = function BIP32() {
var BIP32 = function BIP32(obj) {
if (!(this instanceof BIP32))
return new BIP32();
return new BIP32(obj);
if (typeof obj === 'string') {
var str = obj;
this.fromString(str);
}
}
BIP32.prototype.fromRandom = function(networkstr) {

View File

@ -32,14 +32,14 @@ describe('BIP32', function() {
var vector2_m02147483647h12147483646h2_public = 'xpub6FnCn6nSzZAw5Tw7cgR9bi15UV96gLZhjDstkXXxvCLsUXBGXPdSnLFbdpq8p9HmGsApME5hQTZ3emM2rnY5agb9rXpVGyy3bdW6EEgAtqt';
var vector2_m02147483647h12147483646h2_private = 'xprvA2nrNbFZABcdryreWet9Ea4LvTJcGsqrMzxHx98MMrotbir7yrKCEXw7nadnHM8Dq38EGfSh6dqA9QWTyefMLEcBYJUuekgW4BYPJcr9E7j';
it('should initialize the class', function() {
should.exist(BIP32);
});
it('should create a bip32', function() {
var bip32 = new BIP32();
it('should make a new a bip32', function() {
var bip32;
bip32 = new BIP32();
should.exist(bip32);
bip32 = BIP32();
should.exist(bip32);
new BIP32(vector1_m_private).toString().should.equal(vector1_m_private);
BIP32(vector1_m_private).toString().should.equal(vector1_m_private);
});
it('should initialize test vector 1 from the extended public key', function() {
@ -266,6 +266,21 @@ describe('BIP32', function() {
child2.extendedPublicKeyString().should.equal(vector2_m02147483647h12147483646h2_public);
});
describe('testnet', function() {
it('should initialize a new BIP32 correctly from a random BIP32', function() {
var b1 = new BIP32();
b1.fromRandom('testnet');
var b2 = new BIP32().fromString(b1.extendedPublicKeyString());
b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString());
});
it('should generate valid ext pub key for testnet', function() {
var b = new BIP32();
b.fromRandom('testnet');
b.extendedPublicKeyString().substring(0,4).should.equal('tpub');
});
});
describe('#seed', function() {
it('should initialize a new BIP32 correctly from test vector 1 seed', function() {
@ -285,21 +300,6 @@ describe('BIP32', function() {
});
});
describe('testnet', function() {
it('should initialize a new BIP32 correctly from a random BIP32', function() {
var b1 = new BIP32();
b1.fromRandom('testnet');
var b2 = new BIP32().fromString(b1.extendedPublicKeyString());
b2.extendedPublicKeyString().should.equal(b1.extendedPublicKeyString());
});
it('should generate valid ext pub key for testnet', function() {
var b = new BIP32();
b.fromRandom('testnet');
b.extendedPublicKeyString().substring(0,4).should.equal('tpub');
});
});
describe('#toString', function() {
var bip32 = new BIP32();
bip32.fromRandom('mainnet');