From 282e17a0367a550d258f89b8d2b63c6016f681e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Roy?= <303593+remyroy@users.noreply.github.com> Date: Mon, 10 Jun 2024 15:17:20 -0400 Subject: [PATCH 1/3] Replace asserts with raising a ValidatorError --- .../key_handling/key_derivation/mnemonic.py | 4 +++- ethstaker_deposit/utils/intl.py | 5 +++-- ethstaker_deposit/utils/validation.py | 18 +++++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/ethstaker_deposit/key_handling/key_derivation/mnemonic.py b/ethstaker_deposit/key_handling/key_derivation/mnemonic.py index 60c0f8ec..7babc1ec 100644 --- a/ethstaker_deposit/key_handling/key_derivation/mnemonic.py +++ b/ethstaker_deposit/key_handling/key_derivation/mnemonic.py @@ -7,6 +7,7 @@ Sequence, ) +from ethstaker_deposit.exceptions import ValidationError from ethstaker_deposit.utils.constants import ( MNEMONIC_LANG_OPTIONS, ) @@ -127,7 +128,8 @@ def reconstruct_mnemonic(mnemonic: str, words_path: str) -> Optional[str]: This check guarantees that only one language has a valid mnemonic. It is needed to ensure abbrivated words aren't valid in multiple languages """ - assert reconstructed_mnemonic is None + if reconstructed_mnemonic is not None: + raise ValidationError("This mnemonic abbrivated form is available in multiple languages.") reconstructed_mnemonic = ' '.join([_index_to_word(full_word_list, index) for index in word_indices]) else: pass diff --git a/ethstaker_deposit/utils/intl.py b/ethstaker_deposit/utils/intl.py index a6aad22b..a6a380d2 100644 --- a/ethstaker_deposit/utils/intl.py +++ b/ethstaker_deposit/utils/intl.py @@ -28,11 +28,12 @@ def _get_from_dict(dataDict: Dict[str, Any], mapList: Iterable[str]) -> str: ''' try: ans = reduce(dict.get, mapList, dataDict) - assert isinstance(ans, str) + if not isinstance(ans, str): + raise ValidationError('Incomplete') return ans except TypeError: raise KeyError('%s not in internationalisation json file.' % mapList) - except AssertionError: + except ValidationError: raise KeyError('The provided params (%s) were incomplete.' % mapList) diff --git a/ethstaker_deposit/utils/validation.py b/ethstaker_deposit/utils/validation.py index 375bf940..f80f845e 100644 --- a/ethstaker_deposit/utils/validation.py +++ b/ethstaker_deposit/utils/validation.py @@ -131,14 +131,16 @@ def validate_password_strength(password: str) -> str: def validate_int_range(num: Any, low: int, high: int) -> int: ''' - Verifies that `num` is an `int` andlow <= num < high + Verifies that `num` is an `int` and low <= num < high ''' try: num_int = int(num) # Try cast to int - assert num_int == float(num) # Check num is not float - assert low <= num_int < high # Check num in range + if num_int != float(num): # Check num is not float + raise ValidationError('Num is float') + if not (low <= num_int < high): # Check num in range + raise ValidationError('Num is not in range') return num_int - except (ValueError, AssertionError): + except (ValueError, ValidationError): raise ValidationError(load_text(['err_not_positive_integer'])) @@ -263,9 +265,11 @@ def validate_bls_withdrawal_credentials(bls_withdrawal_credentials: str) -> byte 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): + if len(bls_withdrawal_credentials_bytes) != 32: + raise ValidationError('Lenght is not 32') + if bls_withdrawal_credentials_bytes[:1] != BLS_WITHDRAWAL_PREFIX: + raise ValidationError('Prefix is wrong') + except (ValueError, ValidationError): raise ValidationError(load_text(['err_not_bls_form']) + '\n') return bls_withdrawal_credentials_bytes From e581f6efec4698ab8be95ea607a6f5a567f7c7fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Roy?= <303593+remyroy@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:41:39 -0400 Subject: [PATCH 2/3] Update ethstaker_deposit/key_handling/key_derivation/mnemonic.py Co-authored-by: valefar-on-discord <124839138+valefar-on-discord@users.noreply.github.com> --- ethstaker_deposit/key_handling/key_derivation/mnemonic.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethstaker_deposit/key_handling/key_derivation/mnemonic.py b/ethstaker_deposit/key_handling/key_derivation/mnemonic.py index 7babc1ec..31518f6d 100644 --- a/ethstaker_deposit/key_handling/key_derivation/mnemonic.py +++ b/ethstaker_deposit/key_handling/key_derivation/mnemonic.py @@ -129,7 +129,7 @@ def reconstruct_mnemonic(mnemonic: str, words_path: str) -> Optional[str]: It is needed to ensure abbrivated words aren't valid in multiple languages """ if reconstructed_mnemonic is not None: - raise ValidationError("This mnemonic abbrivated form is available in multiple languages.") + raise ValidationError("This mnemonic abbreviated form is available in multiple languages.") reconstructed_mnemonic = ' '.join([_index_to_word(full_word_list, index) for index in word_indices]) else: pass From c80706b779454ed2c11b92c53f83cf444c0f60f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Roy?= <303593+remyroy@users.noreply.github.com> Date: Wed, 12 Jun 2024 08:41:45 -0400 Subject: [PATCH 3/3] Update ethstaker_deposit/utils/validation.py Co-authored-by: valefar-on-discord <124839138+valefar-on-discord@users.noreply.github.com> --- ethstaker_deposit/utils/validation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ethstaker_deposit/utils/validation.py b/ethstaker_deposit/utils/validation.py index f80f845e..afc417e4 100644 --- a/ethstaker_deposit/utils/validation.py +++ b/ethstaker_deposit/utils/validation.py @@ -266,7 +266,7 @@ def validate_bls_withdrawal_credentials(bls_withdrawal_credentials: str) -> byte try: if len(bls_withdrawal_credentials_bytes) != 32: - raise ValidationError('Lenght is not 32') + raise ValidationError('Length is not 32') if bls_withdrawal_credentials_bytes[:1] != BLS_WITHDRAWAL_PREFIX: raise ValidationError('Prefix is wrong') except (ValueError, ValidationError):