Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
61 changes: 60 additions & 1 deletion app/src/providers/selfClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import { Platform } from 'react-native';

import {
Adapters,
createListenersMap,
reactNativeScannerAdapter,
SdkEvents,
SelfClientProvider as SDKSelfClientProvider,
type TrackEventParams,
webScannerShim,
type WsConn,
} from '@selfxyz/mobile-sdk-alpha';

import { navigationRef } from '@/navigation';
import { unsafe_getPrivateKey } from '@/providers/authProvider';
import { selfClientDocumentsAdapter } from '@/providers/passportDataProvider';
import analytics from '@/utils/analytics';
Expand Down Expand Up @@ -95,8 +98,64 @@ export const SelfClientProvider = ({ children }: PropsWithChildren) => {
[],
);

const appListeners = useMemo(() => {
const { map, addListener } = createListenersMap();

addListener(SdkEvents.PROVING_PASSPORT_DATA_NOT_FOUND, () => {
if (navigationRef.isReady()) {
navigationRef.navigate('DocumentDataNotFound');
}
});

addListener(SdkEvents.PROVING_ACCOUNT_VERIFIED_SUCCESS, () => {
setTimeout(() => {
if (navigationRef.isReady()) {
navigationRef.navigate('AccountVerifiedSuccess');
}
}, 3000);
});

addListener(
SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE,
async ({ hasValidDocument }) => {
setTimeout(() => {
if (navigationRef.isReady()) {
if (hasValidDocument) {
navigationRef.navigate('Home');
} else {
navigationRef.navigate('Launch');
}
}
}, 3000);
},
);

addListener(
SdkEvents.PROVING_PASSPORT_NOT_SUPPORTED,
({ passportData }) => {
if (navigationRef.isReady()) {
navigationRef.navigate('UnsupportedDocument', {
passportData,
} as any);
}
},
);

addListener(SdkEvents.PROVING_ACCOUNT_RECOVERY_REQUIRED, () => {
if (navigationRef.isReady()) {
navigationRef.navigate('AccountRecoveryChoice');
}
});

return map;
}, []);

