Skip to content

Commit 4189571

Browse files
committed
Add EIP-712 example
1 parent 8634806 commit 4189571

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

eth_defi/eip_712.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,50 @@
44
55
- `Based on Gnosis utilities <https://raw.githubusercontent.com/safe-global/safe-eth-py/master/gnosis/eth/eip712/__init__.py>`__.
66
7+
- Used in :py:mod:`eth_defi.usdc` module for crafting EIP-3009 messages
8+
9+
Example:
10+
11+
.. code-block:: python
12+
13+
data = {
14+
"types": {
15+
"EIP712Domain": [
16+
{"name": "name", "type": "string"},
17+
{"name": "version", "type": "string"},
18+
{"name": "chainId", "type": "uint256"},
19+
{"name": "verifyingContract", "type": "address"},
20+
],
21+
authorization_type.value: [
22+
{"name": "from", "type": "address"},
23+
{"name": "to", "type": "address"},
24+
{"name": "value", "type": "uint256"},
25+
{"name": "validAfter", "type": "uint256"},
26+
{"name": "validBefore", "type": "uint256"},
27+
{"name": "nonce", "type": "bytes32"},
28+
],
29+
},
30+
# domainSeparator = makeDomainSeparator(
31+
# "USD Coin",
32+
# "2",
33+
# 1, // hardcoded to 1 because of ganache bug: https://github.com/trufflesuite/ganache/issues/1643
34+
# getFiatToken().address
35+
# );
36+
"domain": {
37+
"name": token.name,
38+
"version": "2", # TODO: Read from USDC contract?
39+
"chainId": chain_id,
40+
"verifyingContract": token.address,
41+
},
42+
"primaryType": authorization_type.value,
43+
"message": {"from": from_, "to": to, "value": value, "validAfter": valid_after, "validBefore": valid_before, "nonce": secrets.token_bytes(32)}, # 256-bit random nonce
44+
}
45+
46+
message_hash = eip712_encode_hash(data)
47+
signed_message = local_account.signHash(message_hash)
48+
49+
Past copyright:
50+
751
.. code-block:: text
852
953
Copyright (C) 2022 Judd Vinet <[email protected]>

0 commit comments

Comments
 (0)