From 0db46185d7c10918945d1bc8fe56ea875a40384a Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:25:01 -0400 Subject: [PATCH 1/5] fix: force sig to include 0x prepended --- py_order_utils/builders/order_builder.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/py_order_utils/builders/order_builder.py b/py_order_utils/builders/order_builder.py index 88dec6a..065e2b0 100644 --- a/py_order_utils/builders/order_builder.py +++ b/py_order_utils/builders/order_builder.py @@ -59,7 +59,10 @@ def build_order_signature(self, _order: Order) -> str: """ Signs the order """ - return "0x" + self.sign(self._create_struct_hash(_order)) + sig = self.sign(self._create_struct_hash(_order)) + if len(sig) > 2 and sig[:2] != "0x": + sig = f"0x{sig}" + return sig def build_signed_order(self, data: OrderData) -> SignedOrder: """ From 60c75e173a56fc52275bc8ea4c16cf2c3aa9d2e1 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:31:48 -0400 Subject: [PATCH 2/5] chore: ensure 0x is always prepended --- py_order_utils/builders/base_builder.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/py_order_utils/builders/base_builder.py b/py_order_utils/builders/base_builder.py index 16e0577..79e4e72 100644 --- a/py_order_utils/builders/base_builder.py +++ b/py_order_utils/builders/base_builder.py @@ -29,7 +29,10 @@ 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() + struct_hash = keccak(order.signable_bytes(domain=self.domain_separator)).hex() + if len(struct_hash) > 2 and struct_hash[:2] != "0x": + struct_hash = f"0x{struct_hash}" + return struct_hash def sign(self, struct_hash): """ From b190219e740d4d6e5e75be14acdb09e141c65cb2 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:43:07 -0400 Subject: [PATCH 3/5] fix: util --- py_order_utils/builders/base_builder.py | 7 ++----- py_order_utils/builders/order_builder.py | 7 ++----- py_order_utils/utils.py | 9 +++++++++ tests/test_utils.py | 11 ++++++++++- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/py_order_utils/builders/base_builder.py b/py_order_utils/builders/base_builder.py index 79e4e72..c41b916 100644 --- a/py_order_utils/builders/base_builder.py +++ b/py_order_utils/builders/base_builder.py @@ -1,5 +1,5 @@ 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 @@ -29,10 +29,7 @@ def _create_struct_hash(self, order: EIP712Struct): """ Creates an EIP712 compliant struct hash for the Order """ - struct_hash = keccak(order.signable_bytes(domain=self.domain_separator)).hex() - if len(struct_hash) > 2 and struct_hash[:2] != "0x": - struct_hash = f"0x{struct_hash}" - return struct_hash + 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 065e2b0..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,10 +59,7 @@ def build_order_signature(self, _order: Order) -> str: """ Signs the order """ - sig = self.sign(self._create_struct_hash(_order)) - if len(sig) > 2 and sig[:2] != "0x": - sig = f"0x{sig}" - return sig + 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/tests/test_utils.py b/tests/test_utils.py index 499297d..41141b7 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,12 @@ 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)) + + From 833754599a3d16bf37b2f75cd2a53529bc2dcfb2 Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:44:32 -0400 Subject: [PATCH 4/5] chore: fm --- py_order_utils/builders/base_builder.py | 5 ++++- setup.py | 2 +- tests/test_utils.py | 4 +--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/py_order_utils/builders/base_builder.py b/py_order_utils/builders/base_builder.py index c41b916..13853f2 100644 --- a/py_order_utils/builders/base_builder.py +++ b/py_order_utils/builders/base_builder.py @@ -3,6 +3,7 @@ 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 prepend_zx(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/setup.py b/setup.py index 9e014de..968d5aa 100644 --- a/setup.py +++ b/setup.py @@ -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 41141b7..57c6254 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -15,8 +15,6 @@ def test_generate_seed(self): def test_prepend_zx(self): s = "302cd9abd0b5fcaa202a344437ec0b6660da984e24ae9ad915a592a90facf5a51bb8a873cd8d270f070217fea1986531d5eec66f1162a81f66e026db653bf7ce1c" self.assertEqual("0x" + s, prepend_zx(s)) - + s = "02ca1d1aa31103804173ad1acd70066cb6c1258a4be6dada055111f9a7ea4e55" self.assertEqual("0x" + s, prepend_zx(s)) - - From aba1c4f7d819ee72e36fb5c7ae5d500da26887ca Mon Sep 17 00:00:00 2001 From: JonathanAmenechi <7217608+JonathanAmenechi@users.noreply.github.com> Date: Mon, 29 Jul 2024 18:44:56 -0400 Subject: [PATCH 5/5] chore: bump version --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 968d5aa..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",