Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate checksum for eth1_withdrawal_address #338

Merged
merged 2 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion 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 staking_deposit.credentials import (
CredentialList,
)
Expand Down
59 changes: 59 additions & 0 deletions tests/test_cli/test_existing_menmonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,65 @@ def test_existing_mnemonic_eth1_address_withdrawal() -> None:
clean_key_folder(my_folder_path)


def test_existing_mnemonic_eth1_address_withdrawal_bad_checksum() -> None:
# Prepare folder
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)

runner = CliRunner()

# NOTE: final 'A' needed to be an 'a'
wrong_eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705FA'
correct_eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705Fa'

inputs = [
'TREZOR',
correct_eth1_withdrawal_address, correct_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)
arguments = [
'--language', 'english',
'existing-mnemonic',
'--folder', my_folder_path,
'--mnemonic-password', 'TREZOR',
'--eth1_withdrawal_address', wrong_eth1_withdrawal_address,
]
result = runner.invoke(cli, arguments, input=data)

assert result.exit_code == 0

# Check files
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
_, _, key_files = next(os.walk(validator_keys_folder_path))

deposit_file = [key_file for key_file in key_files if key_file.startswith('deposit_data')][0]
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 == (
ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + decode_hex(correct_eth1_withdrawal_address)
)

all_uuid = [
get_uuid(validator_keys_folder_path + '/' + key_file)
for key_file in key_files
if key_file.startswith('keystore')
]
assert len(set(all_uuid)) == 5

# Verify file permissions
if os.name == 'posix':
for file_name in key_files:
assert get_permissions(validator_keys_folder_path, file_name) == '0o440'
# Clean up
clean_key_folder(my_folder_path)


@pytest.mark.asyncio
async def test_script() -> None:
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
Expand Down
61 changes: 61 additions & 0 deletions tests/test_cli/test_new_mnemonic.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,67 @@ def mock_get_mnemonic(language, words_path, entropy=None) -> str:
clean_key_folder(my_folder_path)


def test_new_mnemonic_eth1_address_withdrawal_bad_checksum(monkeypatch) -> None:
# monkeypatch get_mnemonic
def mock_get_mnemonic(language, words_path, entropy=None) -> str:
return "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"

monkeypatch.setattr(new_mnemonic, "get_mnemonic", mock_get_mnemonic)

# Prepare folder
my_folder_path = os.path.join(os.getcwd(), 'TESTING_TEMP_FOLDER')
clean_key_folder(my_folder_path)
if not os.path.exists(my_folder_path):
os.mkdir(my_folder_path)

runner = CliRunner()

# NOTE: final 'A' needed to be an 'a'
wrong_eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705FA'
correct_eth1_withdrawal_address = '0x00000000219ab540356cBB839Cbe05303d7705Fa'

inputs = [correct_eth1_withdrawal_address, correct_eth1_withdrawal_address,
'english', '1', 'mainnet', 'MyPassword', 'MyPassword',
'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about']
data = '\n'.join(inputs)
arguments = [
'--language', 'english',
'new-mnemonic',
'--folder', my_folder_path,
'--eth1_withdrawal_address', wrong_eth1_withdrawal_address,
]
result = runner.invoke(cli, arguments, input=data)
assert result.exit_code == 0

# Check files
validator_keys_folder_path = os.path.join(my_folder_path, DEFAULT_VALIDATOR_KEYS_FOLDER_NAME)
_, _, key_files = next(os.walk(validator_keys_folder_path))

deposit_file = [key_file for key_file in key_files if key_file.startswith('deposit_data')][0]
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 == (
ETH1_ADDRESS_WITHDRAWAL_PREFIX + b'\x00' * 11 + decode_hex(correct_eth1_withdrawal_address)
)

all_uuid = [
get_uuid(validator_keys_folder_path + '/' + key_file)
for key_file in key_files
if key_file.startswith('keystore')
]
assert len(set(all_uuid)) == 1

# Verify file permissions
if os.name == 'posix':
for file_name in key_files:
assert get_permissions(validator_keys_folder_path, file_name) == '0o440'

# Clean up
clean_key_folder(my_folder_path)


@pytest.mark.asyncio
async def test_script_bls_withdrawal() -> None:
# Prepare folder
Expand Down