Skip to content

Commit

Permalink
refactor app code, dscSecret gen and mockpassport gen
Browse files Browse the repository at this point in the history
  • Loading branch information
remicolin committed Aug 27, 2024
1 parent c7c93d9 commit 0ba5933
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 82 deletions.
22 changes: 3 additions & 19 deletions app/src/screens/MockDataScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { bgGreen, textBlack } from '../utils/colors';
import useUserStore from '../stores/userStore';
import useNavigationStore from '../stores/navigationStore';
import CustomButton from '../components/CustomButton';
import { mock_dsc_sha256_rsa_4096, mock_dsc_sha1_rsa_4096, mock_dsc_sha256_rsapss_4096 } from '../../../common/src/constants/mockCertificates';
import DatePicker from 'react-native-date-picker';
import { genMockPassportData } from '../../../common/scripts/passportData/genMockPassportData';
import { countryCodes } from '../../../common/src/constants/constants';
Expand All @@ -26,28 +25,13 @@ const MockDataScreen: React.FC = () => {
const castDate = (date: Date) => {
return (date.toISOString().slice(2, 4) + date.toISOString().slice(5, 7) + date.toISOString().slice(8, 10)).toString();
}
const getDSC = () => {
switch (signatureAlgorithm) {
case "rsa sha1":
return mock_dsc_sha1_rsa_4096
case "rsa sha256":
return mock_dsc_sha256_rsa_4096
case "rsapss sha256":
return mock_dsc_sha256_rsapss_4096
default:
return mock_dsc_sha256_rsa_4096
}
}

const handleGenerate = () => {

let mockPassportData = genMockPassportData(signatureAlgorithm as "rsa sha256" | "rsa sha1" | "rsapss sha256", nationality, castDate(dateOfBirthDatePicker), castDate(dateOfExpiryDatePicker));
const dsc = getDSC()
console.log(mockPassportData)
console.log(dsc)
mockPassportData.dsc = dsc
const mockPassportData = genMockPassportData(signatureAlgorithm as "rsa sha256" | "rsa sha1" | "rsapss sha256", nationality, castDate(dateOfBirthDatePicker), castDate(dateOfExpiryDatePicker));
useUserStore.getState().registerPassportData(mockPassportData)
useUserStore.getState().setRegistered(true);
const sigAlgName = getSignatureAlgorithm(dsc);
const sigAlgName = getSignatureAlgorithm(mockPassportData.dsc as string);
const circuitName = getCircuitName("prove", sigAlgName.signatureAlgorithm, sigAlgName.hashFunction);
downloadZkey(circuitName as any);
useNavigationStore.getState().setSelectedTab("app");
Expand Down
9 changes: 2 additions & 7 deletions app/src/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { sendRegisterTransaction } from '../utils/transactions';
import { loadPassportData, loadSecret, loadSecretOrCreateIt, storePassportData } from '../utils/keychain';
import { ethers } from 'ethers';
import { isCommitmentRegistered } from '../utils/registration';
import { generateDscSecret } from '../../../common/src/utils/csca';


interface UserState {
Expand All @@ -28,7 +29,6 @@ interface UserState {
registered: boolean
passportData: PassportData
secret: string
dscCertificate: any
cscaProof: Proof | null
localProof: Proof | null
dscSecret: string | null
Expand Down Expand Up @@ -57,7 +57,6 @@ const useUserStore = create<UserState>((set, get) => ({
registered: false,
passportData: mockPassportData_sha256_rsa_65537,
secret: "",
dscCertificate: null,
cscaProof: null,
localProof: null,
setRegistered: (registered: boolean) => {
Expand Down Expand Up @@ -135,7 +134,6 @@ const useUserStore = create<UserState>((set, get) => ({
console.log("registerCommitment")
const {
toast,
setStep,
update: updateNavigationStore,
} = useNavigationStore.getState();
const secret = await loadSecret() as string;
Expand All @@ -161,10 +159,7 @@ const useUserStore = create<UserState>((set, get) => ({

try {
if (get().dscSecret === null) {
console.log("DSC secret is not set, generating a new one");
const secretBytes = forge.random.getBytesSync(31);
dsc_secret = BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString();
console.log('Generated secret:', dsc_secret.toString());
dsc_secret = generateDscSecret();
get().setDscSecret(dsc_secret);
}
const inputs = generateCircuitInputsRegister(
Expand Down
52 changes: 0 additions & 52 deletions app/src/utils/nfcScanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import { Buffer } from 'buffer';
import * as amplitude from '@amplitude/analytics-react-native';
import useUserStore from '../stores/userStore';
import useNavigationStore from '../stores/navigationStore';
import { k_csca, k_dsc, max_cert_bytes, n_csca, n_dsc } from '../../../common/src/constants/constants';
import { getCSCAInputs } from '../../../common/src/utils/csca';
import { sendCSCARequest } from './cscaRequest';
import { getSignatureAlgorithm, getCircuitName } from '../../../common/src/utils/handleCertificate';
import { downloadZkey } from './zkeyDownload';

Expand Down Expand Up @@ -54,7 +51,6 @@ const scanAndroid = async (setModalProofStep: (modalProofStep: number) => void)
passportNumber,
dateOfBirth,
dateOfExpiry,
dscCertificate
} = useUserStore.getState()
const { toast, setNfcSheetIsOpen } = useNavigationStore.getState();
setNfcSheetIsOpen(true);
Expand Down Expand Up @@ -172,32 +168,9 @@ const handleResponseIOS = async (
photoBase64: "data:image/jpeg;base64," + parsed.passportPhoto,
};
useUserStore.getState().registerPassportData(passportData)

let secret = useUserStore.getState().dscSecret;
if (secret === null) {
// Finally, generate CSCA Inputs and request modal server
// Generate a cryptographically secure random secret of (31 bytes)
const secretBytes = forge.random.getBytesSync(31);
secret = BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString();
console.log('Generated secret:', secret.toString());
useUserStore.getState().setDscSecret(secret);
}
const sigAlgName = getSignatureAlgorithm(pem);
const circuitName = getCircuitName("prove", sigAlgName.signatureAlgorithm, sigAlgName.hashFunction);
downloadZkey(circuitName as any);
// const inputs_csca = getCSCAInputs(
// secret as string,
// certificate,
// null,
// n_dsc,
// k_dsc,
// n_csca,
// k_csca,
// max_cert_bytes,
// false
// );

//sendCSCARequest(inputs_csca, setModalProofStep);
useNavigationStore.getState().setSelectedTab("next");
} catch (e: any) {
console.log('error during parsing:', e);
Expand Down Expand Up @@ -280,33 +253,8 @@ const handleResponseAndroid = async (
console.log("documentSigningCertificate", documentSigningCertificate)
useUserStore.getState().registerPassportData(passportData)

// Finally request the Modal server to verify the DSC certificate
const certificate = forge.pki.certificateFromPem(documentSigningCertificate);
useUserStore.getState().dscCertificate = certificate;

let secret = useUserStore.getState().dscSecret;
if (secret === null) {
// Finally, generate CSCA Inputs and request modal server
// Generate a cryptographically secure random secret of (31 bytes)
const secretBytes = forge.random.getBytesSync(31);
secret = BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString();
console.log('Generated secret:', secret.toString());
useUserStore.getState().setDscSecret(secret);
}
const sigAlgName = getSignatureAlgorithm(pem);
const circuitName = getCircuitName("prove", sigAlgName.signatureAlgorithm, sigAlgName.hashFunction);
downloadZkey(circuitName as any);
// const inputs_csca = getCSCAInputs(
// secret as string,
// certificate,
// null,
// n_dsc,
// k_dsc,
// n_csca,
// k_csca,
// max_cert_bytes,
// false
// );
//sendCSCARequest(inputs_csca, setModalProofStep);
useNavigationStore.getState().setSelectedTab("next");
};
12 changes: 9 additions & 3 deletions common/scripts/passportData/genMockPassportData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { PassportData } from "../../src/utils/types";
import { hash, assembleEContent, formatAndConcatenateDataHashes, formatMrz, hexToDecimal } from "../../src/utils/utils";
import * as forge from 'node-forge';
import * as rs from 'jsrsasign';
import { mock_dsc_key_sha1_rsa_4096, mock_dsc_key_sha256_rsa_4096, mock_dsc_key_sha256_rsapss_2048, mock_dsc_sha256_rsapss_2048 } from "../../src/constants/mockCertificates";
import { mock_dsc_key_sha1_rsa_4096, mock_dsc_key_sha256_rsa_4096, mock_dsc_key_sha256_rsapss_2048, mock_dsc_key_sha256_rsapss_4096, mock_dsc_sha1_rsa_4096, mock_dsc_sha256_rsa_4096, mock_dsc_sha256_rsapss_2048, mock_dsc_sha256_rsapss_4096 } from "../../src/constants/mockCertificates";
import { sampleDataHashes_rsa_sha1, sampleDataHashes_rsa_sha256, sampleDataHashes_rsapss_sha256 } from "./sampleDataHashes";

export function genMockPassportData(
Expand All @@ -18,25 +18,29 @@ export function genMockPassportData(
let hashLen: number;
let sampleDataHashes: [number, number[]][];
let privateKeyPem: string;
let dsc: string;

switch (signatureType) {
case 'rsa sha1':
signatureAlgorithm = 'sha1WithRSAEncryption';
hashLen = 20;
sampleDataHashes = sampleDataHashes_rsa_sha1;
privateKeyPem = mock_dsc_key_sha1_rsa_4096;
dsc = mock_dsc_sha1_rsa_4096;
break;
case 'rsa sha256':
signatureAlgorithm = 'sha256WithRSAEncryption';
hashLen = 32;
sampleDataHashes = sampleDataHashes_rsa_sha256;
privateKeyPem = mock_dsc_key_sha256_rsa_4096;
dsc = mock_dsc_sha256_rsa_4096;
break;
case 'rsapss sha256':
signatureAlgorithm = 'sha256WithRSASSAPSS';
hashLen = 32;
sampleDataHashes = sampleDataHashes_rsapss_sha256;
privateKeyPem = mock_dsc_key_sha256_rsapss_2048;
privateKeyPem = mock_dsc_key_sha256_rsapss_4096;
dsc = mock_dsc_sha256_rsapss_4096;
break;
}

Expand Down Expand Up @@ -74,6 +78,7 @@ export function genMockPassportData(
const signatureBytes = Array.from(signature, byte => byte < 128 ? byte : byte - 256);

return {
dsc: dsc,
mrz: mrz,
signatureAlgorithm: signatureAlgorithm,
pubKey: {
Expand All @@ -85,4 +90,5 @@ export function genMockPassportData(
encryptedDigest: signatureBytes,
photoBase64: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIy3..."
};
}
}

7 changes: 6 additions & 1 deletion common/src/utils/csca.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,9 @@ export const sendCSCARequest = async (inputs_csca: any): Promise<any> => {
}
throw error;
}
};
};

export const generateDscSecret = () => {
const secretBytes = forge.random.getBytesSync(31);
return BigInt(`0x${forge.util.bytesToHex(secretBytes)}`).toString();
}

0 comments on commit 0ba5933

Please sign in to comment.