From 9101f6a05d1ff125a47cc2bf2b4527f65beb425e Mon Sep 17 00:00:00 2001 From: 0xturboblitz Date: Sun, 2 Jun 2024 18:54:24 +0200 Subject: [PATCH] delete prev file, add tutorial --- circuits/scripts/genMockPassportData.ts | 126 ------------------ .../scripts/genMockPassportData/README.md | 9 ++ common/src/utils/utils.ts | 19 +-- 3 files changed, 19 insertions(+), 135 deletions(-) delete mode 100644 circuits/scripts/genMockPassportData.ts create mode 100644 circuits/scripts/genMockPassportData/README.md diff --git a/circuits/scripts/genMockPassportData.ts b/circuits/scripts/genMockPassportData.ts deleted file mode 100644 index 9e978636..00000000 --- a/circuits/scripts/genMockPassportData.ts +++ /dev/null @@ -1,126 +0,0 @@ -import { PassportData } from "../../common/src/utils/types"; -import { hash, assembleEContent, formatAndConcatenateDataHashes, formatMrz, hexToDecimal } from "../../common/src/utils/utils"; -import * as forge from 'node-forge'; - -const sampleMRZ = "P c.charCodeAt(0)); - - return { - mrz: sampleMRZ, - signatureAlgorithm: signatureAlgorithm, - pubKey: { - modulus: hexToDecimal(modulus), - exponent: '65537', - }, - dataGroupHashes: concatenatedDataHashes, - eContent: eContent, - encryptedDigest: signatureBytes, - photoBase64: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIw3..." - } -} - -export function genMockPassportData_sha1WithRSAEncryption(): PassportData { - const signatureAlgorithm = 'sha1WithRSAEncryption' - const mrzHash = hash(signatureAlgorithm, formatMrz(sampleMRZ)); - sampleDataHashes_160.unshift([1, mrzHash]); - const concatenatedDataHashes = formatAndConcatenateDataHashes( - mrzHash, - sampleDataHashes_160 as [number, number[]][], - ); - - const eContent = assembleEContent( - hash(signatureAlgorithm, concatenatedDataHashes), - sampleTimeOfSig, - ); - - const rsa = forge.pki.rsa; - const privKey = rsa.generateKeyPair({ bits: 2048 }).privateKey; - const modulus = privKey.n.toString(16); - - const md = forge.md.sha1.create(); - md.update(forge.util.binary.raw.encode(new Uint8Array(eContent))); - - const signature = privKey.sign(md) - const signatureBytes = Array.from(signature, (c: string) => c.charCodeAt(0)); - - return { - mrz: sampleMRZ, - signatureAlgorithm: signatureAlgorithm, - pubKey: { - modulus: hexToDecimal(modulus), - exponent: '65537', - }, - dataGroupHashes: concatenatedDataHashes, - eContent: eContent, - encryptedDigest: signatureBytes, - photoBase64: "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABjElEQVR42mL8//8/AyUYiBQYmIw3..." - } -} - -console.log(JSON.stringify(genMockPassportData_sha256WithRSAEncryption_65537(), null, 2)); \ No newline at end of file diff --git a/circuits/scripts/genMockPassportData/README.md b/circuits/scripts/genMockPassportData/README.md new file mode 100644 index 00000000..79b6c752 --- /dev/null +++ b/circuits/scripts/genMockPassportData/README.md @@ -0,0 +1,9 @@ +# How to generate mock passport data based on your real data? + +- Build the app and scan your passport to log your passport data. +- Copy one of the files of this folder and paste your passport data. +- Adapt the `verify` function to verify it. Once this is done, adapt the `genMockPassportData` to generate a mock one. +- Once the mock passport data generated is verified correctly by the same `verify` function that verifies yours, you're all set! +- Run the script to generate a mock passport data and add it to `common/src/utils/mockPassportData.ts` +- Do a PR +- DM us to collect your bounty! \ No newline at end of file diff --git a/common/src/utils/utils.ts b/common/src/utils/utils.ts index 4e760f1d..48fbf5e6 100644 --- a/common/src/utils/utils.ts +++ b/common/src/utils/utils.ts @@ -1,7 +1,6 @@ import { LeanIMT } from '@zk-kit/lean-imt'; -import { assert } from './shaPad'; import { sha256 } from 'js-sha256'; -//import {sha1} from 'js-sha1'; +import { sha1 } from 'js-sha1'; export function formatMrz(mrz: string) { const mrzCharcodes = [...mrz].map(char => char.charCodeAt(0)); @@ -63,7 +62,6 @@ export function formatAndConcatenateDataHashes( export function assembleEContent( messageDigest: number[], - timeOfSignature: number[], ) { const constructedEContent = []; @@ -82,8 +80,8 @@ export function assembleEContent( constructedEContent.push( ...[48, 28, 6, 9, 42, -122, 72, -122, -9, 13, 1, 9, 5], ); - // time of the signature - constructedEContent.push(...timeOfSignature); + // mock time of signature + constructedEContent.push(...[49, 15, 23, 13, 49, 57, 49, 50, 49, 54, 49, 55, 50, 50, 51, 56, 90]); // 1.2.840.113549.1.9.4 is RFC_3369_MESSAGE_DIGEST_OID constructedEContent.push( ...[48, 47, 6, 9, 42, -122, 72, -122, -9, 13, 1, 9, 4], @@ -150,9 +148,12 @@ export function hexToDecimal(hex: string): string { // hash logic here because the one in utils.ts only works with node export function hash(signatureAlgorithm: string, bytesArray: number[]) { - let unsignedBytesArray = bytesArray.map(toUnsignedByte); - let hash = (signatureAlgorithm == 'sha1WithRSAEncryption') ? - sha1(unsignedBytesArray) : sha256(unsignedBytesArray); + const unsignedBytesArray = bytesArray.map(toUnsignedByte); + const hash = (signatureAlgorithm == 'sha1WithRSAEncryption') + ? sha1(unsignedBytesArray) + : (signatureAlgorithm == 'sha256WithRSAEncryption' || signatureAlgorithm == 'rsassaPss') + ? sha256(unsignedBytesArray) + : sha256(unsignedBytesArray); //defaults to sha256 return hexToSignedBytes(hash); } @@ -260,4 +261,4 @@ export function generateMerkleProof(imt: LeanIMT, _index: number, maxDepth: numb } } return { merkleProofSiblings, merkleProofIndices, depthForThisOne } -} +} \ No newline at end of file