diff --git a/py_order_utils/builders/base_builder.py b/py_order_utils/builders/base_builder.py index 16e0577..13853f2 100644 --- a/py_order_utils/builders/base_builder.py +++ b/py_order_utils/builders/base_builder.py @@ -1,8 +1,9 @@ from ..signer import Signer -from ..utils import normalize_address +from ..utils import normalize_address, prepend_zx from poly_eip712_structs import make_domain, EIP712Struct from eth_utils import keccak + class BaseBuilder: def __init__( self, exchange_address: str, chain_id: int, signer: Signer, salt_generator @@ -29,7 +30,9 @@ def _create_struct_hash(self, order: EIP712Struct): """ Creates an EIP712 compliant struct hash for the Order """ - return "0x" + keccak(order.signable_bytes(domain=self.domain_separator)).hex() + return prepend_zx( + keccak(order.signable_bytes(domain=self.domain_separator)).hex() + ) def sign(self, struct_hash): """ diff --git a/py_order_utils/builders/order_builder.py b/py_order_utils/builders/order_builder.py index 88dec6a..380696a 100644 --- a/py_order_utils/builders/order_builder.py +++ b/py_order_utils/builders/order_builder.py @@ -1,7 +1,7 @@ from ..signer import Signer from .base_builder import BaseBuilder from .exception import ValidationException -from ..utils import generate_seed, normalize_address +from ..utils import generate_seed, normalize_address, prepend_zx from ..model.order import Order, SignedOrder, OrderData from ..model.sides import BUY, SELL from ..model.signatures import EOA, POLY_GNOSIS_SAFE, POLY_PROXY @@ -59,7 +59,7 @@ def build_order_signature(self, _order: Order) -> str: """ Signs the order """ - return "0x" + self.sign(self._create_struct_hash(_order)) + return prepend_zx(self.sign(self._create_struct_hash(_order))) def build_signed_order(self, data: OrderData) -> SignedOrder: """ diff --git a/py_order_utils/utils.py b/py_order_utils/utils.py index 2756d3d..99e1983 100644 --- a/py_order_utils/utils.py +++ b/py_order_utils/utils.py @@ -25,3 +25,12 @@ def generate_seed() -> int: now = datetime.now().replace(tzinfo=timezone.utc).timestamp() return round(now * random()) + +def prepend_zx(in_str: str) -> str: + """ + Prepend 0x to the input string if it is missing + """ + s = in_str + if len(s) > 2 and s[:2] != "0x": + s = f"0x{s}" + return s diff --git a/setup.py b/setup.py index 9e014de..6b24d54 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py_order_utils", - version="0.3.1", + version="0.3.2", author="Polymarket Engineering", author_email="engineering@polymarket.com", maintainer="Polymarket Engineering", @@ -18,7 +18,7 @@ "eth-utils>=4.1.1", "eth-account>=0.13.0", "poly-eip712-structs", - "pytest" + "pytest", ], package_data={ "py_order_utils": [ diff --git a/tests/test_utils.py b/tests/test_utils.py index 499297d..57c6254 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,5 +1,5 @@ from unittest import TestCase -from py_order_utils.utils import generate_seed, normalize_address +from py_order_utils.utils import generate_seed, normalize_address, prepend_zx class TestUtils(TestCase): @@ -11,3 +11,10 @@ def test_normalize_address(self): def test_generate_seed(self): self.assertIsNotNone(generate_seed()) + + def test_prepend_zx(self): + s = "302cd9abd0b5fcaa202a344437ec0b6660da984e24ae9ad915a592a90facf5a51bb8a873cd8d270f070217fea1986531d5eec66f1162a81f66e026db653bf7ce1c" + self.assertEqual("0x" + s, prepend_zx(s)) + + s = "02ca1d1aa31103804173ad1acd70066cb6c1258a4be6dada055111f9a7ea4e55" + self.assertEqual("0x" + s, prepend_zx(s))