Handle missing parsed DSC before fetching

This commit is contained in:
Justin Hernandez
2025-12-13 19:08:37 -08:00
parent 59f9780ffb
commit 78e2e783d7
2 changed files with 113 additions and 14 deletions

View File

@@ -898,17 +898,23 @@ export const useProvingStore = create<ProvingState>((set, get) => {
set({ passportData, secret, env });
set({ circuitType });
// Skip parsing for disclosure if passport is already parsed
// Only skip parsing when the document has already been parsed for non-DSC circuits
// Re-parsing would overwrite the alternative CSCA used during registration and is unnecessary
// skip also the register circuit as the passport already got parsed in during the dsc step
// for already parsed passports or ID cards
const shouldParseDocument =
circuitType === 'dsc' ||
((passportData.documentCategory === 'passport' || passportData.documentCategory === 'id_card') &&
!passportData.dsc_parsed);
console.log('circuitType', circuitType);
if (circuitType !== 'dsc') {
if (shouldParseDocument) {
console.log('parsing id document');
actor.send({ type: 'PARSE_ID_DOCUMENT' });
selfClient.trackEvent(ProofEvents.PARSE_ID_DOCUMENT_STARTED);
} else {
console.log('skipping id document parsing');
actor.send({ type: 'FETCH_DATA' });
selfClient.trackEvent(ProofEvents.FETCH_DATA_STARTED);
} else {
actor.send({ type: 'PARSE_ID_DOCUMENT' });
selfClient.trackEvent(ProofEvents.PARSE_ID_DOCUMENT_STARTED);
}
},

View File

@@ -36,6 +36,106 @@ vitest.mock('../../src/stores', async () => {
};
});
const createMockSelfClient = () =>
({
getPrivateKey: vitest.fn().mockResolvedValue('secret'),
trackEvent: vitest.fn(),
logProofEvent: vitest.fn(),
getSelfAppState: () => useSelfAppStore.getState(),
getProvingState: () => useProvingStore.getState(),
getProtocolState: () => useProtocolStore.getState(),
}) as unknown as SelfClient;
describe('init parsing decision', () => {
beforeEach(() => {
vitest.clearAllMocks();
useProvingStore.setState({ circuitType: null, passportData: null, env: null });
});
it('skips parsing for disclose when dsc_parsed exists', async () => {
const mockSelfClient = createMockSelfClient();
const loadSelectedDocumentSpy = vitest.spyOn(documentUtils, 'loadSelectedDocument');
loadSelectedDocumentSpy.mockResolvedValue({
data: {
documentCategory: 'passport',
mock: false,
dsc_parsed: { authorityKeyIdentifier: 'key' },
},
} as any);
await useProvingStore.getState().init(mockSelfClient, 'disclose');
expect(actorMock.send).toHaveBeenCalledWith({ type: 'FETCH_DATA' });
expect(mockSelfClient.trackEvent).toHaveBeenCalledWith(ProofEvents.FETCH_DATA_STARTED);
});
it('parses first for disclose when dsc_parsed is missing', async () => {
const mockSelfClient = createMockSelfClient();
const loadSelectedDocumentSpy = vitest.spyOn(documentUtils, 'loadSelectedDocument');
loadSelectedDocumentSpy.mockResolvedValue({
data: {
documentCategory: 'passport',
mock: false,
},
} as any);
await useProvingStore.getState().init(mockSelfClient, 'disclose');
expect(actorMock.send).toHaveBeenCalledWith({ type: 'PARSE_ID_DOCUMENT' });
expect(mockSelfClient.trackEvent).toHaveBeenCalledWith(ProofEvents.PARSE_ID_DOCUMENT_STARTED);
});
it('skips parsing for register when dsc_parsed exists', async () => {
const mockSelfClient = createMockSelfClient();
const loadSelectedDocumentSpy = vitest.spyOn(documentUtils, 'loadSelectedDocument');
loadSelectedDocumentSpy.mockResolvedValue({
data: {
documentCategory: 'id_card',
mock: false,
dsc_parsed: { authorityKeyIdentifier: 'key' },
},
} as any);
await useProvingStore.getState().init(mockSelfClient, 'register');
expect(actorMock.send).toHaveBeenCalledWith({ type: 'FETCH_DATA' });
expect(mockSelfClient.trackEvent).toHaveBeenCalledWith(ProofEvents.FETCH_DATA_STARTED);
});
it('parses first for register when dsc_parsed is missing', async () => {
const mockSelfClient = createMockSelfClient();
const loadSelectedDocumentSpy = vitest.spyOn(documentUtils, 'loadSelectedDocument');
loadSelectedDocumentSpy.mockResolvedValue({
data: {
documentCategory: 'id_card',
mock: false,
},
} as any);
await useProvingStore.getState().init(mockSelfClient, 'register');
expect(actorMock.send).toHaveBeenCalledWith({ type: 'PARSE_ID_DOCUMENT' });
expect(mockSelfClient.trackEvent).toHaveBeenCalledWith(ProofEvents.PARSE_ID_DOCUMENT_STARTED);
});
it('parses for dsc circuit regardless of existing parsed data', async () => {
const mockSelfClient = createMockSelfClient();
const loadSelectedDocumentSpy = vitest.spyOn(documentUtils, 'loadSelectedDocument');
loadSelectedDocumentSpy.mockResolvedValue({
data: {
documentCategory: 'passport',
mock: false,
dsc_parsed: { authorityKeyIdentifier: 'key' },
},
} as any);
await useProvingStore.getState().init(mockSelfClient, 'dsc');
expect(actorMock.send).toHaveBeenCalledWith({ type: 'PARSE_ID_DOCUMENT' });
expect(mockSelfClient.trackEvent).toHaveBeenCalledWith(ProofEvents.PARSE_ID_DOCUMENT_STARTED);
});
});
describe('startFetchingData', () => {
let mockSelfClient: SelfClient;
beforeEach(async () => {
@@ -52,14 +152,7 @@ describe('startFetchingData', () => {
} as any);
// Create mock selfClient
mockSelfClient = {
getPrivateKey: vitest.fn().mockResolvedValue('secret'), // or mock-secret?
trackEvent: vitest.fn(),
logProofEvent: vitest.fn(),
getSelfAppState: () => useSelfAppStore.getState(),
getProvingState: () => useProvingStore.getState(),
getProtocolState: () => useProtocolStore.getState(),
} as unknown as SelfClient;
mockSelfClient = createMockSelfClient();
await useProvingStore.getState().init(mockSelfClient, 'register');
actorMock.send.mockClear();