Skip to content

Commit

Permalink
feat: Merge anoncreds proof request credentials
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Lanser <[email protected]>
  • Loading branch information
Tommylans committed Jul 4, 2024
1 parent 2823206 commit fe46e8f
Showing 1 changed file with 38 additions and 8 deletions.
46 changes: 38 additions & 8 deletions packages/agent/src/hooks/useDidCommPresentationActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { filter, first, timeout } from 'rxjs/operators'
import { useAgent } from '../agent'
import { getDidCommCredentialExchangeDisplayMetadata } from '../didcomm/metadata'

type ProofedCredentialEntry = FormattedSubmission['entries'][number]

export function useDidCommPresentationActions(proofExchangeId: string) {
const { agent } = useAgent()

Expand All @@ -40,10 +42,22 @@ export function useDidCommPresentationActions(proofExchangeId: string) {
throw new CredoError('Invalid proof request.')
}

const submission: FormattedSubmission = {
areAllSatisfied: false,
entries: [],
name: proofRequest?.name ?? 'Unknown',
const entries = new Map<string, ProofedCredentialEntry>()
const mergeOrSetEntry = (key: string, newEntry: ProofedCredentialEntry) => {
const entry = entries.get(key)
if (entry) {
entries.set(key, {
name: entry.name || newEntry.name,
backgroundColor: entry.backgroundColor || newEntry.backgroundColor,
description: entry.description || newEntry.description,
credentialName: entry.credentialName || newEntry.credentialName,
issuerName: entry.issuerName || newEntry.issuerName,
isSatisfied: entry.isSatisfied && newEntry.isSatisfied, // Check if both are true otherwise it's not satisfied
requestedAttributes: [...(entry.requestedAttributes ?? []), ...(newEntry.requestedAttributes ?? [])],
})
} else {
entries.set(key, newEntry)
}
}

await Promise.all(
Expand All @@ -54,8 +68,12 @@ export function useDidCommPresentationActions(proofExchangeId: string) {

const firstMatch = attributeArray[0]

const credentialKey = groupName.includes('__CREDENTIAL__')
? groupName.split('__CREDENTIAL__')[0]
: firstMatch?.credentialId ?? groupName

if (!firstMatch) {
submission.entries.push({
mergeOrSetEntry(credentialKey, {
credentialName: 'Credential', // TODO: we can extract this from the schema name, but we would have to fetch it
isSatisfied: false,
name: groupName, // TODO
Expand All @@ -70,7 +88,7 @@ export function useDidCommPresentationActions(proofExchangeId: string) {
? getDidCommCredentialExchangeDisplayMetadata(credentialExchange)
: undefined

submission.entries.push({
mergeOrSetEntry(credentialKey, {
name: groupName, // TODO: humanize string? Or should we let this out?
credentialName: credentialDisplayMetadata?.credentialName ?? 'Credential',
isSatisfied: true,
Expand All @@ -94,8 +112,12 @@ export function useDidCommPresentationActions(proofExchangeId: string) {
// This should probably be fixed in AFJ.
const firstMatch = predicateArray[0]

const credentialKey = groupName.includes('__CREDENTIAL__')
? groupName.split('__CREDENTIAL__')[0]
: firstMatch?.credentialId ?? groupName

if (!firstMatch) {
submission.entries.push({
mergeOrSetEntry(credentialKey, {
credentialName: 'Credential', // TODO: we can extract this from the schema name, but we would have to fetch it
isSatisfied: false,
name: groupName, // TODO
Expand All @@ -110,7 +132,7 @@ export function useDidCommPresentationActions(proofExchangeId: string) {
? getDidCommCredentialExchangeDisplayMetadata(credentialExchange)
: undefined

submission.entries.push({
mergeOrSetEntry(credentialKey, {
name: groupName, // TODO: humanize string? Or should we let this out?
credentialName: credentialDisplayMetadata?.credentialName ?? 'Credential',
isSatisfied: true,
Expand All @@ -121,6 +143,14 @@ export function useDidCommPresentationActions(proofExchangeId: string) {
})
)

const entriesArray = Array.from(entries.values())

const submission: FormattedSubmission = {
areAllSatisfied: entriesArray.every((entry) => entry.isSatisfied),
entries: entriesArray,
name: proofRequest?.name ?? 'Unknown',
}

submission.areAllSatisfied = submission.entries.every((entry) => entry.isSatisfied)

return submission
Expand Down

0 comments on commit fe46e8f

Please sign in to comment.