Skip to content

Commit 1f8b369

Browse files
refac: IndyProofFormatService, V1ProofService
Signed-off-by: NB Prasad Katkar <[email protected]>
1 parent 4de4303 commit 1f8b369

File tree

3 files changed

+64
-55
lines changed

3 files changed

+64
-55
lines changed

packages/core/src/modules/proofs/formats/ProofFormatService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ interface ProcessPresentationOptions {
6464
options: never // TBD
6565
}
6666

67-
6867
/**
6968
* This abstract class is the base class for any proof format
7069
* specific service.

packages/core/src/modules/proofs/formats/indy/IndyProofFormatService.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { ProofAttributeInfo, ProofRequest, RequestedCredentials } from '../..'
2+
import type { CredentialRepository } from '../../../credentials'
13
import type { CreateProposalOptions } from '../../models/ServiceOptions'
24
import type {
35
CreatePresentationOptions,
@@ -13,6 +15,13 @@ import { ProofFormatService } from '../ProofFormatService'
1315
import { ATTACHMENT_FORMAT } from '../ProofFormats'
1416

1517
export class IndyProofFormatService extends ProofFormatService {
18+
private credentialRepository: CredentialRepository
19+
20+
public constructor(credentialRepository: CredentialRepository) {
21+
super()
22+
this.credentialRepository = credentialRepository
23+
}
24+
1625
public createProposal(options: CreateProposalOptions): ProofAttachmentFormat {
1726
// Handle format in service
1827
throw new Error('Method not implemented.')
@@ -65,4 +74,56 @@ export class IndyProofFormatService extends ProofFormatService {
6574
public getFormatIdentifier(messageType: string): ProofFormatSpec {
6675
return ATTACHMENT_FORMAT[messageType].indy
6776
}
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+
68129
}

packages/core/src/modules/proofs/protocol/v1/V1ProofService.ts

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ export class V1ProofService extends ProofService {
245245
}
246246

247247
// Get the matching attachments to the requested credentials
248-
const attachments = await this.getRequestedAttachmentsForRequestedCredentials(
248+
const attachments = await this.indyProofFormatService.getRequestedAttachmentsForRequestedCredentials(
249249
indyProofRequest,
250250
requestedCredentials
251251
)
252252

253253
// Create proof
254254
const proof = await this.createProof(indyProofRequest, requestedCredentials)
255255

256-
// Create message
256+
// Create payload
257257
const createPresentationOptions: CreatePresentationOptions = {
258258
attachId: INDY_PROOF_ATTACHMENT_ID,
259259
messageType: 'V1_PROOF',
@@ -262,7 +262,7 @@ export class V1ProofService extends ProofService {
262262
}),
263263
}
264264

265-
// Create Indy attachment
265+
// Create indy attachment
266266
const { attachment } = await this.indyProofFormatService.createPresentation(createPresentationOptions)
267267

268268
const presentationMessage = new PresentationMessage({
@@ -307,57 +307,6 @@ export class V1ProofService extends ProofService {
307307
throw new Error('Method not implemented.')
308308
}
309309

310-
public async getRequestedAttachmentsForRequestedCredentials(
311-
indyProofRequest: ProofRequest,
312-
requestedCredentials: RequestedCredentials
313-
): Promise<Attachment[] | undefined> {
314-
const attachments: Attachment[] = []
315-
const credentialIds = new Set<string>()
316-
const requestedAttributesNames: (string | undefined)[] = []
317-
318-
// Get the credentialIds if it contains a hashlink
319-
for (const [referent, requestedAttribute] of Object.entries(requestedCredentials.requestedAttributes)) {
320-
// Find the requested Attributes
321-
const requestedAttributes = indyProofRequest.requestedAttributes.get(referent) as ProofAttributeInfo
322-
323-
// List the requested attributes
324-
requestedAttributesNames.push(...(requestedAttributes.names ?? [requestedAttributes.name]))
325-
326-
// Find the attributes that have a hashlink as a value
327-
for (const attribute of Object.values(requestedAttribute.credentialInfo.attributes)) {
328-
if (attribute.toLowerCase().startsWith('hl:')) {
329-
credentialIds.add(requestedAttribute.credentialId)
330-
}
331-
}
332-
}
333-
334-
// Only continues if there is an attribute value that contains a hashlink
335-
for (const credentialId of credentialIds) {
336-
// Get the credentialRecord that matches the ID
337-
338-
const credentialRecord = await this.credentialRepository.getSingleByQuery({ credentialId })
339-
340-
if (credentialRecord.linkedAttachments) {
341-
// Get the credentials that have a hashlink as value and are requested
342-
const requestedCredentials = credentialRecord.credentialAttributes?.filter(
343-
(credential) =>
344-
credential.value.toLowerCase().startsWith('hl:') && requestedAttributesNames.includes(credential.name)
345-
)
346-
347-
// Get the linked attachments that match the requestedCredentials
348-
const linkedAttachments = credentialRecord.linkedAttachments.filter((attachment) =>
349-
requestedCredentials?.map((credential) => credential.value.split(':')[1]).includes(attachment.id)
350-
)
351-
352-
if (linkedAttachments) {
353-
attachments.push(...linkedAttachments)
354-
}
355-
}
356-
}
357-
358-
return attachments.length ? attachments : undefined
359-
}
360-
361310
/**
362311
* Create indy proof from a given proof request and requested credential object.
363312
*

0 commit comments

Comments
 (0)