Skip to content

Commit 40ad1de

Browse files
committed
chore: more type and format extraction methods
1 parent b966d8c commit 40ad1de

2 files changed

Lines changed: 90 additions & 80 deletions

File tree

packages/common/lib/functions/IssuerMetadataUtils.ts

Lines changed: 1 addition & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
CredentialDefinitionV1_0_13,
3-
CredentialOfferFormat,
4-
CredentialOfferPayload,
5-
JsonLdIssuerCredentialDefinition,
6-
UniformCredentialOfferPayload,
7-
UniformCredentialOfferRequest,
8-
VCI_LOG_COMMON,
9-
} from '../index';
1+
import { getTypesFromObject, VCI_LOG_COMMON } from '../index';
102
import {
113
AuthorizationServerMetadata,
124
CredentialConfigurationSupported,
@@ -157,33 +149,6 @@ export function getSupportedCredential(opts?: {
157149
throw Error(`Either < v11 configurations or V13 configurations should have been filtered at this point`);
158150
}
159151

160-
export function getTypesFromCredentialSupported(
161-
credentialSupported: CredentialConfigurationSupported,
162-
opts?: { filterVerifiableCredential: boolean },
163-
) {
164-
let types: string[] = [];
165-
if (
166-
credentialSupported.format === 'jwt_vc_json' ||
167-
credentialSupported.format === 'jwt_vc' ||
168-
credentialSupported.format === 'jwt_vc_json-ld' ||
169-
credentialSupported.format === 'ldp_vc'
170-
) {
171-
types = getTypesFromObject(credentialSupported) ?? [];
172-
} else if (credentialSupported.format === 'vc+sd-jwt') {
173-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
174-
// @ts-ignore
175-
types = [credentialSupported.vct];
176-
}
177-
178-
if (!types || types.length === 0) {
179-
throw Error('Could not deduce types from credential supported');
180-
}
181-
if (opts?.filterVerifiableCredential) {
182-
return types.filter((type) => type !== 'VerifiableCredential');
183-
}
184-
return types;
185-
}
186-
187152
export function credentialsSupportedV8ToV13(supportedV8: CredentialSupportedTypeV1_0_08): Record<string, CredentialConfigurationSupported> {
188153
const credentialConfigsSupported: Record<string, CredentialConfigurationSupported> = {};
189154
Object.entries(supportedV8).flatMap((entry) => {
@@ -238,47 +203,3 @@ export function getIssuerName(
238203
}
239204
return url;
240205
}
241-
242-
/**
243-
* The specs had many places where types could be expressed. This method ensures we get them in any way possible
244-
* @param subject
245-
*/
246-
export function getTypesFromObject(
247-
subject: CredentialConfigurationSupported | CredentialOfferFormat | CredentialDefinitionV1_0_13 | JsonLdIssuerCredentialDefinition | string,
248-
): string[] | undefined {
249-
if (subject === undefined) {
250-
return undefined;
251-
} else if (typeof subject === 'string') {
252-
return [subject];
253-
} else if ('credential_definition' in subject && subject.credential_definition) {
254-
return getTypesFromObject(subject.credential_definition);
255-
} else if ('types' in subject && subject.types) {
256-
return Array.isArray(subject.types) ? subject.types : [subject.types];
257-
} else if ('type' in subject && subject.type) {
258-
return Array.isArray(subject.type) ? subject.type : [subject.type];
259-
} else if ('vct' in subject && subject.vct) {
260-
return [subject.vct];
261-
}
262-
VCI_LOG_COMMON.warning('Could not deduce credential types. Probably a failure down the line will happen!');
263-
return undefined;
264-
}
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-
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,92 @@
1+
import { AuthorizationDetails, CredentialOfferPayload, UniformCredentialOfferPayload, UniformCredentialOfferRequest, VCI_LOG_COMMON } from '../index';
2+
import { CredentialConfigurationSupported, CredentialDefinitionV1_0_13, CredentialOfferFormat, JsonLdIssuerCredentialDefinition } from '../types';
3+
14
export const getNumberOrUndefined = (input?: string): number | undefined => {
25
return input && !isNaN(+input) ? +input : undefined;
36
};
7+
8+
/**
9+
* The specs had many places where types could be expressed. This method ensures we get them in any way possible
10+
* @param subject
11+
*/
12+
export function getTypesFromObject(
13+
subject: CredentialConfigurationSupported | CredentialOfferFormat | CredentialDefinitionV1_0_13 | JsonLdIssuerCredentialDefinition | string,
14+
): string[] | undefined {
15+
if (subject === undefined) {
16+
return undefined;
17+
} else if (typeof subject === 'string') {
18+
return [subject];
19+
} else if ('credential_definition' in subject && subject.credential_definition) {
20+
return getTypesFromObject(subject.credential_definition);
21+
} else if ('types' in subject && subject.types) {
22+
return Array.isArray(subject.types) ? subject.types : [subject.types];
23+
} else if ('type' in subject && subject.type) {
24+
return Array.isArray(subject.type) ? subject.type : [subject.type];
25+
} else if ('vct' in subject && subject.vct) {
26+
return [subject.vct];
27+
}
28+
VCI_LOG_COMMON.warning('Could not deduce credential types. Probably a failure down the line will happen!');
29+
return undefined;
30+
}
31+
32+
export function getTypesFromCredentialOffer(
33+
offer: UniformCredentialOfferRequest | CredentialOfferPayload | UniformCredentialOfferPayload,
34+
opts?: { configIdAsType?: boolean },
35+
): Array<Array<string>> | undefined {
36+
const { configIdAsType = false } = { ...opts };
37+
if ('credentials' in offer && Array.isArray(offer.credentials)) {
38+
return offer.credentials.map((cred) => getTypesFromObject(cred)).filter((cred): cred is string[] => cred !== undefined);
39+
} else if (configIdAsType && 'credential_configuration_ids' in offer && Array.isArray(offer.credential_configuration_ids)) {
40+
return offer.credential_configuration_ids.map((id) => [id]);
41+
} else if ('credential_offer' in offer && offer.credential_offer) {
42+
return getTypesFromCredentialOffer(offer.credential_offer, opts);
43+
} else if ('credential_type' in offer && offer.credential_type) {
44+
if (typeof offer.credential_type === 'string') {
45+
return [[offer.credential_type]];
46+
} else if (Array.isArray(offer.credential_type)) {
47+
return [offer.credential_type];
48+
}
49+
}
50+
VCI_LOG_COMMON.warning('Could not deduce credential types from offer. Probably a failure down the line will happen!');
51+
return undefined;
52+
}
53+
54+
export function getTypesFromAuthorizationDetails(authDetails: AuthorizationDetails, opts?: { configIdAsType?: boolean }): string[] | undefined {
55+
const { configIdAsType = false } = { ...opts };
56+
if (typeof authDetails === 'string') {
57+
return [authDetails];
58+
} else if ('types' in authDetails && Array.isArray(authDetails.types)) {
59+
return authDetails.types;
60+
} else if (configIdAsType && authDetails.credential_configuration_id) {
61+
return [authDetails.credential_configuration_id];
62+
}
63+
64+
return undefined;
65+
}
66+
67+
export function getTypesFromCredentialSupported(
68+
credentialSupported: CredentialConfigurationSupported,
69+
opts?: { filterVerifiableCredential: boolean },
70+
) {
71+
let types: string[] = [];
72+
if (
73+
credentialSupported.format === 'jwt_vc_json' ||
74+
credentialSupported.format === 'jwt_vc' ||
75+
credentialSupported.format === 'jwt_vc_json-ld' ||
76+
credentialSupported.format === 'ldp_vc'
77+
) {
78+
types = getTypesFromObject(credentialSupported) ?? [];
79+
} else if (credentialSupported.format === 'vc+sd-jwt') {
80+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
81+
// @ts-ignore
82+
types = [credentialSupported.vct];
83+
}
84+
85+
if (!types || types.length === 0) {
86+
throw Error('Could not deduce types from credential supported');
87+
}
88+
if (opts?.filterVerifiableCredential) {
89+
return types.filter((type) => type !== 'VerifiableCredential');
90+
}
91+
return types;
92+
}

0 commit comments

Comments
 (0)