-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy pathutils.go
More file actions
215 lines (181 loc) · 6.92 KB
/
utils.go
File metadata and controls
215 lines (181 loc) · 6.92 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
package wallet
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/mitchellh/go-homedir"
"github.com/rocket-pool/rocketpool-go/types"
"github.com/rocket-pool/smartnode/rocketpool-cli/wallet/bip39"
"github.com/rocket-pool/smartnode/shared/services/config"
"github.com/rocket-pool/smartnode/shared/services/passwords"
"github.com/rocket-pool/smartnode/shared/services/rocketpool"
"github.com/rocket-pool/smartnode/shared/types/api"
cliutils "github.com/rocket-pool/smartnode/shared/utils/cli"
hexutils "github.com/rocket-pool/smartnode/shared/utils/hex"
"gopkg.in/yaml.v2"
)
const bold string = "\033[1m"
const unbold string = "\033[0m"
// Generate a secure password with 32 bytes of entropy, encoded as a base64 string.
func generatePassword() string {
randomBytes := make([]byte, 32)
_, err := rand.Read(randomBytes)
if err != nil {
panic("failed to generate random bytes")
}
return base64.RawStdEncoding.EncodeToString(randomBytes)
}
// Prompt for a wallet password
func promptPassword() string {
fmt.Println("Your keystore will be encrypted on disk using a password.")
fmt.Println("You do not need to store or remember this password.")
fmt.Print("You only need the seed phrase (from the next step) to recover your wallet.\n\n")
if cliutils.Confirm("Would you like the password to be automatically generated?") {
fmt.Print("Using randomly-generated password.\n\n")
return generatePassword()
}
for {
password := cliutils.PromptPassword(
"Please enter a password to secure your wallet with:",
fmt.Sprintf("^.{%d,}$", passwords.MinPasswordLength),
fmt.Sprintf("Your password must be at least %d characters long. Please try again:", passwords.MinPasswordLength),
)
confirmation := cliutils.PromptPassword("Please confirm your password:", "^.*$", "")
if password == confirmation {
return password
}
fmt.Print("Password confirmation does not match.\n\n")
}
}
// Prompt for a recovery mnemonic phrase
func promptMnemonic() string {
for {
lengthInput := cliutils.Prompt(
"Please enter the "+bold+"number"+unbold+" 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() == false {
prompt := fmt.Sprintf("Enter %sWord Number %d%s of your mnemonic:", bold, i+1, unbold)
word := cliutils.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(rp *rocketpool.Client, cfg *config.RocketPoolConfig, testOnly bool) (string, error) {
// Check for the custom key directory
datapath, err := homedir.Expand(cfg.Smartnode.DataPath.Value.(string))
if err != nil {
return "", fmt.Errorf("error expanding data directory: %w", err)
}
customKeyDir := filepath.Join(datapath, "custom-keys")
info, err := os.Stat(customKeyDir)
if os.IsNotExist(err) || !info.IsDir() {
return "", nil
}
// Get the custom keystore files
files, err := ioutil.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 Smartnode 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", colorRed, colorReset)
if !cliutils.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 := []types.ValidatorPubkey{}
for _, file := range files {
// Read the file
bytes, err := ioutil.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.\nYou 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.\n")
// Get the passwords for each one
pubkeyPasswords := map[string]string{}
for _, pubkey := range customPubkeys {
password := cliutils.PromptPassword(
fmt.Sprintf("Please enter the password that the keystore for %s was encrypted with:", pubkey.Hex()), "^.*$", "",
)
formattedPubkey := strings.ToUpper(hexutils.RemovePrefix(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 := filepath.Join(datapath, "custom-key-passwords")
err = ioutil.WriteFile(passwordFile, fileBytes, 0600)
if err != nil {
return "", fmt.Errorf("error writing keystore passwords file: %w", err)
}
return passwordFile, nil
}
// Deletes the custom key password file
func deleteCustomKeyPasswordFile(passwordFile string) error {
_, err := os.Stat(passwordFile)
if os.IsNotExist(err) {
return nil
}
err = os.Remove(passwordFile)
return err
}