Skip to content

Commit b966d8c

Browse files
committed
feat: add get types from offer function to get the types from multiple versions of credential offers
1 parent 31597bc commit b966d8c

1 file changed

Lines changed: 22 additions & 13 deletions

File tree

packages/common/lib/functions/IssuerMetadataUtils.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
CredentialOfferFormat,
44
CredentialOfferPayload,
55
JsonLdIssuerCredentialDefinition,
6+
UniformCredentialOfferPayload,
67
UniformCredentialOfferRequest,
78
VCI_LOG_COMMON,
89
} from '../index';
@@ -243,14 +244,7 @@ export function getIssuerName(
243244
* @param subject
244245
*/
245246
export function getTypesFromObject(
246-
subject:
247-
| CredentialConfigurationSupported
248-
| CredentialOfferFormat
249-
| CredentialOfferPayload
250-
| CredentialDefinitionV1_0_13
251-
| JsonLdIssuerCredentialDefinition
252-
| UniformCredentialOfferRequest
253-
| string,
247+
subject: CredentialConfigurationSupported | CredentialOfferFormat | CredentialDefinitionV1_0_13 | JsonLdIssuerCredentialDefinition | string,
254248
): string[] | undefined {
255249
if (subject === undefined) {
256250
return undefined;
@@ -264,12 +258,27 @@ export function getTypesFromObject(
264258
return Array.isArray(subject.type) ? subject.type : [subject.type];
265259
} else if ('vct' in subject && subject.vct) {
266260
return [subject.vct];
267-
} else if ('credentials' in subject && subject.credentials) {
268-
return getTypesFromObject(subject.credentials);
269-
} else if ('credential_offer' in subject && subject.credential_offer) {
270-
return getTypesFromObject(subject.credential_offer);
271261
}
272-
273262
VCI_LOG_COMMON.warning('Could not deduce credential types. Probably a failure down the line will happen!');
274263
return undefined;
275264
}
265+
266+
export function getTypesFromCredentialOffer(
267+
offer: UniformCredentialOfferRequest | CredentialOfferPayload | UniformCredentialOfferPayload,
268+
): Array<Array<string>> | undefined {
269+
if ('credentials' in offer && Array.isArray(offer.credentials)) {
270+
return offer.credentials.map((cred) => getTypesFromObject(cred)).filter((cred): cred is string[] => cred !== undefined);
271+
} else if ('credential_configuration_ids' in offer && Array.isArray(offer.credential_configuration_ids)) {
272+
return offer.credential_configuration_ids.map((id) => [id]);
273+
} else if ('credential_offer' in offer && offer.credential_offer) {
274+
return getTypesFromCredentialOffer(offer.credential_offer);
275+
} else if ('credential_type' in offer && offer.credential_type) {
276+
if (typeof offer.credential_type === 'string') {
277+
return [[offer.credential_type]];
278+
} else if (Array.isArray(offer.credential_type)) {
279+
return [offer.credential_type];
280+
}
281+
}
282+
VCI_LOG_COMMON.warning('Could not deduce credential types from offer. Probably a failure down the line will happen!');
283+
return undefined;
284+
}

0 commit comments

Comments
 (0)