Skip to content

Commit

Permalink
refactor: cleanup credential exchange code (#602)
Browse files Browse the repository at this point in the history
  • Loading branch information
Harasz committed Jul 19, 2022
1 parent ac8a598 commit 56444c6
Showing 1 changed file with 27 additions and 14 deletions.
Expand Up @@ -4,6 +4,7 @@ import {
PEX,
SelectResults,
} from '@sphereon/pex';
import { InputDescriptorV1, InputDescriptorV2 } from '@sphereon/pex-models';
import { v4 as uuid } from 'uuid';
import axios from 'axios';
import {
Expand Down Expand Up @@ -37,6 +38,7 @@ import {
import { ERROR_MESSAGES } from '../../errors';
import { CacheClient } from '../cache-client';
import { KEY_TYPE } from './verifiable-credentials.const';
import { Claim } from '../claims/claims.types';

/**
* Service responsible for managing verifiable credentials and presentations.
Expand Down Expand Up @@ -517,25 +519,18 @@ export abstract class VerifiableCredentialsServiceBase {
isAccepted: true,
}
);

const claimWithVp = (
claim: Claim
): claim is Claim & { vp: VerifiablePresentation } => !!claim.vp;

const vc = claims
.filter((claim) => claim.vp)
.flatMap((claim) => claim.vp!.verifiableCredential);
.filter(claimWithVp)
.flatMap((claim) => claim.vp.verifiableCredential);

const pex = new PEX();
return pex.selectFrom(presentationDefinition, vc);
}
/*
* We have observed that pex may have bugs when dealing with subject_is_issuer credentials
* and when handling a presentation definition with more than one input descriptor.
* This could be related to these issues:
* - https://github.com/Sphereon-Opensource/pex/issues/96
* - https://github.com/Sphereon-Opensource/pex/issues/91
* We are therefore trying to simplify the input to pex so remove the possibility of it generating incorrect results.
* Once the above issues are fixed, pex can be updated and perhaps this filtering will not needed.
*/
private filterSelfSignDescriptors(descriptors) {
return descriptors?.filter((desc) => !desc?.constraints?.subject_is_issuer);
}

/**
* Create a credential with given parameters.
Expand Down Expand Up @@ -702,4 +697,22 @@ export abstract class VerifiableCredentialsServiceBase {
return null;
}
}

/**
* We have observed that pex may have bugs when dealing with subject_is_issuer credentials
* and when handling a presentation definition with more than one input descriptor.
* This could be related to these issues:
* - https://github.com/Sphereon-Opensource/pex/issues/96
* - https://github.com/Sphereon-Opensource/pex/issues/91
* We are therefore trying to simplify the input to pex so remove the possibility of it generating incorrect results.
* Once the above issues are fixed, pex can be updated and perhaps this filtering will not needed.
*
* @param {InputDescriptorV1 | InputDescriptorV2} descriptors input descriptors
* @returns filtered input descriptors
*/
private filterSelfSignDescriptors(
descriptors: Array<InputDescriptorV1 | InputDescriptorV2>
): Array<InputDescriptorV1 | InputDescriptorV2> {
return descriptors?.filter((desc) => !desc?.constraints?.subject_is_issuer);
}
}

0 comments on commit 56444c6

Please sign in to comment.