Skip to content

Commit

Permalink
fix: fixed some issue in the IssuerMetadataUtils plus added some unit…
Browse files Browse the repository at this point in the history
…tests for it
  • Loading branch information
sksadjad committed May 21, 2024
1 parent 8a6c16f commit d348641
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
38 changes: 38 additions & 0 deletions packages/common/lib/__tests__/IssuerMetadataUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { getTypesFromCredentialSupported } from '../functions';
import { CredentialConfigurationSupportedV1_0_13 } from '../types';

describe('IssuerMetadataUtils should', () => {
it('return the types from a credential supported with jwt_vc_json format', () => {
const credentialSupported: CredentialConfigurationSupportedV1_0_13 = {
format: 'jwt_vc_json',
credential_definition: {
type: ['VerifiableCredential', 'BevoegdheidUittreksel'],
},
};

const result: string[] = getTypesFromCredentialSupported(credentialSupported);
expect(result).toEqual(['VerifiableCredential', 'BevoegdheidUittreksel']);
});

it('filter out "VerifiableCredential" type if filterVerifiableCredential option is true', () => {
const credentialSupported: CredentialConfigurationSupportedV1_0_13 = {
format: 'jwt_vc_json',
credential_definition: {
type: ['VerifiableCredential', 'BevoegdheidUittreksel'],
},
};

const result: string[] = getTypesFromCredentialSupported(credentialSupported, { filterVerifiableCredential: true });
expect(result).toEqual(['BevoegdheidUittreksel']);
});

it('throw an error if types cannot be deduced', () => {
const credentialSupported: CredentialConfigurationSupportedV1_0_13 = {
format: 'unknown_format',
} as unknown as CredentialConfigurationSupportedV1_0_13;

expect(() => {
getTypesFromCredentialSupported(credentialSupported);
}).toThrow('Could not deduce types from credential supported');
});
});
25 changes: 13 additions & 12 deletions packages/common/lib/functions/IssuerMetadataUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,26 @@ export function getSupportedCredential(opts?: {
return {};
}

const configurationIds: string[] = Object.keys(issuerMetadata.credential_configurations_supported);
const credentialConfigurations: Record<string, CredentialConfigurationSupportedV1_0_13> =
issuerMetadata.credential_configurations_supported as Record<string, CredentialConfigurationSupportedV1_0_13>;
const normalizedTypes: string[] = Array.isArray(types) ? types : types ? [types] : [];
const normalizedFormats: string[] = Array.isArray(format) ? format : format ? [format] : [];

return configurationIds.reduce((filteredConfigs, id) => {
const config = (issuerMetadata.credential_configurations_supported as Record<string, CredentialConfigurationSupportedV1_0_13>)[id];
return Object.entries(credentialConfigurations).reduce(
(filteredConfigs, [id, config]) => {
const isTypeMatch = normalizedTypes.length === 0 || normalizedTypes.some((type) => config.credential_definition.type.includes(type));
const isFormatMatch = normalizedFormats.length === 0 || normalizedFormats.includes(config.format);

const isTypeMatch = normalizedTypes.length === 0 || normalizedTypes.includes(id);
const isFormatMatch = normalizedFormats.length === 0 || normalizedFormats.includes(config.format);

if (isTypeMatch && isFormatMatch) {
filteredConfigs[id] = config;
}
if (isTypeMatch && isFormatMatch) {
filteredConfigs[id] = config;
}

return filteredConfigs;
}, {} as Record<string, CredentialConfigurationSupportedV1_0_13>);
return filteredConfigs;
},
{} as Record<string, CredentialConfigurationSupportedV1_0_13>,
);
}


export function getTypesFromCredentialSupported(
credentialSupported: CredentialConfigurationSupported,
opts?: { filterVerifiableCredential: boolean },
Expand Down

0 comments on commit d348641

Please sign in to comment.