staking-deposit-cli/staking_deposit/cli/existing_mnemonic.py

73 lines
2.5 KiB
Python
Raw Normal View History

2020-08-24 11:55:06 +00:00
import click
from typing import (
Any,
2020-08-24 11:55:06 +00:00
)
2021-08-23 10:33:04 +00:00
from staking_deposit.exceptions import ValidationError
from staking_deposit.key_handling.key_derivation.mnemonic import (
2020-08-24 11:55:06 +00:00
verify_mnemonic,
)
2021-08-23 10:33:04 +00:00
from staking_deposit.utils.constants import (
2020-08-24 11:55:06 +00:00
WORD_LISTS_PATH,
)
2021-08-23 10:33:04 +00:00
from staking_deposit.utils.click import (
captive_prompt_callback,
jit_option,
)
2021-08-23 10:33:04 +00:00
from staking_deposit.utils.intl import load_text
from staking_deposit.utils.validation import validate_int_range
from .generate_keys import (
generate_keys,
2020-09-28 15:20:08 +00:00
generate_keys_arguments_decorator,
)
2020-08-24 11:55:06 +00:00
2021-04-23 14:23:23 +00:00
def validate_mnemonic(ctx: click.Context, param: Any, mnemonic: str) -> str:
2020-08-24 11:55:06 +00:00
if verify_mnemonic(mnemonic, WORD_LISTS_PATH):
return mnemonic
else:
raise ValidationError(load_text(['err_invalid_mnemonic']))
2020-08-24 11:55:06 +00:00
@click.command(
help=load_text(['arg_existing_mnemonic', 'help'], func='existing_mnemonic'),
)
@jit_option(
2020-09-29 14:24:18 +00:00
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'),
2020-08-24 11:55:06 +00:00
type=str,
)
@jit_option(
2021-04-27 12:41:52 +00:00
callback=captive_prompt_callback(
lambda x: x,
2021-05-04 13:46:32 +00:00
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'),
2021-04-27 12:41:52 +00:00
True,
2021-04-23 14:23:23 +00:00
),
default='',
2021-04-23 14:23:23 +00:00
help=lambda: load_text(['arg_mnemonic_password', 'help'], func='existing_mnemonic'),
hidden=True,
param_decls='--mnemonic-password',
prompt=False,
2020-08-24 11:55:06 +00:00
)
@jit_option(
callback=captive_prompt_callback(
lambda num: validate_int_range(num, 0, 2**32),
2021-05-04 13:46:32 +00:00
lambda: load_text(['arg_validator_start_index', 'prompt'], func='existing_mnemonic'),
lambda: load_text(['arg_validator_start_index', 'confirm'], func='existing_mnemonic'),
),
2020-09-29 14:24:18 +00:00
default=0,
help=lambda: load_text(['arg_validator_start_index', 'help'], func='existing_mnemonic'),
param_decls="--validator_start_index",
prompt=lambda: load_text(['arg_validator_start_index', 'prompt'], func='existing_mnemonic'),
2020-09-29 14:24:18 +00:00
)
@generate_keys_arguments_decorator
2021-04-06 12:48:52 +00:00
@click.pass_context
def existing_mnemonic(ctx: click.Context, mnemonic: str, mnemonic_password: str, **kwargs: Any) -> None:
2021-04-06 12:48:52 +00:00
ctx.obj = {} if ctx.obj is None else ctx.obj # Create a new ctx.obj if it doesn't exist
ctx.obj.update({'mnemonic': mnemonic, 'mnemonic_password': mnemonic_password})
ctx.forward(generate_keys)