Skip to content

Commit

Permalink
Merge pull request #35 from Polymarket/fix/force-zx-in-sig
Browse files Browse the repository at this point in the history
Fix: force sig to include 0x prepended
  • Loading branch information
JonathanAmenechi authored Jul 29, 2024
2 parents 113e913 + aba1c4f commit 2f173a3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
7 changes: 5 additions & 2 deletions py_order_utils/builders/base_builder.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions py_order_utils/builders/order_builder.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
"""
Expand Down
9 changes: 9 additions & 0 deletions py_order_utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="py_order_utils",
version="0.3.1",
version="0.3.2",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand All @@ -18,7 +18,7 @@
"eth-utils>=4.1.1",
"eth-account>=0.13.0",
"poly-eip712-structs",
"pytest"
"pytest",
],
package_data={
"py_order_utils": [
Expand Down
9 changes: 8 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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))

0 comments on commit 2f173a3

Please sign in to comment.