github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/valkeystore.go (about) 1 package launcher 2 3 import ( 4 "crypto/ecdsa" 5 "fmt" 6 "io/ioutil" 7 "strings" 8 9 "github.com/unicornultrafoundation/go-u2u/cmd/utils" 10 "github.com/unicornultrafoundation/go-u2u/crypto" 11 "github.com/unicornultrafoundation/go-u2u/log" 12 "github.com/unicornultrafoundation/go-u2u/node" 13 cli "gopkg.in/urfave/cli.v1" 14 15 "github.com/unicornultrafoundation/go-u2u/native/validatorpk" 16 "github.com/unicornultrafoundation/go-u2u/valkeystore" 17 ) 18 19 func addFakeValidatorKey(ctx *cli.Context, key *ecdsa.PrivateKey, pubkey validatorpk.PubKey, valKeystore valkeystore.RawKeystoreI) { 20 // add fake validator key 21 if key != nil && !valKeystore.Has(pubkey) { 22 err := valKeystore.Add(pubkey, crypto.FromECDSA(key), validatorpk.FakePassword) 23 if err != nil { 24 utils.Fatalf("Failed to add fake validator key: %v", err) 25 } 26 } 27 } 28 29 func getValKeystoreDir(cfg node.Config) string { 30 _, _, keydir, err := cfg.AccountConfig() 31 if err != nil { 32 utils.Fatalf("Failed to setup account config: %v", err) 33 } 34 return keydir 35 } 36 37 // makeValidatorPasswordList reads password lines from the file specified by the global --validator.password flag. 38 func makeValidatorPasswordList(ctx *cli.Context) []string { 39 if path := ctx.GlobalString(validatorPasswordFlag.Name); path != "" { 40 text, err := ioutil.ReadFile(path) 41 if err != nil { 42 utils.Fatalf("Failed to read password file: %v", err) 43 } 44 lines := strings.Split(string(text), "\n") 45 // Sanitise DOS line endings. 46 for i := range lines { 47 lines[i] = strings.TrimRight(lines[i], "\r") 48 } 49 return lines 50 } 51 if ctx.GlobalIsSet(FakeNetFlag.Name) { 52 return []string{validatorpk.FakePassword} 53 } 54 return nil 55 } 56 57 func unlockValidatorKey(ctx *cli.Context, pubKey validatorpk.PubKey, valKeystore valkeystore.KeystoreI) error { 58 if !valKeystore.Has(pubKey) { 59 return valkeystore.ErrNotFound 60 } 61 var err error 62 for trials := 0; trials < 3; trials++ { 63 prompt := fmt.Sprintf("Unlocking validator key %s | Attempt %d/%d", pubKey.String(), trials+1, 3) 64 password := getPassPhrase(prompt, false, 0, makeValidatorPasswordList(ctx)) 65 err = valKeystore.Unlock(pubKey, password) 66 if err == nil { 67 log.Info("Unlocked validator key", "pubkey", pubKey.String()) 68 return nil 69 } 70 if err.Error() != "could not decrypt key with given password" { 71 return err 72 } 73 } 74 // All trials expended to unlock account, bail out 75 return err 76 }