-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_eoa.py
More file actions
77 lines (60 loc) · 2.48 KB
/
auth_eoa.py
File metadata and controls
77 lines (60 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Agent-key approval and auth setup (sync).
Demonstrates the full flow:
1. Generate a fresh agent key (LocalSigner.create()).
2. Build the EIP-712 typed data the user must sign to approve the agent.
3. Sign with the user's wallet (also a LocalSigner here for the demo).
4. Submit the approval to Hyperliquid.
5. Wire the agent into the adapter via ``auth.init_auth``.
In production, replace the user-side LocalSigner with whatever signs for
the user (e.g. a wallet popup in the UI, a hardware wallet bridge, KMS).
"""
from __future__ import annotations
import os
import time
from hip4 import (
LocalSigner,
create_hip4_adapter,
get_agent_approval_typed_data,
submit_agent_approval,
)
def main() -> None:
is_mainnet = os.environ.get("HIP4_NETWORK") == "mainnet"
# 1. Adapter on the chosen network.
adapter = create_hip4_adapter()
adapter.client.testnet = not is_mainnet # already true by default; explicit here for clarity.
# 2. User's wallet (in real life this is the connected wallet - Metamask, etc.)
user_pk = os.environ.get("HIP4_USER_PRIVATE_KEY")
if not user_pk:
raise SystemExit("Set HIP4_USER_PRIVATE_KEY to a hex private key for this demo.")
user_signer = LocalSigner(user_pk)
user_address = user_signer.get_address()
# 3. Agent key - a fresh, scoped signer used for order signing only.
agent_signer = LocalSigner.create()
agent_address = agent_signer.get_address()
agent_name = "hip4-py-demo"
# 4. Build the typed data the user signs to approve the agent.
typed = get_agent_approval_typed_data(
agent_address=agent_address,
agent_name=agent_name,
nonce=int(time.time() * 1000),
is_mainnet=is_mainnet,
)
sig = user_signer.sign_typed_data(typed["domain"], typed["types"], typed["message"])
# 5. Submit the approval.
result = submit_agent_approval(
signature=sig,
agent_address=agent_address,
agent_name=agent_name,
nonce=typed["message"]["nonce"],
is_mainnet=is_mainnet,
)
print("approve_agent:", result)
# 6. Wire the agent into the adapter - orders are now signed by the agent
# on behalf of the user's address.
adapter.auth.init_auth(user_address, agent_signer)
print("auth status:", adapter.auth.get_auth_status())
print("agent address (save this for future runs):", agent_address)
print("agent private key:", agent_signer.private_key)
adapter.destroy()
if __name__ == "__main__":
main()