Skip to content

Commit

Permalink
Apply PR feedback by catching the exceptions. Improve error handling …
Browse files Browse the repository at this point in the history
…and messages.
  • Loading branch information
hwwhww committed Mar 13, 2023
1 parent 2cd1d99 commit aae55a6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 10 deletions.
6 changes: 5 additions & 1 deletion staking_deposit/cli/generate_bls_to_execution_change.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ def generate_bls_to_execution_change(

# Check if the given old bls_withdrawal_credentials is as same as the mnemonic generated
for i, credential in enumerate(credentials.credentials):
validate_bls_withdrawal_credentials_matching(bls_withdrawal_credentials_list[i], credential)
try:
validate_bls_withdrawal_credentials_matching(bls_withdrawal_credentials_list[i], credential)
except ValidationError as e:
click.echo('\n[Error] ' + str(e))
return

btec_file = credentials.export_bls_to_execution_change_json(bls_to_execution_changes_folder, validator_indices)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"arg_bls_withdrawal_credentials_list": {
"help": "A list of 32-byte old BLS withdrawal credentials of the certain validator(s)",
"prompt": "Please enter a list of the old BLS withdrawal credentials of your validator(s). Split multiple items with whitespaces or commas."
"prompt": "Please enter a list of the old BLS withdrawal credentials of your validator(s). Split multiple items with whitespaces or commas. The withdrawal credentials are in hexadecimal encoded form."
},
"arg_validator_start_index": {
"help": "The index position for the keys to start generating withdrawal credentials in ERC-2334 format",
Expand Down
7 changes: 7 additions & 0 deletions staking_deposit/intl/en/utils/validation.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,12 @@
},
"verify_bls_to_execution_change_json": {
"msg_bls_to_execution_change_verification": "Verifying your BLSToExecutionChange file:\t"
},
"normalize_bls_withdrawal_credentials_to_bytes" :{
"err_incorrect_hex_form": "The given input is not in hexadecimal encoded form."

},
"normalize_input_list": {
"err_incorrect_list": "The given input should be a list of the old BLS withdrawal credentials of your validator(s). Split multiple items with whitespaces or commas."
}
}
2 changes: 1 addition & 1 deletion staking_deposit/utils/click.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def callback(ctx: click.Context, param: Any, user_input: str) -> Any:
raise ValidationError(confirmation_mismatch_msg())
return processed_input
except ValidationError as e:
click.echo(e)
click.echo('\n[Error] ' + str(e))
user_input = click.prompt(prompt(), hide_input=hide_input)
return callback

Expand Down
21 changes: 14 additions & 7 deletions staking_deposit/utils/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ def normalize_bls_withdrawal_credentials_to_bytes(bls_withdrawal_credentials: st
if bls_withdrawal_credentials.startswith('0x'):
bls_withdrawal_credentials = bls_withdrawal_credentials[2:]

bls_withdrawal_credentials_bytes = bytes.fromhex(bls_withdrawal_credentials)
try:
bls_withdrawal_credentials_bytes = bytes.fromhex(bls_withdrawal_credentials)
except Exception:
raise ValidationError(load_text(['err_incorrect_hex_form']) + '\n')
return bls_withdrawal_credentials_bytes


Expand All @@ -226,21 +229,25 @@ def validate_bls_withdrawal_credentials(bls_withdrawal_credentials: str) -> byte
bls_withdrawal_credentials_bytes = normalize_bls_withdrawal_credentials_to_bytes(bls_withdrawal_credentials)

if is_eth1_address_withdrawal_credentials(bls_withdrawal_credentials_bytes):
raise ValidationError('\n' + load_text(['err_is_already_eth1_form']) + '\n')
raise ValidationError(load_text(['err_is_already_eth1_form']) + '\n')

try:
assert len(bls_withdrawal_credentials_bytes) == 32
assert bls_withdrawal_credentials_bytes[:1] == BLS_WITHDRAWAL_PREFIX
except (ValueError, AssertionError):
raise ValidationError('\n' + load_text(['err_not_bls_form']) + '\n')
raise ValidationError(load_text(['err_not_bls_form']) + '\n')

return bls_withdrawal_credentials_bytes


def normalize_input_list(input: str) -> Sequence[str]:
input = input.strip('[({})]')
input = re.sub(' +', ' ', input)
return re.split(r'; |, | |,|;', input)
try:
input = input.strip('[({})]')
input = re.sub(' +', ' ', input)
result = re.split(r'; |, | |,|;', input)
except Exception:
raise ValidationError(load_text(['err_incorrect_list']) + '\n')
return result


def validate_bls_withdrawal_credentials_list(input_bls_withdrawal_credentials_list: str) -> Sequence[bytes]:
Expand All @@ -256,4 +263,4 @@ def validate_validator_indices(input_validator_indices: str) -> Sequence[int]:

def validate_bls_withdrawal_credentials_matching(bls_withdrawal_credentials: bytes, credential: Credential) -> None:
if bls_withdrawal_credentials[1:] != SHA256(credential.withdrawal_pk)[1:]:
raise ValidationError('\n' + load_text(['err_not_matching']) + '\n')
raise ValidationError(load_text(['err_not_matching']) + '\n')

0 comments on commit aae55a6

Please sign in to comment.