-
Notifications
You must be signed in to change notification settings - Fork 180
feat: use SelfClient for MRZ parsing #930
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
transphorm
merged 13 commits into
dev
from
codex/create-shared-client-instance-and-update-imports
Aug 20, 2025
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5a2be82
feat: add Self SDK provider
transphorm 429ffd5
test: cover passport camera and self client provider
transphorm ddffb46
allow this to fail for now cuz the runner doesn't support 16.4
transphorm 87da297
sort imports
transphorm 4eec728
upgrade and fix
transphorm 40b7963
fix header issue
transphorm 8f4cd31
pr feedback
transphorm 9117bb9
fix linter
transphorm 58abf5a
Update app/scripts/check-license-headers.mjs
transphorm 9ee9a0e
Update app/scripts/check-license-headers.mjs
transphorm e110dc6
update extensions and lock file
transphorm 20000fa
simplify call
transphorm ae65a47
use caching
transphorm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
| import { type PropsWithChildren, useMemo } from 'react'; | ||
|
|
||
| import { | ||
| SelfClientProvider as SDKSelfClientProvider, | ||
| webScannerShim, | ||
| type WsConn, | ||
| } from '@selfxyz/mobile-sdk-alpha'; | ||
|
|
||
| /** | ||
| * Provides a configured Self SDK client instance to all descendants. | ||
| * | ||
| * Adapters: | ||
| * - `webScannerShim` for basic MRZ/QR scanning stubs | ||
| * - `fetch`/`WebSocket` for network communication | ||
| * - Web Crypto hashing with a stub signer | ||
| */ | ||
| export const SelfClientProvider = ({ children }: PropsWithChildren) => { | ||
| const config = useMemo(() => ({}), []); | ||
| const adapters = useMemo( | ||
| () => ({ | ||
| scanner: webScannerShim, | ||
| network: { | ||
| http: { | ||
| fetch: (input: RequestInfo, init?: RequestInit) => fetch(input, init), | ||
| }, | ||
| ws: { | ||
| connect: (url: string): WsConn => { | ||
| const socket = new WebSocket(url); | ||
| return { | ||
| send: (data: string | ArrayBufferView | ArrayBuffer) => | ||
| socket.send(data), | ||
| close: () => socket.close(), | ||
| onMessage: cb => { | ||
| socket.addEventListener('message', ev => | ||
| cb((ev as MessageEvent).data), | ||
| ); | ||
| }, | ||
| onError: cb => { | ||
| socket.addEventListener('error', e => cb(e)); | ||
| }, | ||
| onClose: cb => { | ||
| socket.addEventListener('close', () => cb()); | ||
| }, | ||
| }; | ||
| }, | ||
| }, | ||
| }, | ||
| crypto: { | ||
| async hash( | ||
| data: Uint8Array, | ||
| algo: 'sha256' = 'sha256', | ||
| ): Promise<Uint8Array> { | ||
| const buf = await crypto.subtle.digest(algo, data as BufferSource); | ||
| return new Uint8Array(buf); | ||
| }, | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async sign(_data: Uint8Array, _keyRef: string): Promise<Uint8Array> { | ||
| return new Uint8Array(); | ||
| }, | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }), | ||
| [], | ||
| ); | ||
|
|
||
| return ( | ||
| <SDKSelfClientProvider config={config} adapters={adapters}> | ||
| {children} | ||
| </SDKSelfClientProvider> | ||
| ); | ||
| }; | ||
|
|
||
| export default SelfClientProvider; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
| import React from 'react'; | ||
| import { render } from '@testing-library/react-native'; | ||
|
|
||
| import { PassportCamera as NativePassportCamera } from '@/components/native/PassportCamera'; | ||
| import { PassportCamera as WebPassportCamera } from '@/components/native/PassportCamera.web'; | ||
|
|
||
| // Mock the SDK client hook to provide a spyable MRZ parser | ||
| const mockExtract = jest.fn(); | ||
| jest.mock('@selfxyz/mobile-sdk-alpha', () => ({ | ||
| useSelfClient: () => ({ extractMRZInfo: mockExtract }), | ||
| })); | ||
|
|
||
| // Capture props passed to the native view so we can trigger callbacks | ||
| let nativeProps: any; | ||
| jest.mock('react-native', () => ({ | ||
| Platform: { OS: 'ios', select: ({ ios }: any) => ios }, | ||
| PixelRatio: { getPixelSizeForLayoutSize: () => 0 }, | ||
| requireNativeComponent: () => (props: any) => { | ||
| nativeProps = props; | ||
| return null; | ||
| }, | ||
| })); | ||
|
|
||
| describe('PassportCamera components', () => { | ||
| beforeEach(() => { | ||
| mockExtract.mockReset(); | ||
| }); | ||
|
|
||
| it('invokes MRZ parser for string data on native', () => { | ||
| const onPassportRead = jest.fn(); | ||
| render(<NativePassportCamera isMounted onPassportRead={onPassportRead} />); | ||
| const mrz = `P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<\nL898902C36UTO7408122F1204159ZE184226B<<<<<10`; | ||
| const parsed = { | ||
| passportNumber: 'L898902C3', | ||
| validation: { overall: true }, | ||
| } as any; | ||
| mockExtract.mockReturnValue(parsed); | ||
|
|
||
| nativeProps.onPassportRead({ nativeEvent: { data: mrz } }); | ||
|
|
||
| expect(mockExtract).toHaveBeenCalledWith(mrz); | ||
| expect(onPassportRead).toHaveBeenCalledWith(null, parsed); | ||
| }); | ||
|
|
||
| it('maps object-form MRZ data directly on native', () => { | ||
| const onPassportRead = jest.fn(); | ||
| render(<NativePassportCamera isMounted onPassportRead={onPassportRead} />); | ||
|
|
||
| const obj = { | ||
| documentNumber: '123456789', | ||
| expiryDate: '240101', | ||
| birthDate: '900101', | ||
| documentType: 'P', | ||
| countryCode: 'UTO', | ||
| }; | ||
|
|
||
| nativeProps.onPassportRead({ nativeEvent: { data: obj } }); | ||
|
|
||
| expect(mockExtract).not.toHaveBeenCalled(); | ||
| expect(onPassportRead).toHaveBeenCalledWith( | ||
| null, | ||
| expect.objectContaining({ | ||
| passportNumber: '123456789', | ||
| dateOfExpiry: '240101', | ||
| dateOfBirth: '900101', | ||
| documentType: 'P', | ||
| issuingCountry: 'UTO', | ||
| nationality: 'UTO', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('invokes MRZ parser for string data on web', () => { | ||
| const onPassportRead = jest.fn(); | ||
| render(<WebPassportCamera isMounted onPassportRead={onPassportRead} />); | ||
|
|
||
| const mrz = `P<UTOERIKSSON<<ANNA<MARIA<<<<<<<<<<<<<<<<<<<\nL898902C36UTO7408122F1204159ZE184226B<<<<<10`; | ||
| const parsed = { | ||
| passportNumber: 'L898902C3', | ||
| validation: { overall: true }, | ||
| } as any; | ||
| mockExtract.mockReturnValue(parsed); | ||
|
|
||
| // Simulate web read by manually invoking the parsing then forwarding result | ||
| onPassportRead(null, mockExtract(mrz)); | ||
|
|
||
| expect(mockExtract).toHaveBeenCalledWith(mrz); | ||
| expect(onPassportRead).toHaveBeenCalledWith(null, parsed); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // SPDX-License-Identifier: BUSL-1.1; Copyright (c) 2025 Social Connect Labs, Inc.; Licensed under BUSL-1.1 (see LICENSE); Apache-2.0 from 2029-06-11 | ||
| import React, { type ReactNode } from 'react'; | ||
| import { renderHook } from '@testing-library/react-native'; | ||
|
|
||
| import { useSelfClient } from '@selfxyz/mobile-sdk-alpha'; | ||
|
|
||
| import SelfClientProvider from '@/providers/selfClientProvider'; | ||
|
|
||
| describe('SelfClientProvider', () => { | ||
| it('memoises the client instance', () => { | ||
| const wrapper = ({ children }: { children: ReactNode }) => ( | ||
| <SelfClientProvider>{children}</SelfClientProvider> | ||
| ); | ||
| const { result, rerender } = renderHook(() => useSelfClient(), { wrapper }); | ||
| const first = result.current; | ||
| rerender(); | ||
| expect(result.current).toBe(first); | ||
| }); | ||
|
|
||
| it('wires Web Crypto hashing and network adapters', async () => { | ||
| const fetchSpy = jest.fn(async () => new Response(null)); | ||
| (global as any).fetch = fetchSpy; | ||
| class MockSocket { | ||
| url: string; | ||
| constructor(url: string) { | ||
| this.url = url; | ||
| } | ||
| addEventListener() {} | ||
| send() {} | ||
| close() {} | ||
| } | ||
| (global as any).WebSocket = MockSocket; | ||
|
|
||
| const wrapper = ({ children }: { children: ReactNode }) => ( | ||
| <SelfClientProvider>{children}</SelfClientProvider> | ||
| ); | ||
| renderHook(() => useSelfClient(), { wrapper }); | ||
|
|
||
| const data = new TextEncoder().encode('hello'); | ||
| const digest = await crypto.subtle.digest('SHA-256', data); | ||
| expect(digest.byteLength).toBeGreaterThan(0); | ||
|
|
||
| await expect(fetch('https://example.com')).resolves.toBeDefined(); | ||
| const socket = new WebSocket('ws://example.com'); | ||
| expect(typeof (socket as any).send).toBe('function'); | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this fixes the failing
16.4error. missed it from earlier