forked from douglasborthwick-crypto/insumer-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify.py
More file actions
265 lines (224 loc) · 7.61 KB
/
verify.py
File metadata and controls
265 lines (224 loc) · 7.61 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
"""
InsumerAPI — Python example
Demonstrates:
1. Getting a free API key
2. Verifying token holdings (POST /v1/attest)
3. Checking NFT ownership
4. Multi-condition verification across chains
5. Listing merchants and checking discounts
6. XRPL native XRP verification
7. XRPL trust line token (RLUSD) verification
Usage:
pip install httpx
INSUMER_API_KEY=insr_live_... python verify.py
Or get a new key automatically:
INSUMER_EMAIL=you@example.com python verify.py
"""
import os
import sys
import json
import httpx
API = "https://api.insumermodel.com"
KEY_URL = "https://api.insumermodel.com/v1/keys/create"
# Vitalik's public wallet (for demo purposes)
DEMO_WALLET = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
# SHIB on Ethereum
SHIB_CONTRACT = "0x95aD61b0a150d79219dCF64E1E6Cc01f0B64C4cE"
# BAYC on Ethereum
BAYC_CONTRACT = "0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D"
def get_api_key() -> str:
"""Get API key from env or create a new free one."""
key = os.environ.get("INSUMER_API_KEY")
if key:
return key
email = os.environ.get("INSUMER_EMAIL")
if not email:
print("Set INSUMER_API_KEY or INSUMER_EMAIL environment variable")
sys.exit(1)
print(f"Creating free API key for {email}...")
resp = httpx.post(KEY_URL, json={
"email": email,
"appName": "python-example",
"tier": "free",
})
data = resp.json()
if not data.get("success"):
print(f"Failed to create key: {data}")
sys.exit(1)
key = data["key"]
print(f"Got key: {key[:20]}...")
print(f"Credits: {data.get('apiKeyCredits', 'N/A')}")
print(f"Daily limit: {data.get('dailyLimit', 'N/A')}")
print()
return key
def check_rpc_failure(resp: httpx.Response, result: dict) -> bool:
"""Check for rpc_failure (503) — retryable, NOT a verification failure."""
if resp.status_code == 503 and result.get("error", {}).get("code") == "rpc_failure":
print(" rpc_failure: data source temporarily unavailable")
for fc in result["error"].get("failedConditions", []):
print(f" {fc['source']} chain {fc.get('chainId', '?')}: {fc['message']}")
print(" Retry after 2-5 seconds. Do NOT treat as pass: false.")
return True
return False
def verify_token(client: httpx.Client, wallet: str, contract: str,
chain_id: int, threshold: int, label: str) -> dict:
"""Verify a single token balance condition."""
resp = client.post(f"{API}/v1/attest", json={
"wallet": wallet,
"conditions": [{
"type": "token_balance",
"contractAddress": contract,
"chainId": chain_id,
"threshold": threshold,
"label": label,
}],
})
result = resp.json()
if check_rpc_failure(resp, result):
return result
return result
def verify_nft(client: httpx.Client, wallet: str, contract: str,
chain_id: int, label: str) -> dict:
"""Verify NFT ownership."""
resp = client.post(f"{API}/v1/attest", json={
"wallet": wallet,
"conditions": [{
"type": "nft_ownership",
"contractAddress": contract,
"chainId": chain_id,
"label": label,
}],
})
return resp.json()
def multi_verify(client: httpx.Client, wallet: str,
conditions: list) -> dict:
"""Verify multiple conditions in a single call (up to 10)."""
resp = client.post(f"{API}/v1/attest", json={
"wallet": wallet,
"conditions": conditions,
})
return resp.json()
def check_discount(client: httpx.Client, wallet: str,
merchant: str) -> dict:
"""Check what discount a wallet qualifies for at a merchant."""
resp = client.get(f"{API}/v1/discount/check", params={
"wallet": wallet,
"merchant": merchant,
})
return resp.json()
def list_merchants(client: httpx.Client) -> dict:
"""List available merchants."""
resp = client.get(f"{API}/v1/merchants")
return resp.json()
def check_credits(client: httpx.Client) -> dict:
"""Check remaining verification credits."""
resp = client.get(f"{API}/v1/credits")
return resp.json()
def main():
api_key = get_api_key()
client = httpx.Client(headers={
"Content-Type": "application/json",
"X-API-Key": api_key,
})
# --- 1. Verify SHIB holdings on Ethereum ---
print("=" * 60)
print("1. Verify SHIB holdings on Ethereum")
print("=" * 60)
result = verify_token(
client, DEMO_WALLET, SHIB_CONTRACT,
chain_id=1, threshold=1_000_000, label="SHIB holder"
)
print(json.dumps(result, indent=2))
print()
# --- 2. Verify BAYC NFT ownership ---
print("=" * 60)
print("2. Verify BAYC NFT ownership")
print("=" * 60)
result = verify_nft(
client, DEMO_WALLET, BAYC_CONTRACT,
chain_id=1, label="BAYC owner"
)
print(json.dumps(result, indent=2))
print()
# --- 3. Multi-condition verification ---
print("=" * 60)
print("3. Multi-condition: SHIB + BAYC in one call")
print("=" * 60)
result = multi_verify(client, DEMO_WALLET, [
{
"type": "token_balance",
"contractAddress": SHIB_CONTRACT,
"chainId": 1,
"threshold": 1_000_000,
"label": "SHIB holder",
},
{
"type": "nft_ownership",
"contractAddress": BAYC_CONTRACT,
"chainId": 1,
"label": "BAYC owner",
},
])
print(json.dumps(result, indent=2))
if result.get("ok"):
attestation = result["data"]["attestation"]
print(f"\nOverall pass: {attestation['pass']}")
for r in attestation["results"]:
print(f" {r['label']}: {'PASS' if r['met'] else 'FAIL'}")
print(f"Signature: {result['data']['sig'][:40]}...")
print()
# --- 4. List merchants ---
print("=" * 60)
print("4. List merchants")
print("=" * 60)
result = list_merchants(client)
if result.get("ok"):
merchants = result["data"] if isinstance(result["data"], list) else []
print(f"Found {len(merchants)} merchant(s)")
for m in merchants[:5]:
print(f" {m.get('id', 'N/A')}: {m.get('companyName', 'N/A')}")
else:
print(json.dumps(result, indent=2))
print()
# --- 5. Check credits ---
print("=" * 60)
print("5. Check remaining credits")
print("=" * 60)
result = check_credits(client)
print(json.dumps(result, indent=2))
print()
# --- 6. XRPL — native XRP balance ---
print("=" * 60)
print("6. XRPL — Verify native XRP balance")
print("=" * 60)
resp = client.post(f"{API}/v1/attest", json={
"xrplWallet": "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn",
"conditions": [{
"type": "token_balance",
"contractAddress": "native",
"chainId": "xrpl",
"threshold": 100,
"label": "XRP >= 100",
}],
})
print(json.dumps(resp.json(), indent=2))
print()
# --- 7. XRPL — RLUSD trust line token ---
print("=" * 60)
print("7. XRPL — Verify RLUSD trust line balance")
print("=" * 60)
resp = client.post(f"{API}/v1/attest", json={
"xrplWallet": "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn",
"conditions": [{
"type": "token_balance",
"contractAddress": "rMxCKbEDwqr76QuheSUMdEGf4B9xJ8m5De",
"chainId": "xrpl",
"currency": "RLUSD",
"threshold": 10,
"label": "RLUSD >= 10 on XRPL",
}],
})
print(json.dumps(resp.json(), indent=2))
client.close()
if __name__ == "__main__":
main()