-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhd_wallet.js
137 lines (122 loc) · 4.13 KB
/
hd_wallet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const bitcoin = require('bitcoinjs-lib')
const bip39 = require('bip39')
const {
P2PKH, // the standard(most used one) should use P2PKH purpose
P2WPKH, // we need to use P2WPKH purpose for BIP84 (Native SegWit bech32 P2WPKH) address generation
NestedP2WPKH,
P2TR
} = require("./common/hd_address_util").bipPurpose
//https://github.com/iancoleman/bip39/blob/c4f0c2908faab1452937e50a7d3a400fed42a0a8/src/js/bitcoinjs-extensions.js
class HDWallet {
constructor({
network,
mnemonicStrength = 128
}) {
this.mnemonicStrength = mnemonicStrength
this.mnemonic = null
this.seedPassphrase = null
this.rootNode = null
this.network = network
}
generateRootNode({mnemonic, seedPassphrase} = {}) {
if (! mnemonic) {
this.mnemonic = bip39.generateMnemonic(this.mnemonicStrength)
} else {
this.mnemonic = mnemonic
}
this.seedPassphrase = seedPassphrase
const seed = bip39.mnemonicToSeedSync(this.mnemonic, this.seedPassphrase)
this.rootNode = bitcoin.bip32.fromSeed(seed, this.network)
}
getRootNode() {
return this.rootNode
}
getSecret() {
return {
mnemonic: this.mnemonic,
passphrase: this.seedPassphrase
}
}
restore({
accountsNumber = 2,
receivedAddressesNumber = 1,
changeAddressesNumber = 0,
mnemonic,
seedPassphrase,
purpose = P2WPKH
}) {
if (! bip39.validateMnemonic(mnemonic)) {
throw "Not valid mnemonic"
}
this.mnemonic = mnemonic
this.seedPassphrase = seedPassphrase
const seed = bip39.mnemonicToSeedSync(this.mnemonic, this.seedPassphrase)
this.rootNode = bitcoin.bip32.fromSeed(seed)
let deriveParams = {
purpose,
accountIndex: 0,
addressIndex: 0,
isTestnet: false,
isChange: false
}
let accountsList = {
receive: [],
change: [],
}
for (let account = 0; account < accountsNumber; account++) {
deriveParams.accountIndex = account
for (let n = 0; n < receivedAddressesNumber; n++) {
deriveParams.addressIndex = n
deriveParams.isChange = false
accountsList.receive.push(this.deriveAccountAddress(deriveParams))
}
for (let n = 0; n < changeAddressesNumber; n++) {
deriveParams.addressIndex = n
deriveParams.isChange = true
accountsList.change.push(this.deriveAccountAddress(deriveParams))
}
}
return accountsList
}
deriveAccountAddress({
purpose,
accountIndex = 0,
addressIndex = 0,
isTestnet = false,
isChange = false
}) {
if (! this.rootNode) {
this.generateRootNode()
}
// m / purpose' / coin_type' / account' / change / address_index
const derivedKey = this.rootNode
.deriveHardened(purpose.bip) // purpose
.deriveHardened(isTestnet ? 1 : 0) // 0 - is BTC coin type
.deriveHardened(accountIndex) // account index
.derive(isChange ? 1 : 0) // 0 - external(receive), 1 - internal(change) address
.derive(addressIndex); // address_index
const network = (this.network === null) ? (isTestnet ? purpose.network.testnet : purpose.network.main) : this.network
return {
derivedKey,
address: purpose.addressGenerator(derivedKey, network),
// addresses: calculateAddresses(derivedKey, network)
}
}
deriveAccountReceiveAddress(params) {
params.isChange = false;
return this.deriveAccountAddress(params)
}
deriveAccountChangeAddress(params) {
params.isChange = true;
return this.deriveAccountAddress(params)
}
backup(){
return {
mnemonic: this.mnemonic,
seedPassphrase: this.seedPassphrase
}
}
}
module.exports = {
HDWallet: HDWallet
}