From 9e58ca3e15d6301f356bee94867994b3582abb93 Mon Sep 17 00:00:00 2001 From: Meyanis95 Date: Wed, 10 Apr 2024 17:56:33 +0200 Subject: [PATCH] fix: cleaned --- example/src/BenchmarkView.tsx | 16 ----- src/aadhaarScanner.tsx | 15 ----- src/constants.ts | 7 ++ src/groth16Prover.ts | 94 +-------------------------- src/util.ts | 117 ++++++++++++++++++++++++---------- 5 files changed, 90 insertions(+), 159 deletions(-) create mode 100644 src/constants.ts diff --git a/example/src/BenchmarkView.tsx b/example/src/BenchmarkView.tsx index b3a7098..bd86fde 100644 --- a/example/src/BenchmarkView.tsx +++ b/example/src/BenchmarkView.tsx @@ -77,22 +77,6 @@ export default function BenchmarkView({}) { } }, [qrCodeValue]); - // useEffect(() => { - // let temp: string[] = []; - // if (ready) { - // for (let i = 0; i < 10; i++) { - // let chunkPath = `${zkeyChunksFolderPath}/circuit_final_${i}.zkey`; - // RNFS.exists(chunkPath).then((resp) => { - // console.log(chunkPath); - // console.log(resp); - // }); - // temp.push(chunkPath); - // } - // console.log('Chunked paths: ', temp); - // setChunkPaths(temp); - // } - // }, [ready]); - if (!ready) { const startSetup = Date.now(); setupProver().then(() => { diff --git a/src/aadhaarScanner.tsx b/src/aadhaarScanner.tsx index c4faf48..f54515f 100644 --- a/src/aadhaarScanner.tsx +++ b/src/aadhaarScanner.tsx @@ -76,26 +76,11 @@ const styles = StyleSheet.create({ alignItems: 'center', backgroundColor: 'rgba(0, 0, 0, 0.7)', }, - // camera: { - // height: '100%', - // width: '100%', - // alignSelf: 'center', - // }, cutout: { height: 350, // The size of the QR code cutout width: 350, borderColor: '#000', // Border color from your design }, - // overlay: { - // position: 'absolute', // Overlay must be absolutely positioned - // top: 0, // Full overlay over the camera view - // left: 0, - // right: 0, - // bottom: 0, - // alignItems: 'center', - // justifyContent: 'center', - // backgroundColor: 'transparent', // Transparent background - // }, camera: { height: '100%', // Take up full height width: '100%', // Take up full width diff --git a/src/constants.ts b/src/constants.ts new file mode 100644 index 0000000..aa4ae59 --- /dev/null +++ b/src/constants.ts @@ -0,0 +1,7 @@ +export const fileUrls = { + 'aadhaar-verifier.dat': + 'https://anon-aadhaar.s3.ap-south-1.amazonaws.com/v2.0.0/aadhaar-verifier.dat', + 'vkey.json': 'https://d1re67zv2jtrxt.cloudfront.net/v2.0.0/vkey.json', + 'circuit_final.zkey': + 'https://d1re67zv2jtrxt.cloudfront.net/v2.0.0/circuit_final.zkey', +}; diff --git a/src/groth16Prover.ts b/src/groth16Prover.ts index 6f8906f..e556662 100644 --- a/src/groth16Prover.ts +++ b/src/groth16Prover.ts @@ -1,9 +1,7 @@ import { NativeModules, Platform } from 'react-native'; import RNFS from 'react-native-fs'; import fetchBlob from 'react-native-blob-util'; -// import base64 from 'react-native-base64'; -// import { TextDecoder } from 'text-encoding'; -// import pako from 'pako'; +import { fileUrls } from './constants'; const LINKING_ERROR = `The package 'react-native-rapidsnark' doesn't seem to be linked. Make sure: \n\n` + @@ -25,62 +23,6 @@ export type AnonAadhaarArgs = { revealPinCode: string[]; }; -const fileUrls = { - 'aadhaar-verifier.dat': - 'https://anon-aadhaar.s3.ap-south-1.amazonaws.com/v2.0.0/aadhaar-verifier.dat', - 'vkey.json': 'https://d1re67zv2jtrxt.cloudfront.net/v2.0.0/vkey.json', - 'circuit_final.zkey': - 'https://d1re67zv2jtrxt.cloudfront.net/v2.0.0/circuit_final.zkey', -}; - -const chunkedDirectoryPath = `${RNFS.DocumentDirectoryPath}/chunked`; - -async function ensureDirectoryExists(path: string) { - const directoryExists = await RNFS.exists(path); - if (!directoryExists) { - await RNFS.mkdir(path); - } -} - -// async function downloadFileForChunkedZkey(url: string, targetPath: string) { -// // Determine the file extension -// const fileExtension = url.split('.').pop(); -// const tempPath = targetPath + (fileExtension === 'gz' ? '.gz' : ''); - -// try { -// // Download the file to a temporary path -// await fetchBlob.config({ path: tempPath }).fetch('GET', url); -// console.log('The file is temporarily saved to ', tempPath); - -// // If the file is a .gz file, read it, decompress it, and write the decompressed content -// if (fileExtension === 'gz') { -// // Read the .gz file as base64 -// const base64Data = await RNFS.readFile(tempPath, 'base64'); -// // Convert base64 to ArrayBuffer using react-native-base64 -// const binaryData = base64.decode(base64Data); -// let bytes = new Uint8Array(binaryData.length); -// for (let i = 0; i < binaryData.length; i++) { -// bytes[i] = binaryData.charCodeAt(i); -// } -// // Decompress with pako -// const decompressed = pako.ungzip(bytes); -// // Convert the decompressed data back to a string to write it -// const decoder = new TextDecoder('utf-8'); -// const decompressedStr = decoder.decode(decompressed); - -// // Write the decompressed data to the target path -// await RNFS.writeFile(targetPath, decompressedStr, 'utf8'); -// console.log('File decompressed to ', targetPath); - -// // Optionally, remove the original .gz file after decompression -// await RNFS.unlink(tempPath); -// console.log('Original .gz file removed'); -// } -// } catch (error) { -// console.error('Error during file download or decompression:', error); -// } -// } - async function downloadFile(url: string, targetPath: string) { try { // Download the file to a temporary path @@ -91,40 +33,6 @@ async function downloadFile(url: string, targetPath: string) { } } -export async function setupProverWithChunkedZkey() { - console.log('Starting setup!'); - const directoryPath = RNFS.DocumentDirectoryPath; - - for (const [key, url] of Object.entries(fileUrls)) { - console.log('Round for key: ', key); - // If Zkey, loading zkey chunks - if (key === 'circuit_final') { - for (let i = 0; i < 10; i++) { - const filePath = `${chunkedDirectoryPath}/${key}_${i}.zkey`; - const fileExists = await RNFS.exists(filePath); - - if (fileExists) continue; - - console.log('Fetching => ', url + key + `_${i}.gz`); - console.log('Stored at => ', filePath); - - await ensureDirectoryExists(chunkedDirectoryPath); - - await downloadFile(url + key + `_${i}.gz`, filePath); - } - } else { - const filePath = `${directoryPath}/${key}`; - const fileExists = await RNFS.exists(filePath); - - if (!fileExists) { - await downloadFile(url, filePath); - } - - console.log(`${key} loaded at ${filePath}`); - } - } -} - export async function setupProver() { console.log('Starting setup!'); for (const [key, url] of Object.entries(fileUrls)) { diff --git a/src/util.ts b/src/util.ts index 6f09a92..2960022 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,4 +1,9 @@ import pako from 'pako'; +import RNFS from 'react-native-fs'; +import base64 from 'react-native-base64'; +import { TextDecoder } from 'text-encoding'; +import { fileUrls } from './constants'; +import fetchBlob from 'react-native-blob-util'; export function str2ab(str: string) { const buf = new ArrayBuffer(str.length); @@ -123,42 +128,84 @@ export function Uint8ArrayToCharArray(a: Uint8Array): string[] { return Array.from(a).map((x) => x.toString()); } -export function utf8ArrayToStr(array: any) { - let out = '', - i = 0, - len = array.length; - let char2, char3; - - while (i < len) { - let c = array[i++]; - switch (c >> 4) { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - // 0xxxxxxx - out += String.fromCharCode(c); - break; - case 12: - case 13: - // 110x xxxx 10xx xxxx - char2 = array[i++]; - out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f)); - break; - case 14: - // 1110 xxxx 10xx xxxx 10xx xxxx - char2 = array[i++]; - char3 = array[i++]; - out += String.fromCharCode( - ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0) - ); - break; +export async function setupProverWithChunkedZkey() { + console.log('Starting setup!'); + const directoryPath = RNFS.DocumentDirectoryPath; + + for (const [key, url] of Object.entries(fileUrls)) { + console.log('Round for key: ', key); + // If Zkey, loading zkey chunks + if (key === 'circuit_final') { + for (let i = 0; i < 10; i++) { + const filePath = `${chunkedDirectoryPath}/${key}_${i}.zkey`; + const fileExists = await RNFS.exists(filePath); + + if (fileExists) continue; + + console.log('Fetching => ', url + key + `_${i}.gz`); + console.log('Stored at => ', filePath); + + await ensureDirectoryExists(chunkedDirectoryPath); + + await downloadFileForChunkedZkey(url + key + `_${i}.gz`, filePath); + } + } else { + const filePath = `${directoryPath}/${key}`; + const fileExists = await RNFS.exists(filePath); + + if (!fileExists) { + await downloadFileForChunkedZkey(url, filePath); + } + + console.log(`${key} loaded at ${filePath}`); } } +} + +const chunkedDirectoryPath = `${RNFS.DocumentDirectoryPath}/chunked`; - return out; +async function ensureDirectoryExists(path: string) { + const directoryExists = await RNFS.exists(path); + if (!directoryExists) { + await RNFS.mkdir(path); + } +} + +async function downloadFileForChunkedZkey(url: string, targetPath: string) { + // Determine the file extension + const fileExtension = url.split('.').pop(); + const tempPath = targetPath + (fileExtension === 'gz' ? '.gz' : ''); + + try { + // Download the file to a temporary path + await fetchBlob.config({ path: tempPath }).fetch('GET', url); + console.log('The file is temporarily saved to ', tempPath); + + // If the file is a .gz file, read it, decompress it, and write the decompressed content + if (fileExtension === 'gz') { + // Read the .gz file as base64 + const base64Data = await RNFS.readFile(tempPath, 'base64'); + // Convert base64 to ArrayBuffer using react-native-base64 + const binaryData = base64.decode(base64Data); + let bytes = new Uint8Array(binaryData.length); + for (let i = 0; i < binaryData.length; i++) { + bytes[i] = binaryData.charCodeAt(i); + } + // Decompress with pako + const decompressed = pako.ungzip(bytes); + // Convert the decompressed data back to a string to write it + const decoder = new TextDecoder('utf-8'); + const decompressedStr = decoder.decode(decompressed); + + // Write the decompressed data to the target path + await RNFS.writeFile(targetPath, decompressedStr, 'utf8'); + console.log('File decompressed to ', targetPath); + + // Optionally, remove the original .gz file after decompression + await RNFS.unlink(tempPath); + console.log('Original .gz file removed'); + } + } catch (error) { + console.error('Error during file download or decompression:', error); + } }