From 65a13cbf4147b9ba6b94ccc0ea9fdce81b13e7d3 Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Fri, 28 Feb 2014 18:07:31 +0800 Subject: [PATCH] pull out hdwallet network specific values --- src/hdwallet.js | 29 +++++++++++++---------------- src/network.js | 13 +++++++++++-- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/hdwallet.js b/src/hdwallet.js index 98cf7b8..8a65b1a 100644 --- a/src/hdwallet.js +++ b/src/hdwallet.js @@ -28,11 +28,6 @@ var HDWallet = module.exports = function(seed, network) { HDWallet.HIGHEST_BIT = 0x80000000 HDWallet.LENGTH = 78 -HDWallet.VERSIONS = { - mainnet: [0x0488B21E, 0x0488ADE4], - testnet: [0x043587CF, 0x04358394] -} - function arrayEqual(a, b) { return !(a < b || a > b) } @@ -78,16 +73,17 @@ HDWallet.fromBytes = function(input) { // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; // testnet: 0x043587CF public, 0x04358394 private) var versionBytes = input.slice(0, 4) - , versionWord = util.bytesToWords(versionBytes)[0] - , type - - Object.keys(HDWallet.VERSIONS).forEach(function(name) { - HDWallet.VERSIONS[name].forEach(function(word, i) { - if (versionWord != word) return - type = i ? 'private' : 'public' + var versionWord = util.bytesToWords(versionBytes)[0] + var type + + for(var name in Network) { + var network = Network[name] + for(var t in network.hdVersions) { + if (versionWord != network.hdVersions[t]) continue + type = t hd.network = name - }) - }) + } + } if (!hd.network) { throw new Error(format('Could not find version %s', convert.bytesToHex(versionBytes))) @@ -110,7 +106,7 @@ HDWallet.fromBytes = function(input) { // 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for // public keys, 0x00 + k for private keys) - if (type == 'private') { + if (type == 'priv') { hd.priv = new ECKey(input.slice(46, 78).concat([1]), true, hd.getKeyVersion()) hd.pub = hd.priv.getPub() } else { @@ -138,7 +134,8 @@ HDWallet.prototype.toBytes = function(priv) { // Version // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, // 0x04358394 private) - var vBytes = util.wordsToBytes([HDWallet.VERSIONS[this.network][priv ? 1 : 0]]) + var version = Network[this.network].hdVersions[priv ? 'priv' : 'pub'] + var vBytes = util.wordsToBytes([version]) buffer = buffer.concat(vBytes) assert.equal(buffer.length, 4) diff --git a/src/network.js b/src/network.js index e02fd01..78e7617 100644 --- a/src/network.js +++ b/src/network.js @@ -1,10 +1,19 @@ module.exports = { mainnet: { addressVersion: 0, - p2shVersion: 5 + p2shVersion: 5, + hdVersions: { + pub: 0x0488B21E, + priv: 0x0488ADE4 + } }, testnet: { addressVersion: 111, - p2shVersion: 196 + p2shVersion: 196, + hdVersions: { + pub: 0x043587CF, + priv: 0x04358394 + } } }; +