-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
233 lines (205 loc) · 5.66 KB
/
wallet.go
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package blockchain
import (
"errors"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/hdkeychain"
"github.com/tyler-smith/go-bip39"
)
// bitcoin blockchain network type
const (
Mainnet = "mainnet"
Testnet = "testnet"
Regtest = "regtest"
Simnet = "simnet"
)
// HDWallet is BIP44 HD Wallet format. Apostrophe is a hardend key to enhance security
// m / purpose' / coin_type' / account' / change / address_index
type HDWallet struct {
Mnemonic string
ExtendedKey *hdkeychain.ExtendedKey
ChainParams *chaincfg.Params
}
// Account is HDWallet account
type Account struct {
ExtendedKey *hdkeychain.ExtendedKey
ChainParams *chaincfg.Params
}
// ChildWallet is created from HDWallet by index
type ChildWallet struct {
Address btcutil.Address
WIF *btcutil.WIF
PublicKey btcec.PublicKey
}
// Purpose is BIP44 purpose
const Purpose = 44
// BIP44 CoinType
const (
CoinTypeBitcoin uint32 = 0
CoinTypeBitcoinTestnet uint32 = 1
)
// HardenedKey BIP44 hardened key
const HardenedKey = 0x80000000
// BIP44 change type
const (
ChangeTypeExternal uint32 = 0
ChangeTypeInternal uint32 = 1
)
//
const (
AddressP2KH string = "p2kh"
AddressP2SH string = "p2sh"
AddressBech32 string = "bech32"
)
// GetCoinType return BIP44 cointype by network
func GetCoinType(network string) uint32 {
if network == Mainnet {
return CoinTypeBitcoin
}
return CoinTypeBitcoinTestnet
}
// GetParamsFromNetwork return chain params from BlockChain Network type
func GetParamsFromNetwork(network string) (*chaincfg.Params, error) {
switch network {
case Mainnet:
return &chaincfg.MainNetParams, nil
case Testnet:
return &chaincfg.TestNet3Params, nil
case Regtest:
return &chaincfg.RegressionNetParams, nil
case Simnet:
return &chaincfg.SimNetParams, nil
}
return nil, errors.New("invalid BlockChain Network")
}
// NewHDWallet return mnemonic and HDWallet ExtendedKey and network Params
func NewHDWallet(bitSize int, mnemonic string, network string, password string) (*HDWallet, error) {
if mnemonic == "" {
m, err := GenerateMnemonic(bitSize)
if err != nil {
return nil, err
}
mnemonic = m
}
seed, err := GenerateSeed(mnemonic, password)
if err != nil {
return nil, err
}
networkParams, err := GetParamsFromNetwork(network)
if err != nil {
return nil, err
}
extendedKey, err := hdkeychain.NewMaster(seed, networkParams)
if err != nil {
return nil, err
}
return &HDWallet{
Mnemonic: mnemonic,
ExtendedKey: extendedKey,
ChainParams: networkParams,
}, nil
}
// GenerateMnemonic return mnemonic (bitSize must be [128, 256] and a multiple of 32)
func GenerateMnemonic(bitSize int) (string, error) {
entropy, err := bip39.NewEntropy(bitSize)
if err != nil {
return "", err
}
return bip39.NewMnemonic(entropy)
}
// GenerateSeed return seed from mnemonic and password
func GenerateSeed(mnemonic, password string) ([]byte, error) {
return bip39.NewSeedWithErrorChecking(mnemonic, password)
}
// NewAccount create Account by BIP44 settings
func (hd *HDWallet) NewAccount(purpose, coinType, account uint32) (*Account, error) {
// add hardened
purpose = purpose + HardenedKey
coinType = coinType + HardenedKey
account = account + HardenedKey
purposeChild, err := hd.ExtendedKey.Child(purpose)
if err != nil {
return nil, err
}
coinTypeChild, err := purposeChild.Child(coinType)
if err != nil {
return nil, err
}
accountChild, err := coinTypeChild.Child(account)
if err != nil {
return nil, err
}
return &Account{
ExtendedKey: accountChild,
ChainParams: hd.ChainParams,
}, nil
}
// DeriveAddress return ChildWallet by change type and addressIndex
func (a *Account) DeriveAddress(change, addressIndex uint32, addressType string) (*ChildWallet, error) {
changeChild, err := a.ExtendedKey.Child(change)
if err != nil {
return nil, err
}
childWallet, err := changeChild.Child(addressIndex)
if err != nil {
return nil, err
}
ecPrivKey, err := childWallet.ECPrivKey()
if err != nil {
return nil, err
}
// compressed set true
wif, err := btcutil.NewWIF(ecPrivKey, a.ChainParams, true)
if err != nil {
return nil, err
}
ecPubkey, err := childWallet.ECPubKey()
if err != nil {
return nil, err
}
address, err := encodeAddress(*ecPubkey, addressType, *a.ChainParams)
decodedAddress, err := btcutil.DecodeAddress(address, a.ChainParams)
if err != nil {
return nil, err
}
return &ChildWallet{
Address: decodedAddress,
WIF: wif,
PublicKey: *ecPubkey,
}, nil
}
// encodeAddress return address by addressType
func encodeAddress(ecPubkey btcec.PublicKey, addressType string, networkParams chaincfg.Params) (address string, err error) {
switch addressType {
case AddressBech32:
witnessHash := btcutil.Hash160(ecPubkey.SerializeCompressed())
witnessPubKeyHash, err := btcutil.NewAddressWitnessPubKeyHash(witnessHash, &networkParams)
if err != nil {
return address, err
}
address = witnessPubKeyHash.EncodeAddress()
return address, nil
case AddressP2KH:
addressPubkey, err := btcutil.NewAddressPubKey(ecPubkey.SerializeCompressed(), &networkParams)
if err != nil {
return address, err
}
address = addressPubkey.EncodeAddress()
return address, err
case AddressP2SH:
keyHash := btcutil.Hash160(ecPubkey.SerializeCompressed())
scriptSig, err := txscript.NewScriptBuilder().AddOp(txscript.OP_0).AddData(keyHash).Script()
if err != nil {
return address, err
}
addressScript, err := btcutil.NewAddressScriptHash(scriptSig, &networkParams)
if err != nil {
return address, err
}
address = addressScript.EncodeAddress()
return address, err
}
return address, errors.New("invalid address type")
}