forked from planetdecred/dcrlibwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
322 lines (264 loc) · 8.45 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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package dcrlibwallet
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"decred.org/dcrwallet/v2/errors"
w "decred.org/dcrwallet/v2/wallet"
"decred.org/dcrwallet/v2/walletseed"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/planetdecred/dcrlibwallet/internal/loader"
"github.com/planetdecred/dcrlibwallet/internal/vsp"
"github.com/planetdecred/dcrlibwallet/walletdata"
)
type Wallet struct {
ID int `storm:"id,increment"`
Name string `storm:"unique"`
CreatedAt time.Time `storm:"index"`
DbDriver string
EncryptedSeed []byte
IsRestored bool
HasDiscoveredAccounts bool
PrivatePassphraseType int32
chainParams *chaincfg.Params
dataDir string
loader *loader.Loader
walletDataDB *walletdata.DB
synced bool
syncing bool
waitingForHeaders bool
shuttingDown chan bool
cancelFuncs []context.CancelFunc
cancelAccountMixer context.CancelFunc
cancelAutoTicketBuyerMu sync.Mutex
cancelAutoTicketBuyer context.CancelFunc
vspClientsMu sync.Mutex
vspClients map[string]*vsp.Client
// setUserConfigValue saves the provided key-value pair to a config database.
// This function is ideally assigned when the `wallet.prepare` method is
// called from a MultiWallet instance.
setUserConfigValue configSaveFn
// readUserConfigValue returns the previously saved value for the provided
// key from a config database. Returns nil if the key wasn't previously set.
// This function is ideally assigned when the `wallet.prepare` method is
// called from a MultiWallet instance.
readUserConfigValue configReadFn
}
// prepare gets a wallet ready for use by opening the transactions index database
// and initializing the wallet loader which can be used subsequently to create,
// load and unload the wallet.
func (wallet *Wallet) prepare(rootDir string, chainParams *chaincfg.Params,
setUserConfigValueFn configSaveFn, readUserConfigValueFn configReadFn) (err error) {
wallet.chainParams = chainParams
wallet.dataDir = filepath.Join(rootDir, strconv.Itoa(wallet.ID))
wallet.vspClients = make(map[string]*vsp.Client)
wallet.setUserConfigValue = setUserConfigValueFn
wallet.readUserConfigValue = readUserConfigValueFn
// open database for indexing transactions for faster loading
walletDataDBPath := filepath.Join(wallet.dataDir, walletdata.DbName)
oldTxDBPath := filepath.Join(wallet.dataDir, walletdata.OldDbName)
if exists, _ := fileExists(oldTxDBPath); exists {
moveFile(oldTxDBPath, walletDataDBPath)
}
wallet.walletDataDB, err = walletdata.Initialize(walletDataDBPath, chainParams, &Transaction{})
if err != nil {
log.Error(err.Error())
return err
}
// init loader
wallet.loader = initWalletLoader(wallet.chainParams, wallet.dataDir, wallet.DbDriver)
// init cancelFuncs slice to hold cancel functions for long running
// operations and start go routine to listen for shutdown signal
wallet.cancelFuncs = make([]context.CancelFunc, 0)
wallet.shuttingDown = make(chan bool)
go func() {
<-wallet.shuttingDown
for _, cancel := range wallet.cancelFuncs {
cancel()
}
}()
return nil
}
func (wallet *Wallet) Shutdown() {
// Trigger shuttingDown signal to cancel all contexts created with
// `wallet.shutdownContext()` or `wallet.shutdownContextWithCancel()`.
wallet.shuttingDown <- true
if _, loaded := wallet.loader.LoadedWallet(); loaded {
err := wallet.loader.UnloadWallet()
if err != nil {
log.Errorf("Failed to close wallet: %v", err)
} else {
log.Info("Closed wallet")
}
}
if wallet.walletDataDB != nil {
err := wallet.walletDataDB.Close()
if err != nil {
log.Errorf("tx db closed with error: %v", err)
} else {
log.Info("tx db closed successfully")
}
}
}
// WalletCreationTimeInMillis returns the wallet creation time for new
// wallets. Restored wallets would return an error.
func (wallet *Wallet) WalletCreationTimeInMillis() (int64, error) {
if wallet.IsRestored {
return 0, errors.New(ErrWalletIsRestored)
}
return wallet.CreatedAt.UnixNano() / int64(time.Millisecond), nil
}
func (wallet *Wallet) NetType() string {
return wallet.chainParams.Name
}
func (wallet *Wallet) Internal() *w.Wallet {
lw, _ := wallet.loader.LoadedWallet()
return lw
}
func (wallet *Wallet) WalletExists() (bool, error) {
return wallet.loader.WalletExists()
}
func (wallet *Wallet) createWallet(privatePassphrase, seedMnemonic string) error {
log.Info("Creating Wallet")
if len(seedMnemonic) == 0 {
return errors.New(ErrEmptySeed)
}
pubPass := []byte(w.InsecurePubPassphrase)
privPass := []byte(privatePassphrase)
seed, err := walletseed.DecodeUserInput(seedMnemonic)
if err != nil {
log.Error(err)
return err
}
_, err = wallet.loader.CreateNewWallet(wallet.shutdownContext(), pubPass, privPass, seed)
if err != nil {
log.Error(err)
return err
}
log.Info("Created Wallet")
return nil
}
func (wallet *Wallet) createWatchingOnlyWallet(extendedPublicKey string) error {
pubPass := []byte(w.InsecurePubPassphrase)
_, err := wallet.loader.CreateWatchingOnlyWallet(wallet.shutdownContext(), extendedPublicKey, pubPass)
if err != nil {
log.Error(err)
return err
}
log.Info("Created Watching Only Wallet")
return nil
}
func (wallet *Wallet) IsWatchingOnlyWallet() bool {
if w, ok := wallet.loader.LoadedWallet(); ok {
return w.WatchingOnly()
}
return false
}
func (wallet *Wallet) openWallet() error {
pubPass := []byte(w.InsecurePubPassphrase)
_, err := wallet.loader.OpenExistingWallet(wallet.shutdownContext(), pubPass)
if err != nil {
log.Error(err)
return translateError(err)
}
return nil
}
func (wallet *Wallet) WalletOpened() bool {
return wallet.Internal() != nil
}
func (wallet *Wallet) UnlockWallet(privPass []byte) error {
loadedWallet, ok := wallet.loader.LoadedWallet()
if !ok {
return fmt.Errorf("wallet has not been loaded")
}
ctx, _ := wallet.shutdownContextWithCancel()
err := loadedWallet.Unlock(ctx, privPass, nil)
if err != nil {
return translateError(err)
}
return nil
}
func (wallet *Wallet) LockWallet() {
if wallet.IsAccountMixerActive() {
log.Error("LockWallet ignored due to active account mixer")
return
}
if !wallet.Internal().Locked() {
wallet.Internal().Lock()
}
}
func (wallet *Wallet) IsLocked() bool {
return wallet.Internal().Locked()
}
func (wallet *Wallet) changePrivatePassphrase(oldPass []byte, newPass []byte) error {
defer func() {
for i := range oldPass {
oldPass[i] = 0
}
for i := range newPass {
newPass[i] = 0
}
}()
err := wallet.Internal().ChangePrivatePassphrase(wallet.shutdownContext(), oldPass, newPass)
if err != nil {
return translateError(err)
}
return nil
}
func (wallet *Wallet) deleteWallet(privatePassphrase []byte) error {
defer func() {
for i := range privatePassphrase {
privatePassphrase[i] = 0
}
}()
if _, loaded := wallet.loader.LoadedWallet(); !loaded {
return errors.New(ErrWalletNotLoaded)
}
if !wallet.IsWatchingOnlyWallet() {
err := wallet.Internal().Unlock(wallet.shutdownContext(), privatePassphrase, nil)
if err != nil {
return translateError(err)
}
wallet.Internal().Lock()
}
wallet.Shutdown()
log.Info("Deleting Wallet")
return os.RemoveAll(wallet.dataDir)
}
// DecryptSeed decrypts wallet.EncryptedSeed using privatePassphrase
func (wallet *Wallet) DecryptSeed(privatePassphrase []byte) (string, error) {
if wallet.EncryptedSeed == nil {
return "", errors.New(ErrInvalid)
}
return decryptWalletSeed(privatePassphrase, wallet.EncryptedSeed)
}
// AccountXPubMatches checks if the xpub of the provided account matches the
// provided legacy or SLIP0044 xpub. While both the legacy and SLIP0044 xpubs
// will be checked for watch-only wallets, other wallets will only check the
// xpub that matches the coin type key used by the wallet.
func (wallet *Wallet) AccountXPubMatches(account uint32, legacyXPub, slip044XPub string) (bool, error) {
ctx := wallet.shutdownContext()
acctXPubKey, err := wallet.Internal().AccountXpub(ctx, account)
if err != nil {
return false, err
}
acctXPub := acctXPubKey.String()
if wallet.IsWatchingOnlyWallet() {
// Coin type info isn't saved for watch-only wallets, so check
// against both legacy and SLIP0044 coin types.
return acctXPub == legacyXPub || acctXPub == slip044XPub, nil
}
cointype, err := wallet.Internal().CoinType(ctx)
if err != nil {
return false, err
}
if cointype == wallet.chainParams.LegacyCoinType {
return acctXPub == legacyXPub, nil
} else {
return acctXPub == slip044XPub, nil
}
}