Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Merge anoncreds proof request credentials #118

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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