-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt.py
More file actions
81 lines (66 loc) · 2.42 KB
/
jwt.py
File metadata and controls
81 lines (66 loc) · 2.42 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
78
79
80
81
import os
import asyncio
from eth_account import Account
from eth_account.messages import encode_defunct
import httpx
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Create a wallet to sign the message (must be the orders' `maker`)
private_key = os.getenv("WALLET_PRIVATE_KEY")
account = Account.from_key(private_key)
# Load API key from environment variables
api_key = os.getenv("API_KEY")
async def main():
async with httpx.AsyncClient() as client:
# step-1 GET auth/message
# Send the `GET auth/message` request
message_response = await client.get(
"https://api.predict.fun/v1/auth/message",
headers={
"x-api-key": api_key,
}
)
# Await for the response
message_data = message_response.json()
print(f"Message Response Status: {message_response.status_code}")
print(f"Message Response: {message_data}")
# Retrieve the message to sign
message = message_data["data"]["message"]
# Sign the message
message_hash = encode_defunct(text=message)
signed_message = account.sign_message(message_hash)
signature = signed_message.signature.hex()
# Add 0x prefix if not present
if not signature.startswith('0x'):
signature = '0x' + signature
print(f"\nSigner Address: {account.address}")
print(f"Message to sign: {message}")
print(f"Signature: {signature}")
# Send the `POST auth` request
jwt_response = await client.post(
"https://api.predict.fun/v1/auth",
headers={
"Content-Type": "application/json",
"x-api-key": api_key,
},
json={
"signer": account.address,
"message": message,
"signature": signature,
},
)
# Await for the response
jwt_data = jwt_response.json()
print(f"\nJWT Response Status: {jwt_response.status_code}")
print(f"JWT Response: {jwt_data}")
# Fetch the JWT token
if jwt_response.status_code == 200 and "data" in jwt_data:
jwt = jwt_data["data"]["token"]
print(f"\nJWT Token: {jwt}")
return jwt
else:
print(f"\nError: {jwt_data.get('message', 'Unknown error')}")
return None
if __name__ == "__main__":
asyncio.run(main())