-
Notifications
You must be signed in to change notification settings - Fork 200
/
AnonCredsCredentialFormatService.ts
678 lines (577 loc) · 26.4 KB
/
AnonCredsCredentialFormatService.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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import type { AnonCredsCredentialFormat, AnonCredsCredentialProposalFormat } from './AnonCredsCredentialFormat'
import type { AnonCredsCredential, AnonCredsCredentialOffer, AnonCredsCredentialRequest } from '../models'
import type { AnonCredsIssuerService, AnonCredsHolderService } from '../services'
import type { AnonCredsCredentialMetadata, AnonCredsCredentialRequestMetadata } from '../utils/metadata'
import type {
CredentialFormatService,
AgentContext,
CredentialFormatCreateProposalOptions,
CredentialFormatCreateProposalReturn,
CredentialFormatProcessOptions,
CredentialFormatAcceptProposalOptions,
CredentialFormatCreateOfferReturn,
CredentialFormatCreateOfferOptions,
CredentialFormatAcceptOfferOptions,
CredentialFormatCreateReturn,
CredentialFormatAcceptRequestOptions,
CredentialFormatProcessCredentialOptions,
CredentialFormatAutoRespondProposalOptions,
CredentialFormatAutoRespondOfferOptions,
CredentialFormatAutoRespondRequestOptions,
CredentialFormatAutoRespondCredentialOptions,
CredentialExchangeRecord,
CredentialPreviewAttributeOptions,
LinkedAttachment,
} from '@credo-ts/core'
import {
ProblemReportError,
MessageValidator,
CredentialFormatSpec,
CredoError,
Attachment,
JsonEncoder,
utils,
CredentialProblemReportReason,
JsonTransformer,
} from '@credo-ts/core'
import { AnonCredsCredentialProposal } from '../models/AnonCredsCredentialProposal'
import {
AnonCredsCredentialDefinitionRepository,
AnonCredsRevocationRegistryDefinitionPrivateRepository,
AnonCredsRevocationRegistryState,
} from '../repository'
import { AnonCredsIssuerServiceSymbol, AnonCredsHolderServiceSymbol } from '../services'
import {
dateToTimestamp,
fetchCredentialDefinition,
fetchRevocationRegistryDefinition,
fetchRevocationStatusList,
fetchSchema,
} from '../utils'
import {
convertAttributesToCredentialValues,
assertCredentialValuesMatch,
checkCredentialValuesMatch,
assertAttributesMatch,
createAndLinkAttachmentsToPreview,
} from '../utils/credential'
import { AnonCredsCredentialMetadataKey, AnonCredsCredentialRequestMetadataKey } from '../utils/metadata'
import { getStoreCredentialOptions } from '../utils/w3cAnonCredsUtils'
const ANONCREDS_CREDENTIAL_OFFER = 'anoncreds/credential-offer@v1.0'
const ANONCREDS_CREDENTIAL_REQUEST = 'anoncreds/credential-request@v1.0'
const ANONCREDS_CREDENTIAL_FILTER = 'anoncreds/credential-filter@v1.0'
const ANONCREDS_CREDENTIAL = 'anoncreds/credential@v1.0'
export class AnonCredsCredentialFormatService implements CredentialFormatService<AnonCredsCredentialFormat> {
/** formatKey is the key used when calling agent.credentials.xxx with credentialFormats.anoncreds */
public readonly formatKey = 'anoncreds' as const
/**
* credentialRecordType is the type of record that stores the credential. It is stored in the credential
* record binding in the credential exchange record.
*/
public readonly credentialRecordType = 'w3c' as const
/**
* Create a {@link AttachmentFormats} object dependent on the message type.
*
* @param options The object containing all the options for the proposed credential
* @returns object containing associated attachment, format and optionally the credential preview
*
*/
public async createProposal(
agentContext: AgentContext,
{ credentialFormats, credentialRecord }: CredentialFormatCreateProposalOptions<AnonCredsCredentialFormat>
): Promise<CredentialFormatCreateProposalReturn> {
const format = new CredentialFormatSpec({
format: ANONCREDS_CREDENTIAL_FILTER,
})
const anoncredsFormat = credentialFormats.anoncreds
if (!anoncredsFormat) {
throw new CredoError('Missing anoncreds payload in createProposal')
}
// We want all properties except for `attributes` and `linkedAttachments` attributes.
// The easiest way is to destructure and use the spread operator. But that leaves the other properties unused
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { attributes, linkedAttachments, ...anoncredsCredentialProposal } = anoncredsFormat
const proposal = new AnonCredsCredentialProposal(anoncredsCredentialProposal)
try {
MessageValidator.validateSync(proposal)
} catch (error) {
throw new CredoError(`Invalid proposal supplied: ${anoncredsCredentialProposal} in AnonCredsFormatService`)
}
const attachment = this.getFormatData(JsonTransformer.toJSON(proposal), format.attachmentId)
const { previewAttributes } = this.getCredentialLinkedAttachments(
anoncredsFormat.attributes,
anoncredsFormat.linkedAttachments
)
// Set the metadata
credentialRecord.metadata.set<AnonCredsCredentialMetadata>(AnonCredsCredentialMetadataKey, {
schemaId: proposal.schemaId,
credentialDefinitionId: proposal.credentialDefinitionId,
})
return { format, attachment, previewAttributes }
}
public async processProposal(
agentContext: AgentContext,
{ attachment }: CredentialFormatProcessOptions
): Promise<void> {
const proposalJson = attachment.getDataAsJson()
JsonTransformer.fromJSON(proposalJson, AnonCredsCredentialProposal)
}
public async acceptProposal(
agentContext: AgentContext,
{
attachmentId,
credentialFormats,
credentialRecord,
proposalAttachment,
}: CredentialFormatAcceptProposalOptions<AnonCredsCredentialFormat>
): Promise<CredentialFormatCreateOfferReturn> {
const anoncredsFormat = credentialFormats?.anoncreds
const proposalJson = proposalAttachment.getDataAsJson<AnonCredsCredentialProposalFormat>()
const credentialDefinitionId = anoncredsFormat?.credentialDefinitionId ?? proposalJson.cred_def_id
const attributes = anoncredsFormat?.attributes ?? credentialRecord.credentialAttributes
if (!credentialDefinitionId) {
throw new CredoError('No credential definition id in proposal or provided as input to accept proposal method.')
}
if (!attributes) {
throw new CredoError('No attributes in proposal or provided as input to accept proposal method.')
}
const { format, attachment, previewAttributes } = await this.createAnonCredsOffer(agentContext, {
credentialRecord,
attachmentId,
attributes,
credentialDefinitionId,
revocationRegistryDefinitionId: anoncredsFormat?.revocationRegistryDefinitionId,
revocationRegistryIndex: anoncredsFormat?.revocationRegistryIndex,
linkedAttachments: anoncredsFormat?.linkedAttachments,
})
return { format, attachment, previewAttributes }
}
/**
* Create a credential attachment format for a credential request.
*
* @param options The object containing all the options for the credential offer
* @returns object containing associated attachment, formats and offersAttach elements
*
*/
public async createOffer(
agentContext: AgentContext,
{ credentialFormats, credentialRecord, attachmentId }: CredentialFormatCreateOfferOptions<AnonCredsCredentialFormat>
): Promise<CredentialFormatCreateOfferReturn> {
const anoncredsFormat = credentialFormats.anoncreds
if (!anoncredsFormat) {
throw new CredoError('Missing anoncreds credential format data')
}
const { format, attachment, previewAttributes } = await this.createAnonCredsOffer(agentContext, {
credentialRecord,
attachmentId,
attributes: anoncredsFormat.attributes,
credentialDefinitionId: anoncredsFormat.credentialDefinitionId,
revocationRegistryDefinitionId: anoncredsFormat.revocationRegistryDefinitionId,
revocationRegistryIndex: anoncredsFormat.revocationRegistryIndex,
linkedAttachments: anoncredsFormat.linkedAttachments,
})
return { format, attachment, previewAttributes }
}
public async processOffer(
agentContext: AgentContext,
{ attachment, credentialRecord }: CredentialFormatProcessOptions
) {
agentContext.config.logger.debug(
`Processing anoncreds credential offer for credential record ${credentialRecord.id}`
)
const credOffer = attachment.getDataAsJson<AnonCredsCredentialOffer>()
if (!credOffer.schema_id || !credOffer.cred_def_id) {
throw new ProblemReportError('Invalid credential offer', {
problemCode: CredentialProblemReportReason.IssuanceAbandoned,
})
}
}
public async acceptOffer(
agentContext: AgentContext,
{
credentialRecord,
attachmentId,
offerAttachment,
credentialFormats,
}: CredentialFormatAcceptOfferOptions<AnonCredsCredentialFormat>
): Promise<CredentialFormatCreateReturn> {
const holderService = agentContext.dependencyManager.resolve<AnonCredsHolderService>(AnonCredsHolderServiceSymbol)
const credentialOffer = offerAttachment.getDataAsJson<AnonCredsCredentialOffer>()
// Get credential definition
const { credentialDefinition } = await fetchCredentialDefinition(agentContext, credentialOffer.cred_def_id)
const { credentialRequest, credentialRequestMetadata } = await holderService.createCredentialRequest(agentContext, {
credentialOffer,
credentialDefinition,
linkSecretId: credentialFormats?.anoncreds?.linkSecretId,
})
credentialRecord.metadata.set<AnonCredsCredentialRequestMetadata>(
AnonCredsCredentialRequestMetadataKey,
credentialRequestMetadata
)
credentialRecord.metadata.set<AnonCredsCredentialMetadata>(AnonCredsCredentialMetadataKey, {
credentialDefinitionId: credentialOffer.cred_def_id,
schemaId: credentialOffer.schema_id,
})
const format = new CredentialFormatSpec({
attachmentId,
format: ANONCREDS_CREDENTIAL_REQUEST,
})
const attachment = this.getFormatData(credentialRequest, format.attachmentId)
return { format, attachment }
}
/**
* Starting from a request is not supported for anoncreds credentials, this method only throws an error.
*/
public async createRequest(): Promise<CredentialFormatCreateReturn> {
throw new CredoError('Starting from a request is not supported for anoncreds credentials')
}
/**
* We don't have any models to validate an anoncreds request object, for now this method does nothing
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public async processRequest(agentContext: AgentContext, options: CredentialFormatProcessOptions): Promise<void> {
// not needed for anoncreds
}
public async acceptRequest(
agentContext: AgentContext,
{
credentialRecord,
attachmentId,
offerAttachment,
requestAttachment,
}: CredentialFormatAcceptRequestOptions<AnonCredsCredentialFormat>
): Promise<CredentialFormatCreateReturn> {
// Assert credential attributes
const credentialAttributes = credentialRecord.credentialAttributes
if (!credentialAttributes) {
throw new CredoError(
`Missing required credential attribute values on credential record with id ${credentialRecord.id}`
)
}
const anonCredsIssuerService =
agentContext.dependencyManager.resolve<AnonCredsIssuerService>(AnonCredsIssuerServiceSymbol)
const credentialOffer = offerAttachment?.getDataAsJson<AnonCredsCredentialOffer>()
if (!credentialOffer) throw new CredoError('Missing anoncreds credential offer in createCredential')
const credentialRequest = requestAttachment.getDataAsJson<AnonCredsCredentialRequest>()
if (!credentialRequest) throw new CredoError('Missing anoncreds credential request in createCredential')
// We check locally for credential definition info. If it supports revocation, we need to search locally for
// an active revocation registry
const credentialDefinition = (
await agentContext.dependencyManager
.resolve(AnonCredsCredentialDefinitionRepository)
.getByCredentialDefinitionId(agentContext, credentialRequest.cred_def_id)
).credentialDefinition.value
let revocationRegistryDefinitionId
let revocationRegistryIndex
let revocationStatusList
if (credentialDefinition.revocation) {
const credentialMetadata =
credentialRecord.metadata.get<AnonCredsCredentialMetadata>(AnonCredsCredentialMetadataKey)
revocationRegistryDefinitionId = credentialMetadata?.revocationRegistryId
if (credentialMetadata?.credentialRevocationId) {
revocationRegistryIndex = Number(credentialMetadata.credentialRevocationId)
}
if (!revocationRegistryDefinitionId || !revocationRegistryIndex) {
throw new CredoError(
'Revocation registry definition id and revocation index are mandatory to issue AnonCreds revocable credentials'
)
}
const revocationRegistryDefinitionPrivateRecord = await agentContext.dependencyManager
.resolve(AnonCredsRevocationRegistryDefinitionPrivateRepository)
.getByRevocationRegistryDefinitionId(agentContext, revocationRegistryDefinitionId)
if (revocationRegistryDefinitionPrivateRecord.state !== AnonCredsRevocationRegistryState.Active) {
throw new CredoError(
`Revocation registry ${revocationRegistryDefinitionId} is in ${revocationRegistryDefinitionPrivateRecord.state} state`
)
}
const revocationStatusListResult = await fetchRevocationStatusList(
agentContext,
revocationRegistryDefinitionId,
dateToTimestamp(new Date())
)
revocationStatusList = revocationStatusListResult.revocationStatusList
}
const { credential, credentialRevocationId } = await anonCredsIssuerService.createCredential(agentContext, {
credentialOffer,
credentialRequest,
credentialValues: convertAttributesToCredentialValues(credentialAttributes),
revocationRegistryDefinitionId,
revocationRegistryIndex,
revocationStatusList,
})
// If the credential is revocable, store the revocation identifiers in the credential record
if (credential.rev_reg_id) {
credentialRecord.metadata.add<AnonCredsCredentialMetadata>(AnonCredsCredentialMetadataKey, {
revocationRegistryId: revocationRegistryDefinitionId ?? undefined,
credentialRevocationId: credentialRevocationId ?? undefined,
})
credentialRecord.setTags({
anonCredsRevocationRegistryId: revocationRegistryDefinitionId,
anonCredsCredentialRevocationId: credentialRevocationId,
})
}
const format = new CredentialFormatSpec({
attachmentId,
format: ANONCREDS_CREDENTIAL,
})
const attachment = this.getFormatData(credential, format.attachmentId)
return { format, attachment }
}
/**
* Processes an incoming credential - retrieve metadata, retrieve payload and store it in wallet
* @param options the issue credential message wrapped inside this object
* @param credentialRecord the credential exchange record for this credential
*/
public async processCredential(
agentContext: AgentContext,
{ credentialRecord, attachment }: CredentialFormatProcessCredentialOptions
): Promise<void> {
const credentialRequestMetadata = credentialRecord.metadata.get<AnonCredsCredentialRequestMetadata>(
AnonCredsCredentialRequestMetadataKey
)
const anonCredsHolderService =
agentContext.dependencyManager.resolve<AnonCredsHolderService>(AnonCredsHolderServiceSymbol)
if (!credentialRequestMetadata) {
throw new CredoError(
`Missing required request metadata for credential exchange with thread id with id ${credentialRecord.id}`
)
}
if (!credentialRecord.credentialAttributes) {
throw new CredoError('Missing credential attributes on credential record. Unable to check credential attributes')
}
const anonCredsCredential = attachment.getDataAsJson<AnonCredsCredential>()
const { credentialDefinition, credentialDefinitionId } = await fetchCredentialDefinition(
agentContext,
anonCredsCredential.cred_def_id
)
const { schema, indyNamespace } = await fetchSchema(agentContext, anonCredsCredential.schema_id)
// Resolve revocation registry if credential is revocable
const revocationRegistryResult = anonCredsCredential.rev_reg_id
? await fetchRevocationRegistryDefinition(agentContext, anonCredsCredential.rev_reg_id)
: undefined
// assert the credential values match the offer values
const recordCredentialValues = convertAttributesToCredentialValues(credentialRecord.credentialAttributes)
assertCredentialValuesMatch(anonCredsCredential.values, recordCredentialValues)
const storeCredentialOptions = getStoreCredentialOptions(
{
credentialId: utils.uuid(),
credentialRequestMetadata,
credential: anonCredsCredential,
credentialDefinitionId,
credentialDefinition,
schema,
revocationRegistry: revocationRegistryResult?.revocationRegistryDefinition
? {
definition: revocationRegistryResult.revocationRegistryDefinition,
id: revocationRegistryResult.revocationRegistryDefinitionId,
}
: undefined,
},
indyNamespace
)
const credentialId = await anonCredsHolderService.storeCredential(agentContext, storeCredentialOptions)
// If the credential is revocable, store the revocation identifiers in the credential record
if (anonCredsCredential.rev_reg_id) {
const credential = await anonCredsHolderService.getCredential(agentContext, { id: credentialId })
credentialRecord.metadata.add<AnonCredsCredentialMetadata>(AnonCredsCredentialMetadataKey, {
credentialRevocationId: credential.credentialRevocationId ?? undefined,
revocationRegistryId: credential.revocationRegistryId ?? undefined,
})
credentialRecord.setTags({
anonCredsRevocationRegistryId: credential.revocationRegistryId,
anonCredsCredentialRevocationId: credential.credentialRevocationId,
})
}
credentialRecord.credentials.push({
credentialRecordType: this.credentialRecordType,
credentialRecordId: credentialId,
})
}
public supportsFormat(format: string): boolean {
const supportedFormats = [
ANONCREDS_CREDENTIAL_REQUEST,
ANONCREDS_CREDENTIAL_OFFER,
ANONCREDS_CREDENTIAL_FILTER,
ANONCREDS_CREDENTIAL,
]
return supportedFormats.includes(format)
}
/**
* Gets the attachment object for a given attachmentId. We need to get out the correct attachmentId for
* anoncreds and then find the corresponding attachment (if there is one)
* @param formats the formats object containing the attachmentId
* @param messageAttachments the attachments containing the payload
* @returns The Attachment if found or undefined
*
*/
public getAttachment(formats: CredentialFormatSpec[], messageAttachments: Attachment[]): Attachment | undefined {
const supportedAttachmentIds = formats.filter((f) => this.supportsFormat(f.format)).map((f) => f.attachmentId)
const supportedAttachment = messageAttachments.find((attachment) => supportedAttachmentIds.includes(attachment.id))
return supportedAttachment
}
public async deleteCredentialById(agentContext: AgentContext, credentialRecordId: string): Promise<void> {
const anonCredsHolderService =
agentContext.dependencyManager.resolve<AnonCredsHolderService>(AnonCredsHolderServiceSymbol)
await anonCredsHolderService.deleteCredential(agentContext, credentialRecordId)
}
public async shouldAutoRespondToProposal(
agentContext: AgentContext,
{ offerAttachment, proposalAttachment }: CredentialFormatAutoRespondProposalOptions
) {
const proposalJson = proposalAttachment.getDataAsJson<AnonCredsCredentialProposalFormat>()
const offerJson = offerAttachment.getDataAsJson<AnonCredsCredentialOffer>()
// We want to make sure the credential definition matches.
// TODO: If no credential definition is present on the proposal, we could check whether the other fields
// of the proposal match with the credential definition id.
return proposalJson.cred_def_id === offerJson.cred_def_id
}
public async shouldAutoRespondToOffer(
agentContext: AgentContext,
{ offerAttachment, proposalAttachment }: CredentialFormatAutoRespondOfferOptions
) {
const proposalJson = proposalAttachment.getDataAsJson<AnonCredsCredentialProposalFormat>()
const offerJson = offerAttachment.getDataAsJson<AnonCredsCredentialOffer>()
// We want to make sure the credential definition matches.
// TODO: If no credential definition is present on the proposal, we could check whether the other fields
// of the proposal match with the credential definition id.
return proposalJson.cred_def_id === offerJson.cred_def_id
}
public async shouldAutoRespondToRequest(
agentContext: AgentContext,
{ offerAttachment, requestAttachment }: CredentialFormatAutoRespondRequestOptions
) {
const credentialOfferJson = offerAttachment.getDataAsJson<AnonCredsCredentialOffer>()
const credentialRequestJson = requestAttachment.getDataAsJson<AnonCredsCredentialRequest>()
return credentialOfferJson.cred_def_id === credentialRequestJson.cred_def_id
}
public async shouldAutoRespondToCredential(
agentContext: AgentContext,
{ credentialRecord, requestAttachment, credentialAttachment }: CredentialFormatAutoRespondCredentialOptions
) {
const credentialJson = credentialAttachment.getDataAsJson<AnonCredsCredential>()
const credentialRequestJson = requestAttachment.getDataAsJson<AnonCredsCredentialRequest>()
// make sure the credential definition matches
if (credentialJson.cred_def_id !== credentialRequestJson.cred_def_id) return false
// If we don't have any attributes stored we can't compare so always return false.
if (!credentialRecord.credentialAttributes) return false
const attributeValues = convertAttributesToCredentialValues(credentialRecord.credentialAttributes)
// check whether the values match the values in the record
return checkCredentialValuesMatch(attributeValues, credentialJson.values)
}
private async createAnonCredsOffer(
agentContext: AgentContext,
{
credentialRecord,
attachmentId,
credentialDefinitionId,
revocationRegistryDefinitionId,
revocationRegistryIndex,
attributes,
linkedAttachments,
}: {
credentialDefinitionId: string
revocationRegistryDefinitionId?: string
revocationRegistryIndex?: number
credentialRecord: CredentialExchangeRecord
attachmentId?: string
attributes: CredentialPreviewAttributeOptions[]
linkedAttachments?: LinkedAttachment[]
}
): Promise<CredentialFormatCreateOfferReturn> {
const anonCredsIssuerService =
agentContext.dependencyManager.resolve<AnonCredsIssuerService>(AnonCredsIssuerServiceSymbol)
// if the proposal has an attachment Id use that, otherwise the generated id of the formats object
const format = new CredentialFormatSpec({
attachmentId: attachmentId,
format: ANONCREDS_CREDENTIAL,
})
const offer = await anonCredsIssuerService.createCredentialOffer(agentContext, {
credentialDefinitionId,
})
const { previewAttributes } = this.getCredentialLinkedAttachments(attributes, linkedAttachments)
if (!previewAttributes) {
throw new CredoError('Missing required preview attributes for anoncreds offer')
}
await this.assertPreviewAttributesMatchSchemaAttributes(agentContext, offer, previewAttributes)
// We check locally for credential definition info. If it supports revocation, revocationRegistryIndex
// and revocationRegistryDefinitionId are mandatory
const credentialDefinition = (
await agentContext.dependencyManager
.resolve(AnonCredsCredentialDefinitionRepository)
.getByCredentialDefinitionId(agentContext, offer.cred_def_id)
).credentialDefinition.value
if (credentialDefinition.revocation) {
if (!revocationRegistryDefinitionId || revocationRegistryIndex === undefined) {
throw new CredoError(
'AnonCreds revocable credentials require revocationRegistryDefinitionId and revocationRegistryIndex'
)
}
// Set revocation tags
credentialRecord.setTags({
anonCredsRevocationRegistryId: revocationRegistryDefinitionId,
anonCredsCredentialRevocationId: revocationRegistryIndex.toString(),
})
}
// Set the metadata
credentialRecord.metadata.set<AnonCredsCredentialMetadata>(AnonCredsCredentialMetadataKey, {
schemaId: offer.schema_id,
credentialDefinitionId: offer.cred_def_id,
credentialRevocationId: revocationRegistryIndex?.toString(),
revocationRegistryId: revocationRegistryDefinitionId,
})
const attachment = this.getFormatData(offer, format.attachmentId)
return { format, attachment, previewAttributes }
}
private async assertPreviewAttributesMatchSchemaAttributes(
agentContext: AgentContext,
offer: AnonCredsCredentialOffer,
attributes: CredentialPreviewAttributeOptions[]
): Promise<void> {
const { schema } = await fetchSchema(agentContext, offer.schema_id)
assertAttributesMatch(schema, attributes)
}
/**
* Get linked attachments for anoncreds format from a proposal message. This allows attachments
* to be copied across to old style credential records
*
* @param options ProposeCredentialOptions object containing (optionally) the linked attachments
* @return array of linked attachments or undefined if none present
*/
private getCredentialLinkedAttachments(
attributes?: CredentialPreviewAttributeOptions[],
linkedAttachments?: LinkedAttachment[]
): {
attachments?: Attachment[]
previewAttributes?: CredentialPreviewAttributeOptions[]
} {
if (!linkedAttachments && !attributes) {
return {}
}
let previewAttributes = attributes ?? []
let attachments: Attachment[] | undefined
if (linkedAttachments) {
// there are linked attachments so transform into the attribute field of the CredentialPreview object for
// this proposal
previewAttributes = createAndLinkAttachmentsToPreview(linkedAttachments, previewAttributes)
attachments = linkedAttachments.map((linkedAttachment) => linkedAttachment.attachment)
}
return { attachments, previewAttributes }
}
/**
* Returns an object of type {@link Attachment} for use in credential exchange messages.
* It looks up the correct format identifier and encodes the data as a base64 attachment.
*
* @param data The data to include in the attach object
* @param id the attach id from the formats component of the message
*/
public getFormatData(data: unknown, id: string): Attachment {
const attachment = new Attachment({
id,
mimeType: 'application/json',
data: {
base64: JsonEncoder.toBase64(data),
},
})
return attachment
}
}