Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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 app/src/providers/selfClientProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { TrackEventParams } from '@selfxyz/mobile-sdk-alpha';
import { selfClientDocumentsAdapter } from '@/providers/passportDataProvider';
import analytics from '@/utils/analytics';

import { unsafe_getPrivateKey } from './authProvider';

/**
* Provides a configured Self SDK client instance to all descendants.
*
Expand Down Expand Up @@ -82,6 +84,9 @@ export const SelfClientProvider = ({ children }: PropsWithChildren) => {
analytics().trackEvent(event, data);
},
},
auth: {
getPrivateKey: () => unsafe_getPrivateKey(),
},
}),
[],
);
Expand Down
12 changes: 7 additions & 5 deletions app/src/screens/dev/DevPrivateKeyScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { useCallback, useEffect, useState } from 'react';
import { Button, Text, XStack, YStack } from 'tamagui';
import Clipboard from '@react-native-clipboard/clipboard';

import { unsafe_getPrivateKey } from '@/providers/authProvider';
import { useSelfClient } from '@selfxyz/mobile-sdk-alpha';

import { black, slate50, slate200, teal500, white } from '@/utils/colors';
import { confirmTap } from '@/utils/haptic';

Expand All @@ -16,12 +17,13 @@ const DevPrivateKeyScreen: React.FC = () => {
);
const [isPrivateKeyRevealed, setIsPrivateKeyRevealed] = useState(false);
const [copied, setCopied] = useState(false);
const selfClient = useSelfClient();

useEffect(() => {
unsafe_getPrivateKey().then(key =>
setPrivateKey(key || 'No private key found'),
);
}, []);
selfClient
.getPrivateKey()
.then(key => setPrivateKey(key || 'No private key found'));
}, [selfClient]);

const handleRevealPrivateKey = useCallback(() => {
confirmTap();
Expand Down
5 changes: 1 addition & 4 deletions app/src/utils/proving/provingMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ import {
} from '@selfxyz/mobile-sdk-alpha/constants/analytics';

import { navigationRef } from '@/navigation';
// this will be pass as property of from selfClient
import { unsafe_getPrivateKey } from '@/providers/authProvider';
// will need to be passed in from selfClient
import {
clearPassportData,
Expand Down Expand Up @@ -651,8 +649,7 @@ export const useProvingStore = create<ProvingState>((set, get) => {

const { data: passportData } = selectedDocument;

// TODO call on self client
const secret = await unsafe_getPrivateKey();
const secret = await selfClient.getPrivateKey();
if (!secret) {
console.error('Could not load secret');
trackEvent(ProofEvents.LOAD_SECRET_FAILED);
Expand Down
2 changes: 0 additions & 2 deletions packages/mobile-sdk-alpha/src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export type { SdkErrorCategory } from './errors';

export { SCANNER_ERROR_CODES, notImplemented, sdkError } from './errors';
export { SelfClientContext, SelfClientProvider, useSelfClient } from './context';
// Browser-only high-level component (DOM-based)
export { SelfMobileSdk as SelfMobileSdkHighLevel } from './components/SelfMobileSdk';

export { createSelfClient } from './client';

Expand Down
23 changes: 22 additions & 1 deletion packages/mobile-sdk-alpha/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const optionalDefaults: Partial<Adapters> = {
},
};

const REQUIRED_ADAPTERS = ['scanner', 'network', 'crypto', 'documents'] as const;
const REQUIRED_ADAPTERS = ['auth', 'scanner', 'network', 'crypto', 'documents'] as const;

/**
* Creates a fully configured {@link SelfClient} instance.
Expand Down Expand Up @@ -128,10 +128,31 @@ export function createSelfClient({ config, adapters }: { config: Config; adapter
return adapters.analytics.trackEvent(event, payload);
}

/**
* Retrieves the private key via the auth adapter.
* With great power comes great responsibility
*/
async function getPrivateKey(): Promise<string> {
if (!adapters.auth) throw notImplemented('auth');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this check necessary if we're enforcing the auth adapter in required adapters anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in short it kinda was because we were not enforcing it in types. but i have a new commit comming

return adapters.auth.getPrivateKey();
}

