diff --git a/staking_deposit/cli/existing_mnemonic.py b/staking_deposit/cli/existing_mnemonic.py index 97e090f6..8aa87394 100644 --- a/staking_deposit/cli/existing_mnemonic.py +++ b/staking_deposit/cli/existing_mnemonic.py @@ -1,6 +1,7 @@ import click from typing import ( Any, + Callable, ) from staking_deposit.exceptions import ValidationError @@ -22,6 +23,39 @@ ) +def load_mnemonic_arguments_decorator(function: Callable[..., Any]) -> Callable[..., Any]: + ''' + This is a decorator that, when applied to a parent-command, implements the + to obtain the necessary arguments for the generate_keys() subcommand. + ''' + decorators = [ + jit_option( + callback=validate_mnemonic, + help=lambda: load_text(['arg_mnemonic', 'help'], func='existing_mnemonic'), + param_decls='--mnemonic', + prompt=lambda: load_text(['arg_mnemonic', 'prompt'], func='existing_mnemonic'), + type=str, + ), + jit_option( + callback=captive_prompt_callback( + lambda x: x, + lambda: load_text(['arg_mnemonic_password', 'prompt'], func='existing_mnemonic'), + lambda: load_text(['arg_mnemonic_password', 'confirm'], func='existing_mnemonic'), + lambda: load_text(['arg_mnemonic_password', 'mismatch'], func='existing_mnemonic'), + True, + ), + default='', + help=lambda: load_text(['arg_mnemonic_password', 'help'], func='existing_mnemonic'), + hidden=True, + param_decls='--mnemonic-password', + prompt=False, + ), + ] + for decorator in reversed(decorators): + function = decorator(function) + return function + + def validate_mnemonic(ctx: click.Context, param: Any, mnemonic: str) -> str: mnemonic = reconstruct_mnemonic(mnemonic, WORD_LISTS_PATH) if mnemonic is not None: @@ -33,27 +67,7 @@ def validate_mnemonic(ctx: click.Context, param: Any, mnemonic: str) -> str: @click.command( help=load_text(['arg_existing_mnemonic', 'help'], func='existing_mnemonic'), ) -@jit_option( - callback=validate_mnemonic, - help=lambda: load_text(['arg_mnemonic', 'help'], func='existing_mnemonic'), - param_decls='--mnemonic', - prompt=lambda: load_text(['arg_mnemonic', 'prompt'], func='existing_mnemonic'), - type=str, -) -@jit_option( - callback=captive_prompt_callback( - lambda x: x, - lambda: load_text(['arg_mnemonic_password', 'prompt'], func='existing_mnemonic'), - lambda: load_text(['arg_mnemonic_password', 'confirm'], func='existing_mnemonic'), - lambda: load_text(['arg_mnemonic_password', 'mismatch'], func='existing_mnemonic'), - True, - ), - default='', - help=lambda: load_text(['arg_mnemonic_password', 'help'], func='existing_mnemonic'), - hidden=True, - param_decls='--mnemonic-password', - prompt=False, -) +@load_mnemonic_arguments_decorator @jit_option( callback=captive_prompt_callback( lambda num: validate_int_range(num, 0, 2**32), diff --git a/staking_deposit/cli/generate_keys.py b/staking_deposit/cli/generate_keys.py index 4caa3b7b..e28d9ae8 100644 --- a/staking_deposit/cli/generate_keys.py +++ b/staking_deposit/cli/generate_keys.py @@ -6,7 +6,6 @@ ) from eth_typing import HexAddress -from eth_utils import is_hex_address, to_normalized_address from staking_deposit.credentials import ( CredentialList, @@ -16,6 +15,7 @@ verify_deposit_data_json, validate_int_range, validate_password_strength, + validate_eth1_withdrawal_address, ) from staking_deposit.utils.constants import ( MAX_DEPOSIT_AMOUNT, @@ -43,17 +43,6 @@ def get_password(text: str) -> str: return click.prompt(text, hide_input=True, show_default=False, type=str) -def validate_eth1_withdrawal_address(cts: click.Context, param: Any, address: str) -> HexAddress: - if address is None: - return None - if not is_hex_address(address): - raise ValueError(load_text(['err_invalid_ECDSA_hex_addr'])) - - normalized_address = to_normalized_address(address) - click.echo('\n%s\n' % load_text(['msg_ECDSA_addr_withdrawal'])) - return normalized_address - - def generate_keys_arguments_decorator(function: Callable[..., Any]) -> Callable[..., Any]: ''' This is a decorator that, when applied to a parent-command, implements the diff --git a/staking_deposit/intl/en/cli/generate_keys.json b/staking_deposit/intl/en/cli/generate_keys.json index c1f33b3b..a6a71f30 100644 --- a/staking_deposit/intl/en/cli/generate_keys.json +++ b/staking_deposit/intl/en/cli/generate_keys.json @@ -1,8 +1,4 @@ { - "validate_eth1_withdrawal_address": { - "err_invalid_ECDSA_hex_addr": "The given Eth1 address is not in hexadecimal encoded form.", - "msg_ECDSA_addr_withdrawal": "**[Warning] you are setting an Eth1 address as your withdrawal address. Please ensure that you have control over this address.**" - }, "generate_keys_arguments_decorator": { "num_validators": { "help": "The number of new validator keys you want to generate (you can always generate more later)", diff --git a/staking_deposit/intl/en/utils/validation.json b/staking_deposit/intl/en/utils/validation.json index 50145b22..80a24110 100644 --- a/staking_deposit/intl/en/utils/validation.json +++ b/staking_deposit/intl/en/utils/validation.json @@ -10,5 +10,9 @@ }, "validate_choice": { "err_invalid_choice": "That is not one of the valid choices. Please retype your choice." + }, + "validate_eth1_withdrawal_address": { + "err_invalid_ECDSA_hex_addr": "The given Eth1 address is not in hexadecimal encoded form.", + "msg_ECDSA_addr_withdrawal": "**[Warning] you are setting an Eth1 address as your withdrawal address. Please ensure that you have control over this address.**" } } \ No newline at end of file diff --git a/staking_deposit/intl/tr/cli/generate_keys.json b/staking_deposit/intl/tr/cli/generate_keys.json index bcfc3bb5..975a3e4f 100644 --- a/staking_deposit/intl/tr/cli/generate_keys.json +++ b/staking_deposit/intl/tr/cli/generate_keys.json @@ -1,8 +1,4 @@ { - "validate_eth1_withdrawal_address": { - "err_invalid_ECDSA_hex_addr": "Girilen Eth1 adresi onaltılık sistemde kodlanmamıştır.", - "msg_ECDSA_addr_withdrawal": "**[Uyarı] bir Eth1 adresini varlık çekme adresi olarak giriyorsunuz. Lütfen bu adresin kontrolünün sizde olduğundan emin olun.**" - }, "generate_keys_arguments_decorator": { "num_validators": { "help": "Oluşturmak istediğiniz yeni doğrulayıcı anahtarlarının sayısını giriniz (daha fazlasını daha sonra da oluşturabilirsiniz)", diff --git a/staking_deposit/intl/tr/utils/validation.json b/staking_deposit/intl/tr/utils/validation.json index 73fcef98..bdbf3d60 100644 --- a/staking_deposit/intl/tr/utils/validation.json +++ b/staking_deposit/intl/tr/utils/validation.json @@ -10,5 +10,9 @@ }, "validate_choice": { "err_invalid_choice": "Geçerli bir seçim değil. Lütfen seçiminizi tekrar girin." + }, + "validate_eth1_withdrawal_address": { + "err_invalid_ECDSA_hex_addr": "Girilen Eth1 adresi onaltılık sistemde kodlanmamıştır.", + "msg_ECDSA_addr_withdrawal": "**[Uyarı] bir Eth1 adresini varlık çekme adresi olarak giriyorsunuz. Lütfen bu adresin kontrolünün sizde olduğundan emin olun.**" } }