From b7550fc8625f78383290d27d0a50a2826ade21a1 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Sat, 22 Mar 2014 15:03:22 -0700 Subject: [PATCH] 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'); --- BIP32.js | 18 ++++++++++++++++++ test/test.BIP32.js | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/BIP32.js b/BIP32.js index 64d602179..3e0d5e54f 100644 --- a/BIP32.js +++ b/BIP32.js @@ -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); diff --git a/test/test.BIP32.js b/test/test.BIP32.js index 132cd5347..95d7ae081 100644 --- a/test/test.BIP32.js +++ b/test/test.BIP32.js @@ -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);