Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 17 additions & 2 deletions app/src/screens/prove/ConfirmBelongingScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ActivityIndicator, View } from 'react-native';
import type { StaticScreenProps } from '@react-navigation/native';
import { usePreventRemove } from '@react-navigation/native';

import { useSelfClient } from '@selfxyz/mobile-sdk-alpha';
import { loadSelectedDocument, useSelfClient } from '@selfxyz/mobile-sdk-alpha';
import {
PassportEvents,
ProofEvents,
Expand Down Expand Up @@ -46,7 +46,22 @@ const ConfirmBelongingScreen: React.FC<ConfirmBelongingScreenProps> = () => {
const isReadyToProve = currentState === 'ready_to_prove';
useEffect(() => {
notificationSuccess();
init(selfClient, 'dsc');

const initializeProving = async () => {
try {
const selectedDocument = await loadSelectedDocument(selfClient);
if (selectedDocument?.data?.documentCategory === 'aadhaar') {
init(selfClient, 'register');
} else {
init(selfClient, 'dsc');
}
} catch (error) {
console.error('Error loading selected document:', error);
init(selfClient, 'dsc');
}
};

initializeProving();
}, [init, selfClient]);

const onOkPress = async () => {
Expand Down
62 changes: 41 additions & 21 deletions app/src/utils/proving/provingMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
getPayload,
getWSDbRelayerUrl,
} from '@selfxyz/common/utils/proving';
import type { IDDocument } from '@selfxyz/common/utils/types';
import type { SelfClient } from '@selfxyz/mobile-sdk-alpha';
import {
clearPassportData,
Expand Down Expand Up @@ -72,10 +73,20 @@ const getMappingKey = (
documentCategory: DocumentCategory,
): string => {
if (circuitType === 'disclose') {
return documentCategory === 'passport' ? 'DISCLOSE' : 'DISCLOSE_ID';
if (documentCategory === 'passport') return 'DISCLOSE';
if (documentCategory === 'id_card') return 'DISCLOSE_ID';
if (documentCategory === 'aadhaar') return 'DISCLOSE_AADHAAR';
throw new Error(
`Unsupported document category for disclose: ${documentCategory}`,
);
}
if (circuitType === 'register') {
return documentCategory === 'passport' ? 'REGISTER' : 'REGISTER_ID';
if (documentCategory === 'passport') return 'REGISTER';
if (documentCategory === 'id_card') return 'REGISTER_ID';
if (documentCategory === 'aadhaar') return 'REGISTER_AADHAAR';
throw new Error(
`Unsupported document category for register: ${documentCategory}`,
);
}
// circuitType === 'dsc'
return documentCategory === 'passport' ? 'DSC' : 'DSC_ID';
Expand All @@ -95,10 +106,10 @@ const resolveWebSocketUrl = (
};

// Helper functions for _generatePayload refactoring
const _generateCircuitInputs = (
const _generateCircuitInputs = async (
circuitType: 'disclose' | 'register' | 'dsc',
secret: string | undefined | null,
passportData: PassportData,
passportData: IDDocument,
env: 'prod' | 'stg',
) => {
const document: DocumentCategory = passportData.documentCategory;
Expand All @@ -114,17 +125,19 @@ const _generateCircuitInputs = (
switch (circuitType) {
case 'register':
({ inputs, circuitName, endpointType, endpoint } =
generateTEEInputsRegister(
await generateTEEInputsRegister(
secret as string,
passportData,
protocolStore[document].dsc_tree,
document === 'aadhaar'
? protocolStore[document].public_keys
: protocolStore[document].dsc_tree,
env,
));
circuitTypeWithDocumentExtension = `${circuitType}${document === 'passport' ? '' : '_id'}`;
break;
case 'dsc':
({ inputs, circuitName, endpointType, endpoint } = generateTEEInputsDSC(
passportData,
passportData as PassportData,
protocolStore[document].csca_tree as string[][],
env,
));
Expand Down Expand Up @@ -154,7 +167,10 @@ const _generateCircuitInputs = (
}
},
));
circuitTypeWithDocumentExtension = `disclose`;
circuitTypeWithDocumentExtension = getCircuitTypeWithDocumentExtension(
circuitType,
document,
);
break;
default:
throw new Error('Invalid circuit type:' + circuitType);
Expand Down Expand Up @@ -1099,8 +1115,10 @@ export const useProvingStore = create<ProvingState>((set, get) => {
secret as string,
{
getCommitmentTree,
getAltCSCA: docType =>
useProtocolStore.getState()[docType].alternative_csca,
getAltCSCA: (docType: DocumentCategory) =>
docType === 'aadhaar'
? useProtocolStore.getState().aadhaar.public_keys
: useProtocolStore.getState()[docType].alternative_csca,
},
);
logProofEvent(
Expand Down Expand Up @@ -1150,16 +1168,18 @@ export const useProvingStore = create<ProvingState>((set, get) => {
return;
}
const document: DocumentCategory = passportData.documentCategory;
const isDscRegistered = await checkIfPassportDscIsInTree(
passportData,
useProtocolStore.getState()[document].dsc_tree,
);
logProofEvent('info', 'DSC tree check', context, {
dsc_registered: isDscRegistered,
});
if (isDscRegistered) {
selfClient.trackEvent(ProofEvents.DSC_IN_TREE);
set({ circuitType: 'register' });
if (document === 'passport' || document === 'id_card') {
const isDscRegistered = await checkIfPassportDscIsInTree(
passportData,
useProtocolStore.getState()[document].dsc_tree,
);
logProofEvent('info', 'DSC tree check', context, {
dsc_registered: isDscRegistered,
});
if (isDscRegistered) {
selfClient.trackEvent(ProofEvents.DSC_IN_TREE);
set({ circuitType: 'register' });
}
}
logProofEvent('info', 'Validation succeeded', context, {
duration_ms: Date.now() - startTime,
Expand Down Expand Up @@ -1448,7 +1468,7 @@ export const useProvingStore = create<ProvingState>((set, get) => {
endpointType,
endpoint,
circuitTypeWithDocumentExtension,
} = _generateCircuitInputs(
} = await _generateCircuitInputs(
circuitType as 'disclose' | 'register' | 'dsc',
secret,
passportData,
Expand Down
22 changes: 22 additions & 0 deletions common/src/utils/aadhaar/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export const createCustomV2TestData = ({
photo,
name,
timestamp,
aadhaarLast4Digits,
}: {
signedData: Uint8Array;
dob?: string;
Expand All @@ -103,6 +104,7 @@ export const createCustomV2TestData = ({
photo?: boolean;
name?: string;
timestamp?: string;
aadhaarLast4Digits?: string;
}) => {
const allDataParsed: number[][] = [];
const delimiterIndices: number[] = [];
Expand All @@ -123,6 +125,18 @@ export const createCustomV2TestData = ({
}
}

console.log('createCustomV2TestData', {
signedData,
dob,
pincode,
gender,
state,
photo,
name,
timestamp,
aadhaarLast4Digits,
});

// Set new timestamp to the time of the signature
const newDateString = returnNewDateString(timestamp);
const newTimestamp = new TextEncoder().encode(newDateString);
Expand Down Expand Up @@ -175,6 +189,14 @@ export const createCustomV2TestData = ({
);
}

if (!aadhaarLast4Digits) {

for (let i = 2; i < 6; i++) {
modifiedSignedData[i] = Math.floor(Math.random() * 10) + 48;
}

}

if (name) {
const newName = new TextEncoder().encode(name);
modifiedSignedData = replaceBytesBetween(
Expand Down
16 changes: 12 additions & 4 deletions common/src/utils/circuits/circuitsName.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { PassportData } from '../types.js';
import type { IDDocument, PassportData } from '../types.js';

export function getCircuitNameFromPassportData(
passportData: PassportData,
passportData: IDDocument,
circuitType: 'register' | 'dsc'
) {
if (circuitType === 'register') {
Expand All @@ -11,9 +11,13 @@ export function getCircuitNameFromPassportData(
}
}

function getDSCircuitNameFromPassportData(passportData: PassportData) {
function getDSCircuitNameFromPassportData(passportData: IDDocument) {
console.log('Getting DSC circuit name from passport data...');

if (passportData.documentCategory === 'aadhaar') {
throw new Error('Aadhaar does not have a DSC circuit');
}

if (!passportData.passportMetadata) {
console.error('Passport metadata is missing');
throw new Error('Passport data are not parsed');
Expand Down Expand Up @@ -76,9 +80,13 @@ function getDSCircuitNameFromPassportData(passportData: PassportData) {
}
}

function getRegisterNameFromPassportData(passportData: PassportData) {
function getRegisterNameFromPassportData(passportData: IDDocument) {
console.log('Getting register circuit name from passport data...');

if (passportData.documentCategory === 'aadhaar') {
return 'register_aadhaar';
}

if (!passportData.passportMetadata) {
console.error('Passport metadata is missing');
throw new Error('Passport data are not parsed');
Expand Down
Loading
Loading