async function hasPrivateKey(): Promise<boolean> {
if (!adapters.auth) throw notImplemented('auth');
try {
const key = await adapters.auth.getPrivateKey();
return !!key;
} catch {
return false;
}
}

return {
scanDocument,
validateDocument,
trackEvent,
getPrivateKey,
hasPrivateKey,
checkRegistration,
registerDocument,
generateProof,
Expand Down
73 changes: 0 additions & 73 deletions packages/mobile-sdk-alpha/src/components/SelfMobileSdk.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion packages/mobile-sdk-alpha/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type {
// MRZ module
export type { DG1, DG2, NFCScanOptions, ParsedNFCResponse } from './nfc';

export type { DocumentData, DocumentMetadata, ExternalAdapter, PassportCameraProps, ScreenProps } from './types/ui';
export type { DocumentData, DocumentMetadata, PassportCameraProps, ScreenProps } from './types/ui';

export type { MRZScanOptions } from './mrz';

Expand Down
9 changes: 8 additions & 1 deletion packages/mobile-sdk-alpha/src/types/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ export interface AnalyticsAdapter {
trackEvent(event: string, payload?: TrackEventParams): void;
}

export interface AuthAdapter {
getPrivateKey(): Promise<string>;
}

export interface ClockAdapter {
now(): number;
sleep(ms: number, signal?: AbortSignal): Promise<void>;
Expand Down Expand Up @@ -91,7 +95,8 @@ export interface Adapters {
network: NetworkAdapter;
clock: ClockAdapter;
logger: LoggerAdapter;
analytics: AnalyticsAdapter;
analytics?: AnalyticsAdapter;
auth: AuthAdapter;
documents: DocumentsAdapter;
}

Expand Down Expand Up @@ -171,6 +176,8 @@ export interface SelfClient {
): Promise<ProofHandle>;
extractMRZInfo(mrz: string): MRZInfo;
trackEvent(event: string, payload?: TrackEventParams): void;
getPrivateKey(): Promise<string>;
hasPrivateKey(): Promise<boolean>;
on<E extends SDKEvent>(event: E, cb: (payload: SDKEventMap[E]) => void): Unsubscribe;
emit<E extends SDKEvent>(event: E, payload: SDKEventMap[E]): void;

Expand Down
13 changes: 0 additions & 13 deletions packages/mobile-sdk-alpha/src/types/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ export interface DocumentData {
metadata: DocumentMetadata;
}

// External adapter interface
export interface ExternalAdapter {
getSecret: () => Promise<string>;
getAllDocuments: () => Promise<{
[documentId: string]: DocumentData;
}>;
setDocument: (doc: DocumentData, documentId: string) => Promise<boolean>;
onOnboardingSuccess: () => void;
onOnboardingFailure: (error: Error) => void;
onDisclosureSuccess: () => void;
onDisclosureFailure: (error: Error) => void;
}

// Screen component props
export interface ScreenProps {
onSuccess: () => void;
Expand Down
19 changes: 19 additions & 0 deletions packages/mobile-sdk-alpha/tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,25 @@ describe('createSelfClient', () => {
expect(trackEvent).toHaveBeenCalledWith('another_event', { foo: 'bar' });
});
});
describe('when auth adapter is given', () => {
it('getPrivateKey becomes callable on the client', () => {
const getPrivateKey = vi.fn(() => Promise.resolve('stubbed-private-key'));
const client = createSelfClient({
config: {},
adapters: { scanner, network, crypto, auth: { getPrivateKey } },
});

expect(client.getPrivateKey()).resolves.toBe('stubbed-private-key');
});
it('hasPrivateKey becomes callable on the client', () => {
const getPrivateKey = vi.fn(() => Promise.resolve('stubbed-private-key'));
const client = createSelfClient({
config: {},
adapters: { scanner, network, crypto, auth: { getPrivateKey } },
});
expect(client.hasPrivateKey()).resolves.toBe(true);
});
});
});

const scanner: ScannerAdapter = {
Expand Down
Loading