Skip to content
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

Fix: force sig to include 0x prepended #35

Merged
merged 5 commits into from
Jul 29, 2024
Merged
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
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))
Loading