Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/bumpy-ghosts-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wallet-frontend": minor
---

Support multi-configuration OID4VCI issuance
139 changes: 88 additions & 51 deletions src/hooks/useOID4VCIFlow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useContext, useEffect, useRef, useState } from 'react';
import { useOIDFlowTransportSafe } from '@/context/OIDFlowTransportContext';
import { CredentialOfferSchema, VerifiableCredentialFormat } from 'wallet-common';
import type { ParsingEngineI } from 'wallet-common';
import type { OID4VCIFlowResult } from '@/lib/openid-flow/types/OID4VCITypes';
import type { OIDFlowActiveTransportType, OIDFlowProgressEvent } from '@/lib/openid-flow/types/OIDFlowTypes';
import { DISPLAY_ISSUANCE_WARNINGS, OPENID4VCI_REDIRECT_URI } from '@/config';
Expand Down Expand Up @@ -424,63 +425,28 @@ export function useOID4VCIFlow(options: UseOID4VCIFlowOptions = {}): UseOID4VCIF
setError(null);

const credentialIssuer = credentialIssuerIdentifier ?? '';
const credentialConfigId = credentialConfigurationId ?? '';

try {
if (!credentials || credentials.length === 0) throw new Error('No credentials to store');

const formats = credentials.map(c =>
c.format ? c.format as VerifiableCredentialFormat : inferFormatFromCredential(c.credential)
);

const batchFormat = formats[0];
const batchId = Date.now();

if (formats.some(f => f !== batchFormat)) {
throw new Error('Mixed credential formats in a single batch are not supported');
}

const credentialsToStore = await Promise.all(credentials.map(async (c, idx) => {
let kid = '';

// Derive kid from credential based on format
if (
batchFormat === VerifiableCredentialFormat.VC_SDJWT ||
batchFormat === VerifiableCredentialFormat.DC_SDJWT ||
batchFormat === VerifiableCredentialFormat.MSO_MDOC
) {
kid = await deriveHolderKidFromCredential(c.credential, batchFormat) ?? '';
}

return {
data: c.credential,
format: batchFormat,
kid: kid,
batchId,
credentialIssuerIdentifier: credentialIssuer,
credentialConfigurationId: credentialConfigId,
instanceId: idx,
};
}));

// We assume that all credentials in the batch are the same,
// so parsing one is sufficient for validation and metadata extraction.
const batches = groupByConfiguration(credentials, credentialConfigurationId ?? '');
const { credentialParsingEngine } = await credentialEngine;
const parseResult = await credentialParsingEngine.parse({
rawCredential: credentialsToStore[0].data,
credentialIssuer: {
credentialIssuerIdentifier: credentialsToStore[0].credentialIssuerIdentifier,
credentialConfigurationId: credentialsToStore[0].credentialConfigurationId,
},
});

if (parseResult.success === false) {
throw new Error(`Credential parsing failed: ${parseResult.error}`);
}

if (parseResult.value.warnings?.length > 0) {
const batchIdBase = Date.now();
const prepared = await Promise.all([...batches].map(([id, batch], index) =>
prepareBatch(batch, {
batchId: batchIdBase + index,
credentialIssuer,
credentialConfigurationId: id,
parsingEngine: credentialParsingEngine,
}),
));

const credentialsToStore = prepared.flatMap(b => b.credentialsToStore);
const warnings = prepared.flatMap(b => b.warnings);

if (warnings.length > 0) {
if (DISPLAY_ISSUANCE_WARNINGS && onIssuanceWarnings) {
const consent = await onIssuanceWarnings(parseResult.value.warnings);
const consent = await onIssuanceWarnings(warnings);
if (!consent) throw new Error('User declined to store credential due to warnings');
}
}
Expand Down Expand Up @@ -570,6 +536,77 @@ function loadAndClearPendingFlow(): PendingOID4VCIFlow | null {
}
}

type IssuedCredential = NonNullable<OID4VCIFlowResult['credentials']>[number];

/**
* A batch holds one kind of credential, so each configuration gets its own.
*/
function groupByConfiguration(
credentials: IssuedCredential[],
fallbackId: string,
): Map<string, IssuedCredential[]> {
const batches = new Map<string, IssuedCredential[]>();
for (const c of credentials) {
const id = c.credentialConfigurationId ?? fallbackId;
batches.set(id, [...(batches.get(id) ?? []), c]);
}
return batches;
}

/**
* All credentials of a batch are alike, so parsing one validates it.
*/
async function prepareBatch(
batch: IssuedCredential[],
{ batchId, credentialIssuer, credentialConfigurationId, parsingEngine }: {
batchId: number;
credentialIssuer: string;
credentialConfigurationId: string;
parsingEngine: ParsingEngineI;
},
) {
const formats = batch.map(c =>
c.format ? c.format as VerifiableCredentialFormat : inferFormatFromCredential(c.credential)
);
const format = formats[0];
if (formats.some(f => f !== format)) {
throw new Error('Mixed credential formats in a single batch are not supported');
}

const credentialsToStore = await Promise.all(batch.map(async (c, idx) => {
let kid = '';
if (
format === VerifiableCredentialFormat.VC_SDJWT ||
format === VerifiableCredentialFormat.DC_SDJWT ||
format === VerifiableCredentialFormat.MSO_MDOC
) {
kid = await deriveHolderKidFromCredential(c.credential, format) ?? '';
}
return {
data: c.credential,
format,
kid,
batchId,
credentialIssuerIdentifier: credentialIssuer,
credentialConfigurationId,
instanceId: idx,
};
}));

const parseResult = await parsingEngine.parse({
rawCredential: credentialsToStore[0].data,
credentialIssuer: {
credentialIssuerIdentifier: credentialIssuer,
credentialConfigurationId,
},
});
if (parseResult.success === false) {
throw new Error(`Credential parsing failed: ${parseResult.error}`);
}

return { credentialsToStore, warnings: parseResult.value.warnings ?? [] };
}

/**
* Infer credential format from the credential string
*/
Expand Down
16 changes: 8 additions & 8 deletions src/lib/openid-flow/__tests__/OIDFlowNotification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { dispatchCredentialNotifications } from '../utils/credentialNotification
describe('dispatchCredentialNotifications', () => {
it('is a no-op when transport is null', () => {
expect(() => {
dispatchCredentialNotifications(null, [{ format: 'vc+sd-jwt', credential: 'eyJ...', notification_id: 'n1' }], 'flow-1');
dispatchCredentialNotifications(null, [{ format: 'vc+sd-jwt', credential: 'eyJ...', notificationId: 'n1' }], 'flow-1');
}).not.toThrow();
});

Expand All @@ -33,13 +33,13 @@ describe('dispatchCredentialNotifications', () => {

it('is a no-op when flowId is undefined', () => {
const transport = { sendCredentialNotification: vi.fn() } as any;
dispatchCredentialNotifications(transport, [{ format: 'vc+sd-jwt', credential: 'eyJ...', notification_id: 'n1' }], undefined);
dispatchCredentialNotifications(transport, [{ format: 'vc+sd-jwt', credential: 'eyJ...', notificationId: 'n1' }], undefined);
expect(transport.sendCredentialNotification).not.toHaveBeenCalled();
});

it('is a no-op when flowId is empty string', () => {
const transport = { sendCredentialNotification: vi.fn() } as any;
dispatchCredentialNotifications(transport, [{ format: 'vc+sd-jwt', credential: 'eyJ...', notification_id: 'n1' }], '');
dispatchCredentialNotifications(transport, [{ format: 'vc+sd-jwt', credential: 'eyJ...', notificationId: 'n1' }], '');
expect(transport.sendCredentialNotification).not.toHaveBeenCalled();
});

Expand All @@ -55,7 +55,7 @@ describe('dispatchCredentialNotifications', () => {
expect(transport.sendCredentialNotification).not.toHaveBeenCalled();
});

it('skips credentials without notification_id', () => {
it('skips credentials without notificationId', () => {
const transport = { sendCredentialNotification: vi.fn() } as any;
dispatchCredentialNotifications(
transport,
Expand All @@ -65,14 +65,14 @@ describe('dispatchCredentialNotifications', () => {
expect(transport.sendCredentialNotification).not.toHaveBeenCalled();
});

it('sends a single notification for a batch sharing one notification_id', () => {
it('sends a single notification for a batch sharing one notificationId', () => {
const transport = { sendCredentialNotification: vi.fn() } as any;
dispatchCredentialNotifications(
transport,
[
{ format: 'vc+sd-jwt', credential: 'eyJ1', notification_id: 'notif-1' },
{ format: 'vc+sd-jwt', credential: 'eyJ2', notification_id: 'notif-1' },
{ format: 'vc+sd-jwt', credential: 'eyJ3', notification_id: 'notif-1' },
{ format: 'vc+sd-jwt', credential: 'eyJ1', notificationId: 'notif-1' },
{ format: 'vc+sd-jwt', credential: 'eyJ2', notificationId: 'notif-1' },
{ format: 'vc+sd-jwt', credential: 'eyJ3', notificationId: 'notif-1' },
],
'flow-42',
);
Expand Down
Loading
Loading