pull out hdwallet network specific values

This commit is contained in:
Wei Lu 2014-02-28 18:07:31 +08:00
parent 2e97828379
commit 65a13cbf41
2 changed files with 23 additions and 17 deletions

View file

@ -28,11 +28,6 @@ var HDWallet = module.exports = function(seed, network) {
HDWallet.HIGHEST_BIT = 0x80000000 HDWallet.HIGHEST_BIT = 0x80000000
HDWallet.LENGTH = 78 HDWallet.LENGTH = 78
HDWallet.VERSIONS = {
mainnet: [0x0488B21E, 0x0488ADE4],
testnet: [0x043587CF, 0x04358394]
}
function arrayEqual(a, b) { function arrayEqual(a, b) {
return !(a < b || a > b) return !(a < b || a > b)
} }
@ -78,16 +73,17 @@ HDWallet.fromBytes = function(input) {
// 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private;
// testnet: 0x043587CF public, 0x04358394 private) // testnet: 0x043587CF public, 0x04358394 private)
var versionBytes = input.slice(0, 4) var versionBytes = input.slice(0, 4)
, versionWord = util.bytesToWords(versionBytes)[0] var versionWord = util.bytesToWords(versionBytes)[0]
, type var type
Object.keys(HDWallet.VERSIONS).forEach(function(name) { for(var name in Network) {
HDWallet.VERSIONS[name].forEach(function(word, i) { var network = Network[name]
if (versionWord != word) return for(var t in network.hdVersions) {
type = i ? 'private' : 'public' if (versionWord != network.hdVersions[t]) continue
type = t
hd.network = name hd.network = name
}) }
}) }
if (!hd.network) { if (!hd.network) {
throw new Error(format('Could not find version %s', convert.bytesToHex(versionBytes))) 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 // 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for
// public keys, 0x00 + k for private keys) // 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.priv = new ECKey(input.slice(46, 78).concat([1]), true, hd.getKeyVersion())
hd.pub = hd.priv.getPub() hd.pub = hd.priv.getPub()
} else { } else {
@ -138,7 +134,8 @@ HDWallet.prototype.toBytes = function(priv) {
// Version // Version
// 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, // 4 byte: version bytes (mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public,
// 0x04358394 private) // 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) buffer = buffer.concat(vBytes)
assert.equal(buffer.length, 4) assert.equal(buffer.length, 4)

View file

@ -1,10 +1,19 @@
module.exports = { module.exports = {
mainnet: { mainnet: {
addressVersion: 0, addressVersion: 0,
p2shVersion: 5 p2shVersion: 5,
hdVersions: {
pub: 0x0488B21E,
priv: 0x0488ADE4
}
}, },
testnet: { testnet: {
addressVersion: 111, addressVersion: 111,
p2shVersion: 196 p2shVersion: 196,
hdVersions: {
pub: 0x043587CF,
priv: 0x04358394
}
} }
}; };