Skip to content

Commit

Permalink
Merge pull request #170 from Sphereon-Opensource/feature/WAL-660
Browse files Browse the repository at this point in the history
Fix required creds screen
  • Loading branch information
sanderPostma authored Oct 26, 2023
2 parents 25edbbb + 4c33420 commit e20eab5
Showing 1 changed file with 37 additions and 17 deletions.
54 changes: 37 additions & 17 deletions src/screens/SSICredentialsRequiredScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<StackParamList, ScreenRoutesEnum.CREDENTIALS_REQUIRED>;

Expand Down Expand Up @@ -50,10 +52,17 @@ const SSICredentialsRequiredScreen: FC<Props> = (props: Props): JSX.Element => {
restrictToFormats: format,
restrictToDIDMethods: subjectSyntaxTypesSupported,
});
const matchedVCs: Array<UniqueVerifiableCredential> = 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<UniqueVerifiableCredential> = 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);
});
Expand Down Expand Up @@ -151,24 +160,35 @@ const SSICredentialsRequiredScreen: FC<Props> = (props: Props): JSX.Element => {
? undefined
: () => onItemPress(itemInfo.item.id, availableCredentials.get(itemInfo.item.id)!, itemInfo.item.purpose);

const checkIsMatching = (itemInfo: ListRenderItemInfo<InputDescriptorV1 | InputDescriptorV2>,
selectedCredentials: Map<string, Array<UniqueVerifiableCredential>>) : boolean => {
if (!selectedCredentials.has(itemInfo.item.id)) {
return false;
}
const selectedCredential: Array<UniqueVerifiableCredential> | undefined = selectedCredentials.get(itemInfo.item.id);
if(!selectedCredential) {
return false
}

const credentials : Array<OriginalVerifiableCredential> = selectedCredential.map((uniqueVC: UniqueVerifiableCredential) =>
getOriginalVerifiableCredential(uniqueVC.verifiableCredential));
return pex.evaluateCredentials(
{
id: itemInfo.item.id,
input_descriptors: [itemInfo.item]
},
credentials
).areRequiredCredentialsPresent === Status.INFO;
};

return (
<SSICredentialRequiredViewItem
id={itemInfo.item.id}
title={itemInfo.item.name || itemInfo.item.id}
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}
/>
Expand Down

0 comments on commit e20eab5

Please sign in to comment.