Skip to content

Commit

Permalink
feat: import perf improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bitwise-constructs committed Dec 18, 2024
1 parent 356070b commit 2b08a2a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
18 changes: 10 additions & 8 deletions eip712/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@
Message classes for typed structured data hashing and signing in Ethereum.
"""

from typing import Any, Optional
from typing import TYPE_CHECKING, Any, Optional

from eth_abi.abi import is_encodable_type # type: ignore[import-untyped]
from eth_account.messages import SignableMessage, hash_domain, hash_eip712_message
from eth_pydantic_types import Address, HexBytes
from eth_pydantic_types.abi import bytes32, string, uint256
from eth_utils import keccak
from eth_utils.curried import ValidationError
from pydantic import BaseModel, model_validator
from typing_extensions import _AnnotatedAlias

if TYPE_CHECKING:
from eth_pydantic_types.abi import bytes32, string, uint256

# ! Do not change the order of the fields in this list !
# To correctly encode and hash the domain fields, they
# must be in this precise order.
Expand Down Expand Up @@ -50,7 +52,7 @@ def _types_(self) -> dict:
types: dict[str, list] = {repr(self): []}

for field in {
k: v.annotation.__name__
k: v.annotation.__name__ # type: ignore[union-attr]
for k, v in self.model_fields.items()
if not k.startswith("eip712_")
}:
Expand Down Expand Up @@ -116,11 +118,11 @@ class EIP712Message(EIP712Type):
"""

# NOTE: Must override at least one of these fields
eip712_name_: Optional[string] = None
eip712_version_: Optional[string] = None
eip712_chainId_: Optional[uint256] = None
eip712_verifyingContract_: Optional[string] = None
eip712_salt_: Optional[bytes32] = None
eip712_name_: Optional["string"] = None
eip712_version_: Optional["string"] = None
eip712_chainId_: Optional["uint256"] = None
eip712_verifyingContract_: Optional["string"] = None
eip712_salt_: Optional["bytes32"] = None

@model_validator(mode="after")
@classmethod
Expand Down
36 changes: 20 additions & 16 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from typing import TYPE_CHECKING

import pytest
from eth_pydantic_types.abi import address, bytes32, string, uint256
from hexbytes import HexBytes

from eip712.common import create_permit_def
from eip712.messages import EIP712Message, EIP712Type

if TYPE_CHECKING:
from eth_pydantic_types.abi import address, bytes32, string, uint256

PERMIT_NAME = "Yearn Vault"
PERMIT_VERSION = "0.3.5"
PERMIT_CHAIN_ID = 1
Expand All @@ -18,34 +22,34 @@


class SubType(EIP712Type):
inner: uint256
inner: "uint256"


class ValidMessageWithNameDomainField(EIP712Message):
eip712_name_: string = "Valid Test Message"
value: uint256
default_value: address = "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF" # type: ignore
eip712_name_: "string" = "Valid Test Message"
value: "uint256"
default_value: "address" = "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF"
sub: SubType


class MessageWithNonCanonicalDomainFieldOrder(EIP712Message):
eip712_name_: string = PERMIT_NAME
eip712_salt_: bytes32 = PERMIT_SALT
eip712_chainId_: uint256 = PERMIT_CHAIN_ID
eip712_version_: string = PERMIT_VERSION
eip712_verifyingContract_: address = PERMIT_VAULT_ADDRESS
eip712_name_: "string" = PERMIT_NAME
eip712_salt_: "bytes32" = PERMIT_SALT
eip712_chainId_: "uint256" = PERMIT_CHAIN_ID
eip712_version_: "string" = PERMIT_VERSION
eip712_verifyingContract_: "address" = PERMIT_VAULT_ADDRESS


class MessageWithCanonicalDomainFieldOrder(EIP712Message):
eip712_name_: string = PERMIT_NAME
eip712_version_: string = PERMIT_VERSION
eip712_chainId_: uint256 = PERMIT_CHAIN_ID
eip712_verifyingContract_: address = PERMIT_VAULT_ADDRESS
eip712_salt_: bytes32 = PERMIT_SALT
eip712_name_: "string" = PERMIT_NAME
eip712_version_: "string" = PERMIT_VERSION
eip712_chainId_: "uint256" = PERMIT_CHAIN_ID
eip712_verifyingContract_: "address" = PERMIT_VAULT_ADDRESS
eip712_salt_: "bytes32" = PERMIT_SALT


class InvalidMessageMissingDomainFields(EIP712Message):
value: uint256
value: "uint256"


@pytest.fixture
Expand Down

0 comments on commit 2b08a2a

Please sign in to comment.