-
Notifications
You must be signed in to change notification settings - Fork 41
/
multiwallet_utils.go
282 lines (241 loc) · 7.36 KB
/
multiwallet_utils.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package dcrlibwallet
import (
"context"
"os"
"path/filepath"
"strings"
"decred.org/dcrwallet/v2/deployments"
"decred.org/dcrwallet/v2/errors"
w "decred.org/dcrwallet/v2/wallet"
"decred.org/dcrwallet/v2/walletseed"
"github.com/asdine/storm"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/hdkeychain/v3"
"github.com/kevinburke/nacl"
"github.com/kevinburke/nacl/secretbox"
"golang.org/x/crypto/scrypt"
)
const (
logFileName = "dcrlibwallet.log"
walletsDbName = "wallets.db"
walletsMetadataBucketName = "metadata"
walletstartupPassphraseField = "startup-passphrase"
)
var (
Mainnet = chaincfg.MainNetParams().Name
Testnet3 = chaincfg.TestNet3Params().Name
)
func (mw *MultiWallet) batchDbTransaction(dbOp func(node storm.Node) error) (err error) {
dbTx, err := mw.db.Begin(true)
if err != nil {
return err
}
// Commit or rollback the transaction after f returns or panics. Do not
// recover from the panic to keep the original stack trace intact.
panicked := true
defer func() {
if panicked || err != nil {
dbTx.Rollback()
return
}
err = dbTx.Commit()
}()
err = dbOp(dbTx)
panicked = false
return err
}
func (mw *MultiWallet) loadWalletTemporarily(ctx context.Context, walletDataDir, walletPublicPass string,
onLoaded func(*w.Wallet) error) error {
if walletPublicPass == "" {
walletPublicPass = w.InsecurePubPassphrase
}
// initialize the wallet loader
walletLoader := initWalletLoader(mw.chainParams, walletDataDir, mw.dbDriver)
// open the wallet to get ready for temporary use
wallet, err := walletLoader.OpenExistingWallet(ctx, []byte(walletPublicPass))
if err != nil {
return translateError(err)
}
// unload wallet after temporary use
defer walletLoader.UnloadWallet()
if onLoaded != nil {
return onLoaded(wallet)
}
return nil
}
func (mw *MultiWallet) markWalletAsDiscoveredAccounts(walletID int) error {
wallet := mw.WalletWithID(walletID)
if wallet == nil {
return errors.New(ErrNotExist)
}
log.Infof("Set discovered accounts = true for wallet %d", wallet.ID)
wallet.HasDiscoveredAccounts = true
err := mw.db.Save(wallet)
if err != nil {
return err
}
return nil
}
// RootDirFileSizeInBytes returns the total directory size of
// multiwallet's root directory in bytes.
func (mw *MultiWallet) RootDirFileSizeInBytes() (int64, error) {
var size int64
err := filepath.Walk(mw.rootDir, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
// DCP0001ActivationBlockHeight returns the hardcoded block height that
// the DCP0001 deployment activates at. DCP0001 specifies hard forking
// changes to the stake difficulty algorithm.
func (mw *MultiWallet) DCP0001ActivationBlockHeight() int32 {
var activationHeight int32 = -1
switch strings.ToLower(mw.chainParams.Name) {
case strings.ToLower(Mainnet):
activationHeight = deployments.DCP0001.MainNetActivationHeight
case strings.ToLower(Testnet3):
activationHeight = deployments.DCP0001.TestNet3ActivationHeight
default:
}
return activationHeight
}
// WalletWithXPub returns the ID of the wallet that has an account with the
// provided xpub. Returns -1 if there is no such wallet.
func (mw *MultiWallet) WalletWithXPub(xpub string) (int, error) {
ctx, cancel := mw.contextWithShutdownCancel()
defer cancel()
for _, w := range mw.wallets {
if !w.WalletOpened() {
return -1, errors.Errorf("wallet %d is not open and cannot be checked", w.ID)
}
accounts, err := w.Internal().Accounts(ctx)
if err != nil {
return -1, err
}
for _, account := range accounts.Accounts {
if account.AccountNumber == ImportedAccountNumber {
continue
}
acctXPub, err := w.Internal().AccountXpub(ctx, account.AccountNumber)
if err != nil {
return -1, err
}
if acctXPub.String() == xpub {
return w.ID, nil
}
}
}
return -1, nil
}
// WalletWithSeed returns the ID of the wallet that was created or restored
// using the same seed as the one provided. Returns -1 if no wallet uses the
// provided seed.
func (mw *MultiWallet) WalletWithSeed(seedMnemonic string) (int, error) {
if len(seedMnemonic) == 0 {
return -1, errors.New(ErrEmptySeed)
}
newSeedLegacyXPUb, newSeedSLIP0044XPUb, err := deriveBIP44AccountXPubs(seedMnemonic, DefaultAccountNum, mw.chainParams)
if err != nil {
return -1, err
}
for _, wallet := range mw.wallets {
if !wallet.WalletOpened() {
return -1, errors.Errorf("cannot check if seed matches unloaded wallet %d", wallet.ID)
}
// NOTE: Existing watch-only wallets may have been created using the
// xpub of an account that is NOT the default account and may return
// incorrect result from the check below. But this would return true
// if the watch-only wallet was created using the xpub of the default
// account of the provided seed.
usesSameSeed, err := wallet.AccountXPubMatches(DefaultAccountNum, newSeedLegacyXPUb, newSeedSLIP0044XPUb)
if err != nil {
return -1, err
}
if usesSameSeed {
return wallet.ID, nil
}
}
return -1, nil
}
// deriveBIP44AccountXPub derives and returns the legacy and SLIP0044 account
// xpubs using the BIP44 HD path for accounts: m/44'/<coin type>'/<account>'.
func deriveBIP44AccountXPubs(seedMnemonic string, account uint32, params *chaincfg.Params) (string, string, error) {
seed, err := walletseed.DecodeUserInput(seedMnemonic)
if err != nil {
return "", "", err
}
defer func() {
for i := range seed {
seed[i] = 0
}
}()
// Derive the master extended key from the provided seed.
masterNode, err := hdkeychain.NewMaster(seed, params)
if err != nil {
return "", "", err
}
defer masterNode.Zero()
// Derive the purpose key as a child of the master node.
purpose, err := masterNode.Child(44 + hdkeychain.HardenedKeyStart)
if err != nil {
return "", "", err
}
defer purpose.Zero()
accountXPub := func(coinType uint32) (string, error) {
coinTypePrivKey, err := purpose.Child(coinType + hdkeychain.HardenedKeyStart)
if err != nil {
return "", err
}
defer coinTypePrivKey.Zero()
acctPrivKey, err := coinTypePrivKey.Child(account + hdkeychain.HardenedKeyStart)
if err != nil {
return "", err
}
defer acctPrivKey.Zero()
return acctPrivKey.Neuter().String(), nil
}
legacyXPUb, err := accountXPub(params.LegacyCoinType)
if err != nil {
return "", "", err
}
slip0044XPUb, err := accountXPub(params.SLIP0044CoinType)
if err != nil {
return "", "", err
}
return legacyXPUb, slip0044XPUb, nil
}
// naclLoadFromPass derives a nacl.Key from pass using scrypt.Key.
func naclLoadFromPass(pass []byte) (nacl.Key, error) {
const N, r, p = 1 << 15, 8, 1
hash, err := scrypt.Key(pass, nil, N, r, p, 32)
if err != nil {
return nil, err
}
return nacl.Load(EncodeHex(hash))
}
// encryptWalletSeed encrypts the seed with secretbox.EasySeal using pass.
func encryptWalletSeed(pass []byte, seed string) ([]byte, error) {
key, err := naclLoadFromPass(pass)
if err != nil {
return nil, err
}
return secretbox.EasySeal([]byte(seed), key), nil
}
// decryptWalletSeed decrypts the encryptedSeed with secretbox.EasyOpen using pass.
func decryptWalletSeed(pass []byte, encryptedSeed []byte) (string, error) {
key, err := naclLoadFromPass(pass)
if err != nil {
return "", err
}
decryptedSeed, err := secretbox.EasyOpen(encryptedSeed, key)
if err != nil {
return "", errors.New(ErrInvalidPassphrase)
}
return string(decryptedSeed), nil
}