-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathutils.go
More file actions
235 lines (207 loc) · 8.36 KB
/
utils.go
File metadata and controls
235 lines (207 loc) · 8.36 KB
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
package wallet
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/goccy/go-json"
"github.com/rocket-pool/node-manager-core/beacon"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v2"
"github.com/rocket-pool/node-manager-core/utils/input"
"github.com/rocket-pool/smartnode/v2/rocketpool-cli/commands/wallet/bip39"
"github.com/rocket-pool/smartnode/v2/rocketpool-cli/utils"
"github.com/rocket-pool/smartnode/v2/rocketpool-cli/utils/terminal"
"github.com/rocket-pool/smartnode/v2/shared/config"
"github.com/rocket-pool/smartnode/v2/shared/types/api"
)
var (
PasswordFlag *cli.StringFlag = &cli.StringFlag{
Name: "password",
Aliases: []string{"p"},
Usage: "The password to secure the wallet with (if not already set)",
}
SavePasswordFlag *cli.StringFlag = &cli.StringFlag{
Name: "save-password",
Aliases: []string{"s"},
Usage: "Save the node wallet password to disk, so the wallet can be automatically reloaded upon starting up",
}
derivationPathFlag *cli.StringFlag = &cli.StringFlag{
Name: "derivation-path",
Aliases: []string{"d"},
Usage: "Specify the derivation path for the wallet.\nOmit this flag (or leave it blank) for the default of \"m/44'/60'/0'/0/%d\" (where %d is the index).\nSet this to \"ledgerLive\" to use Ledger Live's path of \"m/44'/60'/%d/0/0\".\nSet this to \"mew\" to use MyEtherWallet's path of \"m/44'/60'/0'/%d\".\nFor custom paths, simply enter them here.",
}
walletIndexFlag *cli.Uint64Flag = &cli.Uint64Flag{
Name: "wallet-index",
Aliases: []string{"i"},
Usage: "Specify the index to use with the derivation path when recovering your wallet",
Value: 0,
}
mnemonicFlag *cli.StringFlag = &cli.StringFlag{
Name: "mnemonic",
Aliases: []string{"m"},
Usage: "The mnemonic phrase to recover the wallet from",
}
skipValidatorRecoveryFlag *cli.BoolFlag = &cli.BoolFlag{
Name: "skip-validator-key-recovery",
Aliases: []string{"k"},
Usage: "Recover the node wallet, but do not regenerate its validator keys",
}
addressFlag *cli.StringFlag = &cli.StringFlag{
Name: "address",
Aliases: []string{"a"},
Usage: "If you are recovering a wallet that was not generated by the Smartnode and don't know the derivation path or index of it, enter the address here. The Smartnode will search through its library of paths and indices to try to find it.",
}
enablePartialRebuild = &cli.StringSliceFlag{
Name: "enable-partial-rebuild",
Aliases: []string{"p"},
Usage: "Allows the wallet rebuild process to partially succeed, responding with public keys for successfully rebuilt targets and errors for rebuild failures",
}
)
// Prompt for a new wallet password
func PromptNewPassword() string {
for {
password := utils.PromptPassword(
"Please enter a password to secure your wallet with:",
fmt.Sprintf("^.{%d,}$", input.MinPasswordLength),
fmt.Sprintf("Your password must be at least %d characters long. Please try again:", input.MinPasswordLength),
)
confirmation := utils.PromptPassword("Please confirm your password:", "^.*$", "")
if password == confirmation {
return password
}
fmt.Println("Password confirmation does not match.")
fmt.Println("")
}
}
// Prompt for the password to a wallet that already exists
func PromptExistingPassword() string {
for {
password := utils.PromptPassword(
"Please enter the password your wallet was originally secured with:",
fmt.Sprintf("^.{%d,}$", input.MinPasswordLength),
fmt.Sprintf("Your password must be at least %d characters long. Please try again:", input.MinPasswordLength),
)
return password
}
}
// Prompt for a recovery mnemonic phrase
func PromptMnemonic() string {
for {
lengthInput := utils.Prompt(
"Please enter the "+terminal.ColorBold+"number"+terminal.ColorReset+" of words in your mnemonic phrase (24 by default):",
"^[1-9][0-9]*$",
"Please enter a valid number.")
length, err := strconv.Atoi(lengthInput)
if err != nil {
fmt.Println("Please enter a valid number.")
continue
}
mv := bip39.Create(length)
if mv == nil {
fmt.Println("Please enter a valid mnemonic length.")
continue
}
i := 0
for !mv.Filled() {
prompt := fmt.Sprintf("Enter %sWord Number %d%s of your mnemonic:", terminal.ColorBold, i+1, terminal.ColorReset)
word := utils.PromptPassword(prompt, "^[a-zA-Z]+$", "Please enter a single word only.")
if err := mv.AddWord(strings.ToLower(word)); err != nil {
fmt.Println("Inputted word not valid, please retry.")
continue
}
i++
}
mnemonic, err := mv.Finalize()
if err != nil {
fmt.Printf("Error validating mnemonic: %s\n", err)
fmt.Println("Please try again.")
fmt.Println("")
continue
}
return mnemonic
}
}
// Confirm a recovery mnemonic phrase
func confirmMnemonic(mnemonic string) {
for {
fmt.Println("Please enter your mnemonic phrase to confirm.")
confirmation := PromptMnemonic()
if mnemonic == confirmation {
return
}
fmt.Println("The mnemonic phrase you entered does not match your recovery phrase. Please try again.")
fmt.Println("")
}
}
// Check for custom keys, prompt for their passwords, and store them in the custom keys file
func promptForCustomKeyPasswords(cfg *config.SmartNodeConfig, testOnly bool) (string, error) {
// Check for the custom key directory
customKeyDir := cfg.GetCustomKeyPath()
info, err := os.Stat(customKeyDir)
if os.IsNotExist(err) {
return "", nil
} else if err != nil {
return "", fmt.Errorf("error checking for custom keystore directory [%s]: %w", customKeyDir, err)
} else if !info.IsDir() {
return "", fmt.Errorf("custom keystore path [%s] is not a directory", customKeyDir)
}
// Get the custom keystore files
files, err := os.ReadDir(customKeyDir)
if err != nil {
return "", fmt.Errorf("error enumerating custom keystores: %w", err)
}
if len(files) == 0 {
return "", nil
}
// Prompt the user with a warning message
if !testOnly {
fmt.Printf("%sWARNING:\nThe Smart Node has detected that you have custom (externally-derived) validator keys for your minipools.\nIf these keys were actively used for validation by a service such as Allnodes, you MUST CONFIRM WITH THAT SERVICE that they have stopped validating and disabled those keys, and will NEVER validate with them again.\nOtherwise, you may both run the same keys at the same time which WILL RESULT IN YOUR VALIDATORS BEING SLASHED.%s\n\n", terminal.ColorRed, terminal.ColorReset)
if !utils.Confirm("Please confirm that you have coordinated with the service that was running your minipool validators previously to ensure they have STOPPED validation for your minipools, will NEVER start them again, and you have manually confirmed on a Blockchain explorer such as https://beaconcha.in that your minipools are no longer attesting.") {
fmt.Println("Cancelled.")
os.Exit(0)
}
}
// Get the pubkeys for the custom keystores
customPubkeys := []beacon.ValidatorPubkey{}
for _, file := range files {
// Read the file
bytes, err := os.ReadFile(filepath.Join(customKeyDir, file.Name()))
if err != nil {
return "", fmt.Errorf("error reading custom keystore %s: %w", file.Name(), err)
}
// Deserialize it
keystore := api.ValidatorKeystore{}
err = json.Unmarshal(bytes, &keystore)
if err != nil {
return "", fmt.Errorf("error deserializing custom keystore %s: %w", file.Name(), err)
}
customPubkeys = append(customPubkeys, keystore.Pubkey)
}
// Notify the user
fmt.Println("It looks like you have some custom keystores for your minipool's validators.")
fmt.Println("You will be prompted for the passwords each one was encrypted with, so they can be loaded into the Validator Client that Rocket Pool manages for you.")
fmt.Println()
// Get the passwords for each one
pubkeyPasswords := map[string]string{}
for _, pubkey := range customPubkeys {
password := utils.PromptPassword(
fmt.Sprintf("Please enter the password that the keystore for %s was encrypted with:", pubkey.HexWithPrefix()), "^.*$", "",
)
formattedPubkey := strings.ToUpper(pubkey.Hex())
pubkeyPasswords[formattedPubkey] = password
fmt.Println()
}
// Store them in the file
fileBytes, err := yaml.Marshal(pubkeyPasswords)
if err != nil {
return "", fmt.Errorf("error serializing keystore passwords file: %w", err)
}
passwordFile := cfg.GetCustomKeyPasswordFilePath()
err = os.WriteFile(passwordFile, fileBytes, 0600)
if err != nil {
return "", fmt.Errorf("error writing keystore passwords file: %w", err)
}
return passwordFile, nil
}