Skip to content

Commit

Permalink
Extract load_mnemonic_arguments_decorator func
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Jan 4, 2023
1 parent 4da5add commit d3fd1da
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
56 changes: 35 additions & 21 deletions staking_deposit/cli/existing_mnemonic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click
from typing import (
Any,
Callable,
)

from staking_deposit.exceptions import ValidationError
Expand All @@ -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:
Expand All @@ -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),
Expand Down
13 changes: 1 addition & 12 deletions staking_deposit/cli/generate_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions staking_deposit/intl/en/cli/generate_keys.json
Original file line number Diff line number Diff line change
@@ -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)",
Expand Down
4 changes: 4 additions & 0 deletions staking_deposit/intl/en/utils/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.**"
}
}
4 changes: 0 additions & 4 deletions staking_deposit/intl/tr/cli/generate_keys.json
Original file line number Diff line number Diff line change
@@ -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)",
Expand Down
4 changes: 4 additions & 0 deletions staking_deposit/intl/tr/utils/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.**"
}
}

0 comments on commit d3fd1da

Please sign in to comment.