-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,11 @@ | ||
import binascii | ||
|
||
from django.core.exceptions import ValidationError | ||
from django.forms import CharField as CharFieldForm | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
import django_filters | ||
from hexbytes import HexBytes | ||
|
||
from ..utils import fast_is_checksum_address | ||
|
||
|
||
class EthereumAddressFieldForm(CharFieldForm): | ||
default_error_messages = { | ||
"invalid": _("Enter a valid checksummed Ethereum Address."), | ||
} | ||
|
||
def prepare_value(self, value): | ||
return value | ||
|
||
def to_python(self, value): | ||
value = super().to_python(value) | ||
if value in self.empty_values: | ||
return None | ||
elif not fast_is_checksum_address(value): | ||
raise ValidationError(self.error_messages["invalid"], code="invalid") | ||
return value | ||
from .forms import EthereumAddressFieldForm, Keccak256FieldForm | ||
|
||
|
||
class EthereumAddressFilter(django_filters.Filter): | ||
field_class = EthereumAddressFieldForm | ||
|
||
|
||
class Keccak256FieldForm(CharFieldForm): | ||
default_error_messages = { | ||
"invalid": _('"%(value)s" is not a valid keccak256 hash.'), | ||
"length": _('"%(value)s" keccak256 hash should be 32 bytes.'), | ||
} | ||
|
||
def prepare_value(self, value): | ||
return value | ||
|
||
def to_python(self, value): | ||
value = super().to_python(value) | ||
if value in self.empty_values: | ||
return None | ||
else: | ||
try: | ||
bytes_value = HexBytes(value) | ||
if len(bytes_value) != 32: | ||
raise ValidationError( | ||
self.error_messages["length"], | ||
code="length", | ||
params={"value": value}, | ||
) | ||
except (binascii.Error, ValueError): | ||
raise ValidationError( | ||
self.error_messages["invalid"], | ||
code="invalid", | ||
params={"value": value}, | ||
) | ||
return value | ||
|
||
|
||
class Keccak256Filter(django_filters.Filter): | ||
field_class = Keccak256FieldForm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import binascii | ||
|
||
from django import forms | ||
from django.core import exceptions | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext as _ | ||
|
||
from hexbytes import HexBytes | ||
|
||
from gnosis.eth.utils import fast_is_checksum_address | ||
|
||
|
||
class EthereumAddressFieldForm(forms.CharField): | ||
default_error_messages = { | ||
"invalid": _("Enter a valid checksummed Ethereum Address."), | ||
} | ||
|
||
def prepare_value(self, value): | ||
return value | ||
|
||
def to_python(self, value): | ||
value = super().to_python(value) | ||
if value in self.empty_values: | ||
return None | ||
elif not fast_is_checksum_address(value): | ||
raise ValidationError(self.error_messages["invalid"], code="invalid") | ||
return value | ||
|
||
|
||
class HexFieldForm(forms.CharField): | ||
default_error_messages = { | ||
"invalid": _("Enter a valid hexadecimal."), | ||
} | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.empty_value = None | ||
|
||
def to_python(self, value: str) -> HexBytes: | ||
value = value.strip() | ||
if value in self.empty_values: | ||
return self.empty_value | ||
try: | ||
return HexBytes(value) | ||
except (binascii.Error, TypeError, ValueError): | ||
raise exceptions.ValidationError( | ||
self.error_messages["invalid"], | ||
code="invalid", | ||
params={"value": value}, | ||
) | ||
|
||
def prepare_value(self, value: memoryview) -> str: | ||
return "0x" + bytes(value).hex() | ||
|
||
|
||
class Keccak256FieldForm(HexFieldForm): | ||
default_error_messages = { | ||
"invalid": _('"%(value)s" is not a valid keccak256 hash.'), | ||
"length": _('"%(value)s" keccak256 hash should be 32 bytes.'), | ||
} | ||
|
||
def prepare_value(self, value: str) -> str: | ||
# Keccak field already returns a hex str | ||
return value | ||
|
||
def to_python(self, value): | ||
value = super().to_python(value) | ||
if value and len(value) != 32: | ||
raise ValidationError( | ||
self.error_messages["length"], | ||
code="length", | ||
params={"value": value.hex()}, | ||
) | ||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters