add convenience constructor for making new bip32s

Added the ability to create a new master bip32 with new private key and chain code. The way this works is like this:

var bip32 = new BIP32('mainnet');

or:

var bip32 = new BIP32('testnet');
This commit is contained in:
Ryan X. Charles 2014-03-22 15:03:22 -07:00
parent 0677ae46f8
commit b7550fc862
2 changed files with 28 additions and 0 deletions

View File

@ -21,6 +21,24 @@ var secp256k1_n = new bignum("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBF
var secp256k1_G = new bignum("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", 16); //x coordinate
var BIP32 = function(bytes) {
if (bytes == 'mainnet' || bytes == 'livenet')
this.version = BITCOIN_MAINNET_PRIVATE;
else if (bytes == 'testnet')
this.version = BITCOIN_TESTNET_PRIVATE;
if (bytes == 'mainnet' || bytes == 'livenet' || bytes == 'testnet') {
this.depth = 0x00;
this.parent_fingerprint = new Buffer([0, 0, 0, 0]);
this.child_index = new Buffer([0, 0, 0, 0]);
this.chain_code = Key.generateSync().private;
this.eckey = Key.generateSync();
this.has_private_key = true;
this.pubKeyHash = coinUtil.sha256ripe160(this.eckey.public);
this.build_extended_public_key();
this.build_extended_private_key();
return;
}
// decode base58
if (typeof bytes === "string") {
var decoded = base58.decode(bytes);

View File

@ -40,6 +40,16 @@ describe('BIP32', function() {
should.exist(BIP32);
});
it('should create a mainnet bip32', function() {
var bip32 = new BIP32('mainnet');
should.exist(bip32);
});
it('should create a testnet bip32', function() {
var bip32 = new BIP32('testnet');
should.exist(bip32);
});
it('should initialize test vector 1 from the extended public key', function() {
var bip32 = new BIP32(vector1_m_public);
should.exist(bip32);