Skip to content

Fixes bug #16 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 145 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,150 @@
__pycache__
.env
.history
.hypothesis/
build/*
reports/*

.project
.pydevproject
.vscode

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Unit test reports
**/junit-*.xml
**/junit-*.html

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
dist/
build/
.pytest_cache/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# mac OS
.DS_Store
tests/.DS_Store

tests/contracts/build
62 changes: 54 additions & 8 deletions eip712_structs/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import json
import operator
import re
import typing
from collections import OrderedDict, defaultdict
from hexbytes import HexBytes
from typing import List, Tuple, NamedTuple

from eth_utils.crypto import keccak
Expand Down Expand Up @@ -84,17 +86,30 @@ def data_dict(self):
for k, v in self.values.items():
if isinstance(v, EIP712Struct):
result[k] = v.data_dict()
elif isinstance(v, list) and len(v) and isinstance(v[0], EIP712Struct):
result[k] = [ e.data_dict() for e in v ]
else:
result[k] = v
return result

def as_function_args(self) -> Tuple:
result = list()
for v in self.values.values():
if isinstance(v, EIP712Struct):
result.append(v.as_function_args())
elif isinstance(v, list) and len(v) and isinstance(v[0], EIP712Struct):
result.append([e.as_function_args() for e in v])
else:
result.append(v)
return tuple(result)

@classmethod
def _encode_type(cls, resolve_references: bool) -> str:
member_sigs = [f'{typ.type_name} {name}' for name, typ in cls.get_members()]
struct_sig = f'{cls.type_name}({",".join(member_sigs)})'

if resolve_references:
reference_structs = set()
reference_structs = list()
cls._gather_reference_structs(reference_structs)
sorted_structs = sorted(list(s for s in reference_structs if s != cls), key=lambda s: s.type_name)
for struct in sorted_structs:
Expand All @@ -105,10 +120,16 @@ def _encode_type(cls, resolve_references: bool) -> str:
def _gather_reference_structs(cls, struct_set):
"""Finds reference structs defined in this struct type, and inserts them into the given set.
"""
structs = [m[1] for m in cls.get_members() if isinstance(m[1], type) and issubclass(m[1], EIP712Struct)]
structs = [
m[1] for m in cls.get_members()
if isinstance(m[1], type) and issubclass(m[1], EIP712Struct)
] + [
m[1].member_type for m in cls.get_members()
if isinstance(m[1], Array) and hasattr(m[1].member_type, "encode_type")
]
for struct in structs:
if struct not in struct_set:
struct_set.add(struct)
struct_set.append(struct)
struct._gather_reference_structs(struct_set)

@classmethod
Expand Down Expand Up @@ -148,7 +169,9 @@ def _assert_domain(domain):
raise ValueError('Domain must be provided, or eip712_structs.default_domain must be set.')
return result

def to_message(self, domain: 'EIP712Struct' = None) -> dict:
def to_message(
self, domain: typing.Optional["EIP712Struct"] = None
) -> dict:
"""Convert a struct into a dictionary suitable for messaging.

Dictionary is of the form:
Expand All @@ -162,7 +185,7 @@ def to_message(self, domain: 'EIP712Struct' = None) -> dict:
:returns: This struct + the domain in dict form, structured as specified for EIP712 messages.
"""
domain = self._assert_domain(domain)
structs = {domain, self}
structs = [domain, self]
self._gather_reference_structs(structs)

# Build type dictionary
Expand All @@ -187,7 +210,9 @@ def to_message_json(self, domain: 'EIP712Struct' = None) -> str:
message = self.to_message(domain)
return json.dumps(message, cls=BytesJSONEncoder)

def signable_bytes(self, domain: 'EIP712Struct' = None) -> bytes:
def signable_bytes(
self, domain: typing.Optional[typing.Union["EIP712Struct", bytes, str]] = None
) -> bytes:
"""Return a ``bytes`` object suitable for signing, as specified for EIP712.

As per the spec, bytes are constructed as follows:
Expand All @@ -196,8 +221,26 @@ def signable_bytes(self, domain: 'EIP712Struct' = None) -> bytes:
:param domain: The domain to include in the hash bytes. If None, uses ``eip712_structs.default_domain``
:return: The bytes object
"""
domain = self._assert_domain(domain)
result = b'\x19\x01' + domain.hash_struct() + self.hash_struct()

