Skip to content

Commit

Permalink
feat: validate vrs
Browse files Browse the repository at this point in the history
  • Loading branch information
NotPeopling2day committed Jan 22, 2025
1 parent a87fba6 commit d17a708
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
21 changes: 19 additions & 2 deletions src/ape/types/signatures.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from collections.abc import Iterator
from typing import TYPE_CHECKING, Optional, Union
from typing import TYPE_CHECKING, Any, Optional, Union

from eth_account import Account
from eth_account.messages import SignableMessage
from eth_pydantic_types import HexBytes
from eth_utils import to_bytes, to_hex
from eth_utils import to_bytes, to_hex, to_int
from pydantic import field_validator
from pydantic.dataclasses import dataclass

from ape.utils.misc import as_our_module, log_instead_of_fail
Expand Down Expand Up @@ -84,6 +85,22 @@ class _Signature:
The signature proof point (``s``) in an ECDSA signature.
"""

@field_validator("v", mode="before")
@classmethod
def convert_to_int(cls, v: Any) -> int:
if isinstance(v, int):
return v

return to_int(v)

@field_validator("r, s", mode="before")
@classmethod
def convert_to_bytes(cls, v: Any) -> bytes:
if isinstance(v, bytes):
return v

return to_bytes(v)

def __iter__(self) -> Iterator[Union[int, bytes]]:
# NOTE: Allows tuple destructuring
yield self.v
Expand Down
4 changes: 2 additions & 2 deletions src/ape_ethereum/ecosystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,8 @@ def create_transaction(self, **kwargs) -> "TransactionAPI":
if all(field in tx_data for field in ("v", "r", "s")):
tx_data["signature"] = TransactionSignature(
v=tx_data["v"],
r=bytes(tx_data["r"]),
s=bytes(tx_data["s"]),
r=tx_data["r"],
s=tx_data["s"],
)

if "gas" not in tx_data:
Expand Down

0 comments on commit d17a708

Please sign in to comment.