Skip to content

Commit

Permalink
fix: reverted adding lodash and fixed a problem with rule checking
Browse files Browse the repository at this point in the history
  • Loading branch information
sksadjad committed Jun 29, 2023
1 parent 5ec6485 commit 07d1c6d
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 164 deletions.
24 changes: 13 additions & 11 deletions lib/evaluation/evaluationClientWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Descriptor, Format, InputDescriptorV1, InputDescriptorV2, PresentationSubmission, Rules, SubmissionRequirement } from '@sphereon/pex-models';
import { IVerifiableCredential, OriginalVerifiableCredential, WrappedVerifiableCredential } from '@sphereon/ssi-types';
import jp from 'jsonpath';
import _ from 'lodash';

import { Checked, Status } from '../ConstraintUtils';
import { IInternalPresentationDefinition, InternalPresentationDefinitionV2, IPresentationDefinition } from '../types';
Expand Down Expand Up @@ -55,10 +54,11 @@ export class EvaluationClientWrapper {
e
)[0].value
);
const areRequiredCredentialsPresent = this.determineAreRequiredCredentialsPresent(matchSubmissionRequirements);
selectResults = {
errors: errors,
errors: areRequiredCredentialsPresent === Status.INFO ? [] : errors,
matches: [...matchSubmissionRequirements],
areRequiredCredentialsPresent: Status.INFO,
areRequiredCredentialsPresent,
verifiableCredential: credentials,
warnings,
};
Expand Down Expand Up @@ -126,7 +126,7 @@ export class EvaluationClientWrapper {
} else {
srm.vc_path.forEach((match, index, matches) => {
const vc = jp.query(verifiableCredentials, match)[0];
const newIndex = vcsToSend?.findIndex((svc) => _.isEqual(svc, vc));
const newIndex = vcsToSend?.findIndex((svc) => JSON.stringify(svc) === JSON.stringify(vc));
if (newIndex === -1) {
throw new Error(
`The index of the VerifiableCredential in your current call can't be found in your previously submitted credentials. Are you trying to send a new Credential?\nverifiableCredential: ${vc}`
Expand Down Expand Up @@ -481,8 +481,10 @@ export class EvaluationClientWrapper {
if (selectResults) {
selectResults.verifiableCredential?.forEach((selectableCredential) => {
const foundIndex: number = ObjectUtils.isString(selectableCredential)
? wrappedVcs.findIndex((wrappedVc) => _.isEqual(selectableCredential, wrappedVc.original))
: wrappedVcs.findIndex((wrappedVc) => _.isEqual((selectableCredential as IVerifiableCredential).proof, wrappedVc.credential.proof));
? wrappedVcs.findIndex((wrappedVc) => selectableCredential === wrappedVc.original)
: wrappedVcs.findIndex(
(wrappedVc) => JSON.stringify((selectableCredential as IVerifiableCredential).proof) === JSON.stringify(wrappedVc.credential.proof)
);
if (foundIndex === -1) {
throw new Error('index is not right');
}
Expand Down Expand Up @@ -546,15 +548,15 @@ export class EvaluationClientWrapper {
innerStatus = Status.ERROR;
}
if (m.rule === Rules.Pick) {
if (m.vc_path.length == 0 && (!m.from_nested || m.from_nested.length == 0)) {
if (m.vc_path.length == 0) {
innerStatus = Status.ERROR;
} else if (m.count && m.vc_path.length < m.count && (!m.from_nested || !m.from_nested?.length)) {
} else if (m.count && m.vc_path.length < m.count) {
innerStatus = Status.ERROR;
} else if (m.count && (m.vc_path.length > m.count || (m.from_nested && m.from_nested?.length > m.count))) {
} else if (m.count && m.vc_path.length > m.count) {
innerStatus = Status.WARN;
} else if (m.min && m.vc_path.length < m.min && m.from_nested && !m.from_nested?.length) {
} else if (m.min && m.vc_path.length < m.min) {
innerStatus = Status.ERROR;
} else if (m.max && (m.vc_path.length > m.max || (m.from_nested && m.from_nested?.length > m.max))) {
} else if (m.max && m.vc_path.length > m.max) {
innerStatus = Status.WARN;
} else if (m.rule === Rules.All && m.vc_path.length > 1) {
innerStatus = Status.ERROR;
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@
"ajv-formats": "^2.1.1",
"jsonpath": "^1.1.1",
"jwt-decode": "^3.1.2",
"lodash": "^4.17.21",
"nanoid": "^3.3.6",
"string.prototype.matchall": "^4.0.8"
},
"devDependencies": {
"@types/jest": "^29.4.0",
"@types/jsonpath": "^0.2.0",
"@types/lodash": "^4.14.195",
"@types/node": "^16.18.25",
"@typescript-eslint/eslint-plugin": "^5.59.1",
"@typescript-eslint/parser": "^5.59.1",
Expand Down
234 changes: 93 additions & 141 deletions test/evaluation/selectFrom.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import fs from 'fs';

import { IVerifiableCredential, WrappedVerifiableCredential } from '@sphereon/ssi-types';
import _ from 'lodash';

import { Status } from '../../lib';
import { EvaluationClientWrapper } from '../../lib/evaluation';
import { InternalPresentationDefinitionV1 } from '../../lib/types/Internal.types';
import { InternalPresentationDefinitionV1, SSITypesBuilder } from '../../lib/types';
import PexMessages from '../../lib/types/Messages';
import { SSITypesBuilder } from '../../lib/types/SSITypesBuilder';

function getFile(path: string) {
return JSON.parse(fs.readFileSync(path, 'utf-8'));
Expand Down Expand Up @@ -914,144 +912,98 @@ describe('selectFrom tests', () => {
holderDIDs: dids,
limitDisclosureSignatureSuites: LIMIT_DISCLOSURE_SIGNATURE_SUITES,
});
expect(
_.isEqual(result, {
areRequiredCredentialsPresent: Status.WARN,
errors: [
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[0]: $.verifiableCredential[1]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[0]: $.verifiableCredential[2]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[1]: $.verifiableCredential[0]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[1]: $.verifiableCredential[2]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[2]: $.verifiableCredential[0]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[2]: $.verifiableCredential[1]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[1]: $.verifiableCredential[0]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[2]: $.verifiableCredential[0]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[0]: $.verifiableCredential[1]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[0]: $.verifiableCredential[2]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[0]: $.verifiableCredential[1]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[0]: $.verifiableCredential[2]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[1]: $.verifiableCredential[0]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[1]: $.verifiableCredential[2]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[2]: $.verifiableCredential[0]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[2]: $.verifiableCredential[1]',
},
],
matches: [
{
from: ['B'],
vc_path: ['$.verifiableCredential[0]', '$.verifiableCredential[1]'],
max: 1,
name: 'Submission of educational transcripts',
rule: 'pick',
},
],
verifiableCredential: [
{
'@context': 'https://business-standards.org/schemas/employment-history.json',
credentialSubject: {
active: true,
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
},
id: 'https://business-standards.org/schemas/employment-history.json',
issuanceDate: '2010-01-01T19:73:24Z',
issuer: 'did:foo:123',
proof: {
created: '2017-06-18T21:19:10Z',
jws: '...',
proofPurpose: 'assertionMethod',
type: 'EcdsaSecp256k1VerificationKey2019',
verificationMethod: 'https://example.edu/issuers/keys/1',
},
type: ['VerifiableCredential', 'GenericEmploymentCredential'],
},
{
'@context': 'https://www.w3.org/2018/credentials/v1',
credentialSubject: {
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
license: {
dob: '07/13/80',
number: '34DGE352',
},
},
id: 'https://eu.com/claims/DriversLicense',
issuanceDate: '2010-01-01T19:73:24Z',
issuer: 'did:foo:123',
proof: {
created: '2017-06-18T21:19:10Z',
jws: '...',
proofPurpose: 'assertionMethod',
type: 'RsaSignature2018',
verificationMethod: 'https://example.edu/issuers/keys/1',
},
type: ['EUDriversLicense'],
},
],
warnings: [],
})
);
expect(result.areRequiredCredentialsPresent).toBe(Status.ERROR);
expect(result.errors).toEqual([
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[0]: $.verifiableCredential[1]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[0]: $.verifiableCredential[2]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[1]: $.verifiableCredential[0]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[1]: $.verifiableCredential[2]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[2]: $.verifiableCredential[0]',
},
{
tag: 'UriEvaluation',
status: 'error',
message: PexMessages.URI_EVALUATION_DIDNT_PASS + ': $.input_descriptors[2]: $.verifiableCredential[1]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[1]: $.verifiableCredential[0]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[2]: $.verifiableCredential[0]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[0]: $.verifiableCredential[1]',
},
{
tag: 'FilterEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_FAILED_FILTER_EVALUATION + ': $.input_descriptors[0]: $.verifiableCredential[2]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[0]: $.verifiableCredential[1]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[0]: $.verifiableCredential[2]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[1]: $.verifiableCredential[0]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[1]: $.verifiableCredential[2]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[2]: $.verifiableCredential[0]',
},
{
tag: 'MarkForSubmissionEvaluation',
status: 'error',
message: PexMessages.INPUT_CANDIDATE_IS_NOT_ELIGIBLE_FOR_PRESENTATION_SUBMISSION + ': $.input_descriptors[2]: $.verifiableCredential[1]',
},
]);
expect(result.matches).toEqual([
{
from: ['B'],
vc_path: ['$.verifiableCredential[0]', '$.verifiableCredential[1]'],
min: 3,
name: 'Submission of educational transcripts',
rule: 'pick',
},
]);
});

it('Evaluate submission requirements max 1 from group B', () => {
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1088,11 +1088,6 @@
resolved "https://registry.yarnpkg.com/@types/jsonpath/-/jsonpath-0.2.0.tgz#13c62db22a34d9c411364fac79fd374d63445aa1"
integrity sha512-v7qlPA0VpKUlEdhghbDqRoKMxFB3h3Ch688TApBJ6v+XLDdvWCGLJIYiPKGZnS6MAOie+IorCfNYVHOPIHSWwQ==

"@types/lodash@^4.14.195":
version "4.14.195"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.195.tgz#bafc975b252eb6cea78882ce8a7b6bf22a6de632"
integrity sha512-Hwx9EUgdwf2GLarOjQp5ZH8ZmblzcbTBC2wtQWNKARBSxM9ezRIAUpeDTgoQRAFB0+8CNWXVA9+MaSOzOF3nPg==

"@types/minimist@^1.2.2":
version "1.2.2"
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
Expand Down Expand Up @@ -3539,11 +3534,6 @@ lodash.merge@^4.6.2:
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==

lodash@^4.17.21:
version "4.17.21"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
Expand Down

0 comments on commit 07d1c6d

Please sign in to comment.