Skip to content

Commit

Permalink
fix: 0x prefix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Dec 8, 2023
1 parent 1ea4082 commit a3b9c90
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions eth_pydantic_types/hex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Any, ClassVar, Optional, Tuple, Union
from typing import Any, ClassVar, Optional, Tuple, Union, cast

from eth_typing import HexStr as EthTypingHexStr
from eth_utils import add_0x_prefix
from hexbytes import HexBytes as BaseHexBytes
from pydantic_core import CoreSchema
from pydantic_core.core_schema import (
Expand Down Expand Up @@ -105,7 +107,9 @@ def __eth_pydantic_validate__(cls, value):

@classmethod
def from_bytes(cls, data: bytes) -> "HexStr":
return HexStr(super().from_bytes(data))
value_str = super().from_bytes(data)
value = add_0x_prefix(cast(EthTypingHexStr, value_str))
return HexStr(value)


def validate_hex_str(value: str) -> str:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_hex.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,9 @@ def test_hexstr_model_dump(bytes32str):
actual = model.model_dump()
expected = {"value": "0x03"}
assert actual == expected


def test_from_bytes():
value = b"\xb7\xfc\xef\x7f\xe7E\xf2\xa9U`\xff_U\x0e;\x8f"
actual = HexStr.from_bytes(value)
assert actual.startswith("0x")

0 comments on commit a3b9c90

Please sign in to comment.