Browse Source

pull out hdwallet network specific values

hk-custom-address
Wei Lu 11 years ago
parent
commit
65a13cbf41
  1. 29
      src/hdwallet.js
  2. 13
      src/network.js

29
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)

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

Loading…
Cancel
Save