|
| 1 | +package v2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/manifoldco/promptui" |
| 11 | + "github.com/pkg/errors" |
| 12 | + "github.com/prysmaticlabs/prysm/shared/bls" |
| 13 | + "github.com/prysmaticlabs/prysm/shared/fileutil" |
| 14 | + "github.com/prysmaticlabs/prysm/shared/params" |
| 15 | + "github.com/prysmaticlabs/prysm/shared/promptutil" |
| 16 | + "github.com/prysmaticlabs/prysm/validator/flags" |
| 17 | + v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2" |
| 18 | + "github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived" |
| 19 | + "github.com/urfave/cli/v2" |
| 20 | +) |
| 21 | + |
| 22 | +// SendDeposit transaction for user specified accounts via an interactive |
| 23 | +// CLI process or via command-line flags. |
| 24 | +func SendDeposit(cliCtx *cli.Context) error { |
| 25 | + // Read the wallet from the specified path. |
| 26 | + wallet, err := OpenWallet(cliCtx) |
| 27 | + if errors.Is(err, ErrNoWalletFound) { |
| 28 | + return errors.Wrap(err, "no wallet found at path, create a new wallet with wallet-v2 create") |
| 29 | + } else if err != nil { |
| 30 | + return errors.Wrap(err, "could not open wallet") |
| 31 | + } |
| 32 | + keymanager, err := wallet.InitializeKeymanager( |
| 33 | + cliCtx, |
| 34 | + true, /* skip mnemonic confirm */ |
| 35 | + ) |
| 36 | + if err != nil && strings.Contains(err.Error(), "invalid checksum") { |
| 37 | + return errors.New("wrong wallet password entered") |
| 38 | + } |
| 39 | + if err != nil { |
| 40 | + return errors.Wrap(err, "could not initialize keymanager") |
| 41 | + } |
| 42 | + switch wallet.KeymanagerKind() { |
| 43 | + case v2keymanager.Derived: |
| 44 | + km, ok := keymanager.(*derived.Keymanager) |
| 45 | + if !ok { |
| 46 | + return errors.New("could not assert keymanager interface to concrete type") |
| 47 | + } |
| 48 | + depositConfig, err := createDepositConfig(cliCtx, km) |
| 49 | + if err != nil { |
| 50 | + return errors.Wrap(err, "could not initialize deposit config") |
| 51 | + } |
| 52 | + if err := km.SendDepositTx(depositConfig); err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + default: |
| 56 | + return errors.New("only Prysm HD wallets support sending deposits at the moment") |
| 57 | + } |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func createDepositConfig(cliCtx *cli.Context, km *derived.Keymanager) (*derived.SendDepositConfig, error) { |
| 62 | + pubKeysBytes, err := km.FetchValidatingPublicKeys(context.Background()) |
| 63 | + if err != nil { |
| 64 | + return nil, errors.Wrap(err, "could not fetch validating public keys") |
| 65 | + } |
| 66 | + pubKeys := make([]bls.PublicKey, len(pubKeysBytes)) |
| 67 | + for i, pk := range pubKeysBytes { |
| 68 | + pubKeys[i], err = bls.PublicKeyFromBytes(pk[:]) |
| 69 | + if err != nil { |
| 70 | + return nil, errors.Wrap(err, "could not parse BLS public key") |
| 71 | + } |
| 72 | + } |
| 73 | + // Allow the user to interactively select the accounts to backup or optionally |
| 74 | + // provide them via cli flags as a string of comma-separated, hex strings. If the user has |
| 75 | + // selected to deposit all accounts, we skip this part. |
| 76 | + if !cliCtx.IsSet(flags.DepositPublicKeysFlag.Name) { |
| 77 | + pubKeys, err = filterPublicKeysFromUserInput( |
| 78 | + cliCtx, |
| 79 | + flags.DepositPublicKeysFlag, |
| 80 | + pubKeysBytes, |
| 81 | + selectAccountsDepositPromptText, |
| 82 | + ) |
| 83 | + if err != nil { |
| 84 | + return nil, errors.Wrap(err, "could not filter validating public keys for deposit") |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + web3Provider := cliCtx.String(flags.HTTPWeb3ProviderFlag.Name) |
| 89 | + // Enter the web3provider information. |
| 90 | + if web3Provider == "" { |
| 91 | + web3Provider, err = promptutil.DefaultAndValidatePrompt( |
| 92 | + "Enter the HTTP address of your eth1 endpoint for the Goerli testnet", |
| 93 | + cliCtx.String(flags.HTTPWeb3ProviderFlag.Name), |
| 94 | + func(input string) error { |
| 95 | + return nil |
| 96 | + }, |
| 97 | + ) |
| 98 | + if err != nil { |
| 99 | + return nil, errors.Wrap(err, "could not validate web3 provider endpoint") |
| 100 | + } |
| 101 | + } |
| 102 | + depositDelaySeconds := cliCtx.Int(flags.DepositDelaySecondsFlag.Name) |
| 103 | + config := &derived.SendDepositConfig{ |
| 104 | + DepositContractAddress: cliCtx.String(flags.DepositContractAddressFlag.Name), |
| 105 | + DepositDelaySeconds: time.Duration(depositDelaySeconds) * time.Second, |
| 106 | + DepositPublicKeys: pubKeys, |
| 107 | + Web3Provider: web3Provider, |
| 108 | + } |
| 109 | + |
| 110 | + if !cliCtx.Bool(flags.SkipDepositConfirmationFlag.Name) { |
| 111 | + confirmDepositPrompt := "You are about to send %d ETH into contract address %s for %d eth2 validator accounts. " + |
| 112 | + "Are you sure you want to do this? Enter the words 'yes I do' to continue" |
| 113 | + gweiPerEth := params.BeaconConfig().GweiPerEth |
| 114 | + ethDepositTotal := uint64(len(pubKeys)) * params.BeaconConfig().MaxEffectiveBalance / gweiPerEth |
| 115 | + if _, err := promptutil.ValidatePrompt( |
| 116 | + os.Stdin, |
| 117 | + fmt.Sprintf(confirmDepositPrompt, ethDepositTotal, config.DepositContractAddress, len(pubKeys)), |
| 118 | + func(input string) error { |
| 119 | + if input != "yes I do" { |
| 120 | + return errors.New("please enter 'yes I do' or exit") |
| 121 | + } |
| 122 | + return nil |
| 123 | + }, |
| 124 | + ); err != nil { |
| 125 | + return nil, errors.Wrap(err, "could not confirm deposit acknowledgement") |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + // If the user passes any of the specified flags, we read them and return the |
| 130 | + // config struct directly, bypassing any CLI input. |
| 131 | + hasPrivateKey := cliCtx.IsSet(flags.Eth1PrivateKeyFileFlag.Name) |
| 132 | + hasEth1Keystore := cliCtx.IsSet(flags.Eth1KeystoreUTCPathFlag.Name) |
| 133 | + if hasPrivateKey || hasEth1Keystore { |
| 134 | + if hasPrivateKey { |
| 135 | + fileBytes, err := fileutil.ReadFileAsBytes(cliCtx.String(flags.Eth1PrivateKeyFileFlag.Name)) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + config.Eth1PrivateKey = strings.TrimRight(string(fileBytes), "\r\n") |
| 140 | + } |
| 141 | + return config, nil |
| 142 | + } |
| 143 | + |
| 144 | + usePrivateKeyPrompt := "Inputting an eth1 private key hex string directly" |
| 145 | + useEth1KeystorePrompt := "Using an encrypted eth1 keystore UTC file" |
| 146 | + eth1Prompt := promptui.Select{ |
| 147 | + Label: "Select how you wish to sign your eth1 transaction", |
| 148 | + Items: []string{ |
| 149 | + usePrivateKeyPrompt, |
| 150 | + useEth1KeystorePrompt, |
| 151 | + }, |
| 152 | + } |
| 153 | + _, selection, err := eth1Prompt.Run() |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + // If the user wants to proceed by inputting their private key directly, ask for it securely. |
| 158 | + if selection == usePrivateKeyPrompt { |
| 159 | + eth1PrivateKeyString, err := promptutil.PasswordPrompt( |
| 160 | + "Enter the hex string value of your eth1 private key", |
| 161 | + promptutil.NotEmpty, |
| 162 | + ) |
| 163 | + if err != nil { |
| 164 | + return nil, errors.Wrap(err, "could not read eth1 private key string") |
| 165 | + } |
| 166 | + config.Eth1PrivateKey = strings.TrimRight(eth1PrivateKeyString, "\r\n") |
| 167 | + } else if selection == useEth1KeystorePrompt { |
| 168 | + // Otherwise, ask the user for paths to their keystore UTC file and its password. |
| 169 | + eth1KeystoreUTCFile, err := promptutil.DefaultAndValidatePrompt( |
| 170 | + "Enter the file path for your encrypted, eth1 keystore-utc file", |
| 171 | + cliCtx.String(flags.Eth1KeystoreUTCPathFlag.Name), |
| 172 | + func(input string) error { |
| 173 | + return nil |
| 174 | + }, |
| 175 | + ) |
| 176 | + if err != nil { |
| 177 | + return nil, errors.Wrap(err, "could not read eth1 keystore UTC path") |
| 178 | + } |
| 179 | + eth1KeystorePasswordFile, err := inputWeakPassword( |
| 180 | + cliCtx, |
| 181 | + flags.Eth1KeystorePasswordFileFlag, |
| 182 | + "Enter the file path a .txt file containing your eth1 keystore password", |
| 183 | + ) |
| 184 | + if err != nil { |
| 185 | + return nil, errors.Wrap(err, "could not read eth1 keystore password file path") |
| 186 | + } |
| 187 | + config.Eth1KeystoreUTCFile = eth1KeystoreUTCFile |
| 188 | + config.Eth1KeystorePasswordFile = eth1KeystorePasswordFile |
| 189 | + } |
| 190 | + return config, nil |
| 191 | +} |
0 commit comments