/
accounts_import.go
334 lines (315 loc) · 10.9 KB
/
accounts_import.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
package accounts
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/cmd/validator/flags"
"github.com/prysmaticlabs/prysm/crypto/bls"
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
"github.com/prysmaticlabs/prysm/io/file"
"github.com/prysmaticlabs/prysm/io/prompt"
"github.com/prysmaticlabs/prysm/validator/accounts/iface"
"github.com/prysmaticlabs/prysm/validator/accounts/userprompt"
"github.com/prysmaticlabs/prysm/validator/accounts/wallet"
"github.com/prysmaticlabs/prysm/validator/keymanager"
"github.com/prysmaticlabs/prysm/validator/keymanager/imported"
"github.com/urfave/cli/v2"
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
)
var derivationPathRegex = regexp.MustCompile(`m_12381_3600_(\d+)_(\d+)_(\d+)`)
// byDerivationPath implements sort.Interface based on a
// derivation path present in a keystore filename, if any. This
// will allow us to sort filenames such as keystore-m_12381_3600_1_0_0.json
// in a directory and import them nicely in order of the derivation path.
type byDerivationPath []string
// Len is the number of elements in the collection.
func (fileNames byDerivationPath) Len() int { return len(fileNames) }
// Less reports whether the element with index i must sort before the element with index j.
func (fileNames byDerivationPath) Less(i, j int) bool {
// We check if file name at index i has a derivation path
// in the filename. If it does not, then it is not less than j, and
// we should swap it towards the end of the sorted list.
if !derivationPathRegex.MatchString(fileNames[i]) {
return false
}
derivationPathA := derivationPathRegex.FindString(fileNames[i])
derivationPathB := derivationPathRegex.FindString(fileNames[j])
if derivationPathA == "" {
return false
}
if derivationPathB == "" {
return true
}
a, err := strconv.Atoi(accountIndexFromFileName(derivationPathA))
if err != nil {
return false
}
b, err := strconv.Atoi(accountIndexFromFileName(derivationPathB))
if err != nil {
return false
}
return a < b
}
// Swap swaps the elements with indexes i and j.
func (fileNames byDerivationPath) Swap(i, j int) {
fileNames[i], fileNames[j] = fileNames[j], fileNames[i]
}
// ImportAccountsConfig defines values to run the import accounts function.
type ImportAccountsConfig struct {
Keystores []*keymanager.Keystore
Keymanager *imported.Keymanager
AccountPassword string
}
// ImportAccountsCli can import external, EIP-2335 compliant keystore.json files as
// new accounts into the Prysm validator wallet. This uses the CLI to extract
// values necessary to run the function.
func ImportAccountsCli(cliCtx *cli.Context) error {
w, err := wallet.OpenWalletOrElseCli(cliCtx, func(cliCtx *cli.Context) (*wallet.Wallet, error) {
walletDir, err := userprompt.InputDirectory(cliCtx, userprompt.WalletDirPromptText, flags.WalletDirFlag)
if err != nil {
return nil, err
}
exists, err := wallet.Exists(walletDir)
if err != nil {
return nil, errors.Wrap(err, wallet.CheckExistsErrMsg)
}
if exists {
isValid, err := wallet.IsValid(walletDir)
if err != nil {
return nil, errors.Wrap(err, wallet.CheckValidityErrMsg)
}
if !isValid {
return nil, errors.New(wallet.InvalidWalletErrMsg)
}
walletPassword, err := wallet.InputPassword(
cliCtx,
flags.WalletPasswordFileFlag,
wallet.PasswordPromptText,
false, /* Do not confirm password */
wallet.ValidateExistingPass,
)
if err != nil {
return nil, err
}
return wallet.OpenWallet(cliCtx.Context, &wallet.Config{
WalletDir: walletDir,
WalletPassword: walletPassword,
})
}
cfg, err := extractWalletCreationConfigFromCli(cliCtx, keymanager.Imported)
if err != nil {
return nil, err
}
w := wallet.New(&wallet.Config{
KeymanagerKind: cfg.WalletCfg.KeymanagerKind,
WalletDir: cfg.WalletCfg.WalletDir,
WalletPassword: cfg.WalletCfg.WalletPassword,
})
if err = createImportedKeymanagerWallet(cliCtx.Context, w); err != nil {
return nil, errors.Wrap(err, "could not create keymanager")
}
log.WithField("wallet-path", cfg.WalletCfg.WalletDir).Info(
"Successfully created new wallet",
)
return w, nil
})
if err != nil {
return errors.Wrap(err, "could not initialize wallet")
}
km, err := w.InitializeKeymanager(cliCtx.Context, iface.InitKeymanagerConfig{ListenForChanges: false})
if err != nil {
return err
}
k, ok := km.(*imported.Keymanager)
if !ok {
return errors.New("only imported wallets can import more keystores")
}
// Check if the user wishes to import a one-off, private key directly
// as an account into the Prysm validator.
if cliCtx.IsSet(flags.ImportPrivateKeyFileFlag.Name) {
return importPrivateKeyAsAccount(cliCtx, w, k)
}
keysDir, err := userprompt.InputDirectory(cliCtx, userprompt.ImportKeysDirPromptText, flags.KeysDirFlag)
if err != nil {
return errors.Wrap(err, "could not parse keys directory")
}
// Consider that the keysDir might be a path to a specific file and handle accordingly.
isDir, err := file.HasDir(keysDir)
if err != nil {
return errors.Wrap(err, "could not determine if path is a directory")
}
keystoresImported := make([]*keymanager.Keystore, 0)
if isDir {
files, err := ioutil.ReadDir(keysDir)
if err != nil {
return errors.Wrap(err, "could not read dir")
}
if len(files) == 0 {
return fmt.Errorf("directory %s has no files, cannot import from it", keysDir)
}
filesInDir := make([]string, 0)
for i := 0; i < len(files); i++ {
if files[i].IsDir() {
continue
}
filesInDir = append(filesInDir, files[i].Name())
}
// Sort the imported keystores by derivation path if they
// specify this value in their filename.
sort.Sort(byDerivationPath(filesInDir))
for _, name := range filesInDir {
keystore, err := readKeystoreFile(cliCtx.Context, filepath.Join(keysDir, name))
if err != nil && strings.Contains(err.Error(), "could not decode keystore json") {
continue
} else if err != nil {
return errors.Wrapf(err, "could not import keystore at path: %s", name)
}
keystoresImported = append(keystoresImported, keystore)
}
} else {
keystore, err := readKeystoreFile(cliCtx.Context, keysDir)
if err != nil {
return errors.Wrap(err, "could not import keystore")
}
keystoresImported = append(keystoresImported, keystore)
}
var accountsPassword string
if cliCtx.IsSet(flags.AccountPasswordFileFlag.Name) {
passwordFilePath := cliCtx.String(flags.AccountPasswordFileFlag.Name)
data, err := ioutil.ReadFile(passwordFilePath) // #nosec G304
if err != nil {
return err
}
accountsPassword = string(data)
} else {
accountsPassword, err = prompt.PasswordPrompt(
"Enter the password for your imported accounts", prompt.NotEmpty,
)
if err != nil {
return fmt.Errorf("could not read account password: %w", err)
}
}
fmt.Println("Importing accounts, this may take a while...")
if err := ImportAccounts(cliCtx.Context, &ImportAccountsConfig{
Keymanager: k,
Keystores: keystoresImported,
AccountPassword: accountsPassword,
}); err != nil {
return err
}
fmt.Printf(
"Successfully imported %s accounts, view all of them by running `accounts list`\n",
au.BrightMagenta(strconv.Itoa(len(keystoresImported))),
)
return nil
}
// ImportAccounts can import external, EIP-2335 compliant keystore.json files as
// new accounts into the Prysm validator wallet.
func ImportAccounts(ctx context.Context, cfg *ImportAccountsConfig) error {
return cfg.Keymanager.ImportKeystores(
ctx,
cfg.Keystores,
cfg.AccountPassword,
)
}
// Imports a one-off file containing a private key as a hex string into
// the Prysm validator's accounts.
func importPrivateKeyAsAccount(cliCtx *cli.Context, wallet *wallet.Wallet, km *imported.Keymanager) error {
privKeyFile := cliCtx.String(flags.ImportPrivateKeyFileFlag.Name)
fullPath, err := file.ExpandPath(privKeyFile)
if err != nil {
return errors.Wrapf(err, "could not expand file path for %s", privKeyFile)
}
if !file.FileExists(fullPath) {
return fmt.Errorf("file %s does not exist", fullPath)
}
privKeyHex, err := ioutil.ReadFile(fullPath) // #nosec G304
if err != nil {
return errors.Wrapf(err, "could not read private key file at path %s", fullPath)
}
privKeyString := string(privKeyHex)
if len(privKeyString) > 2 && strings.Contains(privKeyString, "0x") {
privKeyString = privKeyString[2:] // Strip the 0x prefix, if any.
}
privKeyBytes, err := hex.DecodeString(strings.TrimRight(privKeyString, "\r\n"))
if err != nil {
return errors.Wrap(
err, "could not decode file as hex string, does the file contain a valid hex string?",
)
}
privKey, err := bls.SecretKeyFromBytes(privKeyBytes)
if err != nil {
return errors.Wrap(err, "not a valid BLS private key")
}
keystore, err := createKeystoreFromPrivateKey(privKey, wallet.Password())
if err != nil {
return errors.Wrap(err, "could not encrypt private key into a keystore file")
}
if err := ImportAccounts(
cliCtx.Context,
&ImportAccountsConfig{
Keymanager: km,
AccountPassword: wallet.Password(),
Keystores: []*keymanager.Keystore{keystore},
},
); err != nil {
return errors.Wrap(err, "could not import keystore into wallet")
}
fmt.Printf(
"Imported account with public key %#x, view all accounts by running `accounts list`\n",
au.BrightMagenta(bytesutil.Trunc(privKey.PublicKey().Marshal())),
)
return nil
}
func readKeystoreFile(_ context.Context, keystoreFilePath string) (*keymanager.Keystore, error) {
keystoreBytes, err := ioutil.ReadFile(keystoreFilePath) // #nosec G304
if err != nil {
return nil, errors.Wrap(err, "could not read keystore file")
}
keystoreFile := &keymanager.Keystore{}
if err := json.Unmarshal(keystoreBytes, keystoreFile); err != nil {
return nil, errors.Wrap(err, "could not decode keystore json")
}
if keystoreFile.Pubkey == "" {
return nil, errors.New("could not decode keystore json")
}
return keystoreFile, nil
}
func createKeystoreFromPrivateKey(privKey bls.SecretKey, walletPassword string) (*keymanager.Keystore, error) {
encryptor := keystorev4.New()
id, err := uuid.NewRandom()
if err != nil {
return nil, err
}
cryptoFields, err := encryptor.Encrypt(privKey.Marshal(), walletPassword)
if err != nil {
return nil, errors.Wrapf(
err,
"could not encrypt private key with public key %#x",
privKey.PublicKey().Marshal(),
)
}
return &keymanager.Keystore{
Crypto: cryptoFields,
ID: id.String(),
Version: encryptor.Version(),
Pubkey: fmt.Sprintf("%x", privKey.PublicKey().Marshal()),
Name: encryptor.Name(),
}, nil
}
// Extracts the account index, j, from a derivation path in a file name
// with the format m_12381_3600_j_0_0.
func accountIndexFromFileName(derivationPath string) string {
derivationPath = derivationPath[13:]
accIndexEnd := strings.Index(derivationPath, "_")
return derivationPath[:accIndexEnd]
}