diff --git a/src/screens/SSICredentialsRequiredScreen/index.tsx b/src/screens/SSICredentialsRequiredScreen/index.tsx index 15ed0265..621c6bd6 100644 --- a/src/screens/SSICredentialsRequiredScreen/index.tsx +++ b/src/screens/SSICredentialsRequiredScreen/index.tsx @@ -1,14 +1,15 @@ import {NativeStackScreenProps} from '@react-navigation/native-stack'; -import {PEX, SelectResults} from '@sphereon/pex'; +import { PEX, SelectResults, SubmissionRequirementMatch } from "@sphereon/pex"; import {Status} from '@sphereon/pex/dist/main/lib/ConstraintUtils'; import {InputDescriptorV1, InputDescriptorV2} from '@sphereon/pex-models'; import {ICredentialBranding} from '@sphereon/ssi-sdk.data-store'; -import {CredentialMapper, IVerifiableCredential, OriginalVerifiableCredential, W3CVerifiableCredential} from '@sphereon/ssi-types'; +import {CredentialMapper, OriginalVerifiableCredential} from '@sphereon/ssi-types'; import {UniqueVerifiableCredential} from '@veramo/core'; import React, {FC, useEffect, useState} from 'react'; import {ListRenderItemInfo} from 'react-native'; import {SwipeListView} from 'react-native-swipe-list-view'; + import {OVERVIEW_INITIAL_NUMBER_TO_RENDER} from '../../@config/constants'; import {ibGetCredentialBranding} from '../../agent'; import SSIButtonsContainer from '../../components/containers/SSIButtonsContainer'; @@ -23,6 +24,7 @@ import { import {ScreenRoutesEnum, StackParamList} from '../../types'; import {getMatchingUniqueVerifiableCredential, getOriginalVerifiableCredential} from '../../utils/CredentialUtils'; import {toCredentialSummary} from '../../utils/mappers/credential/CredentialMapper'; +import { JSONPath } from "@astronautlabs/jsonpath/src/jsonpath"; type Props = NativeStackScreenProps; @@ -50,10 +52,17 @@ const SSICredentialsRequiredScreen: FC = (props: Props): JSX.Element => { restrictToFormats: format, restrictToDIDMethods: subjectSyntaxTypesSupported, }); - const matchedVCs: Array = selectResult.verifiableCredential - ? selectResult.verifiableCredential - .map((matchedVC: IVerifiableCredential) => getMatchingUniqueVerifiableCredential(uniqueVCs, matchedVC)) - .filter((matchedVC): matchedVC is UniqueVerifiableCredential => !!matchedVC) // filter out the undefined (should not happen) + if (selectResult.areRequiredCredentialsPresent === Status.ERROR) { + console.debug('pex.selectFrom returned errors:\n', JSON.stringify(selectResult.errors)); + } + const matchedVCs: Array = selectResult.matches && selectResult.verifiableCredential + ? selectResult.matches.map((match: SubmissionRequirementMatch) => { + const matchedVC = JSONPath.query(selectResult, match.vc_path[0]) // TODO Can we have multiple vc_path elements for a single match? + if (matchedVC && matchedVC.length > 0) { + return getMatchingUniqueVerifiableCredential(uniqueVCs, matchedVC[0]); + } + }) + .filter((matchedVC : UniqueVerifiableCredential | undefined): matchedVC is UniqueVerifiableCredential => !!matchedVC) // filter out the undefined (should not happen) : []; availableVCs.set(inputDescriptor.id, matchedVCs); }); @@ -151,6 +160,27 @@ const SSICredentialsRequiredScreen: FC = (props: Props): JSX.Element => { ? undefined : () => onItemPress(itemInfo.item.id, availableCredentials.get(itemInfo.item.id)!, itemInfo.item.purpose); + const checkIsMatching = (itemInfo: ListRenderItemInfo, + selectedCredentials: Map>) : boolean => { + if (!selectedCredentials.has(itemInfo.item.id)) { + return false; + } + const selectedCredential: Array | undefined = selectedCredentials.get(itemInfo.item.id); + if(!selectedCredential) { + return false + } + + const credentials : Array = selectedCredential.map((uniqueVC: UniqueVerifiableCredential) => + getOriginalVerifiableCredential(uniqueVC.verifiableCredential)); + return pex.evaluateCredentials( + { + id: itemInfo.item.id, + input_descriptors: [itemInfo.item] + }, + credentials + ).areRequiredCredentialsPresent === Status.INFO; + }; + return ( = (props: Props): JSX.Element => { purpose={itemInfo.item.purpose} available={availableCredentials.has(itemInfo.item.id) ? availableCredentials.get(itemInfo.item.id)! : undefined} selected={selectedCredentials.has(itemInfo.item.id) ? selectedCredentials.get(itemInfo.item.id)! : []} - isMatching={ - selectedCredentials.has(itemInfo.item.id) - ? pex.evaluateCredentials( - { - id: itemInfo.item.id, - input_descriptors: [itemInfo.item], - }, - selectedCredentials.get(itemInfo.item.id)!.map(uniqueVC => getOriginalVerifiableCredential(uniqueVC.verifiableCredential)), - ).areRequiredCredentialsPresent === Status.INFO - : false - } + isMatching={checkIsMatching(itemInfo, selectedCredentials)} listIndex={itemInfo.index} onPress={onPress} />