1
+ import type { ProofAttributeInfo , ProofRequest , RequestedCredentials } from '../..'
2
+ import type { CredentialRepository } from '../../../credentials'
1
3
import type { CreateProposalOptions } from '../../models/ServiceOptions'
2
4
import type {
3
5
CreatePresentationOptions ,
@@ -13,6 +15,13 @@ import { ProofFormatService } from '../ProofFormatService'
13
15
import { ATTACHMENT_FORMAT } from '../ProofFormats'
14
16
15
17
export class IndyProofFormatService extends ProofFormatService {
18
+ private credentialRepository : CredentialRepository
19
+
20
+ public constructor ( credentialRepository : CredentialRepository ) {
21
+ super ( )
22
+ this . credentialRepository = credentialRepository
23
+ }
24
+
16
25
public createProposal ( options : CreateProposalOptions ) : ProofAttachmentFormat {
17
26
// Handle format in service
18
27
throw new Error ( 'Method not implemented.' )
@@ -65,4 +74,56 @@ export class IndyProofFormatService extends ProofFormatService {
65
74
public getFormatIdentifier ( messageType : string ) : ProofFormatSpec {
66
75
return ATTACHMENT_FORMAT [ messageType ] . indy
67
76
}
77
+
78
+ public async getRequestedAttachmentsForRequestedCredentials (
79
+ indyProofRequest : ProofRequest ,
80
+ requestedCredentials : RequestedCredentials
81
+ ) : Promise < Attachment [ ] | undefined > {
82
+ const attachments : Attachment [ ] = [ ]
83
+ const credentialIds = new Set < string > ( )
84
+ const requestedAttributesNames : ( string | undefined ) [ ] = [ ]
85
+
86
+ // Get the credentialIds if it contains a hashlink
87
+ for ( const [ referent , requestedAttribute ] of Object . entries ( requestedCredentials . requestedAttributes ) ) {
88
+ // Find the requested Attributes
89
+ const requestedAttributes = indyProofRequest . requestedAttributes . get ( referent ) as ProofAttributeInfo
90
+
91
+ // List the requested attributes
92
+ requestedAttributesNames . push ( ...( requestedAttributes . names ?? [ requestedAttributes . name ] ) )
93
+
94
+ // Find the attributes that have a hashlink as a value
95
+ for ( const attribute of Object . values ( requestedAttribute . credentialInfo . attributes ) ) {
96
+ if ( attribute . toLowerCase ( ) . startsWith ( 'hl:' ) ) {
97
+ credentialIds . add ( requestedAttribute . credentialId )
98
+ }
99
+ }
100
+ }
101
+
102
+ // Only continues if there is an attribute value that contains a hashlink
103
+ for ( const credentialId of credentialIds ) {
104
+ // Get the credentialRecord that matches the ID
105
+
106
+ const credentialRecord = await this . credentialRepository . getSingleByQuery ( { credentialId } )
107
+
108
+ if ( credentialRecord . linkedAttachments ) {
109
+ // Get the credentials that have a hashlink as value and are requested
110
+ const requestedCredentials = credentialRecord . credentialAttributes ?. filter (
111
+ ( credential ) =>
112
+ credential . value . toLowerCase ( ) . startsWith ( 'hl:' ) && requestedAttributesNames . includes ( credential . name )
113
+ )
114
+
115
+ // Get the linked attachments that match the requestedCredentials
116
+ const linkedAttachments = credentialRecord . linkedAttachments . filter ( ( attachment ) =>
117
+ requestedCredentials ?. map ( ( credential ) => credential . value . split ( ':' ) [ 1 ] ) . includes ( attachment . id )
118
+ )
119
+
120
+ if ( linkedAttachments ) {
121
+ attachments . push ( ...linkedAttachments )
122
+ }
123
+ }
124
+ }
125
+
126
+ return attachments . length ? attachments : undefined
127
+ }
128
+
68
129
}
0 commit comments