Skip to content

Commit f85a23a

Browse files
authored
cleans up unused parts of sdk interface, adds inline documentation, (#1078)
* cleans up unused parts of sdk interface, adds inline documentation, * fix up build * yolo
1 parent c11c9af commit f85a23a

File tree

10 files changed

+85
-217
lines changed

10 files changed

+85
-217
lines changed

packages/mobile-sdk-alpha/src/browser.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,13 @@ export type {
1717
MRZValidation,
1818
NetworkAdapter,
1919
Progress,
20-
ProofHandle,
21-
ProofRequest,
22-
RegistrationInput,
23-
RegistrationStatus,
2420
ScanMode,
2521
ScanOpts,
2622
ScanResult,
2723
ScannerAdapter,
2824
SelfClient,
2925
StorageAdapter,
3026
Unsubscribe,
31-
ValidationInput,
32-
ValidationResult,
3327
WsAdapter,
3428
WsConn,
3529
} from './types/public';

packages/mobile-sdk-alpha/src/client.ts

Lines changed: 10 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,15 @@ import { defaultConfig } from './config/defaults';
88
import { mergeConfig } from './config/merge';
99
import { notImplemented } from './errors';
1010
import { extractMRZInfo as parseMRZInfo } from './processing/mrz';
11-
import { SDKEvent, SDKEventMap, SdkEvents } from './types/events';
12-
import type {
13-
Adapters,
14-
Config,
15-
Progress,
16-
ProofHandle,
17-
ProofRequest,
18-
RegistrationInput,
19-
RegistrationStatus,
20-
ScanOpts,
21-
ScanResult,
22-
SelfClient,
23-
Unsubscribe,
24-
ValidationInput,
25-
ValidationResult,
26-
} from './types/public';
11+
import { SDKEvent, SDKEventMap } from './types/events';
12+
import type { Adapters, Config, ScanOpts, ScanResult, SelfClient, Unsubscribe } from './types/public';
2713
import { TrackEventParams } from './types/public';
2814
/**
2915
* Optional adapter implementations used when a consumer does not provide their
3016
* own. These defaults are intentionally minimal no-ops suitable for tests and
3117
* non-production environments.
3218
*/
33-
const optionalDefaults: Required<Pick<Adapters, 'storage' | 'clock' | 'logger'>> = {
34-
storage: {
35-
get: async () => null,
36-
set: async () => {},
37-
remove: async () => {},
38-
},
19+
const optionalDefaults: Required<Pick<Adapters, 'clock' | 'logger'>> = {
3920
clock: {
4021
now: () => Date.now(),
4122
sleep: async (ms: number) => {
@@ -115,39 +96,14 @@ export function createSelfClient({
11596
}
11697

11798
async function scanDocument(opts: ScanOpts & { signal?: AbortSignal }): Promise<ScanResult> {
118-
return _adapters.scanner.scan(opts);
119-
}
120-
121-
async function validateDocument(_input: ValidationInput): Promise<ValidationResult> {
122-
return { ok: false, reason: 'SELF_ERR_VALIDATION_STUB' };
123-
}
124-
125-
async function checkRegistration(_input: RegistrationInput): Promise<RegistrationStatus> {
126-
return { registered: false, reason: 'SELF_REG_STATUS_STUB' };
127-
}
128-
129-
async function registerDocument(_input: RegistrationInput): Promise<RegistrationStatus> {
130-
return { registered: false, reason: 'SELF_REG_STATUS_STUB' };
131-
}
99+
// Apply scanner timeout from config if no signal provided
100+
if (!opts.signal && cfg.timeouts.scanMs) {
101+
const controller = new AbortController();
102+
setTimeout(() => controller.abort(), cfg.timeouts.scanMs);
103+
return _adapters.scanner.scan({ ...opts, signal: controller.signal });
104+
}
132105

133-
async function generateProof(
134-
_req: ProofRequest,
135-
opts: {
136-
signal?: AbortSignal;
137-
onProgress?: (p: Progress) => void;
138-
timeoutMs?: number;
139-
} = {},
140-
): Promise<ProofHandle> {
141-
if (!adapters.network) throw notImplemented('network');
142-
if (!adapters.crypto) throw notImplemented('crypto');
143-
const timeoutMs = opts.timeoutMs ?? cfg.timeouts?.proofMs ?? defaultConfig.timeouts.proofMs;
144-
void _adapters.clock.sleep(timeoutMs!, opts.signal).then(() => emit(SdkEvents.ERROR, new Error('timeout')));
145-
return {
146-
id: 'stub',
147-
status: 'pending',
148-
result: async () => ({ ok: false, reason: 'SELF_ERR_PROOF_STUB' }),
149-
cancel: () => {},
150-
};
106+
return _adapters.scanner.scan(opts);
151107
}
152108

153109
async function trackEvent(event: string, payload?: TrackEventParams): Promise<void> {
@@ -177,13 +133,9 @@ export function createSelfClient({
177133

178134
return {
179135
scanDocument,
180-
validateDocument,
181136
trackEvent,
182137
getPrivateKey,
183138
hasPrivateKey,
184-
checkRegistration,
185-
registerDocument,
186-
generateProof,
187139
extractMRZInfo: parseMRZInfo,
188140
on,
189141
emit,

packages/mobile-sdk-alpha/src/config/defaults.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
import type { Config } from '../types/public';
66

77
export const defaultConfig: Required<Config> = {
8-
endpoints: { api: '', teeWs: '', artifactsCdn: '' },
9-
timeouts: { httpMs: 30000, wsMs: 60000, scanMs: 60000, proofMs: 120000 },
8+
timeouts: { scanMs: 60000 },
9+
// in future this can be used to enable/disable experimental features
1010
features: {},
11-
tlsPinning: { enabled: false },
1211
};

packages/mobile-sdk-alpha/src/config/merge.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ export function mergeConfig(base: Required<Config>, override: Config): Required<
88
return {
99
...base,
1010
...override,
11-
endpoints: { ...base.endpoints, ...(override.endpoints ?? {}) },
1211
timeouts: { ...base.timeouts, ...(override.timeouts ?? {}) },
1312
features: { ...base.features, ...(override.features ?? {}) },
14-
tlsPinning: { ...base.tlsPinning, ...(override.tlsPinning ?? {}) },
1513
};
1614
}

packages/mobile-sdk-alpha/src/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ export type {
1818
MRZValidation,
1919
NetworkAdapter,
2020
Progress,
21-
ProofHandle,
22-
ProofRequest,
23-
RegistrationInput,
24-
RegistrationStatus,
2521
ScanMode,
2622
ScanOpts,
2723
ScanResult,
@@ -30,8 +26,6 @@ export type {
3026
StorageAdapter,
3127
TrackEventParams,
3228
Unsubscribe,
33-
ValidationInput,
34-
ValidationResult,
3529
WsAdapter,
3630
WsConn,
3731
} from './types/public';

packages/mobile-sdk-alpha/src/types/events.ts

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,65 @@ import { DocumentCategory } from '@selfxyz/common/types';
77
import type { Progress } from './public';
88

99
export enum SdkEvents {
10+
/**
11+
* Emitted when an error occurs during SDK operations, including timeouts.
12+
*
13+
* **Required:** Handle this event to provide error feedback to users.
14+
* **Recommended:** Log errors for debugging and show appropriate user-friendly error messages.
15+
*/
1016
ERROR = 'ERROR',
17+
18+
/**
19+
* Emitted to provide progress updates during long-running operations.
20+
*
21+
* **Recommended:** Use this to show progress indicators or loading states to improve user experience.
22+
*/
1123
PROGRESS = 'PROGRESS',
12-
STATE = 'STATE',
1324

25+
/**
26+
* Emitted when no passport data is found on the device during initialization.
27+
*
28+
* **Required:** Navigate users to a document scanning/setup screen to capture their passport data.
29+
* **Recommended:** Provide clear instructions on how to scan and register their document properly.
30+
*/
1431
PROVING_PASSPORT_DATA_NOT_FOUND = 'PROVING_PASSPORT_DATA_NOT_FOUND',
32+
33+
/**
34+
* Emitted when identity verification completes successfully.
35+
*
36+
* **Required:** Show success confirmation to the user that their identity was verified.
37+
* **Recommended:** Navigate to your app's main screen or success page after a brief delay (3 seconds)
38+
* to allow users to see the success state.
39+
*/
1540
PROVING_ACCOUNT_VERIFIED_SUCCESS = 'PROVING_ACCOUNT_VERIFIED_SUCCESS',
41+
42+
/**
43+
* Emitted when document registration fails or encounters an error.
44+
*
45+
* **Required:** Handle navigation based on the `hasValidDocument` parameter:
46+
* - If `hasValidDocument` is `true`: Navigate to your app's home screen (user has other valid documents registered)
47+
* - If `hasValidDocument` is `false`: Navigate to launch/onboarding screen (user needs to register documents)
48+
* **Recommended:** Show appropriate error messages and implement a brief delay (3 seconds) before navigation.
49+
*/
1650
PROVING_REGISTER_ERROR_OR_FAILURE = 'PROVING_REGISTER_ERROR_OR_FAILURE',
51+
52+
/**
53+
* Emitted when a passport from an unsupported country or document type is detected during validation.
54+
*
55+
* **Required:** Inform users that their document is not currently supported.
56+
* **Recommended:** Navigate to an unsupported document screen showing the detected country code and
57+
* document category, and provide guidance on alternative verification methods if available.
58+
*/
1759
PROVING_PASSPORT_NOT_SUPPORTED = 'PROVING_PASSPORT_NOT_SUPPORTED',
60+
61+
/**
62+
* Emitted when account recovery is required because the passport was registered with different credentials.
63+
* This happens when a document's nullifier is found on-chain but not registered with the current user's secret.
64+
*
65+
* **Required:** Navigate users to an account recovery screen with recovery options or instructions if the have originally registered with a differnt self app.
66+
* **Recommended:** Explain that their passport was previously registered with different account credentials
67+
* and guide them through the recovery process to regain access.
68+
*/
1869
PROVING_ACCOUNT_RECOVERY_REQUIRED = 'PROVING_ACCOUNT_RECOVERY_REQUIRED',
1970
}
2071

@@ -31,7 +82,6 @@ export interface SDKEventMap {
3182
[SdkEvents.PROVING_ACCOUNT_RECOVERY_REQUIRED]: undefined;
3283

3384
[SdkEvents.PROGRESS]: Progress;
34-
[SdkEvents.STATE]: string;
3585
[SdkEvents.ERROR]: Error;
3686
}
3787

packages/mobile-sdk-alpha/src/types/public.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ import { SDKEvent, SDKEventMap } from './events';
99
export type { PassportValidationCallbacks } from '../validation/document';
1010
export type { DocumentCatalog, PassportData };
1111
export interface Config {
12-
endpoints?: { api?: string; teeWs?: string; artifactsCdn?: string };
1312
timeouts?: {
14-
httpMs?: number;
15-
wsMs?: number;
1613
scanMs?: number;
17-
proofMs?: number;
1814
};
1915
features?: Record<string, boolean>;
20-
tlsPinning?: { enabled: boolean; pins?: string[] };
2116
}
2217
export interface CryptoAdapter {
2318
hash(input: Uint8Array, algo?: 'sha256'): Promise<Uint8Array>;
@@ -107,35 +102,15 @@ export interface Adapters {
107102
documents: DocumentsAdapter;
108103
}
109104

110-
export interface ProofHandle {
111-
id: string;
112-
status: 'pending' | 'completed' | 'failed';
113-
result: () => Promise<{ ok: boolean; reason?: string }>;
114-
cancel: () => void;
115-
}
116105
export interface LoggerAdapter {
117106
log(level: LogLevel, message: string, fields?: Record<string, unknown>): void;
118107
}
119108

120-
export interface ProofRequest {
121-
type: 'register' | 'dsc' | 'disclose';
122-
payload: unknown;
123-
}
124109
export interface NetworkAdapter {
125110
http: HttpAdapter;
126111
ws: WsAdapter;
127112
}
128113

129-
export interface RegistrationInput {
130-
docId?: string;
131-
scan: ScanResult;
132-
}
133-
134-
export interface RegistrationStatus {
135-
registered: boolean;
136-
reason?: string;
137-
}
138-
139114
export type ScanMode = 'mrz' | 'nfc' | 'qr';
140115

141116
export type ScanOpts =
@@ -186,17 +161,6 @@ export interface DocumentsAdapter {
186161

187162
export interface SelfClient {
188163
scanDocument(opts: ScanOpts & { signal?: AbortSignal }): Promise<ScanResult>;
189-
validateDocument(input: ValidationInput): Promise<ValidationResult>;
190-
checkRegistration(input: RegistrationInput): Promise<RegistrationStatus>;
191-
registerDocument(input: RegistrationInput): Promise<RegistrationStatus>;
192-
generateProof(
193-
req: ProofRequest,
194-
opts?: {
195-
signal?: AbortSignal;
196-
onProgress?: (p: Progress) => void;
197-
timeoutMs?: number;
198-
},
199-
): Promise<ProofHandle>;
200164
extractMRZInfo(mrz: string): MRZInfo;
201165
trackEvent(event: string, payload?: TrackEventParams): void;
202166
getPrivateKey(): Promise<string | null>;
@@ -218,13 +182,6 @@ export interface StorageAdapter {
218182
set(key: string, value: string): Promise<void>;
219183
remove(key: string): Promise<void>;
220184
}
221-
export interface ValidationInput {
222-
scan: ScanResult;
223-
}
224-
export interface ValidationResult {
225-
ok: boolean;
226-
reason?: string;
227-
}
228185
export interface WsAdapter {
229186
connect(url: string, opts?: { signal?: AbortSignal; headers?: Record<string, string> }): WsConn;
230187
}

packages/mobile-sdk-alpha/tests/client-mrz.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ describe('createSelfClient API', () => {
1212
const client = createSelfClient({ config: {}, adapters: mockAdapters, listeners: new Map() });
1313

1414
expect(typeof client.extractMRZInfo).toBe('function');
15-
expect(typeof client.registerDocument).toBe('function');
16-
expect(typeof client.validateDocument).toBe('function');
1715
});
1816

1917
it('parses MRZ data correctly', () => {

packages/mobile-sdk-alpha/tests/client.test.ts

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ describe('createSelfClient', () => {
6464
});
6565
const result = await client.scanDocument({ mode: 'qr' });
6666
expect(result).toEqual({ mode: 'qr', data: 'self://ok' });
67-
expect(scanMock).toHaveBeenCalledWith({ mode: 'qr' });
67+
expect(scanMock).toHaveBeenCalledWith(
68+
expect.objectContaining({
69+
mode: 'qr',
70+
signal: expect.any(AbortSignal),
71+
}),
72+
);
6873
});
6974

7075
it('propagates scanner errors', async () => {
@@ -78,22 +83,6 @@ describe('createSelfClient', () => {
7883
await expect(client.scanDocument({ mode: 'qr' })).rejects.toBe(err);
7984
});
8085

81-
it('returns stub proof handle when adapters provided', async () => {
82-
const network = { http: { fetch: vi.fn() }, ws: { connect: vi.fn() } } as any;
83-
const crypto = { hash: vi.fn(), sign: vi.fn() } as any;
84-
const scanner = { scan: vi.fn() } as any;
85-
const client = createSelfClient({
86-
config: {},
87-
adapters: { network, crypto, scanner, documents, auth },
88-
listeners: new Map(),
89-
});
90-
const handle = await client.generateProof({ type: 'register', payload: {} });
91-
expect(handle.id).toBe('stub');
92-
expect(handle.status).toBe('pending');
93-
expect(await handle.result()).toEqual({ ok: false, reason: 'SELF_ERR_PROOF_STUB' });
94-
expect(() => handle.cancel()).not.toThrow();
95-
});
96-
9786
it('emits and unsubscribes events', () => {
9887
const listeners = createListenersMap();
9988

@@ -144,17 +133,6 @@ describe('createSelfClient', () => {
144133
expect(info.validation?.overall).toBe(true);
145134
});
146135

147-
it('returns stub registration status', async () => {
148-
const client = createSelfClient({
149-
config: {},
150-
adapters: { scanner, network, crypto, documents, auth },
151-
listeners: new Map(),
152-
});
153-
await expect(client.registerDocument({} as any)).resolves.toEqual({
154-
registered: false,
155-
reason: 'SELF_REG_STATUS_STUB',
156-
});
157-
});
158136
describe('when analytics adapter is given', () => {
159137
it('calls that adapter for trackEvent', () => {
160138
const trackEvent = vi.fn();

0 commit comments

Comments
 (0)