From d17a70856e61f2fe01e8db79cdc116a50b14c5ae Mon Sep 17 00:00:00 2001 From: NotPeopling2day <32708219+NotPeopling2day@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:10:56 -0500 Subject: [PATCH] feat: validate vrs --- src/ape/types/signatures.py | 21 +++++++++++++++++++-- src/ape_ethereum/ecosystem.py | 4 ++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/ape/types/signatures.py b/src/ape/types/signatures.py index c3f857d919..10ce3f6223 100644 --- a/src/ape/types/signatures.py +++ b/src/ape/types/signatures.py @@ -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 @@ -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 diff --git a/src/ape_ethereum/ecosystem.py b/src/ape_ethereum/ecosystem.py index 94aff193be..e931c2063b 100644 --- a/src/ape_ethereum/ecosystem.py +++ b/src/ape_ethereum/ecosystem.py @@ -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: