@obelyzk/sdk 1.0.1 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,206 @@
1
+ // src/firewall/client.ts
2
+ import { RpcProvider, Contract, CallData } from "starknet";
3
+ var AgentFirewallSDK = class {
4
+ config;
5
+ provider;
6
+ constructor(config) {
7
+ this.config = config;
8
+ this.provider = new RpcProvider({ nodeUrl: config.rpcUrl });
9
+ }
10
+ // ── Classification ─────────────────────────────────────────────────
11
+ /**
12
+ * Classify a transaction through the ZKML classifier.
13
+ *
14
+ * Sends the transaction features to the prove-server, which runs
15
+ * the MLP classifier and generates a GKR+STARK proof. Returns the
16
+ * proven threat score and decision.
17
+ *
18
+ * Does NOT submit anything on-chain — use `evaluateAction()` for
19
+ * the full flow including on-chain submission.
20
+ */
21
+ async classify(tx) {
22
+ const headers = {
23
+ "Content-Type": "application/json"
24
+ };
25
+ if (this.config.apiKey) {
26
+ headers["Authorization"] = `Bearer ${this.config.apiKey}`;
27
+ }
28
+ const body = {
29
+ target: tx.target,
30
+ value: tx.value || "0",
31
+ selector: tx.selector || "0x0",
32
+ calldata: tx.calldata || "0x",
33
+ agent_trust_score: tx.agentTrustScore || 0,
34
+ agent_strikes: tx.agentStrikes || 0,
35
+ agent_age_blocks: tx.agentAgeBlocks || 0,
36
+ target_verified: tx.targetVerified || false,
37
+ target_is_proxy: tx.targetIsProxy || false,
38
+ target_has_source: tx.targetHasSource || false,
39
+ target_interaction_count: tx.targetInteractionCount || 0,
40
+ tx_frequency: tx.txFrequency || 0,
41
+ unique_targets_24h: tx.uniqueTargets24h || 0,
42
+ avg_value_24h: tx.avgValue24h || 0,
43
+ max_value_24h: tx.maxValue24h || 0
44
+ };
45
+ const response = await fetch(`${this.config.proverUrl}/api/v1/classify`, {
46
+ method: "POST",
47
+ headers,
48
+ body: JSON.stringify(body)
49
+ });
50
+ if (!response.ok) {
51
+ const error = await response.json().catch(() => ({ error: response.statusText }));
52
+ throw new Error(`Classification failed (${response.status}): ${error.error || response.statusText}`);
53
+ }
54
+ const data = await response.json();
55
+ return {
56
+ requestId: data.request_id,
57
+ decision: data.decision,
58
+ threatScore: data.threat_score,
59
+ scores: data.scores,
60
+ ioCommitment: data.io_commitment,
61
+ policyCommitment: data.policy_commitment,
62
+ proveTimeMs: data.prove_time_ms
63
+ };
64
+ }
65
+ // ── Agent Management ───────────────────────────────────────────────
66
+ /**
67
+ * Register a new agent on the firewall contract.
68
+ * The calling account becomes the agent owner.
69
+ */
70
+ async registerAgent(agentId) {
71
+ this.requireAccount();
72
+ const tx = await this.config.account.execute({
73
+ contractAddress: this.config.firewallContract,
74
+ entrypoint: "register_agent",
75
+ calldata: CallData.compile({ agent_id: agentId })
76
+ });
77
+ await this.provider.waitForTransaction(tx.transaction_hash);
78
+ return tx.transaction_hash;
79
+ }
80
+ /** Deactivate an agent (owner or contract admin). */
81
+ async deactivateAgent(agentId) {
82
+ this.requireAccount();
83
+ const tx = await this.config.account.execute({
84
+ contractAddress: this.config.firewallContract,
85
+ entrypoint: "deactivate_agent",
86
+ calldata: CallData.compile({ agent_id: agentId })
87
+ });
88
+ await this.provider.waitForTransaction(tx.transaction_hash);
89
+ return tx.transaction_hash;
90
+ }
91
+ /** Reactivate an agent and reset strikes (agent owner only). */
92
+ async reactivateAgent(agentId) {
93
+ this.requireAccount();
94
+ const tx = await this.config.account.execute({
95
+ contractAddress: this.config.firewallContract,
96
+ entrypoint: "reactivate_agent",
97
+ calldata: CallData.compile({ agent_id: agentId })
98
+ });
99
+ await this.provider.waitForTransaction(tx.transaction_hash);
100
+ return tx.transaction_hash;
101
+ }
102
+ // ── Queries ────────────────────────────────────────────────────────
103
+ /** Get the full status of an agent. */
104
+ async getAgentStatus(agentId) {
105
+ const contract = new Contract(
106
+ FIREWALL_ABI,
107
+ this.config.firewallContract,
108
+ this.provider
109
+ );
110
+ const [registered, active, trustScore, strikes, trusted] = await Promise.all([
111
+ contract.is_agent_registered(agentId),
112
+ contract.is_agent_active(agentId),
113
+ contract.get_trust_score(agentId),
114
+ contract.get_strikes(agentId),
115
+ contract.is_trusted(agentId)
116
+ ]);
117
+ return {
118
+ registered,
119
+ active,
120
+ trustScore: Number(trustScore),
121
+ strikes: Number(strikes),
122
+ trusted
123
+ };
124
+ }
125
+ /** Check if a specific action has been approved. */
126
+ async isActionApproved(actionId) {
127
+ const contract = new Contract(
128
+ FIREWALL_ABI,
129
+ this.config.firewallContract,
130
+ this.provider
131
+ );
132
+ return contract.is_action_approved(actionId);
133
+ }
134
+ /** Check if an agent is trusted. */
135
+ async isAgentTrusted(agentId) {
136
+ const contract = new Contract(
137
+ FIREWALL_ABI,
138
+ this.config.firewallContract,
139
+ this.provider
140
+ );
141
+ return contract.is_trusted(agentId);
142
+ }
143
+ // ── Helpers ────────────────────────────────────────────────────────
144
+ requireAccount() {
145
+ if (!this.config.account) {
146
+ throw new Error(
147
+ "Account required for write operations. Pass `account` in FirewallConfig."
148
+ );
149
+ }
150
+ }
151
+ };
152
+ var FIREWALL_ABI = [
153
+ {
154
+ name: "is_agent_registered",
155
+ type: "function",
156
+ inputs: [{ name: "agent_id", type: "felt" }],
157
+ outputs: [{ type: "felt" }],
158
+ state_mutability: "view"
159
+ },
160
+ {
161
+ name: "is_agent_active",
162
+ type: "function",
163
+ inputs: [{ name: "agent_id", type: "felt" }],
164
+ outputs: [{ type: "felt" }],
165
+ state_mutability: "view"
166
+ },
167
+ {
168
+ name: "get_trust_score",
169
+ type: "function",
170
+ inputs: [{ name: "agent_id", type: "felt" }],
171
+ outputs: [{ type: "felt" }],
172
+ state_mutability: "view"
173
+ },
174
+ {
175
+ name: "get_strikes",
176
+ type: "function",
177
+ inputs: [{ name: "agent_id", type: "felt" }],
178
+ outputs: [{ type: "felt" }],
179
+ state_mutability: "view"
180
+ },
181
+ {
182
+ name: "is_trusted",
183
+ type: "function",
184
+ inputs: [{ name: "agent_id", type: "felt" }],
185
+ outputs: [{ type: "felt" }],
186
+ state_mutability: "view"
187
+ },
188
+ {
189
+ name: "is_action_approved",
190
+ type: "function",
191
+ inputs: [{ name: "action_id", type: "felt" }],
192
+ outputs: [{ type: "felt" }],
193
+ state_mutability: "view"
194
+ },
195
+ {
196
+ name: "get_action_decision",
197
+ type: "function",
198
+ inputs: [{ name: "action_id", type: "felt" }],
199
+ outputs: [{ type: "felt" }],
200
+ state_mutability: "view"
201
+ }
202
+ ];
203
+
204
+ export {
205
+ AgentFirewallSDK
206
+ };