-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.did
More file actions
555 lines (555 loc) · 13.9 KB
/
Copy pathexample.did
File metadata and controls
555 lines (555 loc) · 13.9 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
type Voucher =
record {
cumulativeAmount: nat;
sequence: nat;
sessionId: text;
signature: blob;
};
type VerificationMethod =
variant {
AutoSettle;
BuyerConfirm: record {disputeWindowSeconds: nat;};
HashMatch;
ZkGroth16:
record {
bindResult: bool;
verificationKey: blob;
verifierCanister: principal;
};
};
type SpendingPolicy =
record {
allowedCallers: opt vec principal;
blockedCallers: opt vec principal;
maxConcurrentSessions: opt nat;
maxPerDay: opt nat;
maxPerTransaction: opt nat;
maxSessionDeposit: opt nat;
maxSessionDuration: opt int;
rateLimitPerMinute: opt nat;
sessionIdleTimeout: opt int;
};
type SignedTypedData =
record {
digest: text;
r: text;
s: text;
signature: text;
signer: text;
v: nat8;
};
type SignedTransaction =
record {
rawTx: text;
txHash: text;
};
type SignedAuthorization =
record {
authorization:
record {
from: text;
nonce: text;
signature: text;
to: text;
validAfter: nat;
validBefore: nat;
value: nat;
};
header: text;
paidAmount: nat;
};
type SessionStatus =
variant {
closed;
closing;
expired;
open;
};
type SessionState =
record {
consumed: nat;
deposited: nat;
id: text;
lastActivityAt: int;
openedAt: int;
payer: principal;
remaining: nat;
status: SessionStatus;
voucherCount: nat;
};
type SessionIntent =
record {
costPerCall: opt nat;
description: opt text;
expiry: int;
minDeposit: opt nat;
network: text;
recipient: text;
suggestedDeposit: nat;
token: text;
};
type SessionConfig =
record {
autoClose: bool;
idleTimeout: opt int;
maxDeposit: nat;
};
type ServiceType =
variant {
Async;
Sync;
};
type ServiceEntry =
record {
domains: vec text;
endpoint: text;
name: text;
skills: vec text;
version: text;
};
type ServiceDeliveryMethod =
variant {
Both;
Callback;
Poll;
};
type ServiceDefinition =
record {
createdAt: int;
delivery: ServiceDeliveryMethod;
description: text;
enabled: bool;
id: text;
name: text;
operatorId: principal;
pricing: PricingScheme;
serviceType: ServiceType;
timeout: nat;
verification: VerificationMethod;
};
type PricingScheme =
variant {
Exact: nat;
Session;
Upto: nat;
};
type PaymentSignature =
record {
asset: opt text;
authorization: opt Eip3009Authorization;
network: text;
nonce: blob;
publicKey: opt blob;
scheme: text;
sender: text;
signature: blob;
};
type PaymentResult =
variant {
depositBelowMinimum: nat;
expired: text;
insufficientFunds: text;
invalidSignature: text;
networkNotSupported: text;
ok: PaymentReceipt;
policyDenied: text;
reputationTooLow: nat;
settlementFailed: text;
settlementPending: text;
tokenNotAccepted: text;
};
type PaymentRequirement =
record {
amount: nat;
expiry: int;
network: text;
nonce: blob;
recipient: text;
scheme: text;
token: text;
tokenName: opt text;
tokenVersion: opt text;
};
type PaymentReceipt =
record {
amount: nat;
id: text;
network: text;
recipient: text;
refunded: opt nat;
sender: text;
sessionId: opt text;
timestamp: int;
token: text;
txHash: opt text;
};
type ParkedTx =
record {
chainId: nat;
leg: ParkedLeg;
parkedAt: int;
token: text;
txHash: text;
};
type ParkedLeg =
variant {
Refund;
Settle;
UptoRemainder;
};
type JobStatus =
variant {
Assigned;
Computing;
Disputed;
Expired;
Pending;
Refunded;
Settled;
Settling;
Submitted;
Verified;
};
type Job =
record {
actualCost: opt nat;
amount: nat;
buyer: text;
completedAt: opt int;
createdAt: int;
deliveryCallback: opt text;
expiresAt: int;
id: text;
operator: opt principal;
params: blob;
parkedTx: opt ParkedTx;
paymentReceiptId: text;
proof: opt blob;
result: opt blob;
serviceId: text;
status: JobStatus;
};
type HttpResponse =
record {
body: blob;
headers: vec record {
text;
text;
};
status_code: nat16;
upgrade: opt bool;
};
type HttpRequest =
record {
body: blob;
headers: vec record {
text;
text;
};
method: text;
url: text;
};
type Eip3009Authorization =
record {
from: text;
nonce: blob;
r: blob;
s: blob;
to: text;
v: nat8;
validAfter: nat;
validBefore: nat;
value: nat;
};
type DeliveryMethod =
variant {
assetCanister: record {
canisterId: principal;
path: text;
};
canisterQuery: record {
chunkCount: nat;
method: text;
};
httpUrl: text;
inline: blob;
};
type ContentStoreResult =
variant {
chunkNotFound: nat;
chunkTooLarge: nat;
contentAlreadyExists;
contentNotFound;
ok;
};
type ContentRef =
record {
id: text;
metadata: opt vec record {
text;
text;
};
mimeType: opt text;
sizeBytes: opt nat;
};
type ContentEntry =
record {
chunkCount: nat;
createdAt: int;
id: text;
mimeType: text;
totalSize: nat;
};
type ContentDelivery =
record {
delivery: DeliveryMethod;
grant: AccessGrant;
settlementTxHash: opt text;
};
type AgentCard =
record {
description: text;
name: text;
services: vec ServiceEntry;
x402Support: bool;
};
type AccessGrantResult =
variant {
expired: text;
invalidGrant: text;
ok;
revoked: text;
};
type AccessGrant =
record {
contentRef: ContentRef;
expiresAt: int;
grantId: text;
grantee: principal;
hmac: blob;
issuedAt: int;
receiptId: text;
};
/// ic402 Example Canister
///
/// Demonstrates a paid knowledge base API with multiple optional features.
/// The required core is small — everything marked OPTIONAL can be removed.
///
/// Structure:
/// 1. REQUIRED: Gateway + paid endpoint + HTTP 402 serving
/// 2. OPTIONAL: Streaming sessions (escrow + vouchers)
/// 3. OPTIONAL: Encrypted content store (in-canister)
/// 4. OPTIONAL: EVM remote signer (canister signs, client broadcasts)
/// 5. OPTIONAL: ERC-8004 identity metadata
service : {
claimJob: (jobId: text) -> (variant {
err: text;
ok;
});
confirmJob: (jobId: text) -> (variant {
err: text;
ok;
});
deleteContent: (id: text) -> (ContentStoreResult);
disableService: (id: text) -> (variant {
err: text;
ok;
});
disputeJob: (jobId: text, reason: text) -> (variant {
err: text;
ok;
});
enableService: (id: text) -> (variant {
err: text;
ok;
});
endSession: (sessionId: text) -> (PaymentResult);
forceCloseSession: (sessionId: text) -> (PaymentResult);
forceResolveSession: (sessionId: text) -> (variant {
err: text;
ok;
});
getAgentCard: () -> (AgentCard) query;
getAgentId: () -> (opt nat) query;
getChunk: (grant: AccessGrant, index: nat) -> (opt blob) query;
getContent: (contentId: text, paymentSig: opt PaymentSignature) ->
(variant {
error: text;
ok: ContentDelivery;
paymentRequired: vec PaymentRequirement;
});
getEvmAddress: () -> (text);
getEvmDrainMode: () -> (bool) query;
getEvmPublicKey: () -> (blob);
getJob: (jobId: text) -> (opt Job) query;
getJobResult: (jobId: text) -> (opt blob) query;
getJobStatus: (jobId: text) -> (opt JobStatus) query;
getPolicyConfig: () -> (SpendingPolicy) query;
health: () ->
(record {
cyclesBalance: nat;
jobs:
record {
active: nat;
expired: nat;
parked: nat;
refunded: nat;
settled: nat;
settling: nat;
total: nat;
};
sessions:
record {
closed: nat;
closing: nat;
expired: nat;
open: nat;
total: nat;
};
}) query;
http_request: (request: HttpRequest) -> (HttpResponse) query;
http_request_update: (request: HttpRequest) -> (HttpResponse);
/// Helper: compute keccak256 hash of a byte array. Useful for building type hashes.
keccak256: (data: vec nat8) -> (vec nat8) query;
listContent: () -> (vec ContentEntry) query;
listJobs: (serviceId: text, statusFilter: opt JobStatus) -> (vec Job) query;
listPendingEvmDeposits: () ->
(vec
record {
amount: nat;
chainId: nat;
createdAt: int;
payer: principal;
payerEvmAddress: text;
token: text;
txHash: text;
}) query;
listServices: () -> (vec ServiceDefinition) query;
openSession: (config: SessionConfig, sig: PaymentSignature) ->
(variant {
err: text;
ok: SessionState;
});
pendingEvmDepositCount: () -> (nat) query;
quoteServiceRequest: (serviceId: text) ->
(variant {
err: text;
ok:
record {
amount: nat;
enabled: bool;
fee: nat;
pricingKind: text;
total: nat;
};
}) query;
reconcileEvmDeposit: (txHash: text) ->
(variant {
err: text;
notFound;
refunded: text;
reverted;
stillPending;
});
reconcileJob: (jobId: text) -> (variant {
err: text;
ok: text;
});
reconcileSession: (sessionId: text) -> (variant {
err: text;
ok: text;
});
registerService: (name: text, description: text, serviceType: ServiceType,
pricing: PricingScheme, verificationMethod: text, verifierCanisterId:
opt text, verificationKey: opt blob, delivery: ServiceDeliveryMethod,
timeout: nat) -> (variant {
err: text;
ok: text;
});
requestSession: () -> (SessionIntent);
resolveJob: (jobId: text, terminal: JobStatus) -> (variant {
err: text;
ok;
});
search: (searchQuery: text, paymentSig: opt PaymentSignature) ->
(variant {
error: text;
ok: vec text;
paymentRequired: vec PaymentRequirement;
});
sessionQuery: (voucher: Voucher, question: text) ->
(variant {
error: text;
ok: text;
});
setEvmDrainMode: (on: bool) -> ();
setEvmPayout: (address: text) -> (variant {
err: text;
ok;
});
setPolicy: (p: SpendingPolicy) -> ();
/// Sign an ERC-8004 agent registration tx.
/// Client provides nonce + gas from their RPC, broadcasts + polls receipt.
signAgentRegistration: (nonce: nat, maxFeePerGas: nat,
maxPriorityFeePerGas: nat) -> (variant {
err: text;
ok: SignedTransaction;
});
/// Sign an ERC-20 transfer. Client provides nonce + gas from their RPC.
signErc20Transfer: (chainId: nat, tokenAddress: text, recipientAddress:
text, amount: nat, nonce: nat, maxFeePerGas: nat, maxPriorityFeePerGas:
nat) -> (variant {
err: text;
ok: SignedTransaction;
});
/// Sign a native ETH transfer. Client provides nonce + gas from their RPC.
signEthTransfer: (chainId: nat, recipientAddress: text, amountWei:
nat, gasLimit: nat, nonce: nat, maxFeePerGas: nat, maxPriorityFeePerGas:
nat) -> (variant {
err: text;
ok: SignedTransaction;
});
/// Sign arbitrary EIP-712 typed data. The generic primitive for DEX integration.
/// The caller provides pre-computed domainSeparator and structHash (32 bytes each).
/// The canister computes the EIP-712 digest and signs with tECDSA.
signTypedData: (domainSeparator: vec nat8, structHash: vec nat8) ->
(variant {
err: text;
ok: SignedTypedData;
});
/// Sign an EIP-3009 authorization for x402 payment.
/// Client probes the URL, extracts chain/token/recipient/amount from the 402, and calls this.
/// Canister signs → returns header → client retries with header.
signX402Payment: (chainId: nat, tokenAddress: text, recipient: text,
amount: nat, tokenName: text, tokenVersion: text) ->
(variant {
err: text;
ok: SignedAuthorization;
});
submitJobResult: (jobId: text, result: blob, proof: opt blob, actualCost:
opt nat) -> (variant {
err: text;
ok;
});
submitServiceRequest: (serviceId: text, params: blob, paymentSig:
opt PaymentSignature) ->
(variant {
error: text;
ok: record {jobId: text;};
paymentRequired: vec PaymentRequirement;
});
sweepEvm: (chainId: nat, token: text, to: text, amount: nat) ->
(variant {
confirmed: text;
err: text;
pending: text;
reverted: text;
});
uploadContent: (id: text, mimeType: text, data: blob) ->
(ContentStoreResult);
uploadContentChunk: (id: text, index: nat, data: blob) ->
(ContentStoreResult);
uploadContentInit: (id: text, mimeType: text, totalSize: nat, chunkCount:
nat) -> (ContentStoreResult);
verifyGrant: (grant: AccessGrant) -> (AccessGrantResult) query;
}