-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
210 lines (185 loc) · 6.57 KB
/
agent.ts
File metadata and controls
210 lines (185 loc) · 6.57 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
/**
* Auto-Claim Agent
*
* Runs on the user's device (CLI demo; browser extension in production).
* Discovers insurance bounties, monitors target URLs, evaluates conditions
* locally, and auto-submits TLSNotary proofs when a claim is triggered.
*
* The user installs the extension and browses normally.
* When a claimable event occurs, proof is generated and submitted
* without user action. Money they're already owed is auto-recovered.
*
* Usage:
* ANCHR_URL=http://localhost:3000 \
* deno run --allow-all --env example/auto-claim/agent.ts
*/
// Published package: import { Anchr } from "anchr-sdk";
import { Anchr } from "anchr-sdk";
import { spawn } from "@anchr/core-runtime";
const SERVER_URL = Deno.env.get("ANCHR_URL") ?? "http://localhost:3000";
const VERIFIER_HOST = Deno.env.get("TLSN_VERIFIER_HOST") ?? "localhost:7046";
const CHECK_INTERVAL_MS = Number(Deno.env.get("CHECK_INTERVAL_MS") ?? "10000");
const USER_PUBKEY = Deno.env.get("USER_PUBKEY") ?? "user-auto-claim";
const anchr = new Anchr({ serverUrl: SERVER_URL });
const claimed = new Set<string>(); // successfully claimed
const attempted = new Set<string>(); // proof attempted (avoid retry spam)
// --- Local condition evaluation ---
// Mirrors server-side evaluateCondition() to avoid unnecessary proof generation.
// Only generate proof when conditions are locally confirmed.
interface Condition {
type: string;
expression: string;
expected?: string;
description?: string;
}
function evaluateLocally(
body: string,
conditions: Condition[],
): { passed: boolean; details: string[] } {
const details: string[] = [];
for (const cond of conditions) {
switch (cond.type) {
case "contains": {
const ok = body.includes(cond.expression);
details.push(`${ok ? "✓" : "✗"} contains "${cond.expression}"`);
if (!ok) return { passed: false, details };
break;
}
case "regex": {
const match = new RegExp(cond.expression).exec(body);
details.push(
`${match ? "✓" : "✗"} regex → ${match ? match[0] : "no match"}`,
);
if (!match) return { passed: false, details };
break;
}
case "jsonpath": {
try {
const obj = JSON.parse(body);
const value = cond.expression
.split(".")
.reduce((o: Record<string, unknown>, k: string) => (o as Record<string, unknown>)?.[k] as Record<string, unknown>, obj);
const actual = String(value);
if (cond.expected !== undefined) {
const ok = actual === cond.expected;
details.push(
`${ok ? "✓" : "✗"} ${cond.expression} = "${actual}" (expected "${cond.expected}")`,
);
if (!ok) return { passed: false, details };
} else {
details.push(`✓ ${cond.expression} = "${actual}"`);
}
} catch {
details.push("✗ JSON parse failed");
return { passed: false, details };
}
break;
}
}
}
return { passed: true, details };
}
// --- Proof generation ---
async function generateProof(targetUrl: string): Promise<string> {
const outPath = `/tmp/auto-claim-proof-${Date.now()}.tlsn`;
const proc = spawn(
["tlsn-prove", "--verifier", VERIFIER_HOST, targetUrl, "-o", outPath],
{ stdout: "pipe", stderr: "pipe" },
);
await proc.exited;
if (proc.exitCode !== 0) {
const stderr = await new Response(proc.stderr).text();
throw new Error(`tlsn-prove failed: ${stderr}`);
}
const proofBytes = await Deno.readFile(outPath);
try { await Deno.remove(outPath); } catch { /* ignore */ }
return btoa(String.fromCharCode(...proofBytes));
}
// --- Display helpers ---
function formatFlight(body: string): string {
try {
const d = JSON.parse(body);
if (d.flight) {
return `${d.flight} → ${d.status}${d.delay_minutes > 0 ? ` (${d.delay_minutes} min delay)` : ""}`;
}
} catch { /* not flight JSON */ }
return "";
}
// --- Main loop ---
console.log("=== Auto-Claim Agent ===\n");
console.log(`Server: ${SERVER_URL}`);
console.log(`Check interval: ${CHECK_INTERVAL_MS / 1000}s`);
console.log(`\nScanning for claimable bounties...\n`);
while (true) {
try {
const queries = await anchr.listOpenQueries();
const policies = queries.filter((q) =>
q.description.startsWith("Auto-claim:")
);
if (policies.length === 0) {
console.log(`[${new Date().toISOString()}] No active policies found`);
}
for (const policy of policies) {
if (claimed.has(policy.id) || attempted.has(policy.id)) continue;
const reqs = policy.tlsn_requirements as {
target_url: string;
conditions?: Condition[];
} | undefined;
if (!reqs?.target_url) continue;
// 1. Fetch target (lightweight, no proof yet)
let body: string;
try {
const resp = await fetch(reqs.target_url);
body = await resp.text();
} catch (err) {
console.log(
`[${new Date().toISOString()}] Fetch error: ${err}`,
);
continue;
}
// 2. Evaluate conditions locally
const conditions = reqs.conditions ?? [];
const { passed, details } = evaluateLocally(body, conditions);
const display = formatFlight(body) || reqs.target_url;
if (!passed) {
console.log(
`[${new Date().toISOString()}] ${display} — no claim`,
);
continue;
}
// 3. Conditions met — claim!
console.log(
`\n[${new Date().toISOString()}] ${display} — CLAIM TRIGGERED!`,
);
for (const d of details) console.log(` ${d}`);
console.log(` Bounty: ${policy.bounty?.amount_sats ?? 0} sats`);
// 4. Generate TLSNotary proof and submit
try {
console.log(" Generating TLSNotary proof...");
const proof = await generateProof(reqs.target_url);
console.log(" Submitting claim...");
const result = await anchr.submitPresentation(
policy.id,
proof,
USER_PUBKEY,
);
if (result.ok) {
console.log(` ✓ Claim accepted! ${result.message}`);
claimed.add(policy.id);
} else {
console.log(` ✗ Claim rejected: ${result.message}`);
}
} catch (err) {
console.log(` ✗ Proof generation failed: ${err}`);
console.log(
" (In production, the browser extension handles this automatically)",
);
attempted.add(policy.id);
}
console.log();
}
} catch (err) {
console.error(`Error: ${err}`);
}
await new Promise((r) => setTimeout(r, CHECK_INTERVAL_MS));
}