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 2 commits
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
44 changes: 36 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,11 @@ export function useDidCommPresentationActions(proofExchangeId: string) {

const firstMatch = attributeArray[0]

// When the credentialId isn't available, we use the groupName as the key but it will result in multiple entries in the view. But I think it's not an easy task to merge them
const credentialKey = 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 +87,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 +111,11 @@ export function useDidCommPresentationActions(proofExchangeId: string) {
// This should probably be fixed in AFJ.
const firstMatch = predicateArray[0]

// When the credentialId isn't available, we use the groupName as the key but it will result in multiple entries in the view. But I think it's not an easy task to merge them
const credentialKey = firstMatch?.credentialId ?? groupName
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if no credentialId is available we match based on the CREDENTIAL_ prefix like you had before as fallback?

Otherwise there's not an easy way to group them

Copy link
Member Author

@Tommylans Tommylans Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure brought it back but in the different order 😄


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 +130,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 +141,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