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

61 lines
1.8 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
2021-02-12 16:55:39 +00:00
from eth2deposit.intl.utils import load_text
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 .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:
2021-02-12 16:55:39 +00:00
raise ValidationError(load_text('en', ['err_invalid_mnemonic']))
2020-08-24 11:55:06 +00:00
@click.command(
2021-02-12 16:55:39 +00:00
help=load_text('en', ['arg_existing_mnemonic', 'help']),
)
@click.pass_context
2020-08-24 11:55:06 +00:00
@click.option(
2021-02-12 16:55:39 +00:00
load_text('en', ['arg_mnemonic', 'argument']),
2020-09-29 14:24:18 +00:00
callback=validate_mnemonic,
2021-02-12 16:55:39 +00:00
help=load_text('en', ['arg_mnemonic', 'help']),
prompt=load_text('en', ['arg_mnemonic', 'prompt']),
2020-08-24 11:55:06 +00:00
required=True,
type=str,
)
2020-09-29 14:24:18 +00:00
@click.password_option(
2021-02-12 16:55:39 +00:00
load_text('en', ['arg_mnemonic_password', 'argument']),
default='',
2021-02-12 16:55:39 +00:00
help=load_text('en', ['arg_mnemonic_password', 'help']),
prompt=False,
2020-08-24 11:55:06 +00:00
)
2020-09-29 14:24:18 +00:00
@click.option(
2021-02-12 16:55:39 +00:00
load_text('en', ['arg_validator_start_index', 'argument']),
2020-09-29 14:24:18 +00:00
confirmation_prompt=True,
default=0,
2021-02-12 16:55:39 +00:00
help=load_text('en', ['arg_validator_start_index', 'help']),
prompt=load_text('en', ['arg_validator_start_index', 'prompt']),
type=click.IntRange(0, 2**32 - 1),
2020-09-29 14:24:18 +00:00
)
@generate_keys_arguments_decorator
def existing_mnemonic(ctx: click.Context, mnemonic: str, mnemonic_password: str, **kwargs: Any) -> None:
if mnemonic_password != '':
click.clear()
2021-02-12 16:55:39 +00:00
click.confirm(load_text('en', ['msg_mnemonic_password_confirm']), abort=True)
ctx.obj = {'mnemonic': mnemonic, 'mnemonic_password': mnemonic_password}
ctx.forward(generate_keys)