@obelyzk/sdk 1.3.0 → 1.5.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,199 @@
1
+ import * as starknet from 'starknet';
2
+
3
+ /**
4
+ * Types for the ZKML Agent Firewall SDK.
5
+ *
6
+ * These types map 1:1 to the Rust classifier and Cairo AgentFirewallZK contract.
7
+ */
8
+ /** Transaction features sent to the classifier. */
9
+ interface TransactionFeatures {
10
+ /** Target contract address (hex, 0x-prefixed). */
11
+ target: string;
12
+ /** Transaction value (decimal string, u256). */
13
+ value?: string;
14
+ /** Function selector (hex, 4 bytes, 0x-prefixed). */
15
+ selector?: string;
16
+ /** Calldata (hex, after selector, 0x-prefixed). */
17
+ calldata?: string;
18
+ /** Agent's current trust score (0-100000). */
19
+ agentTrustScore?: number;
20
+ /** Agent's current strike count. */
21
+ agentStrikes?: number;
22
+ /** Agent age in blocks since registration. */
23
+ agentAgeBlocks?: number;
24
+ /** Target contract metadata. */
25
+ targetVerified?: boolean;
26
+ targetIsProxy?: boolean;
27
+ targetHasSource?: boolean;
28
+ targetInteractionCount?: number;
29
+ /** Behavioral features. */
30
+ txFrequency?: number;
31
+ uniqueTargets24h?: number;
32
+ avgValue24h?: number;
33
+ maxValue24h?: number;
34
+ }
35
+ /** Classifier decision. */
36
+ type Decision = "approve" | "escalate" | "block";
37
+ /** Result from evaluating a transaction through the ZKML classifier. */
38
+ interface ClassifyResult {
39
+ /** Unique request ID. */
40
+ requestId: string;
41
+ /** Classifier decision. */
42
+ decision: Decision;
43
+ /** Threat score (0-100000). */
44
+ threatScore: number;
45
+ /** Raw output scores: [safe, suspicious, malicious]. */
46
+ scores: [number, number, number];
47
+ /** IO commitment (Poseidon hash of classifier input + output). */
48
+ ioCommitment: string;
49
+ /** Policy commitment (strict policy hash). */
50
+ policyCommitment: string;
51
+ /** Proving time in milliseconds. */
52
+ proveTimeMs: number;
53
+ }
54
+ /** Result from submitting an action to the firewall contract. */
55
+ interface SubmitActionResult {
56
+ /** Action ID assigned by the contract. */
57
+ actionId: number;
58
+ /** Transaction hash. */
59
+ txHash: string;
60
+ }
61
+ /** Result from resolving an action with a verified proof. */
62
+ interface ResolveResult {
63
+ /** Final decision after proof verification. */
64
+ decision: Decision;
65
+ /** Threat score applied to the agent. */
66
+ threatScore: number;
67
+ /** Transaction hash. */
68
+ txHash: string;
69
+ }
70
+ /** Agent trust status. */
71
+ interface AgentStatus {
72
+ /** Whether the agent is registered. */
73
+ registered: boolean;
74
+ /** Whether the agent is active (not frozen). */
75
+ active: boolean;
76
+ /** Current trust score (0-100000, EMA smoothed). */
77
+ trustScore: number;
78
+ /** Current strike count. */
79
+ strikes: number;
80
+ /** Whether the agent is trusted (active + score < threshold + strikes < max). */
81
+ trusted: boolean;
82
+ }
83
+ /** Full evaluation result from classify → submit → resolve. */
84
+ interface EvaluateActionResult {
85
+ /** Classification result from the ZKML classifier. */
86
+ classification: ClassifyResult;
87
+ /** Decision after on-chain resolution (may differ from classification if escalated). */
88
+ decision: Decision;
89
+ /** Threat score applied to the agent's EMA. */
90
+ threatScore: number;
91
+ /** Action ID on the firewall contract. */
92
+ actionId: number;
93
+ /** Transaction hash of the resolve call. */
94
+ txHash: string;
95
+ }
96
+ /** Firewall SDK configuration. */
97
+ interface FirewallConfig {
98
+ /** Prover server URL (for /api/v1/classify). */
99
+ proverUrl: string;
100
+ /** AgentFirewallZK contract address on Starknet. */
101
+ firewallContract: string;
102
+ /** ObelyskVerifier contract address on Starknet. */
103
+ verifierContract: string;
104
+ /** Starknet RPC URL. */
105
+ rpcUrl: string;
106
+ /** Starknet account for signing transactions. */
107
+ account?: starknet.Account;
108
+ /** API key for the prover server (optional). */
109
+ apiKey?: string;
110
+ }
111
+
112
+ /**
113
+ * AgentFirewallSDK — ZKML-powered transaction guardrails for AI agents.
114
+ *
115
+ * Wraps the full flow:
116
+ * 1. Classify transaction via prove-server (/api/v1/classify)
117
+ * 2. Submit action to AgentFirewallZK contract
118
+ * 3. Submit ZKML proof to ObelyskVerifier (streaming or recursive)
119
+ * 4. Resolve action with proven threat score
120
+ * 5. Query approval status
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import { AgentFirewallSDK } from '@obelyzk/sdk/firewall';
125
+ *
126
+ * const firewall = new AgentFirewallSDK({
127
+ * proverUrl: 'https://prover.bitsage.network',
128
+ * firewallContract: '0x...',
129
+ * verifierContract: '0x...',
130
+ * rpcUrl: process.env.STARKNET_RPC!,
131
+ * account: myAccount,
132
+ * });
133
+ *
134
+ * const result = await firewall.evaluateAction({
135
+ * target: '0x1234...',
136
+ * value: '1000000000',
137
+ * selector: '0xa9059cbb',
138
+ * });
139
+ *
140
+ * if (result.decision === 'approve') {
141
+ * // safe to execute
142
+ * }
143
+ * ```
144
+ */
145
+
146
+ declare class AgentFirewallSDK {
147
+ private config;
148
+ private provider;
149
+ constructor(config: FirewallConfig);
150
+ /**
151
+ * Classify a transaction through the ZKML classifier.
152
+ *
153
+ * Sends the transaction features to the prove-server, which runs
154
+ * the MLP classifier and generates a GKR+STARK proof. Returns the
155
+ * proven threat score and decision.
156
+ *
157
+ * Does NOT submit anything on-chain — use `evaluateAction()` for
158
+ * the full flow including on-chain submission.
159
+ */
160
+ classify(tx: TransactionFeatures): Promise<ClassifyResult>;
161
+ /**
162
+ * Register a new agent on the firewall contract.
163
+ * The calling account becomes the agent owner.
164
+ */
165
+ registerAgent(agentId: string): Promise<string>;
166
+ /** Deactivate an agent (owner or contract admin). */
167
+ deactivateAgent(agentId: string): Promise<string>;
168
+ /** Reactivate an agent and reset strikes (agent owner only). */
169
+ reactivateAgent(agentId: string): Promise<string>;
170
+ /** Get the full status of an agent. */
171
+ getAgentStatus(agentId: string): Promise<AgentStatus>;
172
+ /** Check if a specific action has been approved. */
173
+ isActionApproved(actionId: number): Promise<boolean>;
174
+ /** Check if an agent is trusted. */
175
+ isAgentTrusted(agentId: string): Promise<boolean>;
176
+ /** Get the decision for an action (0=pending, 1=approved, 2=escalated, 3=blocked). */
177
+ getActionDecision(actionId: number): Promise<number>;
178
+ /** Get the threat score for a resolved action. */
179
+ getActionThreatScore(actionId: number): Promise<number>;
180
+ /** Get the IO commitment for an action. */
181
+ getActionIoCommitment(actionId: number): Promise<string>;
182
+ /**
183
+ * Submit a pending action to the firewall contract.
184
+ * Returns the action_id assigned by the contract.
185
+ */
186
+ submitAction(agentId: string, target: string, value: string, selector: number, ioCommitment: string): Promise<SubmitActionResult>;
187
+ /**
188
+ * Resolve a pending action with a verified ZKML proof.
189
+ * The proof must already be verified on the ObelyskVerifier contract.
190
+ */
191
+ resolveAction(actionId: number, proofHash: string, originalIoLen: number, packedRawIo: string[]): Promise<ResolveResult>;
192
+ /** Approve an escalated action (human-in-the-loop). */
193
+ approveEscalated(actionId: number): Promise<string>;
194
+ /** Reject an escalated action and add a strike. */
195
+ rejectEscalated(actionId: number): Promise<string>;
196
+ private requireAccount;
197
+ }
198
+
199
+ export { AgentFirewallSDK as A, type ClassifyResult as C, type Decision as D, type EvaluateActionResult as E, type FirewallConfig as F, type ResolveResult as R, type SubmitActionResult as S, type TransactionFeatures as T, type AgentStatus as a };
@@ -0,0 +1,199 @@
1
+ import * as starknet from 'starknet';
2
+
3
+ /**
4
+ * Types for the ZKML Agent Firewall SDK.
5
+ *
6
+ * These types map 1:1 to the Rust classifier and Cairo AgentFirewallZK contract.
7
+ */
8
+ /** Transaction features sent to the classifier. */
9
+ interface TransactionFeatures {
10
+ /** Target contract address (hex, 0x-prefixed). */
11
+ target: string;
12
+ /** Transaction value (decimal string, u256). */
13
+ value?: string;
14
+ /** Function selector (hex, 4 bytes, 0x-prefixed). */
15
+ selector?: string;
16
+ /** Calldata (hex, after selector, 0x-prefixed). */
17
+ calldata?: string;
18
+ /** Agent's current trust score (0-100000). */
19
+ agentTrustScore?: number;
20
+ /** Agent's current strike count. */
21
+ agentStrikes?: number;
22
+ /** Agent age in blocks since registration. */
23
+ agentAgeBlocks?: number;
24
+ /** Target contract metadata. */
25
+ targetVerified?: boolean;
26
+ targetIsProxy?: boolean;
27
+ targetHasSource?: boolean;
28
+ targetInteractionCount?: number;
29
+ /** Behavioral features. */
30
+ txFrequency?: number;
31
+ uniqueTargets24h?: number;
32
+ avgValue24h?: number;
33
+ maxValue24h?: number;
34
+ }
35
+ /** Classifier decision. */
36
+ type Decision = "approve" | "escalate" | "block";
37
+ /** Result from evaluating a transaction through the ZKML classifier. */
38
+ interface ClassifyResult {
39
+ /** Unique request ID. */
40
+ requestId: string;
41
+ /** Classifier decision. */
42
+ decision: Decision;
43
+ /** Threat score (0-100000). */
44
+ threatScore: number;
45
+ /** Raw output scores: [safe, suspicious, malicious]. */
46
+ scores: [number, number, number];
47
+ /** IO commitment (Poseidon hash of classifier input + output). */
48
+ ioCommitment: string;
49
+ /** Policy commitment (strict policy hash). */
50
+ policyCommitment: string;
51
+ /** Proving time in milliseconds. */
52
+ proveTimeMs: number;
53
+ }
54
+ /** Result from submitting an action to the firewall contract. */
55
+ interface SubmitActionResult {
56
+ /** Action ID assigned by the contract. */
57
+ actionId: number;
58
+ /** Transaction hash. */
59
+ txHash: string;
60
+ }
61
+ /** Result from resolving an action with a verified proof. */
62
+ interface ResolveResult {
63
+ /** Final decision after proof verification. */
64
+ decision: Decision;
65
+ /** Threat score applied to the agent. */
66
+ threatScore: number;
67
+ /** Transaction hash. */
68
+ txHash: string;
69
+ }
70
+ /** Agent trust status. */
71
+ interface AgentStatus {
72
+ /** Whether the agent is registered. */
73
+ registered: boolean;
74
+ /** Whether the agent is active (not frozen). */
75
+ active: boolean;
76
+ /** Current trust score (0-100000, EMA smoothed). */
77
+ trustScore: number;
78
+ /** Current strike count. */
79
+ strikes: number;
80
+ /** Whether the agent is trusted (active + score < threshold + strikes < max). */
81
+ trusted: boolean;
82
+ }
83
+ /** Full evaluation result from classify → submit → resolve. */
84
+ interface EvaluateActionResult {
85
+ /** Classification result from the ZKML classifier. */
86
+ classification: ClassifyResult;
87
+ /** Decision after on-chain resolution (may differ from classification if escalated). */
88
+ decision: Decision;
89
+ /** Threat score applied to the agent's EMA. */
90
+ threatScore: number;
91
+ /** Action ID on the firewall contract. */
92
+ actionId: number;
93
+ /** Transaction hash of the resolve call. */
94
+ txHash: string;
95
+ }
96
+ /** Firewall SDK configuration. */
97
+ interface FirewallConfig {
98
+ /** Prover server URL (for /api/v1/classify). */
99
+ proverUrl: string;
100
+ /** AgentFirewallZK contract address on Starknet. */
101
+ firewallContract: string;
102
+ /** ObelyskVerifier contract address on Starknet. */
103
+ verifierContract: string;
104
+ /** Starknet RPC URL. */
105
+ rpcUrl: string;
106
+ /** Starknet account for signing transactions. */
107
+ account?: starknet.Account;
108
+ /** API key for the prover server (optional). */
109
+ apiKey?: string;
110
+ }
111
+
112
+ /**
113
+ * AgentFirewallSDK — ZKML-powered transaction guardrails for AI agents.
114
+ *
115
+ * Wraps the full flow:
116
+ * 1. Classify transaction via prove-server (/api/v1/classify)
117
+ * 2. Submit action to AgentFirewallZK contract
118
+ * 3. Submit ZKML proof to ObelyskVerifier (streaming or recursive)
119
+ * 4. Resolve action with proven threat score
120
+ * 5. Query approval status
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * import { AgentFirewallSDK } from '@obelyzk/sdk/firewall';
125
+ *
126
+ * const firewall = new AgentFirewallSDK({
127
+ * proverUrl: 'https://prover.bitsage.network',
128
+ * firewallContract: '0x...',
129
+ * verifierContract: '0x...',
130
+ * rpcUrl: process.env.STARKNET_RPC!,
131
+ * account: myAccount,
132
+ * });
133
+ *
134
+ * const result = await firewall.evaluateAction({
135
+ * target: '0x1234...',
136
+ * value: '1000000000',
137
+ * selector: '0xa9059cbb',
138
+ * });
139
+ *
140
+ * if (result.decision === 'approve') {
141
+ * // safe to execute
142
+ * }
143
+ * ```
144
+ */
145
+
146
+ declare class AgentFirewallSDK {
147
+ private config;
148
+ private provider;
149
+ constructor(config: FirewallConfig);
150
+ /**
151
+ * Classify a transaction through the ZKML classifier.
152
+ *
153
+ * Sends the transaction features to the prove-server, which runs
154
+ * the MLP classifier and generates a GKR+STARK proof. Returns the
155
+ * proven threat score and decision.
156
+ *
157
+ * Does NOT submit anything on-chain — use `evaluateAction()` for
158
+ * the full flow including on-chain submission.
159
+ */
160
+ classify(tx: TransactionFeatures): Promise<ClassifyResult>;
161
+ /**
162
+ * Register a new agent on the firewall contract.
163
+ * The calling account becomes the agent owner.
164
+ */
165
+ registerAgent(agentId: string): Promise<string>;
166
+ /** Deactivate an agent (owner or contract admin). */
167
+ deactivateAgent(agentId: string): Promise<string>;
168
+ /** Reactivate an agent and reset strikes (agent owner only). */
169
+ reactivateAgent(agentId: string): Promise<string>;
170
+ /** Get the full status of an agent. */
171
+ getAgentStatus(agentId: string): Promise<AgentStatus>;
172
+ /** Check if a specific action has been approved. */
173
+ isActionApproved(actionId: number): Promise<boolean>;
174
+ /** Check if an agent is trusted. */
175
+ isAgentTrusted(agentId: string): Promise<boolean>;
176
+ /** Get the decision for an action (0=pending, 1=approved, 2=escalated, 3=blocked). */
177
+ getActionDecision(actionId: number): Promise<number>;
178
+ /** Get the threat score for a resolved action. */
179
+ getActionThreatScore(actionId: number): Promise<number>;
180
+ /** Get the IO commitment for an action. */
181
+ getActionIoCommitment(actionId: number): Promise<string>;
182
+ /**
183
+ * Submit a pending action to the firewall contract.
184
+ * Returns the action_id assigned by the contract.
185
+ */
186
+ submitAction(agentId: string, target: string, value: string, selector: number, ioCommitment: string): Promise<SubmitActionResult>;
187
+ /**
188
+ * Resolve a pending action with a verified ZKML proof.
189
+ * The proof must already be verified on the ObelyskVerifier contract.
190
+ */
191
+ resolveAction(actionId: number, proofHash: string, originalIoLen: number, packedRawIo: string[]): Promise<ResolveResult>;
192
+ /** Approve an escalated action (human-in-the-loop). */
193
+ approveEscalated(actionId: number): Promise<string>;
194
+ /** Reject an escalated action and add a strike. */
195
+ rejectEscalated(actionId: number): Promise<string>;
196
+ private requireAccount;
197
+ }
198
+
199
+ export { AgentFirewallSDK as A, type ClassifyResult as C, type Decision as D, type EvaluateActionResult as E, type FirewallConfig as F, type ResolveResult as R, type SubmitActionResult as S, type TransactionFeatures as T, type AgentStatus as a };