Skip to content

Commit

Permalink
Merge pull request #34 from Polymarket/fix/fix-sha3
Browse files Browse the repository at this point in the history
Fix: Replace pysha3 with compatible libs
  • Loading branch information
JonathanAmenechi authored Jul 18, 2024
2 parents 41766b0 + eb5168d commit 113e913
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 85 deletions.
7 changes: 3 additions & 4 deletions py_order_utils/builders/base_builder.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from web3 import Web3
from ..signer import Signer
from ..utils import normalize_address
from eip712_structs import make_domain, EIP712Struct

from poly_eip712_structs import make_domain, EIP712Struct
from eth_utils import keccak

class BaseBuilder:
def __init__(
Expand Down Expand Up @@ -30,7 +29,7 @@ def _create_struct_hash(self, order: EIP712Struct):
"""
Creates an EIP712 compliant struct hash for the Order
"""
return Web3.keccak(order.signable_bytes(domain=self.domain_separator))
return "0x" + keccak(order.signable_bytes(domain=self.domain_separator)).hex()

def sign(self, struct_hash):
"""
Expand Down
2 changes: 1 addition & 1 deletion py_order_utils/builders/order_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def build_order_signature(self, _order: Order) -> str:
"""
Signs the order
"""
return self.sign(self._create_struct_hash(_order))
return "0x" + self.sign(self._create_struct_hash(_order))

def build_signed_order(self, data: OrderData) -> SignedOrder:
"""
Expand Down
2 changes: 1 addition & 1 deletion py_order_utils/model/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from ..constants import ZERO_ADDRESS
from .signatures import EOA
from eip712_structs import Address, EIP712Struct, Uint
from poly_eip712_structs import Address, EIP712Struct, Uint


@dataclass
Expand Down
18 changes: 2 additions & 16 deletions py_order_utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
import web3
from eth_utils import to_checksum_address
from string import punctuation
from random import random
from datetime import datetime, timezone
Expand All @@ -15,7 +15,7 @@ def normalize(s: str) -> str:


def normalize_address(address: str) -> str:
return web3.Web3.toChecksumAddress(address)
return to_checksum_address(address)


def generate_seed() -> int:
Expand All @@ -25,17 +25,3 @@ def generate_seed() -> int:
now = datetime.now().replace(tzinfo=timezone.utc).timestamp()
return round(now * random())


def hash_string(s: str):
return solidity_keccak("string", s)


def hash_bytes(b):
return solidity_keccak("bytes", b)


def solidity_keccak(typ, val):
return web3.Web3.solidityKeccak(
abi_types=[typ],
values=[val],
)
53 changes: 4 additions & 49 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,49 +1,4 @@
aiohttp==3.8.1
aiosignal==1.2.0
async-timeout==4.0.2
attrs==21.4.0
base58==2.1.1
bitarray==1.2.2
certifi==2021.10.8
charset-normalizer==2.0.10
cytoolz==0.11.2
eip712-structs==1.1.0
eth-abi==2.1.1
eth-account==0.5.6
eth-hash==0.3.2
eth-keyfile==0.5.1
eth-keys==0.3.4
eth-rlp==0.2.1
eth-typing==2.3.0
eth-utils==1.10.0
frozenlist==1.3.0
hexbytes==0.2.2
idna==3.3
iniconfig==1.1.1
ipfshttpclient==0.8.0a2
jsonschema==3.2.0
lru-dict==1.1.7
multiaddr==0.0.9
multidict==6.0.2
netaddr==0.8.0
packaging==21.3
parsimonious==0.8.1
pluggy==1.0.0
protobuf==3.19.3
py==1.11.0
pycryptodome==3.13.0
pyparsing==3.0.7
pyrsistent==0.18.1
pysha3==1.0.2
pytest==6.2.5
requests==2.27.1
rlp==2.0.1
six==1.16.0
toml==0.10.2
toolz==0.11.2
urllib3==1.26.8
varint==1.0.2
web3==5.26.0
websockets==9.1
yarl==1.7.2
black==22.3.0
eth-account===0.13.0
eth-utils===4.1.1
poly_eip712_structs==0.0.1
pytest==8.2.2
14 changes: 5 additions & 9 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.0",
version="0.3.1",
author="Polymarket Engineering",
author_email="[email protected]",
maintainer="Polymarket Engineering",
Expand All @@ -15,14 +15,10 @@
long_description_content_type="text/markdown",
url="https://github.com/polymarket/python-order-utils",
install_requires=[
"web3>=5.0.0,<6.0.0",
"eth-account>=0.4.0,<0.6.0",
"eip712-structs",
"pytest",
"eth-abi",
"eth_typing",
"eth_utils",
"eth_utils",
"eth-utils>=4.1.1",
"eth-account>=0.13.0",
"poly-eip712-structs",
"pytest"
],
package_data={
"py_order_utils": [
Expand Down
9 changes: 4 additions & 5 deletions tests/test_order_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def test_build_order_neg_risk(self):
self.assertEqual(BUY, _order["side"])
self.assertEqual(EOA, _order["signatureType"])

def test_build_prder_signature(self):
def test_build_order_signature(self):
builder = OrderBuilder(
amoy_contracts["exchange"], chain_id, signer, mock_salt_generator
)
Expand All @@ -200,13 +200,13 @@ def test_build_prder_signature(self):
"0x02ca1d1aa31103804173ad1acd70066cb6c1258a4be6dada055111f9a7ea4e55"
)
struct_hash = builder._create_struct_hash(_order)
self.assertEqual(expected_struct_hash, struct_hash.hex())
self.assertEqual(expected_struct_hash, struct_hash)

expected_signature = "0x302cd9abd0b5fcaa202a344437ec0b6660da984e24ae9ad915a592a90facf5a51bb8a873cd8d270f070217fea1986531d5eec66f1162a81f66e026db653bf7ce1c"
sig = builder.build_order_signature(_order)
self.assertEqual(expected_signature, sig)

def test_build_prder_signature_neg_risk(self):
def test_build_order_signature_neg_risk(self):
builder = OrderBuilder(
amoy_contracts["negRiskExchange"], chain_id, signer, mock_salt_generator
)
Expand All @@ -218,11 +218,10 @@ def test_build_prder_signature_neg_risk(self):
"0xf15790d3edc4b5aed427b0b543a9206fcf4b1a13dfed016d33bfb313076263b8"
)
struct_hash = builder._create_struct_hash(_order)
self.assertEqual(expected_struct_hash, struct_hash.hex())
self.assertEqual(expected_struct_hash, struct_hash)

expected_signature = "0x1b3646ef347e5bd144c65bd3357ba19c12c12abaeedae733cf8579bc51a2752c0454c3bc6b236957e393637982c769b8dc0706c0f5c399983d933850afd1cbcd1c"
sig = builder.build_order_signature(_order)
print(sig)
self.assertEqual(expected_signature, sig)

def test_build_signed_order(self):
Expand Down

0 comments on commit 113e913

Please sign in to comment.