Skip to content

Commit

Permalink
Confirm deposit eth1_withdrawal_address input
Browse files Browse the repository at this point in the history
  • Loading branch information
hwwhww committed Mar 13, 2023
1 parent f79bbac commit 9aed027
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
9 changes: 7 additions & 2 deletions staking_deposit/cli/generate_keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,14 @@ def generate_keys_arguments_decorator(function: Callable[..., Any]) -> Callable[
prompt=lambda: load_text(['keystore_password', 'prompt'], func='generate_keys_arguments_decorator'),
),
jit_option(
callback=validate_eth1_withdrawal_address,
callback=captive_prompt_callback(
lambda address: validate_eth1_withdrawal_address(None, None, address),
lambda: load_text(['arg_execution_address', 'prompt'], func='generate_keys_arguments_decorator'),
lambda: load_text(['arg_execution_address', 'confirm'], func='generate_keys_arguments_decorator'),
lambda: load_text(['arg_execution_address', 'mismatch'], func='generate_keys_arguments_decorator'),
),
default=None,
help=lambda: load_text(['eth1_withdrawal_address', 'help'], func='generate_keys_arguments_decorator'),
help=lambda: load_text(['arg_execution_address', 'help'], func='generate_keys_arguments_decorator'),
param_decls='--eth1_withdrawal_address',
),
]
Expand Down
7 changes: 5 additions & 2 deletions staking_deposit/intl/en/cli/generate_keys.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
"confirm": "Repeat your keystore password for confirmation",
"mismatch": "Error: the two entered values do not match. Please type again."
},
"eth1_withdrawal_address": {
"help": "If this field is set and valid, the given Eth1 address will be used to create the withdrawal credentials. Otherwise, it will generate withdrawal credentials with the mnemonic-derived withdrawal public key."
"arg_execution_address": {
"help": "The 20-byte (Eth1) execution address that will be used in withdrawal",
"prompt": "Please enter the 20-byte execution address for the new withdrawal credentials. Note that you CANNOT change it once you have set it on chain.",
"confirm": "Repeat your execution address for confirmation.",
"mismatch": "Error: the two entered values do not match. Please type again."
}
},
"generate_keys": {
Expand Down
2 changes: 1 addition & 1 deletion staking_deposit/utils/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def callback(ctx: click.Context, param: Any, user_input: str) -> Any:
try:
processed_input = processing_func(user_input)
# Logic for confirming user input:
if confirmation_prompt is not None and processed_input != '':
if confirmation_prompt is not None and processed_input not in ('', None):
confirmation_input = click.prompt(confirmation_prompt(), hide_input=hide_input)
if processing_func(confirmation_input) != processed_input:
raise ValidationError(confirmation_mismatch_msg())
Expand Down
3 changes: 2 additions & 1 deletion tests/test_cli/test_existing_menmonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ def test_existing_mnemonic_eth1_address_withdrawal() -> None:
os.mkdir(my_folder_path)

runner = CliRunner()
eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705Fa'
inputs = [
'TREZOR',
eth1_withdrawal_address,
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
'2', '2', '5', 'mainnet', 'MyPassword', 'MyPassword']
data = '\n'.join(inputs)
eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705Fa'
arguments = [
'--language', 'english',
'existing-mnemonic',
Expand Down
15 changes: 11 additions & 4 deletions tests/test_cli/test_new_mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from staking_deposit.cli import new_mnemonic
from staking_deposit.deposit import cli
from staking_deposit.key_handling.key_derivation.mnemonic import abbreviate_words
from staking_deposit.utils.constants import DEFAULT_VALIDATOR_KEYS_FOLDER_NAME, ETH1_ADDRESS_WITHDRAWAL_PREFIX
from staking_deposit.utils.constants import DEFAULT_VALIDATOR_KEYS_FOLDER_NAME, ETH1_ADDRESS_WITHDRAWAL_PREFIX, BLS_WITHDRAWAL_PREFIX
from staking_deposit.utils.intl import load_text
from .helpers import clean_key_folder, get_permissions, get_uuid

Expand Down Expand Up @@ -69,10 +69,10 @@ def mock_get_mnemonic(language, words_path, entropy=None) -> str:
os.mkdir(my_folder_path)

runner = CliRunner()
inputs = ['english', '1', 'mainnet', 'MyPassword', 'MyPassword',
eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705Fa'
inputs = [eth1_withdrawal_address, 'english', '1', 'mainnet', 'MyPassword', 'MyPassword',
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about']
data = '\n'.join(inputs)
eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705Fa'
arguments = [
'--language', 'english',
'new-mnemonic',
Expand Down Expand Up @@ -112,7 +112,7 @@ def mock_get_mnemonic(language, words_path, entropy=None) -> str:


@pytest.mark.asyncio
async def test_script() -> None:
async def test_script_bls_withdrawal() -> None:
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)
Expand Down Expand Up @@ -164,7 +164,14 @@ async def test_script() -> None:
assert len(seed_phrase) > 0

# Check files
deposit_file = [key_file for key_file in key_files if key_file.startswith('deposit_data')][0]
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
with open(validator_keys_folder_path + '/' + deposit_file, 'r') as f:
deposits_dict = json.load(f)
for deposit in deposits_dict:
withdrawal_credentials = bytes.fromhex(deposit['withdrawal_credentials'])
assert withdrawal_credentials[:2] == BLS_WITHDRAWAL_PREFIX

_, _, key_files = next(os.walk(validator_keys_folder_path))

all_uuid = [
Expand Down

0 comments on commit 9aed027

Please sign in to comment.