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.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
var versionWord = util.bytesToWords(versionBytes)[0]
var type
Object.keys(HDWallet.VERSIONS).forEach(function(name) {
HDWallet.VERSIONS[name].forEach(function(word, i) {
if (versionWord != word) return
type = i ? 'private' : 'public'
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)

View file

@ -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
}
}
};