-
Notifications
You must be signed in to change notification settings - Fork 0
/
wallet.go
470 lines (441 loc) · 15.4 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package wallet
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/cmd/validator/flags"
"github.com/prysmaticlabs/prysm/v4/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/v4/io/file"
"github.com/prysmaticlabs/prysm/v4/io/prompt"
"github.com/prysmaticlabs/prysm/v4/validator/accounts/iface"
accountsprompt "github.com/prysmaticlabs/prysm/v4/validator/accounts/userprompt"
"github.com/prysmaticlabs/prysm/v4/validator/keymanager"
"github.com/prysmaticlabs/prysm/v4/validator/keymanager/derived"
"github.com/prysmaticlabs/prysm/v4/validator/keymanager/local"
remoteweb3signer "github.com/prysmaticlabs/prysm/v4/validator/keymanager/remote-web3signer"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
const (
// KeymanagerConfigFileName for the keymanager used by the wallet: imported, derived, remote, or web3signer.
KeymanagerConfigFileName = "keymanageropts.json"
// NewWalletPasswordPromptText for wallet creation.
NewWalletPasswordPromptText = "New wallet password"
// PasswordPromptText for wallet unlocking.
PasswordPromptText = "Wallet password"
// ConfirmPasswordPromptText for confirming a wallet password.
ConfirmPasswordPromptText = "Confirm password"
// DefaultWalletPasswordFile used to store a wallet password with appropriate permissions
// if a user signs up via the Prysm web UI via RPC.
DefaultWalletPasswordFile = "walletpassword.txt"
// CheckExistsErrMsg for when there is an error while checking for a wallet
CheckExistsErrMsg = "could not check if wallet exists"
// CheckValidityErrMsg for when there is an error while checking wallet validity
CheckValidityErrMsg = "could not check if wallet is valid"
// InvalidWalletErrMsg for when a directory does not contain a valid wallet
InvalidWalletErrMsg = "directory does not contain valid wallet"
)
var (
// ErrNoWalletFound signifies there was no wallet directory found on-disk.
ErrNoWalletFound = errors.New(
"no wallet found. You can create a new wallet with `validator wallet create`. " +
"If you already did, perhaps you created a wallet in a custom directory, which you can specify using " +
"`--wallet-dir=/path/to/my/wallet`",
)
// KeymanagerKindSelections as friendly text.
KeymanagerKindSelections = map[keymanager.Kind]string{
keymanager.Local: "Imported Wallet (Recommended)",
keymanager.Derived: "HD Wallet",
keymanager.Web3Signer: "Consensys Web3Signer (Advanced)",
}
// ValidateExistingPass checks that an input cannot be empty.
ValidateExistingPass = func(input string) error {
if input == "" {
return errors.New("password input cannot be empty")
}
return nil
}
)
// Config to open a wallet programmatically.
type Config struct {
WalletDir string
KeymanagerKind keymanager.Kind
WalletPassword string
}
// Wallet is a primitive in Prysm's account management which
// has the capability of creating new accounts, reading existing accounts,
// and providing secure access to Ethereum proof of stake secrets depending on an
// associated keymanager (either imported, derived, or remote signing enabled).
type Wallet struct {
walletDir string
accountsPath string
configFilePath string
walletPassword string
keymanagerKind keymanager.Kind
}
// New creates a struct from config values.
func New(cfg *Config) *Wallet {
accountsPath := filepath.Join(cfg.WalletDir, cfg.KeymanagerKind.String())
return &Wallet{
walletDir: cfg.WalletDir,
accountsPath: accountsPath,
keymanagerKind: cfg.KeymanagerKind,
walletPassword: cfg.WalletPassword,
}
}
// Exists checks if directory at walletDir exists
func Exists(walletDir string) (bool, error) {
dirExists, err := file.HasDir(walletDir)
if err != nil {
return false, errors.Wrap(err, "could not parse wallet directory")
}
isValid, err := IsValid(walletDir)
if errors.Is(err, ErrNoWalletFound) {
return false, nil
} else if err != nil {
return false, errors.Wrap(err, "could not check if dir is valid")
}
return dirExists && isValid, nil
}
// IsValid checks if a folder contains a single key directory such as `derived`, `remote` or `imported`.
// Returns true if one of those subdirectories exist, false otherwise.
func IsValid(walletDir string) (bool, error) {
expanded, err := file.ExpandPath(walletDir)
if err != nil {
return false, err
}
f, err := os.Open(expanded) // #nosec G304
if err != nil {
if strings.Contains(err.Error(), "no such file") ||
strings.Contains(err.Error(), "cannot find the file") ||
strings.Contains(err.Error(), "cannot find the path") {
return false, nil
}
return false, err
}
defer func() {
if err := f.Close(); err != nil {
log.Debugf("Could not close directory: %s", expanded)
}
}()
names, err := f.Readdirnames(-1)
if err != nil {
return false, err
}
if len(names) == 0 {
return false, ErrNoWalletFound
}
// Count how many wallet types we have in the directory
numWalletTypes := 0
for _, name := range names {
// Nil error means input name is `derived` or `imported`
_, err = keymanager.ParseKind(name)
if err == nil {
numWalletTypes++
}
}
return numWalletTypes == 1, nil
}
// OpenWalletOrElseCli tries to open the wallet and if it fails or no wallet
// is found, invokes a callback function.
func OpenWalletOrElseCli(cliCtx *cli.Context, otherwise func(cliCtx *cli.Context) (*Wallet, error)) (*Wallet, error) {
exists, err := Exists(cliCtx.String(flags.WalletDirFlag.Name))
if err != nil {
return nil, errors.Wrap(err, CheckExistsErrMsg)
}
if !exists {
return otherwise(cliCtx)
}
isValid, err := IsValid(cliCtx.String(flags.WalletDirFlag.Name))
if errors.Is(err, ErrNoWalletFound) {
return otherwise(cliCtx)
}
if err != nil {
return nil, errors.Wrap(err, CheckValidityErrMsg)
}
if !isValid {
return nil, errors.New(InvalidWalletErrMsg)
}
walletDir, err := accountsprompt.InputDirectory(cliCtx, accountsprompt.WalletDirPromptText, flags.WalletDirFlag)
if err != nil {
return nil, err
}
walletPassword, err := InputPassword(
cliCtx,
flags.WalletPasswordFileFlag,
PasswordPromptText,
false, /* Do not confirm password */
ValidateExistingPass,
)
if err != nil {
return nil, err
}
return OpenWallet(cliCtx.Context, &Config{
WalletDir: walletDir,
WalletPassword: walletPassword,
})
}
// NewWalletForWeb3Signer returns a new wallet for web3 signer which is temporary and not stored locally.
func NewWalletForWeb3Signer() *Wallet {
// wallet is just a temporary wallet for web3 signer used to call initialize keymanager.
return &Wallet{
walletDir: "",
accountsPath: "",
keymanagerKind: keymanager.Web3Signer,
walletPassword: "",
}
}
// OpenWallet instantiates a wallet from a specified path. It checks the
// type of keymanager associated with the wallet by reading files in the wallet
// path, if applicable. If a wallet does not exist, returns an appropriate error.
func OpenWallet(_ context.Context, cfg *Config) (*Wallet, error) {
exists, err := Exists(cfg.WalletDir)
if err != nil {
return nil, errors.Wrap(err, CheckExistsErrMsg)
}
if !exists {
return nil, ErrNoWalletFound
}
valid, err := IsValid(cfg.WalletDir)
// ErrNoWalletFound represents both a directory that does not exist as well as an empty directory
if errors.Is(err, ErrNoWalletFound) {
return nil, ErrNoWalletFound
}
if err != nil {
return nil, errors.Wrap(err, CheckValidityErrMsg)
}
if !valid {
return nil, errors.New(InvalidWalletErrMsg)
}
keymanagerKind, err := readKeymanagerKindFromWalletPath(cfg.WalletDir)
if err != nil {
return nil, errors.Wrap(err, "could not read keymanager kind for wallet")
}
accountsPath := filepath.Join(cfg.WalletDir, keymanagerKind.String())
return &Wallet{
walletDir: cfg.WalletDir,
accountsPath: accountsPath,
keymanagerKind: keymanagerKind,
walletPassword: cfg.WalletPassword,
}, nil
}
// SaveWallet persists the wallet's directories to disk.
func (w *Wallet) SaveWallet() error {
if err := file.MkdirAll(w.accountsPath); err != nil {
return errors.Wrap(err, "could not create wallet directory")
}
return nil
}
// KeymanagerKind used by the wallet.
func (w *Wallet) KeymanagerKind() keymanager.Kind {
return w.keymanagerKind
}
// AccountsDir for the wallet.
func (w *Wallet) AccountsDir() string {
return w.accountsPath
}
// Password for the wallet.
func (w *Wallet) Password() string {
return w.walletPassword
}
// InitializeKeymanager reads a keymanager config from disk at the wallet path,
// unmarshals it based on the wallet's keymanager kind, and returns its value.
func (w *Wallet) InitializeKeymanager(ctx context.Context, cfg iface.InitKeymanagerConfig) (keymanager.IKeymanager, error) {
var km keymanager.IKeymanager
var err error
switch w.KeymanagerKind() {
case keymanager.Local:
km, err = local.NewKeymanager(ctx, &local.SetupConfig{
Wallet: w,
ListenForChanges: cfg.ListenForChanges,
})
if err != nil {
return nil, errors.Wrap(err, "could not initialize imported keymanager")
}
case keymanager.Derived:
km, err = derived.NewKeymanager(ctx, &derived.SetupConfig{
Wallet: w,
ListenForChanges: cfg.ListenForChanges,
})
if err != nil {
return nil, errors.Wrap(err, "could not initialize derived keymanager")
}
case keymanager.Web3Signer:
config := cfg.Web3SignerConfig
if config == nil {
return nil, errors.New("web3signer config is nil")
}
// TODO(9883): future work needs to address how initialize keymanager is called for web3signer.
// an error may be thrown for genesis validators root for some InitializeKeymanager calls.
if !bytesutil.IsValidRoot(config.GenesisValidatorsRoot) {
return nil, errors.New("web3signer requires a genesis validators root value")
}
km, err = remoteweb3signer.NewKeymanager(ctx, config)
if err != nil {
return nil, errors.Wrap(err, "could not initialize web3signer keymanager")
}
default:
return nil, fmt.Errorf("keymanager kind not supported: %s", w.keymanagerKind)
}
return km, nil
}
// WriteFileAtPath within the wallet directory given the desired path, filename, and raw data.
func (w *Wallet) WriteFileAtPath(_ context.Context, filePath, fileName string, data []byte) error {
accountPath := filepath.Join(w.accountsPath, filePath)
hasDir, err := file.HasDir(accountPath)
if err != nil {
return err
}
if !hasDir {
if err := file.MkdirAll(accountPath); err != nil {
return errors.Wrapf(err, "could not create path: %s", accountPath)
}
}
fullPath := filepath.Join(accountPath, fileName)
if err := file.WriteFile(fullPath, data); err != nil {
return errors.Wrapf(err, "could not write %s", filePath)
}
log.WithFields(logrus.Fields{
"path": fullPath,
"fileName": fileName,
}).Debug("Wrote new file at path")
return nil
}
// ReadFileAtPath within the wallet directory given the desired path and filename.
func (w *Wallet) ReadFileAtPath(_ context.Context, filePath, fileName string) ([]byte, error) {
accountPath := filepath.Join(w.accountsPath, filePath)
hasDir, err := file.HasDir(accountPath)
if err != nil {
return nil, err
}
if !hasDir {
if err := file.MkdirAll(accountPath); err != nil {
return nil, errors.Wrapf(err, "could not create path: %s", accountPath)
}
}
fullPath := filepath.Join(accountPath, fileName)
matches, err := filepath.Glob(fullPath)
if err != nil {
return []byte{}, errors.Wrap(err, "could not find file")
}
if len(matches) == 0 {
return []byte{}, fmt.Errorf("no files found in path: %s", fullPath)
}
rawData, err := os.ReadFile(matches[0])
if err != nil {
return nil, errors.Wrapf(err, "could not read path: %s", filePath)
}
return rawData, nil
}
// FileNameAtPath return the full file name for the requested file. It allows for finding the file
// with a regex pattern.
func (w *Wallet) FileNameAtPath(_ context.Context, filePath, fileName string) (string, error) {
accountPath := filepath.Join(w.accountsPath, filePath)
if err := file.MkdirAll(accountPath); err != nil {
return "", errors.Wrapf(err, "could not create path: %s", accountPath)
}
fullPath := filepath.Join(accountPath, fileName)
matches, err := filepath.Glob(fullPath)
if err != nil {
return "", errors.Wrap(err, "could not find file")
}
if len(matches) == 0 {
return "", fmt.Errorf("no files found in path: %s", fullPath)
}
fullFileName := filepath.Base(matches[0])
return fullFileName, nil
}
// ReadKeymanagerConfigFromDisk opens a keymanager config file
// for reading if it exists at the wallet path.
func (w *Wallet) ReadKeymanagerConfigFromDisk(_ context.Context) (io.ReadCloser, error) {
configFilePath := filepath.Join(w.accountsPath, KeymanagerConfigFileName)
if !file.FileExists(configFilePath) {
return nil, fmt.Errorf("no keymanager config file found at path: %s", w.accountsPath)
}
w.configFilePath = configFilePath
return os.Open(configFilePath) // #nosec G304
}
// WriteKeymanagerConfigToDisk takes an encoded keymanager config file
// and writes it to the wallet path.
func (w *Wallet) WriteKeymanagerConfigToDisk(_ context.Context, encoded []byte) error {
configFilePath := filepath.Join(w.accountsPath, KeymanagerConfigFileName)
// Write the config file to disk.
if err := file.WriteFile(configFilePath, encoded); err != nil {
return errors.Wrapf(err, "could not write config to path: %s", configFilePath)
}
log.WithField("configFilePath", configFilePath).Debug("Wrote keymanager config file to disk")
return nil
}
func readKeymanagerKindFromWalletPath(walletPath string) (keymanager.Kind, error) {
walletItem, err := os.Open(walletPath) // #nosec G304
if err != nil {
return 0, err
}
defer func() {
if err := walletItem.Close(); err != nil {
log.WithField(
"path", walletPath,
).Errorf("Could not close wallet directory: %v", err)
}
}()
list, err := walletItem.Readdirnames(0) // 0 to read all files and folders.
if err != nil {
return 0, fmt.Errorf("could not read files in directory: %s", walletPath)
}
for _, n := range list {
keymanagerKind, err := keymanager.ParseKind(n)
if err == nil {
return keymanagerKind, nil
}
}
return 0, errors.New("no keymanager folder (imported, remote, derived) found in wallet path")
}
// InputPassword prompts for a password and optionally for password confirmation.
// The password is validated according to custom rules.
func InputPassword(
cliCtx *cli.Context,
passwordFileFlag *cli.StringFlag,
promptText string,
confirmPassword bool,
passwordValidator func(input string) error,
) (string, error) {
if cliCtx.IsSet(passwordFileFlag.Name) {
passwordFilePathInput := cliCtx.String(passwordFileFlag.Name)
data, err := file.ReadFileAsBytes(passwordFilePathInput)
if err != nil {
return "", errors.Wrap(err, "could not read file as bytes")
}
enteredPassword := strings.TrimRight(string(data), "\r\n")
if err := passwordValidator(enteredPassword); err != nil {
return "", errors.Wrap(err, "password did not pass validation")
}
return enteredPassword, nil
}
var hasValidPassword bool
var walletPassword string
var err error
for !hasValidPassword {
walletPassword, err = prompt.PasswordPrompt(promptText, passwordValidator)
if err != nil {
return "", fmt.Errorf("could not read account password: %w", err)
}
if confirmPassword {
passwordConfirmation, err := prompt.PasswordPrompt(ConfirmPasswordPromptText, passwordValidator)
if err != nil {
return "", fmt.Errorf("could not read password confirmation: %w", err)
}
if walletPassword != passwordConfirmation {
log.Error("Passwords do not match")
continue
}
hasValidPassword = true
} else {
return walletPassword, nil
}
}
return walletPassword, nil
}