kontext-sdk 0.1.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.
- package/LICENSE +21 -0
- package/README.md +98 -0
- package/dist/index.d.mts +1320 -0
- package/dist/index.d.ts +1320 -0
- package/dist/index.js +3274 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +3244 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +72 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,1320 @@
|
|
|
1
|
+
/** Supported blockchain networks */
|
|
2
|
+
type Chain = 'ethereum' | 'base' | 'polygon' | 'arbitrum' | 'optimism' | 'arc';
|
|
3
|
+
/** Supported stablecoin tokens */
|
|
4
|
+
type Token = 'USDC' | 'USDT' | 'DAI' | 'EURC';
|
|
5
|
+
/** SDK operating mode */
|
|
6
|
+
type KontextMode = 'local' | 'cloud';
|
|
7
|
+
/** Environment configuration */
|
|
8
|
+
type Environment = 'development' | 'staging' | 'production';
|
|
9
|
+
/** Anomaly severity levels */
|
|
10
|
+
type AnomalySeverity = 'low' | 'medium' | 'high' | 'critical';
|
|
11
|
+
/** Task status lifecycle */
|
|
12
|
+
type TaskStatus = 'pending' | 'in_progress' | 'confirmed' | 'failed' | 'expired';
|
|
13
|
+
/** Supported export formats */
|
|
14
|
+
type ExportFormat = 'json' | 'csv';
|
|
15
|
+
/** Report types */
|
|
16
|
+
type ReportType = 'compliance' | 'transaction' | 'anomaly' | 'sar' | 'ctr';
|
|
17
|
+
/** Anomaly detection rule types */
|
|
18
|
+
type AnomalyRuleType = 'unusualAmount' | 'frequencySpike' | 'newDestination' | 'offHoursActivity' | 'rapidSuccession' | 'roundAmount';
|
|
19
|
+
/** SDK initialization configuration */
|
|
20
|
+
interface KontextConfig {
|
|
21
|
+
/** API key for cloud mode (optional for local/OSS mode) */
|
|
22
|
+
apiKey?: string;
|
|
23
|
+
/** Unique project identifier */
|
|
24
|
+
projectId: string;
|
|
25
|
+
/** Deployment environment */
|
|
26
|
+
environment: Environment;
|
|
27
|
+
/** Backend API URL (defaults to Kontext cloud) */
|
|
28
|
+
apiUrl?: string;
|
|
29
|
+
/** Enable debug logging */
|
|
30
|
+
debug?: boolean;
|
|
31
|
+
/** Batch size for log flushing */
|
|
32
|
+
batchSize?: number;
|
|
33
|
+
/** Batch flush interval in milliseconds */
|
|
34
|
+
flushIntervalMs?: number;
|
|
35
|
+
/** Local file output directory for OSS mode */
|
|
36
|
+
localOutputDir?: string;
|
|
37
|
+
}
|
|
38
|
+
/** Base action log entry */
|
|
39
|
+
interface ActionLog {
|
|
40
|
+
/** Unique action identifier */
|
|
41
|
+
id: string;
|
|
42
|
+
/** Timestamp of the action */
|
|
43
|
+
timestamp: string;
|
|
44
|
+
/** ID of the project */
|
|
45
|
+
projectId: string;
|
|
46
|
+
/** ID of the agent performing the action */
|
|
47
|
+
agentId: string;
|
|
48
|
+
/** Correlation ID for tracing related actions */
|
|
49
|
+
correlationId: string;
|
|
50
|
+
/** Type of action */
|
|
51
|
+
type: string;
|
|
52
|
+
/** Human-readable description */
|
|
53
|
+
description: string;
|
|
54
|
+
/** Arbitrary metadata */
|
|
55
|
+
metadata: Record<string, unknown>;
|
|
56
|
+
/** Rolling SHA-256 digest for tamper-evident audit trail */
|
|
57
|
+
digest?: string;
|
|
58
|
+
/** Prior digest in the chain */
|
|
59
|
+
priorDigest?: string;
|
|
60
|
+
}
|
|
61
|
+
/** Input for logging a generic action */
|
|
62
|
+
interface LogActionInput {
|
|
63
|
+
/** Type of action (e.g., 'transfer', 'approval', 'query') */
|
|
64
|
+
type: string;
|
|
65
|
+
/** Human-readable description */
|
|
66
|
+
description: string;
|
|
67
|
+
/** ID of the agent performing the action */
|
|
68
|
+
agentId: string;
|
|
69
|
+
/** Optional correlation ID (auto-generated if not provided) */
|
|
70
|
+
correlationId?: string;
|
|
71
|
+
/** Arbitrary metadata */
|
|
72
|
+
metadata?: Record<string, unknown>;
|
|
73
|
+
}
|
|
74
|
+
/** Input for logging a cryptocurrency transaction */
|
|
75
|
+
interface LogTransactionInput {
|
|
76
|
+
/** On-chain transaction hash */
|
|
77
|
+
txHash: string;
|
|
78
|
+
/** Blockchain network */
|
|
79
|
+
chain: Chain;
|
|
80
|
+
/** Transaction amount (string to preserve decimal precision) */
|
|
81
|
+
amount: string;
|
|
82
|
+
/** Token being transferred */
|
|
83
|
+
token: Token;
|
|
84
|
+
/** Sender address */
|
|
85
|
+
from: string;
|
|
86
|
+
/** Recipient address */
|
|
87
|
+
to: string;
|
|
88
|
+
/** ID of the agent initiating the transaction */
|
|
89
|
+
agentId: string;
|
|
90
|
+
/** Optional correlation ID */
|
|
91
|
+
correlationId?: string;
|
|
92
|
+
/** Additional transaction metadata */
|
|
93
|
+
metadata?: Record<string, unknown>;
|
|
94
|
+
}
|
|
95
|
+
/** Stored transaction record */
|
|
96
|
+
interface TransactionRecord extends ActionLog {
|
|
97
|
+
type: 'transaction';
|
|
98
|
+
txHash: string;
|
|
99
|
+
chain: Chain;
|
|
100
|
+
amount: string;
|
|
101
|
+
token: Token;
|
|
102
|
+
from: string;
|
|
103
|
+
to: string;
|
|
104
|
+
}
|
|
105
|
+
/** Input for creating a new tracked task */
|
|
106
|
+
interface CreateTaskInput {
|
|
107
|
+
/** Human-readable task description */
|
|
108
|
+
description: string;
|
|
109
|
+
/** ID of the agent responsible for the task */
|
|
110
|
+
agentId: string;
|
|
111
|
+
/** List of evidence types required for confirmation */
|
|
112
|
+
requiredEvidence: string[];
|
|
113
|
+
/** Optional correlation ID */
|
|
114
|
+
correlationId?: string;
|
|
115
|
+
/** Task expiration time in milliseconds from creation */
|
|
116
|
+
expiresInMs?: number;
|
|
117
|
+
/** Additional metadata */
|
|
118
|
+
metadata?: Record<string, unknown>;
|
|
119
|
+
}
|
|
120
|
+
/** Evidence provided for task confirmation */
|
|
121
|
+
interface TaskEvidence {
|
|
122
|
+
/** On-chain transaction hash */
|
|
123
|
+
txHash?: string;
|
|
124
|
+
/** Transaction receipt data */
|
|
125
|
+
receipt?: Record<string, unknown>;
|
|
126
|
+
/** Proof data (e.g., Merkle proof, signature) */
|
|
127
|
+
proof?: string;
|
|
128
|
+
/** Any additional evidence fields */
|
|
129
|
+
[key: string]: unknown;
|
|
130
|
+
}
|
|
131
|
+
/** Input for confirming a task */
|
|
132
|
+
interface ConfirmTaskInput {
|
|
133
|
+
/** ID of the task to confirm */
|
|
134
|
+
taskId: string;
|
|
135
|
+
/** Evidence supporting task completion */
|
|
136
|
+
evidence: TaskEvidence;
|
|
137
|
+
}
|
|
138
|
+
/** Complete task record */
|
|
139
|
+
interface Task {
|
|
140
|
+
/** Unique task identifier */
|
|
141
|
+
id: string;
|
|
142
|
+
/** Project ID */
|
|
143
|
+
projectId: string;
|
|
144
|
+
/** Task description */
|
|
145
|
+
description: string;
|
|
146
|
+
/** Responsible agent ID */
|
|
147
|
+
agentId: string;
|
|
148
|
+
/** Current task status */
|
|
149
|
+
status: TaskStatus;
|
|
150
|
+
/** Required evidence types */
|
|
151
|
+
requiredEvidence: string[];
|
|
152
|
+
/** Provided evidence (if any) */
|
|
153
|
+
providedEvidence: TaskEvidence | null;
|
|
154
|
+
/** Correlation ID */
|
|
155
|
+
correlationId: string;
|
|
156
|
+
/** Creation timestamp */
|
|
157
|
+
createdAt: string;
|
|
158
|
+
/** Last update timestamp */
|
|
159
|
+
updatedAt: string;
|
|
160
|
+
/** Confirmation timestamp */
|
|
161
|
+
confirmedAt: string | null;
|
|
162
|
+
/** Expiration timestamp */
|
|
163
|
+
expiresAt: string | null;
|
|
164
|
+
/** Additional metadata */
|
|
165
|
+
metadata: Record<string, unknown>;
|
|
166
|
+
}
|
|
167
|
+
/** Date range filter */
|
|
168
|
+
interface DateRange {
|
|
169
|
+
/** Start date (inclusive) */
|
|
170
|
+
start: Date;
|
|
171
|
+
/** End date (inclusive) */
|
|
172
|
+
end: Date;
|
|
173
|
+
}
|
|
174
|
+
/** Export configuration */
|
|
175
|
+
interface ExportOptions {
|
|
176
|
+
/** Output format */
|
|
177
|
+
format: ExportFormat;
|
|
178
|
+
/** Date range filter */
|
|
179
|
+
dateRange?: DateRange;
|
|
180
|
+
/** Filter by agent IDs */
|
|
181
|
+
agentIds?: string[];
|
|
182
|
+
/** Filter by action types */
|
|
183
|
+
types?: string[];
|
|
184
|
+
/** Filter by chains */
|
|
185
|
+
chains?: Chain[];
|
|
186
|
+
/** Include task data */
|
|
187
|
+
includeTasks?: boolean;
|
|
188
|
+
/** Include anomaly data */
|
|
189
|
+
includeAnomalies?: boolean;
|
|
190
|
+
}
|
|
191
|
+
/** Report generation options */
|
|
192
|
+
interface ReportOptions {
|
|
193
|
+
/** Type of report */
|
|
194
|
+
type: ReportType;
|
|
195
|
+
/** Reporting period */
|
|
196
|
+
period: DateRange;
|
|
197
|
+
/** Filter by agent IDs */
|
|
198
|
+
agentIds?: string[];
|
|
199
|
+
}
|
|
200
|
+
/** Generated compliance report */
|
|
201
|
+
interface ComplianceReport {
|
|
202
|
+
/** Report ID */
|
|
203
|
+
id: string;
|
|
204
|
+
/** Report type */
|
|
205
|
+
type: ReportType;
|
|
206
|
+
/** Generation timestamp */
|
|
207
|
+
generatedAt: string;
|
|
208
|
+
/** Reporting period */
|
|
209
|
+
period: DateRange;
|
|
210
|
+
/** Project ID */
|
|
211
|
+
projectId: string;
|
|
212
|
+
/** Summary statistics */
|
|
213
|
+
summary: {
|
|
214
|
+
totalActions: number;
|
|
215
|
+
totalTransactions: number;
|
|
216
|
+
totalTasks: number;
|
|
217
|
+
confirmedTasks: number;
|
|
218
|
+
failedTasks: number;
|
|
219
|
+
totalAnomalies: number;
|
|
220
|
+
averageTrustScore: number;
|
|
221
|
+
};
|
|
222
|
+
/** Action logs in the period */
|
|
223
|
+
actions: ActionLog[];
|
|
224
|
+
/** Transaction records in the period */
|
|
225
|
+
transactions: TransactionRecord[];
|
|
226
|
+
/** Tasks in the period */
|
|
227
|
+
tasks: Task[];
|
|
228
|
+
/** Anomalies detected in the period */
|
|
229
|
+
anomalies: AnomalyEvent[];
|
|
230
|
+
}
|
|
231
|
+
/** Exported audit data */
|
|
232
|
+
interface ExportResult {
|
|
233
|
+
/** Export format */
|
|
234
|
+
format: ExportFormat;
|
|
235
|
+
/** Export timestamp */
|
|
236
|
+
exportedAt: string;
|
|
237
|
+
/** Number of records */
|
|
238
|
+
recordCount: number;
|
|
239
|
+
/** The data (JSON string or CSV string) */
|
|
240
|
+
data: string;
|
|
241
|
+
/** Terminal digest of the chain at time of export */
|
|
242
|
+
terminalDigest?: string;
|
|
243
|
+
}
|
|
244
|
+
/** Subject information for SAR/CTR reports */
|
|
245
|
+
interface ReportSubject {
|
|
246
|
+
/** Subject name or identifier */
|
|
247
|
+
name: string;
|
|
248
|
+
/** Agent ID (if applicable) */
|
|
249
|
+
agentId?: string;
|
|
250
|
+
/** Wallet addresses associated with the subject */
|
|
251
|
+
addresses: string[];
|
|
252
|
+
/** Additional identifying information */
|
|
253
|
+
identifiers?: Record<string, string>;
|
|
254
|
+
}
|
|
255
|
+
/** Suspicious Activity Report template */
|
|
256
|
+
interface SARReport {
|
|
257
|
+
/** Report ID */
|
|
258
|
+
id: string;
|
|
259
|
+
/** Report type discriminator */
|
|
260
|
+
type: 'sar';
|
|
261
|
+
/** Generation timestamp */
|
|
262
|
+
generatedAt: string;
|
|
263
|
+
/** Reporting period */
|
|
264
|
+
period: DateRange;
|
|
265
|
+
/** Project ID */
|
|
266
|
+
projectId: string;
|
|
267
|
+
/** Filing institution information */
|
|
268
|
+
filingInstitution: string;
|
|
269
|
+
/** Subject(s) of the report */
|
|
270
|
+
subjects: ReportSubject[];
|
|
271
|
+
/** Narrative summary of suspicious activity */
|
|
272
|
+
narrative: string;
|
|
273
|
+
/** Suspicious activity categories */
|
|
274
|
+
activityCategories: string[];
|
|
275
|
+
/** Total amount involved */
|
|
276
|
+
totalAmount: string;
|
|
277
|
+
/** Currency/token */
|
|
278
|
+
currency: string;
|
|
279
|
+
/** Transactions flagged as suspicious */
|
|
280
|
+
suspiciousTransactions: TransactionRecord[];
|
|
281
|
+
/** Related anomalies */
|
|
282
|
+
anomalies: AnomalyEvent[];
|
|
283
|
+
/** Supporting action logs */
|
|
284
|
+
supportingActions: ActionLog[];
|
|
285
|
+
/** Whether this is a continuing activity report */
|
|
286
|
+
isContinuingActivity: boolean;
|
|
287
|
+
/** Prior report ID if continuing */
|
|
288
|
+
priorReportId: string | null;
|
|
289
|
+
/** Status of the report */
|
|
290
|
+
status: 'draft' | 'review' | 'filed';
|
|
291
|
+
}
|
|
292
|
+
/** Currency Transaction Report template */
|
|
293
|
+
interface CTRReport {
|
|
294
|
+
/** Report ID */
|
|
295
|
+
id: string;
|
|
296
|
+
/** Report type discriminator */
|
|
297
|
+
type: 'ctr';
|
|
298
|
+
/** Generation timestamp */
|
|
299
|
+
generatedAt: string;
|
|
300
|
+
/** Reporting period */
|
|
301
|
+
period: DateRange;
|
|
302
|
+
/** Project ID */
|
|
303
|
+
projectId: string;
|
|
304
|
+
/** Filing institution information */
|
|
305
|
+
filingInstitution: string;
|
|
306
|
+
/** Person/entity conducting the transactions */
|
|
307
|
+
conductors: ReportSubject[];
|
|
308
|
+
/** Transactions included in the report */
|
|
309
|
+
transactions: TransactionRecord[];
|
|
310
|
+
/** Total cash-in amount */
|
|
311
|
+
totalCashIn: string;
|
|
312
|
+
/** Total cash-out amount */
|
|
313
|
+
totalCashOut: string;
|
|
314
|
+
/** Currency/token */
|
|
315
|
+
currency: string;
|
|
316
|
+
/** Whether multiple transactions are aggregated */
|
|
317
|
+
isAggregated: boolean;
|
|
318
|
+
/** Chains involved */
|
|
319
|
+
chainsInvolved: Chain[];
|
|
320
|
+
/** Supporting action logs */
|
|
321
|
+
supportingActions: ActionLog[];
|
|
322
|
+
/** Status of the report */
|
|
323
|
+
status: 'draft' | 'review' | 'filed';
|
|
324
|
+
}
|
|
325
|
+
/** Trust score result for an agent */
|
|
326
|
+
interface TrustScore {
|
|
327
|
+
/** Agent ID */
|
|
328
|
+
agentId: string;
|
|
329
|
+
/** Overall trust score (0-100) */
|
|
330
|
+
score: number;
|
|
331
|
+
/** Score breakdown by factor */
|
|
332
|
+
factors: TrustFactor[];
|
|
333
|
+
/** Timestamp of computation */
|
|
334
|
+
computedAt: string;
|
|
335
|
+
/** Trust level label */
|
|
336
|
+
level: 'untrusted' | 'low' | 'medium' | 'high' | 'verified';
|
|
337
|
+
}
|
|
338
|
+
/** Individual trust factor */
|
|
339
|
+
interface TrustFactor {
|
|
340
|
+
/** Factor name */
|
|
341
|
+
name: string;
|
|
342
|
+
/** Factor score (0-100) */
|
|
343
|
+
score: number;
|
|
344
|
+
/** Factor weight (0-1) */
|
|
345
|
+
weight: number;
|
|
346
|
+
/** Human-readable description */
|
|
347
|
+
description: string;
|
|
348
|
+
}
|
|
349
|
+
/** Transaction evaluation result */
|
|
350
|
+
interface TransactionEvaluation {
|
|
351
|
+
/** Transaction hash */
|
|
352
|
+
txHash: string;
|
|
353
|
+
/** Risk score (0-100, higher = more risky) */
|
|
354
|
+
riskScore: number;
|
|
355
|
+
/** Risk level */
|
|
356
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
357
|
+
/** Individual risk factors */
|
|
358
|
+
factors: RiskFactor[];
|
|
359
|
+
/** Whether the transaction should be flagged */
|
|
360
|
+
flagged: boolean;
|
|
361
|
+
/** Recommended action */
|
|
362
|
+
recommendation: 'approve' | 'review' | 'block';
|
|
363
|
+
/** Evaluation timestamp */
|
|
364
|
+
evaluatedAt: string;
|
|
365
|
+
}
|
|
366
|
+
/** Individual risk factor */
|
|
367
|
+
interface RiskFactor {
|
|
368
|
+
/** Factor name */
|
|
369
|
+
name: string;
|
|
370
|
+
/** Risk score contribution (0-100) */
|
|
371
|
+
score: number;
|
|
372
|
+
/** Human-readable description */
|
|
373
|
+
description: string;
|
|
374
|
+
}
|
|
375
|
+
/** Anomaly detection configuration */
|
|
376
|
+
interface AnomalyDetectionConfig {
|
|
377
|
+
/** Detection rules to enable */
|
|
378
|
+
rules: AnomalyRuleType[];
|
|
379
|
+
/** Thresholds for detection */
|
|
380
|
+
thresholds?: AnomalyThresholds;
|
|
381
|
+
}
|
|
382
|
+
/** Configurable anomaly thresholds */
|
|
383
|
+
interface AnomalyThresholds {
|
|
384
|
+
/** Maximum transaction amount before flagging */
|
|
385
|
+
maxAmount?: string;
|
|
386
|
+
/** Maximum transactions per hour */
|
|
387
|
+
maxFrequency?: number;
|
|
388
|
+
/** Hours considered "off-hours" (24h format, e.g., [22, 23, 0, 1, 2, 3, 4, 5]) */
|
|
389
|
+
offHours?: number[];
|
|
390
|
+
/** Minimum seconds between transactions before "rapid succession" flag */
|
|
391
|
+
minIntervalSeconds?: number;
|
|
392
|
+
}
|
|
393
|
+
/** Detected anomaly event */
|
|
394
|
+
interface AnomalyEvent {
|
|
395
|
+
/** Unique anomaly ID */
|
|
396
|
+
id: string;
|
|
397
|
+
/** Anomaly type/rule that triggered */
|
|
398
|
+
type: AnomalyRuleType;
|
|
399
|
+
/** Severity level */
|
|
400
|
+
severity: AnomalySeverity;
|
|
401
|
+
/** Human-readable description */
|
|
402
|
+
description: string;
|
|
403
|
+
/** ID of the agent involved */
|
|
404
|
+
agentId: string;
|
|
405
|
+
/** Related action log ID */
|
|
406
|
+
actionId: string;
|
|
407
|
+
/** Detection timestamp */
|
|
408
|
+
detectedAt: string;
|
|
409
|
+
/** Related data */
|
|
410
|
+
data: Record<string, unknown>;
|
|
411
|
+
/** Whether the anomaly has been reviewed */
|
|
412
|
+
reviewed: boolean;
|
|
413
|
+
}
|
|
414
|
+
/** Anomaly event callback */
|
|
415
|
+
type AnomalyCallback = (anomaly: AnomalyEvent) => void;
|
|
416
|
+
/** USDC compliance check result */
|
|
417
|
+
interface UsdcComplianceCheck {
|
|
418
|
+
/** Whether the transaction is compliant */
|
|
419
|
+
compliant: boolean;
|
|
420
|
+
/** List of compliance checks performed */
|
|
421
|
+
checks: ComplianceCheckResult[];
|
|
422
|
+
/** Overall risk level */
|
|
423
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
424
|
+
/** Recommendations */
|
|
425
|
+
recommendations: string[];
|
|
426
|
+
}
|
|
427
|
+
/** Individual compliance check result */
|
|
428
|
+
interface ComplianceCheckResult {
|
|
429
|
+
/** Check name */
|
|
430
|
+
name: string;
|
|
431
|
+
/** Whether the check passed */
|
|
432
|
+
passed: boolean;
|
|
433
|
+
/** Human-readable description */
|
|
434
|
+
description: string;
|
|
435
|
+
/** Severity if failed */
|
|
436
|
+
severity: AnomalySeverity;
|
|
437
|
+
}
|
|
438
|
+
/** Error codes for Kontext SDK */
|
|
439
|
+
declare enum KontextErrorCode {
|
|
440
|
+
INITIALIZATION_ERROR = "INITIALIZATION_ERROR",
|
|
441
|
+
VALIDATION_ERROR = "VALIDATION_ERROR",
|
|
442
|
+
TASK_NOT_FOUND = "TASK_NOT_FOUND",
|
|
443
|
+
TASK_ALREADY_CONFIRMED = "TASK_ALREADY_CONFIRMED",
|
|
444
|
+
TASK_EXPIRED = "TASK_EXPIRED",
|
|
445
|
+
INSUFFICIENT_EVIDENCE = "INSUFFICIENT_EVIDENCE",
|
|
446
|
+
API_ERROR = "API_ERROR",
|
|
447
|
+
NETWORK_ERROR = "NETWORK_ERROR",
|
|
448
|
+
EXPORT_ERROR = "EXPORT_ERROR",
|
|
449
|
+
ANOMALY_CONFIG_ERROR = "ANOMALY_CONFIG_ERROR"
|
|
450
|
+
}
|
|
451
|
+
/** Kontext SDK error */
|
|
452
|
+
declare class KontextError extends Error {
|
|
453
|
+
readonly code: KontextErrorCode;
|
|
454
|
+
readonly details?: Record<string, unknown>;
|
|
455
|
+
constructor(code: KontextErrorCode, message: string, details?: Record<string, unknown>);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Microsecond precision timestamp */
|
|
459
|
+
interface PrecisionTimestamp {
|
|
460
|
+
/** ISO 8601 timestamp */
|
|
461
|
+
iso: string;
|
|
462
|
+
/** High-resolution time in nanoseconds (from process.hrtime.bigint) */
|
|
463
|
+
hrtime: bigint;
|
|
464
|
+
/** Microsecond component derived from hrtime */
|
|
465
|
+
microseconds: number;
|
|
466
|
+
}
|
|
467
|
+
/** A single link in the digest chain */
|
|
468
|
+
interface DigestLink {
|
|
469
|
+
/** The computed SHA-256 digest for this event */
|
|
470
|
+
digest: string;
|
|
471
|
+
/** The prior digest (HD-1) */
|
|
472
|
+
priorDigest: string;
|
|
473
|
+
/** The salt derived from the timestamp */
|
|
474
|
+
salt: string;
|
|
475
|
+
/** The high-precision timestamp of this event */
|
|
476
|
+
timestamp: PrecisionTimestamp;
|
|
477
|
+
/** Sequence number in the chain (0-indexed) */
|
|
478
|
+
sequence: number;
|
|
479
|
+
/** The action ID this digest covers */
|
|
480
|
+
actionId: string;
|
|
481
|
+
}
|
|
482
|
+
/** Result of verifying a digest chain */
|
|
483
|
+
interface DigestVerification {
|
|
484
|
+
/** Whether the entire chain is valid */
|
|
485
|
+
valid: boolean;
|
|
486
|
+
/** Number of links verified */
|
|
487
|
+
linksVerified: number;
|
|
488
|
+
/** Index of the first invalid link (-1 if all valid) */
|
|
489
|
+
firstInvalidIndex: number;
|
|
490
|
+
/** Verification time in milliseconds */
|
|
491
|
+
verificationTimeMs: number;
|
|
492
|
+
/** The terminal digest (last digest in the chain) */
|
|
493
|
+
terminalDigest: string;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* DigestChain implements a rolling SHA-256 digest chain for tamper-evident audit trails.
|
|
497
|
+
*
|
|
498
|
+
* Each action logged through Kontext gets a cryptographic digest that chains
|
|
499
|
+
* to all prior actions, creating a tamper-evident audit trail without
|
|
500
|
+
* blockchain overhead.
|
|
501
|
+
*
|
|
502
|
+
* Properties:
|
|
503
|
+
* - Tamper-evident: altering any past event breaks the chain
|
|
504
|
+
* - Independently verifiable: any party can recompute and verify
|
|
505
|
+
* - Energy efficient: <0.00001 kWh per event (99.97% less than PoS)
|
|
506
|
+
* - Fast: <10ms verification at p95
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```typescript
|
|
510
|
+
* const chain = new DigestChain();
|
|
511
|
+
*
|
|
512
|
+
* // Each action gets a digest link
|
|
513
|
+
* const link = chain.append(action);
|
|
514
|
+
* console.log(link.digest); // SHA-256 hash
|
|
515
|
+
*
|
|
516
|
+
* // Verify the entire chain
|
|
517
|
+
* const result = chain.verify();
|
|
518
|
+
* console.log(result.valid); // true if untampered
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
declare class DigestChain {
|
|
522
|
+
private links;
|
|
523
|
+
private currentDigest;
|
|
524
|
+
private readonly hrtimeBase;
|
|
525
|
+
constructor();
|
|
526
|
+
/**
|
|
527
|
+
* Append an action to the digest chain.
|
|
528
|
+
*
|
|
529
|
+
* Computes: HD = SHA-256(HD-1 || Serialize(ED) || SD)
|
|
530
|
+
*
|
|
531
|
+
* @param action - The action log entry to chain
|
|
532
|
+
* @returns The digest link for this event
|
|
533
|
+
*/
|
|
534
|
+
append(action: ActionLog): DigestLink;
|
|
535
|
+
/**
|
|
536
|
+
* Get the terminal digest — the latest digest in the chain.
|
|
537
|
+
* This can be embedded in outgoing messages as proof of the entire action history.
|
|
538
|
+
*/
|
|
539
|
+
getTerminalDigest(): string;
|
|
540
|
+
/**
|
|
541
|
+
* Get the number of links in the chain.
|
|
542
|
+
*/
|
|
543
|
+
getChainLength(): number;
|
|
544
|
+
/**
|
|
545
|
+
* Get all digest links in the chain.
|
|
546
|
+
*/
|
|
547
|
+
getLinks(): ReadonlyArray<DigestLink>;
|
|
548
|
+
/**
|
|
549
|
+
* Get a specific digest link by sequence number.
|
|
550
|
+
*/
|
|
551
|
+
getLink(sequence: number): DigestLink | undefined;
|
|
552
|
+
/**
|
|
553
|
+
* Verify the integrity of the entire digest chain.
|
|
554
|
+
*
|
|
555
|
+
* Recomputes every digest from the genesis hash and compares.
|
|
556
|
+
* Any tampering (modified, inserted, deleted, or reordered events)
|
|
557
|
+
* will cause verification to fail.
|
|
558
|
+
*
|
|
559
|
+
* @param actions - The original action logs to verify against
|
|
560
|
+
* @returns Verification result with timing data
|
|
561
|
+
*/
|
|
562
|
+
verify(actions: ActionLog[]): DigestVerification;
|
|
563
|
+
/**
|
|
564
|
+
* Verify a single link in isolation (given the expected prior digest).
|
|
565
|
+
*
|
|
566
|
+
* @param link - The digest link to verify
|
|
567
|
+
* @param action - The action data for this link
|
|
568
|
+
* @param expectedPriorDigest - The expected prior digest
|
|
569
|
+
* @returns Whether the link is valid
|
|
570
|
+
*/
|
|
571
|
+
verifyLink(link: DigestLink, action: ActionLog, expectedPriorDigest: string): boolean;
|
|
572
|
+
/**
|
|
573
|
+
* Export the chain data for independent verification by a third party.
|
|
574
|
+
* Includes all links and enough data for recomputation.
|
|
575
|
+
*/
|
|
576
|
+
exportChain(): {
|
|
577
|
+
genesisHash: string;
|
|
578
|
+
links: DigestLink[];
|
|
579
|
+
terminalDigest: string;
|
|
580
|
+
};
|
|
581
|
+
/**
|
|
582
|
+
* Compute: HD = SHA-256(HD-1 || Serialize(ED) || SD)
|
|
583
|
+
*/
|
|
584
|
+
private computeDigest;
|
|
585
|
+
/**
|
|
586
|
+
* Deterministically serialize an action log for digest computation.
|
|
587
|
+
* Uses sorted keys to ensure consistent serialization regardless of
|
|
588
|
+
* property insertion order. Excludes digest/priorDigest fields since
|
|
589
|
+
* those are computed from this serialization.
|
|
590
|
+
*/
|
|
591
|
+
private serialize;
|
|
592
|
+
/**
|
|
593
|
+
* Derive a salt from the event's high-precision timestamp.
|
|
594
|
+
* SD = SHA-256(microsecond_timestamp)
|
|
595
|
+
*/
|
|
596
|
+
private deriveSalt;
|
|
597
|
+
/**
|
|
598
|
+
* Get a microsecond-precision timestamp.
|
|
599
|
+
* Combines wall clock time with high-resolution timer for sub-millisecond precision.
|
|
600
|
+
*/
|
|
601
|
+
private getPrecisionTimestamp;
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Independently verify a digest chain exported from another Kontext instance.
|
|
605
|
+
* This enables third-party verification without access to the original SDK.
|
|
606
|
+
*
|
|
607
|
+
* @param chain - The exported chain data
|
|
608
|
+
* @param actions - The original action logs
|
|
609
|
+
* @returns Whether the chain is valid
|
|
610
|
+
*/
|
|
611
|
+
declare function verifyExportedChain(chain: {
|
|
612
|
+
genesisHash: string;
|
|
613
|
+
links: DigestLink[];
|
|
614
|
+
terminalDigest: string;
|
|
615
|
+
}, actions: ActionLog[]): DigestVerification;
|
|
616
|
+
|
|
617
|
+
/**
|
|
618
|
+
* Main Kontext SDK client. Provides a unified interface to all SDK features:
|
|
619
|
+
* action logging, task confirmation, audit export, trust scoring, and
|
|
620
|
+
* anomaly detection.
|
|
621
|
+
*
|
|
622
|
+
* Supports two operating modes:
|
|
623
|
+
* - **Local mode** (no API key): All data stored locally, suitable for
|
|
624
|
+
* open-source usage and development.
|
|
625
|
+
* - **Cloud mode** (with API key): Data synced to Kontext API for
|
|
626
|
+
* persistent storage and advanced features.
|
|
627
|
+
*
|
|
628
|
+
* @example
|
|
629
|
+
* ```typescript
|
|
630
|
+
* import { Kontext } from '@kontext/sdk';
|
|
631
|
+
*
|
|
632
|
+
* const kontext = Kontext.init({
|
|
633
|
+
* projectId: 'my-project',
|
|
634
|
+
* environment: 'development',
|
|
635
|
+
* });
|
|
636
|
+
*
|
|
637
|
+
* await kontext.logTransaction({
|
|
638
|
+
* txHash: '0x...',
|
|
639
|
+
* chain: 'base',
|
|
640
|
+
* amount: '100',
|
|
641
|
+
* token: 'USDC',
|
|
642
|
+
* from: '0xSender',
|
|
643
|
+
* to: '0xReceiver',
|
|
644
|
+
* agentId: 'agent-1',
|
|
645
|
+
* });
|
|
646
|
+
* ```
|
|
647
|
+
*/
|
|
648
|
+
declare class Kontext {
|
|
649
|
+
private readonly config;
|
|
650
|
+
private readonly store;
|
|
651
|
+
private readonly logger;
|
|
652
|
+
private readonly taskManager;
|
|
653
|
+
private readonly auditExporter;
|
|
654
|
+
private readonly trustScorer;
|
|
655
|
+
private readonly anomalyDetector;
|
|
656
|
+
private readonly mode;
|
|
657
|
+
private constructor();
|
|
658
|
+
/**
|
|
659
|
+
* Initialize the Kontext SDK.
|
|
660
|
+
*
|
|
661
|
+
* @param config - Configuration options
|
|
662
|
+
* @returns Initialized Kontext client instance
|
|
663
|
+
*
|
|
664
|
+
* @example
|
|
665
|
+
* ```typescript
|
|
666
|
+
* // Local/OSS mode (no API key)
|
|
667
|
+
* const kontext = Kontext.init({
|
|
668
|
+
* projectId: 'my-project',
|
|
669
|
+
* environment: 'development',
|
|
670
|
+
* });
|
|
671
|
+
*
|
|
672
|
+
* // Cloud mode (with API key)
|
|
673
|
+
* const kontext = Kontext.init({
|
|
674
|
+
* apiKey: 'sk_live_...',
|
|
675
|
+
* projectId: 'my-project',
|
|
676
|
+
* environment: 'production',
|
|
677
|
+
* });
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
680
|
+
static init(config: KontextConfig): Kontext;
|
|
681
|
+
/**
|
|
682
|
+
* Get the current operating mode.
|
|
683
|
+
*/
|
|
684
|
+
getMode(): KontextMode;
|
|
685
|
+
/**
|
|
686
|
+
* Get the current configuration (API key is masked).
|
|
687
|
+
*/
|
|
688
|
+
getConfig(): Omit<KontextConfig, 'apiKey'> & {
|
|
689
|
+
apiKey?: string;
|
|
690
|
+
};
|
|
691
|
+
/**
|
|
692
|
+
* Log a generic agent action.
|
|
693
|
+
*
|
|
694
|
+
* @param input - Action details
|
|
695
|
+
* @returns The created action log entry
|
|
696
|
+
*/
|
|
697
|
+
log(input: LogActionInput): Promise<ActionLog>;
|
|
698
|
+
/**
|
|
699
|
+
* Log a cryptocurrency transaction with full chain details.
|
|
700
|
+
*
|
|
701
|
+
* @param input - Transaction details
|
|
702
|
+
* @returns The created transaction record
|
|
703
|
+
*/
|
|
704
|
+
logTransaction(input: LogTransactionInput): Promise<TransactionRecord>;
|
|
705
|
+
/**
|
|
706
|
+
* Flush any pending log batches.
|
|
707
|
+
*/
|
|
708
|
+
flushLogs(): Promise<void>;
|
|
709
|
+
/**
|
|
710
|
+
* Create a new tracked task that requires evidence for confirmation.
|
|
711
|
+
*
|
|
712
|
+
* @param input - Task details including required evidence types
|
|
713
|
+
* @returns The created task
|
|
714
|
+
*/
|
|
715
|
+
createTask(input: CreateTaskInput): Promise<Task>;
|
|
716
|
+
/**
|
|
717
|
+
* Confirm a task by providing evidence.
|
|
718
|
+
*
|
|
719
|
+
* @param input - Task ID and evidence data
|
|
720
|
+
* @returns The confirmed task
|
|
721
|
+
*/
|
|
722
|
+
confirmTask(input: ConfirmTaskInput): Promise<Task>;
|
|
723
|
+
/**
|
|
724
|
+
* Get the current status of a task.
|
|
725
|
+
*
|
|
726
|
+
* @param taskId - Task identifier
|
|
727
|
+
* @returns The task or undefined if not found
|
|
728
|
+
*/
|
|
729
|
+
getTaskStatus(taskId: string): Promise<Task | undefined>;
|
|
730
|
+
/**
|
|
731
|
+
* Mark a task as in-progress.
|
|
732
|
+
*
|
|
733
|
+
* @param taskId - Task identifier
|
|
734
|
+
* @returns The updated task
|
|
735
|
+
*/
|
|
736
|
+
startTask(taskId: string): Promise<Task>;
|
|
737
|
+
/**
|
|
738
|
+
* Mark a task as failed.
|
|
739
|
+
*
|
|
740
|
+
* @param taskId - Task identifier
|
|
741
|
+
* @param reason - Reason for failure
|
|
742
|
+
* @returns The updated task
|
|
743
|
+
*/
|
|
744
|
+
failTask(taskId: string, reason: string): Promise<Task>;
|
|
745
|
+
/**
|
|
746
|
+
* Get all tasks, optionally filtered by status.
|
|
747
|
+
*
|
|
748
|
+
* @param status - Optional status filter
|
|
749
|
+
* @returns Array of tasks
|
|
750
|
+
*/
|
|
751
|
+
getTasks(status?: TaskStatus): Task[];
|
|
752
|
+
/**
|
|
753
|
+
* Export audit data in JSON or CSV format.
|
|
754
|
+
*
|
|
755
|
+
* @param options - Export configuration
|
|
756
|
+
* @returns Export result with formatted data
|
|
757
|
+
*/
|
|
758
|
+
export(options: ExportOptions): Promise<ExportResult>;
|
|
759
|
+
/**
|
|
760
|
+
* Generate a compliance report for a given period.
|
|
761
|
+
*
|
|
762
|
+
* @param options - Report configuration
|
|
763
|
+
* @returns Compliance report with summary and detailed records
|
|
764
|
+
*/
|
|
765
|
+
generateReport(options: ReportOptions): Promise<ComplianceReport>;
|
|
766
|
+
/**
|
|
767
|
+
* Generate a Suspicious Activity Report (SAR) template.
|
|
768
|
+
*
|
|
769
|
+
* This produces a structured SAR template populated with data from the SDK.
|
|
770
|
+
* It is a template/structure, not an actual regulatory filing.
|
|
771
|
+
*
|
|
772
|
+
* @param options - Report configuration
|
|
773
|
+
* @returns SAR report template
|
|
774
|
+
*/
|
|
775
|
+
generateSARReport(options: ReportOptions): Promise<SARReport>;
|
|
776
|
+
/**
|
|
777
|
+
* Generate a Currency Transaction Report (CTR) template.
|
|
778
|
+
*
|
|
779
|
+
* This produces a structured CTR template for transactions that meet or
|
|
780
|
+
* exceed reporting thresholds. It is a template/structure, not an actual
|
|
781
|
+
* regulatory filing.
|
|
782
|
+
*
|
|
783
|
+
* @param options - Report configuration
|
|
784
|
+
* @returns CTR report template
|
|
785
|
+
*/
|
|
786
|
+
generateCTRReport(options: ReportOptions): Promise<CTRReport>;
|
|
787
|
+
/**
|
|
788
|
+
* Get the trust score for an agent.
|
|
789
|
+
*
|
|
790
|
+
* @param agentId - Agent identifier
|
|
791
|
+
* @returns Trust score with factor breakdown
|
|
792
|
+
*/
|
|
793
|
+
getTrustScore(agentId: string): Promise<TrustScore>;
|
|
794
|
+
/**
|
|
795
|
+
* Evaluate the risk of a specific transaction.
|
|
796
|
+
*
|
|
797
|
+
* @param tx - Transaction to evaluate
|
|
798
|
+
* @returns Transaction evaluation with risk score and recommendation
|
|
799
|
+
*/
|
|
800
|
+
evaluateTransaction(tx: LogTransactionInput): Promise<TransactionEvaluation>;
|
|
801
|
+
/**
|
|
802
|
+
* Enable anomaly detection with the specified rules and thresholds.
|
|
803
|
+
*
|
|
804
|
+
* @param config - Detection configuration
|
|
805
|
+
*/
|
|
806
|
+
enableAnomalyDetection(config: AnomalyDetectionConfig): void;
|
|
807
|
+
/**
|
|
808
|
+
* Disable anomaly detection.
|
|
809
|
+
*/
|
|
810
|
+
disableAnomalyDetection(): void;
|
|
811
|
+
/**
|
|
812
|
+
* Register a callback for anomaly events.
|
|
813
|
+
*
|
|
814
|
+
* @param callback - Function to call when an anomaly is detected
|
|
815
|
+
* @returns Unsubscribe function
|
|
816
|
+
*/
|
|
817
|
+
onAnomaly(callback: AnomalyCallback): () => void;
|
|
818
|
+
/**
|
|
819
|
+
* Get the terminal digest — the latest SHA-256 hash in the rolling digest chain.
|
|
820
|
+
* Embed this in outgoing messages as tamper-evident proof of the entire action history.
|
|
821
|
+
*
|
|
822
|
+
* @returns The terminal SHA-256 digest hex string
|
|
823
|
+
*/
|
|
824
|
+
getTerminalDigest(): string;
|
|
825
|
+
/**
|
|
826
|
+
* Verify the integrity of the digest chain.
|
|
827
|
+
* Recomputes every digest from genesis and compares against stored values.
|
|
828
|
+
* Any tampering will cause verification to fail.
|
|
829
|
+
*
|
|
830
|
+
* @returns Verification result with timing and validity data
|
|
831
|
+
*/
|
|
832
|
+
verifyDigestChain(): DigestVerification;
|
|
833
|
+
/**
|
|
834
|
+
* Export the digest chain for independent third-party verification.
|
|
835
|
+
*
|
|
836
|
+
* @returns Chain data including genesis hash, all links, and terminal digest
|
|
837
|
+
*/
|
|
838
|
+
exportDigestChain(): {
|
|
839
|
+
genesisHash: string;
|
|
840
|
+
links: DigestLink[];
|
|
841
|
+
terminalDigest: string;
|
|
842
|
+
};
|
|
843
|
+
/**
|
|
844
|
+
* Run USDC-specific compliance checks on a transaction.
|
|
845
|
+
*
|
|
846
|
+
* @param tx - Transaction to check
|
|
847
|
+
* @returns Compliance check result
|
|
848
|
+
*/
|
|
849
|
+
checkUsdcCompliance(tx: LogTransactionInput): UsdcComplianceCheck;
|
|
850
|
+
/**
|
|
851
|
+
* Gracefully shut down the SDK, flushing any pending data.
|
|
852
|
+
*/
|
|
853
|
+
destroy(): Promise<void>;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
/**
|
|
857
|
+
* USDC-specific compliance helper functions.
|
|
858
|
+
*
|
|
859
|
+
* Provides pre-built compliance checks for USDC transactions on Base and
|
|
860
|
+
* Ethereum, aligned with the GENIUS Act requirements for stablecoin transfers.
|
|
861
|
+
*
|
|
862
|
+
* Checks include:
|
|
863
|
+
* - Token validation (is it USDC?)
|
|
864
|
+
* - Chain support validation
|
|
865
|
+
* - Amount threshold checks (EDD, reporting, large tx)
|
|
866
|
+
* - Address format validation
|
|
867
|
+
* - Sanctions screening (placeholder for OFAC integration)
|
|
868
|
+
* - Transfer limit checks
|
|
869
|
+
*/
|
|
870
|
+
declare class UsdcCompliance {
|
|
871
|
+
/**
|
|
872
|
+
* Run a full compliance check on a USDC transaction.
|
|
873
|
+
*
|
|
874
|
+
* @param tx - Transaction to evaluate
|
|
875
|
+
* @returns UsdcComplianceCheck with pass/fail results and recommendations
|
|
876
|
+
*
|
|
877
|
+
* @example
|
|
878
|
+
* ```typescript
|
|
879
|
+
* const check = UsdcCompliance.checkTransaction({
|
|
880
|
+
* txHash: '0x...',
|
|
881
|
+
* chain: 'base',
|
|
882
|
+
* amount: '5000',
|
|
883
|
+
* token: 'USDC',
|
|
884
|
+
* from: '0xSender...',
|
|
885
|
+
* to: '0xReceiver...',
|
|
886
|
+
* agentId: 'agent-1',
|
|
887
|
+
* });
|
|
888
|
+
* if (!check.compliant) {
|
|
889
|
+
* console.log('Non-compliant:', check.recommendations);
|
|
890
|
+
* }
|
|
891
|
+
* ```
|
|
892
|
+
*/
|
|
893
|
+
static checkTransaction(tx: LogTransactionInput): UsdcComplianceCheck;
|
|
894
|
+
/**
|
|
895
|
+
* Get the USDC contract address for a given chain.
|
|
896
|
+
*
|
|
897
|
+
* @param chain - The blockchain network
|
|
898
|
+
* @returns The USDC contract address, or undefined for unsupported chains
|
|
899
|
+
*/
|
|
900
|
+
static getContractAddress(chain: Chain): string | undefined;
|
|
901
|
+
/**
|
|
902
|
+
* Get the chains supported for USDC compliance monitoring.
|
|
903
|
+
*/
|
|
904
|
+
static getSupportedChains(): Chain[];
|
|
905
|
+
private static checkTokenType;
|
|
906
|
+
private static checkChainSupport;
|
|
907
|
+
private static checkAddressFormat;
|
|
908
|
+
private static checkAmountValid;
|
|
909
|
+
private static checkSanctions;
|
|
910
|
+
private static checkEnhancedDueDiligence;
|
|
911
|
+
private static checkReportingThreshold;
|
|
912
|
+
private static generateRecommendations;
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
/** CCTP message status */
|
|
916
|
+
type CCTPMessageStatus = 'pending' | 'attested' | 'confirmed' | 'failed';
|
|
917
|
+
/** A cross-chain transfer record */
|
|
918
|
+
interface CrossChainTransfer {
|
|
919
|
+
/** Unique transfer identifier */
|
|
920
|
+
id: string;
|
|
921
|
+
/** Source chain */
|
|
922
|
+
sourceChain: Chain;
|
|
923
|
+
/** Destination chain */
|
|
924
|
+
destinationChain: Chain;
|
|
925
|
+
/** CCTP domain ID for source */
|
|
926
|
+
sourceDomain: number;
|
|
927
|
+
/** CCTP domain ID for destination */
|
|
928
|
+
destinationDomain: number;
|
|
929
|
+
/** Transfer amount (string to preserve precision) */
|
|
930
|
+
amount: string;
|
|
931
|
+
/** Token being transferred */
|
|
932
|
+
token: Token;
|
|
933
|
+
/** Sender address on source chain */
|
|
934
|
+
sender: string;
|
|
935
|
+
/** Recipient address on destination chain */
|
|
936
|
+
recipient: string;
|
|
937
|
+
/** Source chain transaction hash */
|
|
938
|
+
sourceTxHash: string;
|
|
939
|
+
/** Destination chain transaction hash (set after confirmation) */
|
|
940
|
+
destinationTxHash: string | null;
|
|
941
|
+
/** CCTP message hash for attestation tracking */
|
|
942
|
+
messageHash: string | null;
|
|
943
|
+
/** Current status of the transfer */
|
|
944
|
+
status: CCTPMessageStatus;
|
|
945
|
+
/** Nonce from the CCTP MessageSent event */
|
|
946
|
+
nonce: number | null;
|
|
947
|
+
/** Timestamp when the transfer was initiated */
|
|
948
|
+
initiatedAt: string;
|
|
949
|
+
/** Timestamp when attestation was received */
|
|
950
|
+
attestedAt: string | null;
|
|
951
|
+
/** Timestamp when the transfer was confirmed on destination */
|
|
952
|
+
confirmedAt: string | null;
|
|
953
|
+
/** Correlation ID linking source and destination actions */
|
|
954
|
+
correlationId: string;
|
|
955
|
+
/** Agent that initiated the transfer */
|
|
956
|
+
agentId: string;
|
|
957
|
+
/** Additional metadata */
|
|
958
|
+
metadata: Record<string, unknown>;
|
|
959
|
+
}
|
|
960
|
+
/** Input for initiating a cross-chain transfer record */
|
|
961
|
+
interface InitiateCCTPTransferInput {
|
|
962
|
+
/** Source chain */
|
|
963
|
+
sourceChain: Chain;
|
|
964
|
+
/** Destination chain */
|
|
965
|
+
destinationChain: Chain;
|
|
966
|
+
/** Transfer amount */
|
|
967
|
+
amount: string;
|
|
968
|
+
/** Token being transferred */
|
|
969
|
+
token: Token;
|
|
970
|
+
/** Sender address */
|
|
971
|
+
sender: string;
|
|
972
|
+
/** Recipient address */
|
|
973
|
+
recipient: string;
|
|
974
|
+
/** Source chain transaction hash (from depositForBurn) */
|
|
975
|
+
sourceTxHash: string;
|
|
976
|
+
/** Agent initiating the transfer */
|
|
977
|
+
agentId: string;
|
|
978
|
+
/** Optional nonce from the MessageSent event */
|
|
979
|
+
nonce?: number;
|
|
980
|
+
/** Optional correlation ID */
|
|
981
|
+
correlationId?: string;
|
|
982
|
+
/** Optional metadata */
|
|
983
|
+
metadata?: Record<string, unknown>;
|
|
984
|
+
}
|
|
985
|
+
/** Input for recording a CCTP attestation */
|
|
986
|
+
interface CCTPAttestationInput {
|
|
987
|
+
/** The cross-chain transfer ID */
|
|
988
|
+
transferId: string;
|
|
989
|
+
/** The message hash from the attestation service */
|
|
990
|
+
messageHash: string;
|
|
991
|
+
/** Optional attestation metadata */
|
|
992
|
+
metadata?: Record<string, unknown>;
|
|
993
|
+
}
|
|
994
|
+
/** Input for confirming a cross-chain transfer on destination */
|
|
995
|
+
interface ConfirmCCTPTransferInput {
|
|
996
|
+
/** The cross-chain transfer ID */
|
|
997
|
+
transferId: string;
|
|
998
|
+
/** Destination chain transaction hash (from receiveMessage) */
|
|
999
|
+
destinationTxHash: string;
|
|
1000
|
+
/** Optional metadata */
|
|
1001
|
+
metadata?: Record<string, unknown>;
|
|
1002
|
+
}
|
|
1003
|
+
/** Validation result for a cross-chain transfer */
|
|
1004
|
+
interface CCTPValidationResult {
|
|
1005
|
+
/** Whether the transfer configuration is valid */
|
|
1006
|
+
valid: boolean;
|
|
1007
|
+
/** Validation checks performed */
|
|
1008
|
+
checks: CCTPValidationCheck[];
|
|
1009
|
+
/** Overall risk level */
|
|
1010
|
+
riskLevel: 'low' | 'medium' | 'high' | 'critical';
|
|
1011
|
+
/** Recommendations */
|
|
1012
|
+
recommendations: string[];
|
|
1013
|
+
}
|
|
1014
|
+
/** Individual validation check */
|
|
1015
|
+
interface CCTPValidationCheck {
|
|
1016
|
+
/** Check name */
|
|
1017
|
+
name: string;
|
|
1018
|
+
/** Whether the check passed */
|
|
1019
|
+
passed: boolean;
|
|
1020
|
+
/** Description */
|
|
1021
|
+
description: string;
|
|
1022
|
+
/** Severity if failed */
|
|
1023
|
+
severity: AnomalySeverity;
|
|
1024
|
+
}
|
|
1025
|
+
/** Cross-chain audit trail entry */
|
|
1026
|
+
interface CrossChainAuditEntry {
|
|
1027
|
+
/** The transfer record */
|
|
1028
|
+
transfer: CrossChainTransfer;
|
|
1029
|
+
/** Source chain action log ID (from logTransaction) */
|
|
1030
|
+
sourceActionId: string | null;
|
|
1031
|
+
/** Destination chain action log ID (from logTransaction) */
|
|
1032
|
+
destinationActionId: string | null;
|
|
1033
|
+
/** Whether source and destination are linked */
|
|
1034
|
+
linked: boolean;
|
|
1035
|
+
/** Duration from initiation to confirmation in milliseconds */
|
|
1036
|
+
durationMs: number | null;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* CCTPTransferManager handles cross-chain transfer tracking and validation
|
|
1040
|
+
* for Circle's Cross-Chain Transfer Protocol.
|
|
1041
|
+
*
|
|
1042
|
+
* Provides:
|
|
1043
|
+
* - Transfer validation (source chain to destination chain)
|
|
1044
|
+
* - CCTP message attestation logging
|
|
1045
|
+
* - Cross-chain audit trail linking
|
|
1046
|
+
* - Transfer lifecycle tracking (pending -> attested -> confirmed)
|
|
1047
|
+
*/
|
|
1048
|
+
declare class CCTPTransferManager {
|
|
1049
|
+
private transfers;
|
|
1050
|
+
private actionLinks;
|
|
1051
|
+
/**
|
|
1052
|
+
* Validate a cross-chain transfer before execution.
|
|
1053
|
+
*
|
|
1054
|
+
* Checks include:
|
|
1055
|
+
* - Source and destination chain support
|
|
1056
|
+
* - Route validity (different chains)
|
|
1057
|
+
* - Token support on both chains
|
|
1058
|
+
* - Amount validation
|
|
1059
|
+
* - Address format validation
|
|
1060
|
+
*
|
|
1061
|
+
* @param input - Transfer details to validate
|
|
1062
|
+
* @returns Validation result with checks and recommendations
|
|
1063
|
+
*/
|
|
1064
|
+
validateTransfer(input: InitiateCCTPTransferInput): CCTPValidationResult;
|
|
1065
|
+
/**
|
|
1066
|
+
* Record a new cross-chain transfer initiated via CCTP depositForBurn.
|
|
1067
|
+
*
|
|
1068
|
+
* @param input - Transfer initiation details
|
|
1069
|
+
* @returns The created CrossChainTransfer record
|
|
1070
|
+
*/
|
|
1071
|
+
initiateTransfer(input: InitiateCCTPTransferInput): CrossChainTransfer;
|
|
1072
|
+
/**
|
|
1073
|
+
* Record a CCTP attestation for a pending transfer.
|
|
1074
|
+
* Called after the attestation service has signed the burn message.
|
|
1075
|
+
*
|
|
1076
|
+
* @param input - Attestation details
|
|
1077
|
+
* @returns The updated CrossChainTransfer record
|
|
1078
|
+
* @throws Error if transfer not found or not in pending status
|
|
1079
|
+
*/
|
|
1080
|
+
recordAttestation(input: CCTPAttestationInput): CrossChainTransfer;
|
|
1081
|
+
/**
|
|
1082
|
+
* Confirm a cross-chain transfer has been received on the destination chain.
|
|
1083
|
+
* Called after receiveMessage has been executed on the destination.
|
|
1084
|
+
*
|
|
1085
|
+
* @param input - Confirmation details
|
|
1086
|
+
* @returns The updated CrossChainTransfer record
|
|
1087
|
+
* @throws Error if transfer not found or not in attested status
|
|
1088
|
+
*/
|
|
1089
|
+
confirmTransfer(input: ConfirmCCTPTransferInput): CrossChainTransfer;
|
|
1090
|
+
/**
|
|
1091
|
+
* Mark a transfer as failed.
|
|
1092
|
+
*
|
|
1093
|
+
* @param transferId - The transfer to mark as failed
|
|
1094
|
+
* @param reason - Reason for failure
|
|
1095
|
+
* @returns The updated CrossChainTransfer record
|
|
1096
|
+
*/
|
|
1097
|
+
failTransfer(transferId: string, reason: string): CrossChainTransfer;
|
|
1098
|
+
/**
|
|
1099
|
+
* Link a Kontext action log ID to a cross-chain transfer.
|
|
1100
|
+
* Used to correlate source and destination chain actions in the audit trail.
|
|
1101
|
+
*
|
|
1102
|
+
* @param transferId - The cross-chain transfer ID
|
|
1103
|
+
* @param actionId - The action log ID to link
|
|
1104
|
+
* @param side - Whether this is the source or destination action
|
|
1105
|
+
*/
|
|
1106
|
+
linkAction(transferId: string, actionId: string, side: 'source' | 'destination'): void;
|
|
1107
|
+
/**
|
|
1108
|
+
* Get a cross-chain transfer by ID.
|
|
1109
|
+
*/
|
|
1110
|
+
getTransfer(transferId: string): CrossChainTransfer | undefined;
|
|
1111
|
+
/**
|
|
1112
|
+
* Get all cross-chain transfers, optionally filtered by status.
|
|
1113
|
+
*/
|
|
1114
|
+
getTransfers(status?: CCTPMessageStatus): CrossChainTransfer[];
|
|
1115
|
+
/**
|
|
1116
|
+
* Get transfers by correlation ID.
|
|
1117
|
+
* Useful for finding all transfers related to a single workflow.
|
|
1118
|
+
*/
|
|
1119
|
+
getTransfersByCorrelation(correlationId: string): CrossChainTransfer[];
|
|
1120
|
+
/**
|
|
1121
|
+
* Build a cross-chain audit trail for a given transfer.
|
|
1122
|
+
* Links source and destination chain actions together.
|
|
1123
|
+
*
|
|
1124
|
+
* @param transferId - The transfer to build an audit trail for
|
|
1125
|
+
* @returns CrossChainAuditEntry with linked action references
|
|
1126
|
+
*/
|
|
1127
|
+
getAuditEntry(transferId: string): CrossChainAuditEntry | undefined;
|
|
1128
|
+
/**
|
|
1129
|
+
* Build audit trail entries for all transfers, optionally filtered.
|
|
1130
|
+
*
|
|
1131
|
+
* @param agentId - Optional filter by agent
|
|
1132
|
+
* @returns Array of CrossChainAuditEntry records
|
|
1133
|
+
*/
|
|
1134
|
+
getAuditTrail(agentId?: string): CrossChainAuditEntry[];
|
|
1135
|
+
/**
|
|
1136
|
+
* Get the CCTP domain ID for a given chain.
|
|
1137
|
+
*
|
|
1138
|
+
* @param chain - The blockchain network
|
|
1139
|
+
* @returns The CCTP domain ID, or undefined for unsupported chains
|
|
1140
|
+
*/
|
|
1141
|
+
static getDomainId(chain: Chain): number | undefined;
|
|
1142
|
+
/**
|
|
1143
|
+
* Get the chains supported for CCTP transfers.
|
|
1144
|
+
*/
|
|
1145
|
+
static getSupportedChains(): Chain[];
|
|
1146
|
+
private checkChainSupport;
|
|
1147
|
+
private checkRouteValidity;
|
|
1148
|
+
private checkTokenSupport;
|
|
1149
|
+
private checkAmountValidity;
|
|
1150
|
+
private checkAddressFormat;
|
|
1151
|
+
private generateRecommendations;
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
/** Supported webhook event types */
|
|
1155
|
+
type WebhookEventType = 'anomaly.detected' | 'task.confirmed' | 'task.failed' | 'trust.score_changed';
|
|
1156
|
+
/** Webhook registration configuration */
|
|
1157
|
+
interface WebhookConfig {
|
|
1158
|
+
/** Unique identifier for this webhook */
|
|
1159
|
+
id: string;
|
|
1160
|
+
/** Target URL to receive webhook POST requests */
|
|
1161
|
+
url: string;
|
|
1162
|
+
/** Event types to listen for */
|
|
1163
|
+
events: WebhookEventType[];
|
|
1164
|
+
/** Optional secret for HMAC signature verification */
|
|
1165
|
+
secret?: string;
|
|
1166
|
+
/** Whether this webhook is active */
|
|
1167
|
+
active: boolean;
|
|
1168
|
+
/** When this webhook was registered */
|
|
1169
|
+
createdAt: string;
|
|
1170
|
+
/** Optional metadata */
|
|
1171
|
+
metadata?: Record<string, unknown>;
|
|
1172
|
+
}
|
|
1173
|
+
/** Input for registering a new webhook */
|
|
1174
|
+
interface RegisterWebhookInput {
|
|
1175
|
+
/** Target URL */
|
|
1176
|
+
url: string;
|
|
1177
|
+
/** Event types to listen for */
|
|
1178
|
+
events: WebhookEventType[];
|
|
1179
|
+
/** Optional secret for payload signing */
|
|
1180
|
+
secret?: string;
|
|
1181
|
+
/** Optional metadata */
|
|
1182
|
+
metadata?: Record<string, unknown>;
|
|
1183
|
+
}
|
|
1184
|
+
/** Webhook delivery payload */
|
|
1185
|
+
interface WebhookPayload {
|
|
1186
|
+
/** Unique delivery ID */
|
|
1187
|
+
id: string;
|
|
1188
|
+
/** Event type */
|
|
1189
|
+
event: WebhookEventType;
|
|
1190
|
+
/** Timestamp of the event */
|
|
1191
|
+
timestamp: string;
|
|
1192
|
+
/** Event-specific data */
|
|
1193
|
+
data: Record<string, unknown>;
|
|
1194
|
+
}
|
|
1195
|
+
/** Result of a webhook delivery attempt */
|
|
1196
|
+
interface WebhookDeliveryResult {
|
|
1197
|
+
/** Webhook ID */
|
|
1198
|
+
webhookId: string;
|
|
1199
|
+
/** Delivery payload ID */
|
|
1200
|
+
payloadId: string;
|
|
1201
|
+
/** Whether delivery succeeded */
|
|
1202
|
+
success: boolean;
|
|
1203
|
+
/** HTTP status code (if applicable) */
|
|
1204
|
+
statusCode: number | null;
|
|
1205
|
+
/** Number of attempts made */
|
|
1206
|
+
attempts: number;
|
|
1207
|
+
/** Error message if failed */
|
|
1208
|
+
error: string | null;
|
|
1209
|
+
/** Timestamp of last attempt */
|
|
1210
|
+
lastAttemptAt: string;
|
|
1211
|
+
}
|
|
1212
|
+
/** Retry configuration */
|
|
1213
|
+
interface WebhookRetryConfig {
|
|
1214
|
+
/** Maximum number of retry attempts */
|
|
1215
|
+
maxRetries: number;
|
|
1216
|
+
/** Base delay in milliseconds for exponential backoff */
|
|
1217
|
+
baseDelayMs: number;
|
|
1218
|
+
/** Maximum delay in milliseconds */
|
|
1219
|
+
maxDelayMs: number;
|
|
1220
|
+
}
|
|
1221
|
+
/**
|
|
1222
|
+
* WebhookManager handles registration and delivery of webhook notifications
|
|
1223
|
+
* for SDK events including anomaly detection, task confirmation, and trust
|
|
1224
|
+
* score changes.
|
|
1225
|
+
*
|
|
1226
|
+
* Features:
|
|
1227
|
+
* - Register multiple webhook URLs with event type filtering
|
|
1228
|
+
* - Automatic retry with exponential backoff on delivery failure
|
|
1229
|
+
* - Delivery result tracking
|
|
1230
|
+
* - Enable/disable individual webhooks
|
|
1231
|
+
*
|
|
1232
|
+
* @example
|
|
1233
|
+
* ```typescript
|
|
1234
|
+
* const manager = new WebhookManager();
|
|
1235
|
+
*
|
|
1236
|
+
* manager.register({
|
|
1237
|
+
* url: 'https://example.com/webhooks/kontext',
|
|
1238
|
+
* events: ['anomaly.detected', 'task.confirmed'],
|
|
1239
|
+
* });
|
|
1240
|
+
*
|
|
1241
|
+
* // Webhooks fire automatically when events occur
|
|
1242
|
+
* await manager.notifyAnomalyDetected(anomalyEvent);
|
|
1243
|
+
* ```
|
|
1244
|
+
*/
|
|
1245
|
+
declare class WebhookManager {
|
|
1246
|
+
private webhooks;
|
|
1247
|
+
private deliveryResults;
|
|
1248
|
+
private retryConfig;
|
|
1249
|
+
private fetchFn;
|
|
1250
|
+
constructor(retryConfig?: Partial<WebhookRetryConfig>, fetchFn?: typeof fetch);
|
|
1251
|
+
/**
|
|
1252
|
+
* Register a new webhook endpoint.
|
|
1253
|
+
*
|
|
1254
|
+
* @param input - Webhook configuration
|
|
1255
|
+
* @returns The created WebhookConfig
|
|
1256
|
+
*/
|
|
1257
|
+
register(input: RegisterWebhookInput): WebhookConfig;
|
|
1258
|
+
/**
|
|
1259
|
+
* Unregister a webhook by ID.
|
|
1260
|
+
*
|
|
1261
|
+
* @param webhookId - The webhook to remove
|
|
1262
|
+
* @returns Whether the webhook was found and removed
|
|
1263
|
+
*/
|
|
1264
|
+
unregister(webhookId: string): boolean;
|
|
1265
|
+
/**
|
|
1266
|
+
* Enable or disable a webhook.
|
|
1267
|
+
*
|
|
1268
|
+
* @param webhookId - The webhook to update
|
|
1269
|
+
* @param active - Whether to enable or disable
|
|
1270
|
+
* @returns The updated WebhookConfig, or undefined if not found
|
|
1271
|
+
*/
|
|
1272
|
+
setActive(webhookId: string, active: boolean): WebhookConfig | undefined;
|
|
1273
|
+
/**
|
|
1274
|
+
* Get all registered webhooks.
|
|
1275
|
+
*/
|
|
1276
|
+
getWebhooks(): WebhookConfig[];
|
|
1277
|
+
/**
|
|
1278
|
+
* Get a specific webhook by ID.
|
|
1279
|
+
*/
|
|
1280
|
+
getWebhook(webhookId: string): WebhookConfig | undefined;
|
|
1281
|
+
/**
|
|
1282
|
+
* Get delivery results for a specific webhook or all webhooks.
|
|
1283
|
+
*/
|
|
1284
|
+
getDeliveryResults(webhookId?: string): WebhookDeliveryResult[];
|
|
1285
|
+
/**
|
|
1286
|
+
* Notify all subscribed webhooks of an anomaly detection event.
|
|
1287
|
+
*
|
|
1288
|
+
* @param anomaly - The detected anomaly event
|
|
1289
|
+
* @returns Array of delivery results
|
|
1290
|
+
*/
|
|
1291
|
+
notifyAnomalyDetected(anomaly: AnomalyEvent): Promise<WebhookDeliveryResult[]>;
|
|
1292
|
+
/**
|
|
1293
|
+
* Notify all subscribed webhooks of a task confirmation.
|
|
1294
|
+
*
|
|
1295
|
+
* @param task - The confirmed task
|
|
1296
|
+
* @returns Array of delivery results
|
|
1297
|
+
*/
|
|
1298
|
+
notifyTaskConfirmed(task: Task): Promise<WebhookDeliveryResult[]>;
|
|
1299
|
+
/**
|
|
1300
|
+
* Notify all subscribed webhooks of a task failure.
|
|
1301
|
+
*
|
|
1302
|
+
* @param task - The failed task
|
|
1303
|
+
* @returns Array of delivery results
|
|
1304
|
+
*/
|
|
1305
|
+
notifyTaskFailed(task: Task): Promise<WebhookDeliveryResult[]>;
|
|
1306
|
+
/**
|
|
1307
|
+
* Notify all subscribed webhooks of a trust score change.
|
|
1308
|
+
*
|
|
1309
|
+
* @param trustScore - The new trust score
|
|
1310
|
+
* @param previousScore - The previous score value (if known)
|
|
1311
|
+
* @returns Array of delivery results
|
|
1312
|
+
*/
|
|
1313
|
+
notifyTrustScoreChanged(trustScore: TrustScore, previousScore?: number): Promise<WebhookDeliveryResult[]>;
|
|
1314
|
+
private deliver;
|
|
1315
|
+
private deliverToWebhook;
|
|
1316
|
+
private computeSignature;
|
|
1317
|
+
private sleep;
|
|
1318
|
+
}
|
|
1319
|
+
|
|
1320
|
+
export { type ActionLog, type AnomalyCallback, type AnomalyDetectionConfig, type AnomalyEvent, type AnomalyRuleType, type AnomalySeverity, type AnomalyThresholds, type CCTPAttestationInput, type CCTPMessageStatus, CCTPTransferManager, type CCTPValidationCheck, type CCTPValidationResult, type CTRReport, type Chain, type ComplianceCheckResult, type ComplianceReport, type ConfirmCCTPTransferInput, type ConfirmTaskInput, type CreateTaskInput, type CrossChainAuditEntry, type CrossChainTransfer, type DateRange, DigestChain, type DigestLink, type DigestVerification, type Environment, type ExportFormat, type ExportOptions, type ExportResult, type InitiateCCTPTransferInput, Kontext, type KontextConfig, KontextError, KontextErrorCode, type KontextMode, type LogActionInput, type LogTransactionInput, type PrecisionTimestamp, type RegisterWebhookInput, type ReportOptions, type ReportSubject, type ReportType, type RiskFactor, type SARReport, type Task, type TaskEvidence, type TaskStatus, type Token, type TransactionEvaluation, type TransactionRecord, type TrustFactor, type TrustScore, UsdcCompliance, type UsdcComplianceCheck, type WebhookConfig, type WebhookDeliveryResult, type WebhookEventType, WebhookManager, type WebhookPayload, type WebhookRetryConfig, verifyExportedChain };
|