-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathCheqdAnonCredsRegistry.ts
499 lines (447 loc) · 17.7 KB
/
CheqdAnonCredsRegistry.ts
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
import type { CheqdCreateResourceOptions } from '../../dids'
import type {
AnonCredsRegistry,
GetCredentialDefinitionReturn,
GetRevocationStatusListReturn,
GetRevocationRegistryDefinitionReturn,
GetSchemaReturn,
RegisterCredentialDefinitionOptions,
RegisterCredentialDefinitionReturn,
RegisterSchemaReturn,
RegisterSchemaOptions,
RegisterRevocationRegistryDefinitionReturn,
RegisterRevocationStatusListReturn,
RegisterRevocationRegistryDefinitionOptions,
RegisterRevocationStatusListOptions,
} from '@credo-ts/anoncreds'
import type { AgentContext } from '@credo-ts/core'
import { CredoError, Hasher, JsonTransformer, TypedArrayEncoder, utils } from '@credo-ts/core'
import { CheqdDidResolver, CheqdDidRegistrar } from '../../dids'
import { cheqdSdkAnonCredsRegistryIdentifierRegex, parseCheqdDid } from '../utils/identifiers'
import {
CheqdCredentialDefinition,
CheqdRevocationRegistryDefinition,
CheqdRevocationStatusList,
CheqdSchema,
} from '../utils/transform'
export class CheqdAnonCredsRegistry implements AnonCredsRegistry {
public methodName = 'cheqd'
/**
* This class supports resolving and registering objects with cheqd identifiers.
* It needs to include support for the schema, credential definition, revocation registry as well
* as the issuer id (which is needed when registering objects).
*/
public readonly supportedIdentifier = cheqdSdkAnonCredsRegistryIdentifierRegex
public async getSchema(agentContext: AgentContext, schemaId: string): Promise<GetSchemaReturn> {
try {
const cheqdDidResolver = agentContext.dependencyManager.resolve(CheqdDidResolver)
const parsedDid = parseCheqdDid(schemaId)
if (!parsedDid) throw new CredoError(`Invalid schemaId: ${schemaId}`)
agentContext.config.logger.trace(`Submitting get schema request for schema '${schemaId}' to ledger`)
const response = await cheqdDidResolver.resolveResource(agentContext, schemaId)
const schema = JsonTransformer.fromJSON(response.resource, CheqdSchema)
return {
schema: {
attrNames: schema.attrNames,
name: schema.name,
version: schema.version,
issuerId: parsedDid.did,
},
schemaId,
resolutionMetadata: {},
schemaMetadata: {},
}
} catch (error) {
agentContext.config.logger.error(`Error retrieving schema '${schemaId}'`, {
error,
schemaId,
})
return {
schemaId,
resolutionMetadata: {
error: 'notFound',
message: `unable to resolve schema: ${error.message}`,
},
schemaMetadata: {},
}
}
}
public async registerSchema(
agentContext: AgentContext,
options: RegisterSchemaOptions
): Promise<RegisterSchemaReturn> {
try {
const cheqdDidRegistrar = agentContext.dependencyManager.resolve(CheqdDidRegistrar)
const schema = options.schema
const schemaResource = {
id: utils.uuid(),
name: `${schema.name}-Schema`,
resourceType: 'anonCredsSchema',
data: {
name: schema.name,
version: schema.version,
attrNames: schema.attrNames,
},
version: schema.version,
} satisfies CheqdCreateResourceOptions
const response = await cheqdDidRegistrar.createResource(agentContext, schema.issuerId, schemaResource)
if (response.resourceState.state !== 'finished') {
throw new CredoError(response.resourceState.reason ?? 'Unknown error')
}
return {
schemaState: {
state: 'finished',
schema,
schemaId: `${schema.issuerId}/resources/${schemaResource.id}`,
},
registrationMetadata: {},
schemaMetadata: {},
}
} catch (error) {
agentContext.config.logger.debug(`Error registering schema for did '${options.schema.issuerId}'`, {
error,
did: options.schema.issuerId,
schema: options,
})
return {
schemaMetadata: {},
registrationMetadata: {},
schemaState: {
state: 'failed',
schema: options.schema,
reason: `unknownError: ${error.message}`,
},
}
}
}
public async registerCredentialDefinition(
agentContext: AgentContext,
options: RegisterCredentialDefinitionOptions
): Promise<RegisterCredentialDefinitionReturn> {
try {
const cheqdDidRegistrar = agentContext.dependencyManager.resolve(CheqdDidRegistrar)
const { credentialDefinition } = options
const schema = await this.getSchema(agentContext, credentialDefinition.schemaId)
if (!schema.schema) throw new CredoError(`Schema not found for schemaId: ${credentialDefinition.schemaId}`)
const credDefName = `${schema.schema.name}-${credentialDefinition.tag}`
const credDefNameHashBuffer = Hasher.hash(credDefName, 'sha-256')
const credDefResource = {
id: utils.uuid(),
name: TypedArrayEncoder.toHex(credDefNameHashBuffer),
resourceType: 'anonCredsCredDef',
data: {
type: credentialDefinition.type,
tag: credentialDefinition.tag,
value: credentialDefinition.value,
schemaId: credentialDefinition.schemaId,
},
version: utils.uuid(),
} satisfies CheqdCreateResourceOptions
const response = await cheqdDidRegistrar.createResource(
agentContext,
credentialDefinition.issuerId,
credDefResource
)
if (response.resourceState.state !== 'finished')
throw new CredoError(response.resourceState.reason ?? 'Unknown error')
return {
credentialDefinitionState: {
state: 'finished',
credentialDefinition,
credentialDefinitionId: `${credentialDefinition.issuerId}/resources/${credDefResource.id}`,
},
registrationMetadata: {},
credentialDefinitionMetadata: {},
}
} catch (error) {
agentContext.config.logger.error(
`Error registering credential definition for did '${options.credentialDefinition.issuerId}'`,
{
error,
did: options.credentialDefinition.issuerId,
schema: options,
}
)
return {
credentialDefinitionMetadata: {},
registrationMetadata: {},
credentialDefinitionState: {
state: 'failed',
credentialDefinition: options.credentialDefinition,
reason: `unknownError: ${error.message}`,
},
}
}
}
public async getCredentialDefinition(
agentContext: AgentContext,
credentialDefinitionId: string
): Promise<GetCredentialDefinitionReturn> {
try {
const cheqdDidResolver = agentContext.dependencyManager.resolve(CheqdDidResolver)
const parsedDid = parseCheqdDid(credentialDefinitionId)
if (!parsedDid) throw new CredoError(`Invalid credentialDefinitionId: ${credentialDefinitionId}`)
agentContext.config.logger.trace(
`Submitting get credential definition request for '${credentialDefinitionId}' to ledger`
)
const response = await cheqdDidResolver.resolveResource(agentContext, credentialDefinitionId)
const credentialDefinition = JsonTransformer.fromJSON(response.resource, CheqdCredentialDefinition)
return {
credentialDefinition: {
...credentialDefinition,
issuerId: parsedDid.did,
},
credentialDefinitionId,
resolutionMetadata: {},
credentialDefinitionMetadata: (response.resourceMetadata ?? {}) as Record<string, unknown>,
}
} catch (error) {
agentContext.config.logger.error(`Error retrieving credential definition '${credentialDefinitionId}'`, {
error,
credentialDefinitionId,
})
return {
credentialDefinitionId,
resolutionMetadata: {
error: 'notFound',
message: `unable to resolve credential definition: ${error.message}`,
},
credentialDefinitionMetadata: {},
}
}
}
public async getRevocationRegistryDefinition(
agentContext: AgentContext,
revocationRegistryDefinitionId: string
): Promise<GetRevocationRegistryDefinitionReturn> {
try {
const cheqdDidResolver = agentContext.dependencyManager.resolve(CheqdDidResolver)
const parsedDid = parseCheqdDid(revocationRegistryDefinitionId)
if (!parsedDid) throw new CredoError(`Invalid revocationRegistryDefinitionId: ${revocationRegistryDefinitionId}`)
agentContext.config.logger.trace(
`Submitting get revocation registry definition request for '${revocationRegistryDefinitionId}' to ledger`
)
const searchDid = parsedDid.path
? revocationRegistryDefinitionId
: `${revocationRegistryDefinitionId}${
revocationRegistryDefinitionId.includes('?') ? '&' : '?'
}resourceType=anonCredsRevocRegDef`
const response = await cheqdDidResolver.resolveResource(agentContext, searchDid)
const revocationRegistryDefinition = JsonTransformer.fromJSON(
response.resource,
CheqdRevocationRegistryDefinition
)
return {
revocationRegistryDefinition: {
...revocationRegistryDefinition,
issuerId: parsedDid.did,
},
revocationRegistryDefinitionId,
resolutionMetadata: {},
revocationRegistryDefinitionMetadata: (response.resourceMetadata ?? {}) as Record<string, unknown>,
}
} catch (error) {
agentContext.config.logger.error(
`Error retrieving revocation registry definition '${revocationRegistryDefinitionId}'`,
{
error,
revocationRegistryDefinitionId,
}
)
return {
revocationRegistryDefinitionId,
resolutionMetadata: {
error: 'notFound',
message: `unable to resolve revocation registry definition: ${error.message}`,
},
revocationRegistryDefinitionMetadata: {},
}
}
}
public async registerRevocationRegistryDefinition(
agentContext: AgentContext,
{ revocationRegistryDefinition, options }: RegisterRevocationRegistryDefinitionOptions
): Promise<RegisterRevocationRegistryDefinitionReturn> {
try {
const credentialDefinition = await this.getCredentialDefinition(
agentContext,
revocationRegistryDefinition.credDefId
)
if (!credentialDefinition.credentialDefinition)
throw new CredoError(`Credential definition not found for id: ${revocationRegistryDefinition.credDefId}`)
const credentialDefinitionName = credentialDefinition.credentialDefinitionMetadata.name
if (!credentialDefinitionName)
throw new CredoError(`Credential definition name not found for id: ${revocationRegistryDefinition.credDefId}`)
const cheqdDidRegistrar = agentContext.dependencyManager.resolve(CheqdDidRegistrar)
const revocationRegistryDefinitionResource = {
id: utils.uuid(),
name: credentialDefinitionName as string,
resourceType: 'anonCredsRevocRegDef',
data: {
credDefId: revocationRegistryDefinition.credDefId,
revocDefType: revocationRegistryDefinition.revocDefType,
tag: revocationRegistryDefinition.tag,
value: revocationRegistryDefinition.value,
},
version: utils.uuid(),
} satisfies CheqdCreateResourceOptions
const response = await cheqdDidRegistrar.createResource(
agentContext,
revocationRegistryDefinition.issuerId,
revocationRegistryDefinitionResource
)
if (response.resourceState.state !== 'finished')
throw new CredoError(response.resourceState.reason ?? 'Unknown error')
return {
revocationRegistryDefinitionState: {
state: 'finished',
revocationRegistryDefinition,
revocationRegistryDefinitionId: `${revocationRegistryDefinition.issuerId}/resources/${revocationRegistryDefinitionResource.id}`,
},
registrationMetadata: {},
revocationRegistryDefinitionMetadata: (response.resourceMetadata ?? {}) as Record<string, unknown>,
}
} catch (error) {
agentContext.config.logger.error(
`Error registering revocation registry definition for did '${revocationRegistryDefinition.issuerId}'`,
{
error,
did: revocationRegistryDefinition.issuerId,
options,
}
)
return {
revocationRegistryDefinitionMetadata: {},
registrationMetadata: {},
revocationRegistryDefinitionState: {
state: 'failed',
revocationRegistryDefinition,
reason: `unknownError: ${error.message}`,
},
}
}
}
public async getRevocationStatusList(
agentContext: AgentContext,
revocationRegistryId: string,
timestamp: number
): Promise<GetRevocationStatusListReturn> {
try {
const cheqdDidResolver = agentContext.dependencyManager.resolve(CheqdDidResolver)
const parsedDid = parseCheqdDid(revocationRegistryId)
if (!parsedDid) throw new CredoError(`Invalid revocationRegistryId: ${revocationRegistryId}`)
agentContext.config.logger.trace(
`Submitting get revocation status request for '${revocationRegistryId}' to ledger`
)
const revocationRegistryDefinition = await this.getRevocationRegistryDefinition(
agentContext,
revocationRegistryId
)
if (!revocationRegistryDefinition.revocationRegistryDefinition)
throw new CredoError(`Revocation registry definition not found for id: ${revocationRegistryId}`)
const revocationRegistryDefinitionName = revocationRegistryDefinition.revocationRegistryDefinitionMetadata.name
if (!revocationRegistryDefinitionName)
throw new CredoError(`Revocation registry definition name not found for id: ${revocationRegistryId}`)
const response = await cheqdDidResolver.resolveResource(
agentContext,
`${parsedDid.did}?resourceType=anonCredsStatusList&resourceVersionTime=${timestamp}&resourceName=${revocationRegistryDefinitionName}`
)
const revocationStatusList = JsonTransformer.fromJSON(response.resource, CheqdRevocationStatusList)
const statusListTimestamp = response.resourceMetadata?.created
? Math.floor(response.resourceMetadata.created.getTime() / 1000)
: undefined
if (statusListTimestamp === undefined)
throw new CredoError(`Unable to extract revocation status list timestamp from resource ${revocationRegistryId}`)
return {
revocationStatusList: {
...revocationStatusList,
issuerId: parsedDid.did,
timestamp: statusListTimestamp,
},
resolutionMetadata: {},
revocationStatusListMetadata: (response.resourceMetadata ?? {}) as Record<string, unknown>,
}
} catch (error) {
agentContext.config.logger.error(`Error retrieving revocation registry status list '${revocationRegistryId}'`, {
error,
revocationRegistryId,
})
return {
resolutionMetadata: {
error: 'notFound',
message: `unable to resolve revocation registry status list: ${error.message}`,
},
revocationStatusListMetadata: {},
}
}
}
public async registerRevocationStatusList(
agentContext: AgentContext,
{ revocationStatusList, options }: RegisterRevocationStatusListOptions
): Promise<RegisterRevocationStatusListReturn> {
try {
const revocationRegistryDefinition = await this.getRevocationRegistryDefinition(
agentContext,
revocationStatusList.revRegDefId
)
if (!revocationRegistryDefinition.revocationRegistryDefinition) {
throw new CredoError(`Revocation registry definition not found for id: ${revocationStatusList.revRegDefId}`)
}
const revocationRegistryDefinitionName = revocationRegistryDefinition.revocationRegistryDefinitionMetadata.name
if (!revocationRegistryDefinitionName)
throw new CredoError(
`Revocation registry definition name not found for id: ${revocationStatusList.revRegDefId}`
)
const cheqdDidRegistrar = agentContext.dependencyManager.resolve(CheqdDidRegistrar)
const revocationStatusListResource = {
id: utils.uuid(),
name: revocationRegistryDefinitionName as string,
resourceType: 'anonCredsStatusList',
data: {
currentAccumulator: revocationStatusList.currentAccumulator,
revRegDefId: revocationStatusList.revRegDefId,
revocationList: revocationStatusList.revocationList,
},
version: utils.uuid(),
} satisfies CheqdCreateResourceOptions
const response = await cheqdDidRegistrar.createResource(
agentContext,
revocationStatusList.issuerId,
revocationStatusListResource
)
if (response.resourceState.state !== 'finished')
throw new CredoError(response.resourceState.reason ?? 'Unknown error')
// It's not possible to get the timestamp from the response, so we set it to the current time
const nowTimestamp = Math.floor(Date.now() / 1000)
return {
revocationStatusListState: {
state: 'finished',
revocationStatusList: {
...revocationStatusList,
timestamp: nowTimestamp,
},
},
registrationMetadata: {},
revocationStatusListMetadata: (response.resourceMetadata ?? {}) as Record<string, unknown>,
}
} catch (error) {
agentContext.config.logger.error(
`Error registering revocation status list for did '${revocationStatusList.issuerId}'`,
{
error,
did: revocationStatusList.issuerId,
options,
}
)
return {
revocationStatusListMetadata: {},
registrationMetadata: {},
revocationStatusListState: {
state: 'failed',
revocationStatusList,
reason: `unknownError: ${error.message}`,
},
}
}
}
}