if isinstance(domain, str):
try:
if domain.startswith('0x'):
domain = HexBytes(domain)
elif domain.startswith('{') and domain.endswith('}'):
domain = json.loads(domain)
else:
raise ValueError
except Exception as ex:
raise ValueError(f"Invalid EIP domain '{domain}'") from ex

if isinstance(domain, bytes):
if len(domain) != 32:
raise ValueError(f"Domain must be 32 bytes, but was {len(domain)}")
domain_hash = domain
else:
domain_hash: bytes = self._assert_domain(domain).hash_struct()

result = b"\x19\x01" + domain_hash + self.hash_struct()
return result

@classmethod
Expand Down Expand Up @@ -314,6 +357,9 @@ def __eq__(self, other):
def __hash__(self):
value_hashes = [hash(k) ^ hash(v) for k, v in self.values.items()]
return functools.reduce(operator.xor, value_hashes, hash(self.type_name))

def __repr__(self) -> str:
return f"{self.__class__.__name__}({str(self.data_dict())})"


class StructTuple(NamedTuple):
Expand Down
5 changes: 4 additions & 1 deletion eip712_structs/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def __init__(self, member_type: Union[EIP712Type, Type[EIP712Type]], fixed_lengt
def _encode_value(self, value):
"""Arrays are encoded by concatenating their encoded contents, and taking the keccak256 hash."""
encoder = self.member_type
encoded_values = [encoder.encode_value(v) for v in value]
if hasattr(encoder, "hash_struct"):
encoded_values = [v.hash_struct() for v in value]
else:
encoded_values = [encoder.encode_value(v) for v in value]
return keccak(b''.join(encoded_values))


Expand Down
13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@


NAME = 'eip712-structs'
VERSION = '1.1.0'
VERSION = '1001.1.0'

install_requirements = [
'eth-utils>=1.4.0',
'pysha3>=1.0.2',
]

test_requirements = [
'coveralls==1.8.0',
'pytest==4.6.2',
'pytest-cov==2.7.1',
'web3==4.9.2',
'coveralls>=1.8.0',
'pytest==6.2.5',
'pytest-cov==4.0.0',
'web3==5.31.3',
]


Expand Down Expand Up @@ -70,6 +70,9 @@ def run_tests(self):
packages=find_packages(),
install_requires=install_requirements,
tests_require=test_requirements,
extras_require={
"test": test_requirements,
},
cmdclass={
"test": PyTest,
"coveralls": CoverallsCommand,
Expand Down
1 change: 1 addition & 0 deletions tests/contracts/TestContract.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"name":"BarSig","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"Bar_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"FooSig","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"Foo_TYPEHASH","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes1[]","name":"arr","type":"bytes1[]"}],"name":"encodeBytes1Array","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint256","name":"bar_uint","type":"uint256"}],"internalType":"struct TestContract.Bar","name":"bar","type":"tuple"}],"name":"hashBarStruct","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"bar_uint","type":"uint256"}],"name":"hashBarStructFromParams","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"string","name":"s","type":"string"},{"internalType":"uint256","name":"u_i","type":"uint256"},{"internalType":"int8","name":"s_i","type":"int8"},{"internalType":"address","name":"a","type":"address"},{"internalType":"bool","name":"b","type":"bool"},{"internalType":"bytes30","name":"bytes_30","type":"bytes30"},{"internalType":"bytes","name":"dyn_bytes","type":"bytes"},{"components":[{"internalType":"uint256","name":"bar_uint","type":"uint256"}],"internalType":"struct TestContract.Bar","name":"bar","type":"tuple"},{"internalType":"bytes1[]","name":"arr","type":"bytes1[]"}],"internalType":"struct TestContract.Foo","name":"foo","type":"tuple"}],"name":"hashFooStruct","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"string","name":"s","type":"string"},{"internalType":"uint256","name":"u_i","type":"uint256"},{"internalType":"int8","name":"s_i","type":"int8"},{"internalType":"address","name":"a","type":"address"},{"internalType":"bool","name":"b","type":"bool"},{"internalType":"bytes30","name":"bytes_30","type":"bytes30"},{"internalType":"bytes","name":"dyn_bytes","type":"bytes"},{"internalType":"uint256","name":"bar_uint","type":"uint256"},{"internalType":"bytes1[]","name":"arr","type":"bytes1[]"}],"name":"hashFooStructFromParams","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"}]
Loading