|
1 | | -import sha3 |
2 | 1 | import requests |
3 | 2 | from decimal import Decimal |
4 | | -from eip712_structs import make_domain, EIP712Struct, String |
5 | | - |
6 | | -class OrderInfo(EIP712Struct): |
7 | | - SubaccountId = String() |
8 | | - FeeRecipient = String() |
9 | | - Price = String() |
10 | | - Quantity = String() |
11 | | - |
12 | | -class SpotOrder(EIP712Struct): |
13 | | - MarketId = String() |
14 | | - OrderInfo = OrderInfo |
15 | | - Salt = String() |
16 | | - OrderType = String() |
17 | | - TriggerPrice = String() |
18 | | - |
19 | | -class DerivativeOrder(EIP712Struct): |
20 | | - MarketId = String() |
21 | | - OrderInfo = OrderInfo |
22 | | - OrderType = String() |
23 | | - Margin = String() |
24 | | - TriggerPrice = String() |
25 | | - Salt = String() |
26 | | - |
27 | | -EIP712_domain = make_domain( |
28 | | - name='Injective Protocol', |
29 | | - version='2.0.0', |
30 | | - chainId=888, |
31 | | - verifyingContract='0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC', |
32 | | - salt='0x0000000000000000000000000000000000000000000000000000000000000000' |
33 | | -) |
34 | | - |
35 | | -domain_separator = EIP712_domain.hash_struct() |
| 3 | + |
| 4 | +from eip712.messages import EIP712Message, EIP712Type |
| 5 | +from eth_account.messages import _hash_eip191_message as hash_eip191_message |
| 6 | +from hexbytes import HexBytes |
| 7 | + |
| 8 | + |
| 9 | +class OrderInfo(EIP712Type): |
| 10 | + SubaccountId: "string" |
| 11 | + FeeRecipient: "string" |
| 12 | + Price: "string" |
| 13 | + Quantity: "string" |
| 14 | + |
| 15 | + |
| 16 | +class SpotOrder(EIP712Message): |
| 17 | + _name_ = "Injective Protocol" |
| 18 | + _version_ = "2.0.0" |
| 19 | + _chainId_ = 888 |
| 20 | + _verifyingContract_ = "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" |
| 21 | + _salt_ = HexBytes("0x0000000000000000000000000000000000000000000000000000000000000000") |
| 22 | + |
| 23 | + MarketId: "string" |
| 24 | + OrderInfo: OrderInfo |
| 25 | + Salt: "string" |
| 26 | + OrderType: "string" |
| 27 | + TriggerPrice: "string" |
| 28 | + |
| 29 | + |
| 30 | +class DerivativeOrder(EIP712Message): |
| 31 | + _name_ = "Injective Protocol" |
| 32 | + _version_ = "2.0.0" |
| 33 | + _chainId_ = 888 |
| 34 | + _verifyingContract_ = "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" |
| 35 | + _salt_ = HexBytes("0x0000000000000000000000000000000000000000000000000000000000000000") |
| 36 | + |
| 37 | + MarketId: "string" |
| 38 | + OrderInfo: OrderInfo |
| 39 | + OrderType: "string" |
| 40 | + Margin: "string" |
| 41 | + TriggerPrice: "string" |
| 42 | + Salt: "string" |
| 43 | + |
| 44 | + |
| 45 | +# domain_separator = EIP712_domain.hash_struct() |
36 | 46 | order_type_dict = {0: '\x00', 1: '\x01', 2: '\x02', 3: '\x03', 4: '\x04', 5: '\x05', 6: '\x06', 7: '\x07', 8: '\x08'} |
37 | 47 |
|
38 | 48 | class OrderHashResponse: |
@@ -127,8 +137,6 @@ def build_eip712_msg(order, nonce): |
127 | 137 | ) |
128 | 138 |
|
129 | 139 | def hash_order(msg): |
130 | | - typed_data_hash = msg.hash_struct() |
131 | | - typed_bytes = b'\x19\x01' + domain_separator + typed_data_hash |
132 | | - keccak256 = sha3.keccak_256() |
133 | | - keccak256.update(typed_bytes) |
134 | | - return '0x' + keccak256.hexdigest() |
| 140 | + signable_message = msg.signable_message |
| 141 | + hex_digest = hash_eip191_message(signable_message=signable_message).hex() |
| 142 | + return "0x" + hex_digest |
0 commit comments