mirror of
https://github.com/selfxyz/self.git
synced 2026-01-14 09:08:03 -05:00
* Redo "Mobile SDK: move provingMachine from the app (#1052)" (#1084)"
This reverts commit 3397fcf43b. which reverted merging proving machine migration
* fix build
* lint fix
* fix imports
* pr suggestions
* make sure not to create multiple instances of stores
* WIP: don't expose useSelfAppStore directly in the public API
* Update packages/mobile-sdk-alpha/src/proving/provingMachine.ts
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* Apply suggestions from code review
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
* dont call protocol store directly
* fix protocol store tests
* fix deeplinks test
* fix web build and exports
* test fixes
* keep reactivity
* remove file name
* WIP: expose stores through SelfClient only
* move protocolStore usage behind SelfClient
* fix deeplinks tests
* lint
* fix provingMachine tests
* remove provingStore from browser exports
* lint
* lint
* fix provingMachine.generatePayload tests
* fix provingMachine.startFetchingData tests
* fix more tests
* remove not exported
* fix more tests
* remove unused
* simplify getAltCSCA signature (fix build?)
* yarn lint
* final touches
---------
Co-authored-by: Leszek Stachowski <leszek.stachowski@self.xyz>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
261 lines
8.1 KiB
TypeScript
261 lines
8.1 KiB
TypeScript
// SPDX-FileCopyrightText: 2025 Social Connect Labs, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
|
|
|
|
import type { ProvingStateType } from '@selfxyz/mobile-sdk-alpha';
|
|
|
|
import type { PassportMetadata } from '@/utils/proving/loadingScreenStateText';
|
|
import {
|
|
getLoadingScreenText,
|
|
getProvingTimeEstimate,
|
|
} from '@/utils/proving/loadingScreenStateText';
|
|
|
|
describe('stateLoadingScreenText', () => {
|
|
// Default metadata for basic tests
|
|
const defaultMetadata: PassportMetadata = {
|
|
signatureAlgorithm: 'RSA',
|
|
curveOrExponent: '',
|
|
};
|
|
|
|
// Helper function to test a state has a response
|
|
const testStateHasResponse = (state: ProvingStateType) => {
|
|
it(`should return a response for ${state} state`, () => {
|
|
const result = getLoadingScreenText(state, defaultMetadata);
|
|
expect(result).toBeDefined();
|
|
expect(result.actionText).toBeDefined();
|
|
expect(result.actionText.length).toBeGreaterThan(0);
|
|
expect(result.estimatedTime).toBeDefined();
|
|
expect(result.estimatedTime.length).toBeGreaterThan(0);
|
|
});
|
|
};
|
|
|
|
// Test all possible states
|
|
const states: ProvingStateType[] = [
|
|
'account_recovery_choice',
|
|
'completed',
|
|
'error',
|
|
'failure',
|
|
'fetching_data',
|
|
'idle',
|
|
'init_tee_connexion',
|
|
'listening_for_status',
|
|
'passport_data_not_found',
|
|
'passport_not_supported',
|
|
'post_proving',
|
|
'proving',
|
|
'ready_to_prove',
|
|
'validating_document',
|
|
];
|
|
|
|
describe('All states should have a response', () => {
|
|
states.forEach(state => {
|
|
testStateHasResponse(state);
|
|
});
|
|
});
|
|
|
|
// Test edge cases
|
|
describe('Edge cases', () => {
|
|
it('should handle undefined state', () => {
|
|
const result = getLoadingScreenText(
|
|
undefined as ProvingStateType,
|
|
defaultMetadata,
|
|
);
|
|
expect(result).toBeDefined();
|
|
expect(result.actionText).toBeDefined();
|
|
expect(result.estimatedTime).toBeDefined();
|
|
});
|
|
|
|
it('should handle unknown state', () => {
|
|
const result = getLoadingScreenText(
|
|
'unknown' as ProvingStateType,
|
|
defaultMetadata,
|
|
);
|
|
expect(result).toBeDefined();
|
|
expect(result.actionText).toBeDefined();
|
|
expect(result.estimatedTime).toBeDefined();
|
|
});
|
|
|
|
it('should handle undefined metadata', () => {
|
|
const result = getLoadingScreenText('proving', undefined);
|
|
expect(result).toBeDefined();
|
|
expect(result.actionText).toBeDefined();
|
|
expect(result.estimatedTime).toBe('30 - 90 SECONDS'); // Should use default time estimate
|
|
});
|
|
});
|
|
|
|
describe('getLoadingScreenText with passport metadata', () => {
|
|
const rsaMetadata: PassportMetadata = {
|
|
signatureAlgorithm: 'RSA',
|
|
curveOrExponent: '65537',
|
|
};
|
|
|
|
it('should use algorithm information to estimate proving time', () => {
|
|
const result = getLoadingScreenText(
|
|
'proving',
|
|
rsaMetadata.signatureAlgorithm,
|
|
rsaMetadata.curveOrExponent,
|
|
);
|
|
|
|
// Should use RSA (4 SECONDS)
|
|
expect(result.estimatedTime).toBe('4 SECONDS');
|
|
});
|
|
});
|
|
|
|
describe('getProvingTimeEstimate', () => {
|
|
it('should return default time when parameters are undefined', () => {
|
|
const result = getProvingTimeEstimate('', '', 'register');
|
|
expect(result).toBe('30 - 90 SECONDS');
|
|
});
|
|
|
|
describe('RSA algorithms', () => {
|
|
it.each([
|
|
['RSA', '65537', 'register', '4 SECONDS'], // Common RSA exponent
|
|
['RSA', '3', 'register', '4 SECONDS'], // Another common RSA exponent
|
|
['RSA', '65537', 'dsc', '2 SECONDS'], // DSC proof
|
|
['RSA', '3', 'dsc', '2 SECONDS'], // DSC proof
|
|
])(
|
|
'should return correct time for %s with exponent %s and type %s',
|
|
(algorithm, exponent, type, expectedTime) => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: algorithm,
|
|
curveOrExponent: exponent,
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
type as 'dsc' | 'register',
|
|
);
|
|
expect(result).toBe(expectedTime);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
['RSAPSS', '65537', 'register', '6 SECONDS'],
|
|
['RSAPSS', '3', 'register', '6 SECONDS'],
|
|
['RSAPSS', '65537', 'dsc', '3 SECONDS'],
|
|
['RSAPSS', '3', 'dsc', '3 SECONDS'],
|
|
])(
|
|
'should return correct time for %s with exponent %s and type %s',
|
|
(algorithm, exponent, type, expectedTime) => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: algorithm,
|
|
curveOrExponent: exponent,
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
type as 'dsc' | 'register',
|
|
);
|
|
expect(result).toBe(expectedTime);
|
|
},
|
|
);
|
|
});
|
|
|
|
describe('ECDSA curves', () => {
|
|
it.each([
|
|
['secp224r1', 'register', '50 SECONDS'],
|
|
['brainpoolP224r1', 'register', '50 SECONDS'],
|
|
['secp224r1', 'dsc', '25 SECONDS'],
|
|
['brainpoolP224r1', 'dsc', '25 SECONDS'],
|
|
])(
|
|
'should return correct time for 224-bit curve %s with type %s',
|
|
(curve, type, expectedTime) => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: 'ECDSA',
|
|
curveOrExponent: curve,
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
type as 'dsc' | 'register',
|
|
);
|
|
expect(result).toBe(expectedTime);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
['secp256r1', 'register', '50 SECONDS'],
|
|
['brainpoolP256r1', 'register', '50 SECONDS'],
|
|
['secp256r1', 'dsc', '25 SECONDS'],
|
|
['brainpoolP256r1', 'dsc', '25 SECONDS'],
|
|
])(
|
|
'should return correct time for 256-bit curve %s with type %s',
|
|
(curve, type, expectedTime) => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: 'ECDSA',
|
|
curveOrExponent: curve,
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
type as 'dsc' | 'register',
|
|
);
|
|
expect(result).toBe(expectedTime);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
['secp384r1', 'register', '90 SECONDS'],
|
|
['brainpoolP384r1', 'register', '90 SECONDS'],
|
|
['secp384r1', 'dsc', '45 SECONDS'],
|
|
['brainpoolP384r1', 'dsc', '45 SECONDS'],
|
|
])(
|
|
'should return correct time for 384-bit curve %s with type %s',
|
|
(curve, type, expectedTime) => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: 'ECDSA',
|
|
curveOrExponent: curve,
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
type as 'dsc' | 'register',
|
|
);
|
|
expect(result).toBe(expectedTime);
|
|
},
|
|
);
|
|
|
|
it.each([
|
|
['secp521r1', 'register', '200 SECONDS'],
|
|
['brainpoolP512r1', 'register', '200 SECONDS'],
|
|
['secp521r1', 'dsc', '100 SECONDS'],
|
|
['brainpoolP512r1', 'dsc', '100 SECONDS'],
|
|
])(
|
|
'should return correct time for 512/521-bit curve %s with type %s',
|
|
(curve, type, expectedTime) => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: 'ECDSA',
|
|
curveOrExponent: curve,
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
type as 'dsc' | 'register',
|
|
);
|
|
expect(result).toBe(expectedTime);
|
|
},
|
|
);
|
|
});
|
|
|
|
it('should return default time when algorithm is not recognized', () => {
|
|
const metadata: PassportMetadata = {
|
|
signatureAlgorithm: 'UNKNOWN_ALGORITHM',
|
|
curveOrExponent: '',
|
|
};
|
|
|
|
const result = getProvingTimeEstimate(
|
|
metadata.signatureAlgorithm,
|
|
metadata.curveOrExponent,
|
|
'register',
|
|
);
|
|
expect(result).toBe('30 - 90 SECONDS');
|
|
});
|
|
});
|
|
});
|