• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-eth-py / 10793540350

10 Sep 2024 01:31PM UTC coverage: 93.551% (-0.3%) from 93.892%
10793540350

push

github

falvaradorodriguez
Fix cowswap test

8777 of 9382 relevant lines covered (93.55%)

3.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.0
/safe_eth/eth/django/forms.py
1
import binascii
4✔
2
from typing import Any, Optional, Union
4✔
3

4
from django import forms
4✔
5
from django.core import exceptions
4✔
6
from django.core.exceptions import ValidationError
4✔
7
from django.utils.translation import gettext as _
4✔
8

9
from hexbytes import HexBytes
4✔
10

11
from safe_eth.eth.utils import fast_is_checksum_address
4✔
12

13

14
class EthereumAddressFieldForm(forms.CharField):
4✔
15
    default_error_messages = {
4✔
16
        "invalid": _("Enter a valid checksummed Ethereum Address."),
17
    }
18

19
    def prepare_value(self, value):
4✔
20
        return value
×
21

22
    def to_python(self, value):
4✔
23
        value = super().to_python(value)
4✔
24
        if value in self.empty_values:
4✔
25
            return None
×
26
        elif not fast_is_checksum_address(value):
4✔
27
            raise ValidationError(self.error_messages["invalid"], code="invalid")
4✔
28
        return value
4✔
29

30

31
class HexFieldForm(forms.CharField):
4✔
32
    default_error_messages = {
4✔
33
        "invalid": _("Enter a valid hexadecimal."),
34
    }
35

36
    def __init__(self, *args, **kwargs):
4✔
37
        super().__init__(*args, **kwargs)
4✔
38
        self.empty_value = None
4✔
39

40
    def prepare_value(self, value: memoryview) -> str:
4✔
41
        if value:
4✔
42
            return "0x" + bytes(value).hex()
4✔
43
        else:
44
            return ""
×
45

46
    def to_python(self, value: Optional[Any]) -> Optional[HexBytes]:
4✔
47
        if value in self.empty_values:
4✔
48
            return self.empty_value
4✔
49
        try:
4✔
50
            if isinstance(value, str):
4✔
51
                value = value.strip()
4✔
52
            if isinstance(value, (str, bytes, bytearray, int)):
4✔
53
                return HexBytes(value)
4✔
54
            else:
55
                raise TypeError(f"Unsupported type for HexBytes: {type(value)}")
×
56
        except (binascii.Error, TypeError, ValueError):
4✔
57
            raise exceptions.ValidationError(
4✔
58
                self.error_messages["invalid"],
59
                code="invalid",
60
                params={"value": value},
61
            )
62

63

64
class Keccak256FieldForm(HexFieldForm):
4✔
65
    default_error_messages = {
4✔
66
        "invalid": _('"%(value)s" is not a valid keccak256 hash.'),
67
        "length": _('"%(value)s" keccak256 hash should be 32 bytes.'),
68
    }
69

70
    def prepare_value(self, value: Union[str, memoryview]) -> str:
4✔
71
        # Keccak field already returns a hex str
72
        if isinstance(value, str):
×
73
            return value
×
74
        return super().prepare_value(value)
×
75

76
    def to_python(self, value: Optional[Any]) -> Optional[HexBytes]:
4✔
77
        python_value: Optional[HexBytes] = super().to_python(value)
4✔
78
        if python_value and len(python_value) != 32:
4✔
79
            raise ValidationError(
4✔
80
                self.error_messages["length"],
81
                code="length",
82
                params={"value": python_value.hex()},
83
            )
84
        return python_value
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc