Skip to content

Commit dcf319d

Browse files
author
Tom Wang
committed
add: HD Wallet class and key type #root#
1 parent fb11993 commit dcf319d

4 files changed

Lines changed: 69 additions & 18 deletions

File tree

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
let MnemonicHD = require("./lib/utils/mnemonic.hd")
44
let SeedHD = require("./lib/utils/seed.hd")
55
let Base58HD = require("./lib/utils/base58.hd")
6-
let HdWallet = require("./lib/base/hd.wallet.class")
76

7+
let HdWallet = require("./lib/base/hd.wallet.class")
88
let AddressClass = require("./lib/base/address.class")
99

1010
let Address = {
@@ -20,7 +20,8 @@ module.exports = {
2020
"base58": "base58",
2121
"root": "root"
2222
},
23-
AddressClass: AddressClass,
23+
AddressClass,
24+
HdWallet,
2425
HD: (key, keyType = "mnemonic", pwd) => {
2526
let hd
2627
if (keyType == "mnemonic") {
@@ -30,7 +31,7 @@ module.exports = {
3031
} else if (keyType == "base58") {
3132
hd = new Base58HD(key)
3233
} else if (keyType == "root") {
33-
return HdWallet
34+
hd = new HdWallet(key)
3435
} else {
3536
throw "key type unsupported"
3637
}

lib/utils/base58.hd.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
let HdChainCode = require("../base/hd.wallet.class")
44
const bip32 = require('bip32');
5+
const bip39 = require('bip39');
56

67
/**
78
* BIP44 提出了5層的路徑建議 m / purpose' / coin_type' / account' / change / address_index
@@ -27,14 +28,14 @@ module.exports = class Base58HD extends HdChainCode {
2728
const seed = bip39.mnemonicToSeedSync(mnemonic, pwd)
2829
const root = bip32.fromSeed(seed)
2930
const base58 = root.toBase58()
30-
return pwd ? {root, base58, mnemonic, pwd} : {base58, mnemonic}
31+
return pwd ? {root, base58, mnemonic, pwd} : {root,base58, mnemonic}
3132
}
3233

3334
static mnemonicToBase58(mnemonic, pwd) {
3435
const seed = bip39.mnemonicToSeedSync(mnemonic, pwd)
3536
const root = bip32.fromSeed(seed)
3637
const base58 = root.toBase58()
37-
return pwd ? {root, base58, mnemonic, pwd} : {base58, mnemonic}
38+
return pwd ? {root, base58, mnemonic, pwd} : {root,base58, mnemonic}
3839
}
3940
};
4041

lib/utils/seed.hd.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3+
let HdWallet = require("../base/hd.wallet.class")
34
const bip32 = require('bip32');
45
const bip39 = require('bip39');
5-
let HdWallet = require("../base/hd.wallet.class")
6+
67

78
let seedBuff
89

@@ -21,15 +22,13 @@ module.exports = class SeedHD extends HdWallet {
2122
const mnemonic = bip39.generateMnemonic(strength)
2223
let seed = bip39.mnemonicToSeedSync(mnemonic, pwd)
2324
const root = bip32.fromSeed(seed)
24-
seed = seed.toString("hex")
25-
return pwd ? {root, seed, mnemonic, pwd} : {mnemonic, seed}
25+
return pwd ? {root, seed, mnemonic, pwd} : {root,mnemonic, seed}
2626
}
2727

2828
static mnemonicToSeed(mnemonic, pwd) {
2929
let seed = bip39.mnemonicToSeedSync(mnemonic, pwd)
3030
const root = bip32.fromSeed(seed)
31-
seed = seed.toString("hex")
32-
return pwd ? {root, seed, mnemonic, pwd} : {mnemonic, seed}
31+
return pwd ? {root, seed, mnemonic, pwd} : {root,mnemonic, seed}
3332
}
3433
};
3534

test/index.test.js

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1+
let hdAddress = require("../index")
2+
13
let logInfo = (hdCoin, hdIndex) => {
24
let addr = hdCoin.getAddress(hdIndex)
35
console.log(`${hdCoin.coin}:"${addr.address}"`)
46
}
57

6-
(async () => {
7-
let hdData = require("./data").hd
8-
let hdIndex = hdData.index
9-
const mnemonic = hdData.mnemonic
10-
let hdAddress = require("../index")
11-
12-
let hd = hdAddress.HD(mnemonic)
8+
let getAddress = (hd) => {
9+
let hdIndex = 6677
1310
logInfo(hd.BTC, hdIndex)
1411
logInfo(hd.BTC_TEST, hdIndex)
1512
logInfo(hd.BCH, hdIndex)
@@ -18,4 +15,57 @@ let logInfo = (hdCoin, hdIndex) => {
1815
logInfo(hd.LTC_TEST, hdIndex)
1916
logInfo(hd.ETH, hdIndex)
2017
logInfo(hd.TRX, hdIndex)
21-
})()
18+
}
19+
const mnemonic = "star star star star star star"
20+
const pwd = "star"
21+
const seed = "03d0be996b63e90c7625dd3f5319c3bc11669d3d35ae5dc345595e5e59be74084f"
22+
const base58 = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi"
23+
24+
describe("address init", () => {
25+
26+
it(" getRandomMnemonic", () => {
27+
const mnemonic = hdAddress.mnemonic.getRandomMnemonic()
28+
let hd = hdAddress.HD(mnemonic)
29+
getAddress(hd)
30+
})
31+
32+
it(" from mnemonic str", () => {
33+
let hd = hdAddress.HD(mnemonic, hdAddress.keyType.mnemonic, pwd)
34+
getAddress(hd)
35+
})
36+
37+
it(" getRandomSeed", () => {
38+
const {seed} = hdAddress.seed.getRandomSeed()
39+
let hd = hdAddress.HD(seed, hdAddress.keyType.seed)
40+
getAddress(hd)
41+
})
42+
43+
it(" from seed string", () => {
44+
// Seed should be at least 128 bits and most 512 bits
45+
const seedBuf = Buffer.from(seed, "hex")
46+
let hd = hdAddress.HD(seedBuf, hdAddress.keyType.seed)
47+
getAddress(hd)
48+
})
49+
50+
it(" getRandomBase58", () => {
51+
const {base58} = hdAddress.base58.getRandomBase58()
52+
let hd = hdAddress.HD(base58, hdAddress.keyType.base58)
53+
getAddress(hd)
54+
})
55+
56+
57+
it("from base58 str", () => {
58+
let hd = hdAddress.HD(base58, hdAddress.keyType.base58)
59+
getAddress(hd)
60+
})
61+
62+
it(" getHDWallet", () => {
63+
const {root} = hdAddress.seed.mnemonicToSeed(mnemonic)
64+
let hd = hdAddress.HD(root, hdAddress.keyType.root)
65+
let {path, pub} = hd.wallet.getCoinKeyPair("BTC", 0, 0, 0)
66+
console.log(path)
67+
let {address} = hd.TRX.getAddress(1)
68+
console.log(address)
69+
})
70+
71+
})

0 commit comments

Comments
 (0)