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

65 lines
2.0 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
)
from eth2deposit.exceptions import ValidationError
from eth2deposit.key_handling.key_derivation.mnemonic import (
2020-08-24 11:55:06 +00:00
verify_mnemonic,
)
from eth2deposit.utils.constants import (
2020-08-24 11:55:06 +00:00
WORD_LISTS_PATH,
)
from eth2deposit.utils.click import jit_option
from eth2deposit.utils.intl import load_text
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
2020-09-29 14:24:18 +00:00
def validate_mnemonic(cts: 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
required=True,
type=str,
)
@jit_option(
confirmation_prompt=True,
default='',
help=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(
2020-09-29 14:24:18 +00:00
confirmation_prompt=True,
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'),
type=click.IntRange(0, 2**32 - 1),
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:
if mnemonic_password != '':
click.clear()
click.confirm(load_text(['msg_mnemonic_password_confirm']), abort=True)
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)