@kya-os/contracts 1.5.3-canary.16 → 1.5.3-canary.17
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/.turbo/turbo-build.log +17 -0
- package/.turbo/turbo-test$colon$coverage.log +85 -0
- package/.turbo/turbo-test.log +32 -0
- package/coverage/coverage-final.json +38 -0
- package/dist/consent/schemas.d.ts +18 -0
- package/dist/consent/schemas.js +10 -0
- package/dist/dashboard-config/schemas.d.ts +1424 -220
- package/dist/tool-protection/index.d.ts +418 -8
- package/dist/tool-protection/index.js +61 -2
- package/package.json +35 -129
- package/schemas/cli/register-output/v1.0.0.json +69 -0
- package/schemas/identity/v1.0.0.json +46 -0
- package/schemas/proof/v1.0.0.json +80 -0
- package/schemas/registry/receipt-v1.0.0.json +60 -0
- package/schemas/verifier/verify-page/v1.0.0.json +94 -0
- package/schemas/well-known/agent/v1.0.0.json +67 -0
- package/schemas/well-known/did/v1.0.0.json +174 -0
- package/scripts/emit-schemas.js +11 -0
- package/src/agentshield-api/admin-schemas.ts +31 -0
- package/src/agentshield-api/admin-types.ts +47 -0
- package/src/agentshield-api/endpoints.ts +60 -0
- package/src/agentshield-api/index.ts +70 -0
- package/src/agentshield-api/schemas.ts +304 -0
- package/src/agentshield-api/types.ts +317 -0
- package/src/audit/index.ts +128 -0
- package/src/cli.ts +156 -0
- package/src/config/base.ts +107 -0
- package/src/config/builder.ts +97 -0
- package/src/config/delegation.ts +232 -0
- package/src/config/identity.ts +252 -0
- package/src/config/index.ts +78 -0
- package/src/config/proofing.ts +138 -0
- package/src/config/tool-context.ts +41 -0
- package/src/config/tool-protection.ts +174 -0
- package/src/consent/index.ts +32 -0
- package/src/consent/schemas.ts +334 -0
- package/src/consent/types.ts +199 -0
- package/src/dashboard-config/default-config.json +86 -0
- package/src/dashboard-config/default-config.ts +266 -0
- package/src/dashboard-config/index.ts +48 -0
- package/src/dashboard-config/schemas.ts +286 -0
- package/src/dashboard-config/types.ts +404 -0
- package/src/delegation/constraints.ts +267 -0
- package/src/delegation/index.ts +8 -0
- package/src/delegation/schemas.ts +595 -0
- package/src/did/index.ts +9 -0
- package/src/did/resolve-contract.ts +255 -0
- package/src/did/schemas.ts +190 -0
- package/src/did/types.ts +224 -0
- package/src/env/constants.ts +70 -0
- package/src/env/index.ts +5 -0
- package/src/handshake.ts +125 -0
- package/src/index.ts +45 -0
- package/src/proof/index.ts +31 -0
- package/src/proof/proof-record.ts +163 -0
- package/src/proof/signing-spec.ts +146 -0
- package/src/proof.ts +99 -0
- package/src/registry.ts +146 -0
- package/src/runtime/errors.ts +153 -0
- package/src/runtime/headers.ts +136 -0
- package/src/runtime/index.ts +6 -0
- package/src/test.ts +143 -0
- package/src/tlkrc/index.ts +5 -0
- package/src/tlkrc/rotation.ts +153 -0
- package/src/tool-protection/index.ts +343 -0
- package/src/utils/validation.ts +93 -0
- package/src/vc/index.ts +8 -0
- package/src/vc/schemas.ts +277 -0
- package/src/vc/statuslist.ts +279 -0
- package/src/verifier.ts +92 -0
- package/src/well-known/index.ts +237 -0
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delegation Record Schemas
|
|
3
|
+
*
|
|
4
|
+
* Types and schemas for delegation records that link VCs with CRISP constraints.
|
|
5
|
+
* Delegations represent the transfer of authority from one DID to another.
|
|
6
|
+
*
|
|
7
|
+
* **IMPORTANT**: Per Python POC design (Delegation-Service.md:136-146),
|
|
8
|
+
* delegations SHOULD be issued as W3C Verifiable Credentials, not just reference them.
|
|
9
|
+
* This file provides both:
|
|
10
|
+
* - DelegationRecord: Legacy/internal format (contains delegation data)
|
|
11
|
+
* - DelegationCredential: W3C VC format (delegation as credentialSubject)
|
|
12
|
+
*
|
|
13
|
+
* Related Spec: MCP-I §4.1, §4.2, W3C VC Data Model 1.1
|
|
14
|
+
* Python Reference: Delegation-Documentation.md, Delegation-Service.md
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import { z } from 'zod';
|
|
18
|
+
import { DelegationConstraintsSchema, type DelegationConstraints } from './constraints.js';
|
|
19
|
+
import {
|
|
20
|
+
VerifiableCredentialSchema,
|
|
21
|
+
ContextSchema,
|
|
22
|
+
IssuerSchema,
|
|
23
|
+
CredentialStatusSchema,
|
|
24
|
+
ProofSchema,
|
|
25
|
+
type VerifiableCredential,
|
|
26
|
+
} from '../vc/schemas.js';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Delegation Status
|
|
30
|
+
*
|
|
31
|
+
* Lifecycle status of a delegation
|
|
32
|
+
*/
|
|
33
|
+
export const DelegationStatusSchema = z.enum(['active', 'revoked', 'expired']);
|
|
34
|
+
export type DelegationStatus = z.infer<typeof DelegationStatusSchema>;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Delegation Record Schema
|
|
38
|
+
*
|
|
39
|
+
* Complete delegation record linking issuer (delegator) to subject (delegatee)
|
|
40
|
+
* with backing VC and CRISP constraints.
|
|
41
|
+
*
|
|
42
|
+
* **Key Invariants:**
|
|
43
|
+
* - Delegation MUST reference a live VC via `vcId`
|
|
44
|
+
* - Revocation of VC invalidates all linked delegations
|
|
45
|
+
* - Chain: no cycles allowed
|
|
46
|
+
* - Child `notAfter` ≤ parent `notAfter`
|
|
47
|
+
* - Child scopes ⊆ parent scopes
|
|
48
|
+
* - Child budgets ≤ parent budgets (same unit)
|
|
49
|
+
*/
|
|
50
|
+
export const DelegationRecordSchema = z.object({
|
|
51
|
+
/** Unique identifier for the delegation */
|
|
52
|
+
id: z.string().min(1),
|
|
53
|
+
|
|
54
|
+
/** DID of the delegator (issuer, e.g., merchant/user) */
|
|
55
|
+
issuerDid: z.string().min(1),
|
|
56
|
+
|
|
57
|
+
/** DID of the delegatee (subject, e.g., agent) */
|
|
58
|
+
subjectDid: z.string().min(1),
|
|
59
|
+
|
|
60
|
+
/** Optional controller (user account ID or DID) */
|
|
61
|
+
controller: z.string().optional(),
|
|
62
|
+
|
|
63
|
+
/** ID of the backing Verifiable Credential */
|
|
64
|
+
vcId: z.string().min(1),
|
|
65
|
+
|
|
66
|
+
/** Optional parent delegation ID for chain tracking */
|
|
67
|
+
parentId: z.string().optional(),
|
|
68
|
+
|
|
69
|
+
/** CRISP constraints on this delegation */
|
|
70
|
+
constraints: DelegationConstraintsSchema,
|
|
71
|
+
|
|
72
|
+
/** Detached JWS signature over canonical delegation document */
|
|
73
|
+
signature: z.string().min(1),
|
|
74
|
+
|
|
75
|
+
/** Current status of the delegation */
|
|
76
|
+
status: DelegationStatusSchema,
|
|
77
|
+
|
|
78
|
+
/** Timestamp when created (milliseconds since epoch) */
|
|
79
|
+
createdAt: z.number().int().positive().optional(),
|
|
80
|
+
|
|
81
|
+
/** Timestamp when revoked (if status is revoked) */
|
|
82
|
+
revokedAt: z.number().int().positive().optional(),
|
|
83
|
+
|
|
84
|
+
/** Optional reason for revocation */
|
|
85
|
+
revokedReason: z.string().optional(),
|
|
86
|
+
|
|
87
|
+
/** Optional metadata */
|
|
88
|
+
metadata: z.record(z.any()).optional(),
|
|
89
|
+
}).passthrough(); // Allow extensibility
|
|
90
|
+
|
|
91
|
+
export type DelegationRecord = z.infer<typeof DelegationRecordSchema>;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Delegation Chain Entry
|
|
95
|
+
*
|
|
96
|
+
* Represents a single link in a delegation chain
|
|
97
|
+
*/
|
|
98
|
+
export const DelegationChainEntrySchema = z.object({
|
|
99
|
+
/** Delegation ID */
|
|
100
|
+
delegationId: z.string().min(1),
|
|
101
|
+
|
|
102
|
+
/** Issuer DID */
|
|
103
|
+
issuerDid: z.string().min(1),
|
|
104
|
+
|
|
105
|
+
/** Subject DID */
|
|
106
|
+
subjectDid: z.string().min(1),
|
|
107
|
+
|
|
108
|
+
/** VC ID */
|
|
109
|
+
vcId: z.string().min(1),
|
|
110
|
+
|
|
111
|
+
/** Depth in chain (0 = root) */
|
|
112
|
+
depth: z.number().int().nonnegative(),
|
|
113
|
+
|
|
114
|
+
/** Constraints */
|
|
115
|
+
constraints: DelegationConstraintsSchema,
|
|
116
|
+
|
|
117
|
+
/** Status */
|
|
118
|
+
status: DelegationStatusSchema,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
export type DelegationChainEntry = z.infer<typeof DelegationChainEntrySchema>;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Delegation Chain
|
|
125
|
+
*
|
|
126
|
+
* Represents a complete delegation chain from root to leaf
|
|
127
|
+
*/
|
|
128
|
+
export const DelegationChainSchema = z.object({
|
|
129
|
+
/** Root issuer DID */
|
|
130
|
+
rootIssuer: z.string().min(1),
|
|
131
|
+
|
|
132
|
+
/** Leaf subject DID */
|
|
133
|
+
leafSubject: z.string().min(1),
|
|
134
|
+
|
|
135
|
+
/** All delegations in the chain, ordered root to leaf */
|
|
136
|
+
chain: z.array(DelegationChainEntrySchema).min(1),
|
|
137
|
+
|
|
138
|
+
/** Total chain depth */
|
|
139
|
+
depth: z.number().int().nonnegative(),
|
|
140
|
+
|
|
141
|
+
/** Whether the entire chain is valid */
|
|
142
|
+
valid: z.boolean(),
|
|
143
|
+
|
|
144
|
+
/** Optional validation errors */
|
|
145
|
+
errors: z.array(z.string()).optional(),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
export type DelegationChain = z.infer<typeof DelegationChainSchema>;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Delegation Creation Request
|
|
152
|
+
*
|
|
153
|
+
* Input for creating a new delegation
|
|
154
|
+
*/
|
|
155
|
+
export const DelegationCreationRequestSchema = z.object({
|
|
156
|
+
/** Delegator DID */
|
|
157
|
+
issuerDid: z.string().min(1),
|
|
158
|
+
|
|
159
|
+
/** Delegatee DID */
|
|
160
|
+
subjectDid: z.string().min(1),
|
|
161
|
+
|
|
162
|
+
/** Optional controller */
|
|
163
|
+
controller: z.string().optional(),
|
|
164
|
+
|
|
165
|
+
/** Constraints */
|
|
166
|
+
constraints: DelegationConstraintsSchema,
|
|
167
|
+
|
|
168
|
+
/** Optional parent delegation ID */
|
|
169
|
+
parentId: z.string().optional(),
|
|
170
|
+
|
|
171
|
+
/** Optional VC ID (if not provided, will be created) */
|
|
172
|
+
vcId: z.string().optional(),
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
export type DelegationCreationRequest = z.infer<typeof DelegationCreationRequestSchema>;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Delegation Verification Result
|
|
179
|
+
*
|
|
180
|
+
* Result of delegation verification
|
|
181
|
+
*/
|
|
182
|
+
export const DelegationVerificationResultSchema = z.object({
|
|
183
|
+
/** Whether delegation is valid */
|
|
184
|
+
valid: z.boolean(),
|
|
185
|
+
|
|
186
|
+
/** Delegation ID */
|
|
187
|
+
delegationId: z.string().min(1),
|
|
188
|
+
|
|
189
|
+
/** Status */
|
|
190
|
+
status: DelegationStatusSchema,
|
|
191
|
+
|
|
192
|
+
/** Optional reason for invalid status */
|
|
193
|
+
reason: z.string().optional(),
|
|
194
|
+
|
|
195
|
+
/** Whether backing VC is valid */
|
|
196
|
+
credentialValid: z.boolean().optional(),
|
|
197
|
+
|
|
198
|
+
/** Whether chain is valid (if part of chain) */
|
|
199
|
+
chainValid: z.boolean().optional(),
|
|
200
|
+
|
|
201
|
+
/** Timestamp of verification */
|
|
202
|
+
verifiedAt: z.number().int().positive(),
|
|
203
|
+
|
|
204
|
+
/** Optional verification details */
|
|
205
|
+
details: z.record(z.any()).optional(),
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
export type DelegationVerificationResult = z.infer<typeof DelegationVerificationResultSchema>;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Validation Helpers
|
|
212
|
+
*/
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Validate a delegation record
|
|
216
|
+
*
|
|
217
|
+
* @param record - The delegation record to validate
|
|
218
|
+
* @returns Validation result
|
|
219
|
+
*/
|
|
220
|
+
export function validateDelegationRecord(record: unknown) {
|
|
221
|
+
return DelegationRecordSchema.safeParse(record);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Validate a delegation chain
|
|
226
|
+
*
|
|
227
|
+
* @param chain - The delegation chain to validate
|
|
228
|
+
* @returns Validation result
|
|
229
|
+
*/
|
|
230
|
+
export function validateDelegationChain(chain: unknown) {
|
|
231
|
+
return DelegationChainSchema.safeParse(chain);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Check if a delegation is expired based on constraints
|
|
236
|
+
*
|
|
237
|
+
* @param delegation - The delegation to check
|
|
238
|
+
* @returns true if expired
|
|
239
|
+
*/
|
|
240
|
+
export function isDelegationExpired(delegation: DelegationRecord): boolean {
|
|
241
|
+
if (!delegation.constraints.notAfter) {
|
|
242
|
+
return false;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
246
|
+
return nowSec > delegation.constraints.notAfter;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Check if a delegation is not yet valid based on constraints
|
|
251
|
+
*
|
|
252
|
+
* @param delegation - The delegation to check
|
|
253
|
+
* @returns true if not yet valid
|
|
254
|
+
*/
|
|
255
|
+
export function isDelegationNotYetValid(delegation: DelegationRecord): boolean {
|
|
256
|
+
if (!delegation.constraints.notBefore) {
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
261
|
+
return nowSec < delegation.constraints.notBefore;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Check if a delegation is currently valid (active and within time bounds)
|
|
266
|
+
*
|
|
267
|
+
* @param delegation - The delegation to check
|
|
268
|
+
* @returns true if currently valid
|
|
269
|
+
*/
|
|
270
|
+
export function isDelegationCurrentlyValid(delegation: DelegationRecord): boolean {
|
|
271
|
+
if (delegation.status !== 'active') {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (isDelegationExpired(delegation)) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (isDelegationNotYetValid(delegation)) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return true;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Constants
|
|
288
|
+
*/
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Maximum reasonable delegation chain depth
|
|
292
|
+
*/
|
|
293
|
+
export const MAX_DELEGATION_CHAIN_DEPTH = 10;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Default delegation status for new delegations
|
|
297
|
+
*/
|
|
298
|
+
export const DEFAULT_DELEGATION_STATUS: DelegationStatus = 'active';
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Supported delegation statuses
|
|
302
|
+
*/
|
|
303
|
+
export const DELEGATION_STATUSES: DelegationStatus[] = ['active', 'revoked', 'expired'];
|
|
304
|
+
|
|
305
|
+
// ============================================================================
|
|
306
|
+
// W3C VC-BASED DELEGATION CREDENTIALS (Phase 3 Implementation)
|
|
307
|
+
// ============================================================================
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Delegation Credential Context
|
|
311
|
+
*
|
|
312
|
+
* Additional JSON-LD context for delegation credentials
|
|
313
|
+
*/
|
|
314
|
+
export const DELEGATION_CREDENTIAL_CONTEXT =
|
|
315
|
+
'https://schemas.kya-os.ai/xmcp-i/credentials/delegation.v1.0.0.json' as const;
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Delegation Credential Subject Schema
|
|
319
|
+
*
|
|
320
|
+
* The credentialSubject of a DelegationCredential contains:
|
|
321
|
+
* - id: The delegatee DID (subject of the delegation)
|
|
322
|
+
* - delegation: The complete delegation record
|
|
323
|
+
*
|
|
324
|
+
* Per Python POC (Delegation-Service.md:136-146), delegations are issued AS
|
|
325
|
+
* W3C VCs, with the delegation data embedded in the credentialSubject.
|
|
326
|
+
*/
|
|
327
|
+
export const DelegationCredentialSubjectSchema = z.object({
|
|
328
|
+
/** Subject DID (delegatee) */
|
|
329
|
+
id: z.string().min(1),
|
|
330
|
+
|
|
331
|
+
/** The delegation information */
|
|
332
|
+
delegation: z.object({
|
|
333
|
+
/** Unique identifier for the delegation */
|
|
334
|
+
id: z.string().min(1),
|
|
335
|
+
|
|
336
|
+
/** DID of the delegator (issuer, e.g., merchant/user) */
|
|
337
|
+
issuerDid: z.string().min(1),
|
|
338
|
+
|
|
339
|
+
/** DID of the delegatee (subject, e.g., agent) */
|
|
340
|
+
subjectDid: z.string().min(1),
|
|
341
|
+
|
|
342
|
+
/** Optional controller (user account ID or DID) */
|
|
343
|
+
controller: z.string().optional(),
|
|
344
|
+
|
|
345
|
+
/** Optional parent delegation ID for chain tracking */
|
|
346
|
+
parentId: z.string().optional(),
|
|
347
|
+
|
|
348
|
+
/** CRISP constraints on this delegation */
|
|
349
|
+
constraints: DelegationConstraintsSchema,
|
|
350
|
+
|
|
351
|
+
/** Current status of the delegation */
|
|
352
|
+
status: DelegationStatusSchema.default('active'),
|
|
353
|
+
|
|
354
|
+
/** Timestamp when created (milliseconds since epoch) */
|
|
355
|
+
createdAt: z.number().int().positive().optional(),
|
|
356
|
+
|
|
357
|
+
/** Optional metadata */
|
|
358
|
+
metadata: z.record(z.any()).optional(),
|
|
359
|
+
}),
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
export type DelegationCredentialSubject = z.infer<
|
|
363
|
+
typeof DelegationCredentialSubjectSchema
|
|
364
|
+
>;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Delegation Credential Schema
|
|
368
|
+
*
|
|
369
|
+
* W3C Verifiable Credential for delegations.
|
|
370
|
+
* This is the PRIMARY format for delegation issuance and verification.
|
|
371
|
+
*
|
|
372
|
+
* Structure:
|
|
373
|
+
* - @context: [...W3C VC contexts, delegation context]
|
|
374
|
+
* - type: ['VerifiableCredential', 'DelegationCredential']
|
|
375
|
+
* - issuer: Delegator DID
|
|
376
|
+
* - issuanceDate: When delegation was created
|
|
377
|
+
* - expirationDate: Maps to delegation.constraints.notAfter
|
|
378
|
+
* - credentialSubject: Contains delegatee DID + delegation data
|
|
379
|
+
* - credentialStatus: StatusList2021Entry for revocation checking
|
|
380
|
+
* - proof: Ed25519Signature2020
|
|
381
|
+
*
|
|
382
|
+
* Per Python POC design (Delegation-Service.md:136-163):
|
|
383
|
+
* - Every delegation MUST be issued as a VC
|
|
384
|
+
* - Verification checks BOTH the VC signature AND delegation constraints
|
|
385
|
+
* - Revocation updates the StatusList2021
|
|
386
|
+
*
|
|
387
|
+
* Related Spec: MCP-I §4.1, §4.2, W3C VC Data Model 1.1
|
|
388
|
+
*/
|
|
389
|
+
export const DelegationCredentialSchema = VerifiableCredentialSchema.extend({
|
|
390
|
+
/** @context MUST include delegation context */
|
|
391
|
+
'@context': z
|
|
392
|
+
.array(z.union([z.string().url(), z.record(z.any())]))
|
|
393
|
+
.refine(
|
|
394
|
+
(contexts) => {
|
|
395
|
+
// Check for W3C VC context (first)
|
|
396
|
+
const firstContext = contexts[0];
|
|
397
|
+
if (
|
|
398
|
+
typeof firstContext !== 'string' ||
|
|
399
|
+
firstContext !== 'https://www.w3.org/2018/credentials/v1'
|
|
400
|
+
) {
|
|
401
|
+
return false;
|
|
402
|
+
}
|
|
403
|
+
// Optionally include delegation context (recommended)
|
|
404
|
+
return true;
|
|
405
|
+
},
|
|
406
|
+
{
|
|
407
|
+
message: 'First @context must be W3C VC context',
|
|
408
|
+
}
|
|
409
|
+
),
|
|
410
|
+
|
|
411
|
+
/** type MUST include both VerifiableCredential and DelegationCredential */
|
|
412
|
+
type: z
|
|
413
|
+
.array(z.string())
|
|
414
|
+
.refine(
|
|
415
|
+
(types) =>
|
|
416
|
+
types.includes('VerifiableCredential') &&
|
|
417
|
+
types.includes('DelegationCredential'),
|
|
418
|
+
{
|
|
419
|
+
message:
|
|
420
|
+
'type must include both "VerifiableCredential" and "DelegationCredential"',
|
|
421
|
+
}
|
|
422
|
+
),
|
|
423
|
+
|
|
424
|
+
/** issuer is the delegator DID */
|
|
425
|
+
issuer: IssuerSchema,
|
|
426
|
+
|
|
427
|
+
/** issuanceDate maps to delegation creation time */
|
|
428
|
+
issuanceDate: z.string().datetime(),
|
|
429
|
+
|
|
430
|
+
/** expirationDate maps to delegation.constraints.notAfter */
|
|
431
|
+
expirationDate: z.string().datetime().optional(),
|
|
432
|
+
|
|
433
|
+
/** credentialSubject contains delegatee DID + delegation data */
|
|
434
|
+
credentialSubject: DelegationCredentialSubjectSchema,
|
|
435
|
+
|
|
436
|
+
/** credentialStatus for StatusList2021 revocation checking */
|
|
437
|
+
credentialStatus: CredentialStatusSchema.optional(),
|
|
438
|
+
|
|
439
|
+
/** proof is Ed25519Signature2020 */
|
|
440
|
+
proof: ProofSchema.optional(),
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
export type DelegationCredential = z.infer<typeof DelegationCredentialSchema>;
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Validate a delegation credential
|
|
447
|
+
*
|
|
448
|
+
* @param credential - The delegation credential to validate
|
|
449
|
+
* @returns Validation result
|
|
450
|
+
*/
|
|
451
|
+
export function validateDelegationCredential(credential: unknown) {
|
|
452
|
+
return DelegationCredentialSchema.safeParse(credential);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Extract DelegationRecord from DelegationCredential
|
|
457
|
+
*
|
|
458
|
+
* Utility to extract the legacy DelegationRecord format from a W3C VC.
|
|
459
|
+
* Useful for backward compatibility and internal processing.
|
|
460
|
+
*
|
|
461
|
+
* @param vc - The delegation credential
|
|
462
|
+
* @returns DelegationRecord
|
|
463
|
+
*/
|
|
464
|
+
export function extractDelegationFromVC(vc: DelegationCredential): DelegationRecord {
|
|
465
|
+
const delegation = vc.credentialSubject.delegation;
|
|
466
|
+
|
|
467
|
+
// Extract signature from proof (may be in different formats)
|
|
468
|
+
let signature = '';
|
|
469
|
+
if (vc.proof) {
|
|
470
|
+
const proof = vc.proof as any;
|
|
471
|
+
signature = proof.proofValue || proof.jws || proof.signatureValue || '';
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
return {
|
|
475
|
+
id: delegation.id,
|
|
476
|
+
issuerDid: delegation.issuerDid,
|
|
477
|
+
subjectDid: delegation.subjectDid,
|
|
478
|
+
controller: delegation.controller,
|
|
479
|
+
vcId: vc.id || `vc:${delegation.id}`, // VC id becomes vcId
|
|
480
|
+
parentId: delegation.parentId,
|
|
481
|
+
constraints: delegation.constraints,
|
|
482
|
+
signature,
|
|
483
|
+
status: delegation.status,
|
|
484
|
+
createdAt: delegation.createdAt,
|
|
485
|
+
revokedAt: undefined, // Revocation status comes from credentialStatus
|
|
486
|
+
revokedReason: undefined,
|
|
487
|
+
metadata: delegation.metadata,
|
|
488
|
+
};
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Create DelegationCredential from DelegationRecord (unsigned)
|
|
493
|
+
*
|
|
494
|
+
* Wraps a DelegationRecord in a W3C VC structure (without proof).
|
|
495
|
+
* The caller must sign this to create a valid DelegationCredential.
|
|
496
|
+
*
|
|
497
|
+
* @param delegation - The delegation record
|
|
498
|
+
* @param options - Optional VC options (id, issuanceDate, etc.)
|
|
499
|
+
* @returns Unsigned DelegationCredential
|
|
500
|
+
*/
|
|
501
|
+
export function wrapDelegationAsVC(
|
|
502
|
+
delegation: DelegationRecord,
|
|
503
|
+
options?: {
|
|
504
|
+
id?: string;
|
|
505
|
+
issuanceDate?: string;
|
|
506
|
+
expirationDate?: string;
|
|
507
|
+
credentialStatus?: z.infer<typeof CredentialStatusSchema>;
|
|
508
|
+
}
|
|
509
|
+
): Omit<DelegationCredential, 'proof'> {
|
|
510
|
+
const now = new Date().toISOString();
|
|
511
|
+
const expirationDate = delegation.constraints.notAfter
|
|
512
|
+
? new Date(delegation.constraints.notAfter * 1000).toISOString()
|
|
513
|
+
: options?.expirationDate;
|
|
514
|
+
|
|
515
|
+
// Compute issuanceDate
|
|
516
|
+
let issuanceDate = options?.issuanceDate || now;
|
|
517
|
+
if (!options?.issuanceDate && delegation.createdAt) {
|
|
518
|
+
issuanceDate = new Date(delegation.createdAt).toISOString();
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
return {
|
|
522
|
+
'@context': [
|
|
523
|
+
'https://www.w3.org/2018/credentials/v1',
|
|
524
|
+
DELEGATION_CREDENTIAL_CONTEXT,
|
|
525
|
+
],
|
|
526
|
+
id: options?.id || delegation.vcId || `urn:uuid:${delegation.id}`,
|
|
527
|
+
type: ['VerifiableCredential', 'DelegationCredential'],
|
|
528
|
+
issuer: delegation.issuerDid,
|
|
529
|
+
issuanceDate,
|
|
530
|
+
expirationDate,
|
|
531
|
+
credentialSubject: {
|
|
532
|
+
id: delegation.subjectDid,
|
|
533
|
+
delegation: {
|
|
534
|
+
id: delegation.id,
|
|
535
|
+
issuerDid: delegation.issuerDid,
|
|
536
|
+
subjectDid: delegation.subjectDid,
|
|
537
|
+
controller: delegation.controller,
|
|
538
|
+
parentId: delegation.parentId,
|
|
539
|
+
constraints: delegation.constraints,
|
|
540
|
+
status: delegation.status,
|
|
541
|
+
createdAt: delegation.createdAt,
|
|
542
|
+
metadata: delegation.metadata,
|
|
543
|
+
},
|
|
544
|
+
},
|
|
545
|
+
credentialStatus: options?.credentialStatus,
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
/**
|
|
550
|
+
* Check if a delegation credential is expired
|
|
551
|
+
*
|
|
552
|
+
* @param vc - The delegation credential
|
|
553
|
+
* @returns true if expired
|
|
554
|
+
*/
|
|
555
|
+
export function isDelegationCredentialExpired(vc: DelegationCredential): boolean {
|
|
556
|
+
// Check VC expiration
|
|
557
|
+
if (vc.expirationDate) {
|
|
558
|
+
const expirationDate = new Date(vc.expirationDate);
|
|
559
|
+
const now = new Date();
|
|
560
|
+
if (expirationDate < now) {
|
|
561
|
+
return true;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Check delegation constraints notAfter
|
|
566
|
+
const delegation = vc.credentialSubject.delegation;
|
|
567
|
+
if (delegation.constraints.notAfter) {
|
|
568
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
569
|
+
if (nowSec > delegation.constraints.notAfter) {
|
|
570
|
+
return true;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return false;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Check if a delegation credential is not yet valid
|
|
579
|
+
*
|
|
580
|
+
* @param vc - The delegation credential
|
|
581
|
+
* @returns true if not yet valid
|
|
582
|
+
*/
|
|
583
|
+
export function isDelegationCredentialNotYetValid(vc: DelegationCredential): boolean {
|
|
584
|
+
const delegation = vc.credentialSubject.delegation;
|
|
585
|
+
|
|
586
|
+
// Check delegation constraints notBefore
|
|
587
|
+
if (delegation.constraints.notBefore) {
|
|
588
|
+
const nowSec = Math.floor(Date.now() / 1000);
|
|
589
|
+
if (nowSec < delegation.constraints.notBefore) {
|
|
590
|
+
return true;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
return false;
|
|
595
|
+
}
|