return (
<SDKSelfClientProvider config={config} adapters={adapters}>
<SDKSelfClientProvider
config={config}
adapters={adapters}
listeners={appListeners}
>
{children}
</SDKSelfClientProvider>
);
Expand Down
77 changes: 44 additions & 33 deletions app/src/utils/proving/provingMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ import {
import {
hasAnyValidRegisteredDocument,
loadSelectedDocument,
SdkEvents,
SelfClient,
} from '@selfxyz/mobile-sdk-alpha';
import {
PassportEvents,
ProofEvents,
} from '@selfxyz/mobile-sdk-alpha/constants/analytics';

import { navigationRef } from '@/navigation';
// will need to be passed in from selfClient
import {
clearPassportData,
Expand Down Expand Up @@ -207,6 +207,11 @@ interface ProvingState {
_handleWsOpen: () => void;
_handleWsError: (error: Event) => void;
_handleWsClose: (event: CloseEvent) => void;

_handlePassportNotSupported: (selfClient: SelfClient) => void;
_handleAccountRecoveryChoice: (selfClient: SelfClient) => void;
_handleAccountVerifiedSuccess: (selfClient: SelfClient) => void;
_handlePassportDataNotFound: (selfClient: SelfClient) => void;
}

export const useProvingStore = create<ProvingState>((set, get) => {
Expand Down Expand Up @@ -239,16 +244,14 @@ export const useProvingStore = create<ProvingState>((set, get) => {
if (state.value === 'post_proving') {
get().postProving(selfClient);
}

if (
get().circuitType !== 'disclose' &&
(state.value === 'error' || state.value === 'failure')
) {
setTimeout(() => {
if (navigationRef.isReady()) {
get()._handleRegisterErrorOrFailure(selfClient);
}
}, 3000);
get()._handleRegisterErrorOrFailure(selfClient);
}

if (state.value === 'completed') {
trackEvent(ProofEvents.PROOF_COMPLETED, {
circuitType: get().circuitType,
Expand All @@ -266,33 +269,27 @@ export const useProvingStore = create<ProvingState>((set, get) => {
})();
}

if (get().circuitType !== 'disclose' && navigationRef.isReady()) {
setTimeout(() => {
navigationRef.navigate('AccountVerifiedSuccess');
}, 3000);
if (get().circuitType !== 'disclose') {
get()._handleAccountVerifiedSuccess(selfClient);
}

if (get().circuitType === 'disclose') {
useSelfAppStore.getState().handleProofResult(true);
}
}

if (state.value === 'passport_not_supported') {
if (navigationRef.isReady()) {
const currentPassportData = get().passportData;
(navigationRef as any).navigate('UnsupportedDocument', {
passportData: currentPassportData,
});
}
get()._handlePassportNotSupported(selfClient);
}

if (state.value === 'account_recovery_choice') {
if (navigationRef.isReady()) {
navigationRef.navigate('AccountRecoveryChoice');
}
get()._handleAccountRecoveryChoice(selfClient);
}

if (state.value === 'passport_data_not_found') {
if (navigationRef.isReady()) {
navigationRef.navigate('DocumentDataNotFound');
}
get()._handlePassportDataNotFound(selfClient);
}

if (state.value === 'failure') {
if (get().circuitType === 'disclose') {
const { error_code, reason } = get();
Expand Down Expand Up @@ -433,17 +430,13 @@ export const useProvingStore = create<ProvingState>((set, get) => {
try {
const hasValid = await hasAnyValidRegisteredDocument(selfClient);

if (navigationRef.isReady()) {
if (hasValid) {
navigationRef.navigate('Home');
} else {
navigationRef.navigate('Launch');
}
}
} catch {
if (navigationRef.isReady()) {
navigationRef.navigate('Launch');
}
selfClient.emit(SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE, {
hasValidDocument: hasValid,
});
} catch (error) {
selfClient.emit(SdkEvents.PROVING_REGISTER_ERROR_OR_FAILURE, {
hasValidDocument: false,
});
}
},

Expand Down Expand Up @@ -1101,6 +1094,24 @@ export const useProvingStore = create<ProvingState>((set, get) => {
},
};
},

_handlePassportNotSupported: (selfClient: SelfClient) => {
selfClient.emit(SdkEvents.PROVING_PASSPORT_NOT_SUPPORTED, {
passportData: get().passportData as PassportData,
});
},

_handleAccountRecoveryChoice: (selfClient: SelfClient) => {
selfClient.emit(SdkEvents.PROVING_ACCOUNT_RECOVERY_REQUIRED);
},

_handleAccountVerifiedSuccess: (selfClient: SelfClient) => {
selfClient.emit(SdkEvents.PROVING_ACCOUNT_VERIFIED_SUCCESS);
},

_handlePassportDataNotFound: (selfClient: SelfClient) => {
selfClient.emit(SdkEvents.PROVING_PASSPORT_DATA_NOT_FOUND);
},
};
});

Expand Down
108 changes: 106 additions & 2 deletions app/tests/utils/proving/provingMachine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import { act, renderHook } from '@testing-library/react-native';

import type { SelfClient } from '@selfxyz/mobile-sdk-alpha';
import { PassportData } from '@selfxyz/common/types';
import { SdkEvents, type SelfClient } from '@selfxyz/mobile-sdk-alpha';

import { useProvingStore } from '@/utils/proving/provingMachine';

Expand All @@ -16,8 +17,12 @@ jest.mock('@/navigation', () => ({
}));

jest.mock('@selfxyz/mobile-sdk-alpha', () => {
const actual = jest.requireActual('@selfxyz/mobile-sdk-alpha');

return {
...actual,
loadSelectedDocument: jest.fn().mockResolvedValue(null),
hasAnyValidRegisteredDocument: jest.fn().mockResolvedValue(true),
};
});

Expand All @@ -30,7 +35,11 @@ describe('provingMachine registration completion', () => {
const { result: initHook } = renderHook(() =>
useProvingStore(state => state.init),
);
const selfClient = {} as SelfClient;
const emitMock = jest.fn();

const selfClient = {
emit: emitMock,
} as unknown as SelfClient;

expect(initHook.current).toBeDefined();

Expand All @@ -43,5 +52,100 @@ describe('provingMachine registration completion', () => {
);

expect(provingStoreHook.current).toBe('passport_data_not_found');
expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_MACHINE_PASSPORT_DATA_NOT_FOUND,
);
});
});

describe('events', () => {
it('emits PROVING_MACHINE_PASSPORT_NOT_SUPPORTED', async () => {
const emitMock = jest.fn();
const mockPassportData = {
mrz: 'mrz',
dsc: 'dsc',
eContent: [1, 2, 3],
signedAttr: [1, 2, 3],
encryptedDigest: [1, 2, 3],
} as PassportData;

const selfClient = {
emit: emitMock,
} as unknown as SelfClient;

await act(async () => {
useProvingStore.setState({ passportData: mockPassportData });
useProvingStore.getState()._handlePassportNotSupported(selfClient);
});

expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_MACHINE_PASSPORT_NOT_SUPPORTED,
{
passportData: mockPassportData,
},
);
});

it('emits PROVING_MACHINE_ACCOUNT_RECOVERY_CHOICE', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;

await act(async () => {
useProvingStore.getState()._handleAccountRecoveryChoice(selfClient);
});

expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_MACHINE_ACCOUNT_RECOVERY_CHOICE,
);
});

it('emits PROVING_MACHINE_ACCOUNT_VERIFIED_SUCCESS', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;

await act(async () => {
useProvingStore.getState()._handleAccountVerifiedSuccess(selfClient);
});

expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_MACHINE_ACCOUNT_VERIFIED_SUCCESS,
);
});

it('emits PROVING_MACHINE_PASSPORT_DATA_NOT_FOUND', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;

await act(async () => {
useProvingStore.getState()._handlePassportDataNotFound(selfClient);
});

expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_MACHINE_PASSPORT_DATA_NOT_FOUND,
);
});

it('emits PROVING_MACHINE_REGISTER_ERROR_OR_FAILURE', async () => {
const emitMock = jest.fn();
const selfClient = {
emit: emitMock,
} as unknown as SelfClient;

await act(async () => {
useProvingStore.getState()._handleRegisterErrorOrFailure(selfClient);
});

expect(emitMock).toHaveBeenCalledWith(
SdkEvents.PROVING_MACHINE_REGISTER_ERROR_OR_FAILURE,
{
hasValidDocument: true,
},
);
});
});
6 changes: 3 additions & 3 deletions packages/mobile-sdk-alpha/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export type {
ProofRequest,
RegistrationInput,
RegistrationStatus,
SDKEvent,
SDKEventMap,
ScanMode,
ScanOpts,
ScanResult,
Expand All @@ -43,9 +41,11 @@ export type { QRProofOptions } from './qr';
export type { SdkErrorCategory } from './errors';

export { SCANNER_ERROR_CODES, notImplemented, sdkError } from './errors';
export { SdkEvents } from './types/events';

export { SelfClientContext, SelfClientProvider, useSelfClient } from './context';

export { createSelfClient } from './client';
export { createListenersMap, createSelfClient } from './client';

export { defaultConfig } from './config/defaults';

Expand Down
Loading
Loading