Apply PR feedback by catching the exceptions. Improve error handling and messages.

This commit is contained in:
Hsiao-Wei Wang 2023-03-13 21:44:48 +08:00
parent 2cd1d999ea
commit aae55a6886
No known key found for this signature in database
GPG Key ID: AE3D6B174F971DE4
5 changed files with 28 additions and 10 deletions

View File

@ -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)

View File

@ -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",

View File

@ -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."
}
}

View File

@ -104,7 +104,7 @@ def captive_prompt_callback(
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

View File

@ -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
@ -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]:
@ -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')