From e430da977d19aa32ae8a42fb30786ef9cf0f365d Mon Sep 17 00:00:00 2001 From: 0xturboblitz Date: Thu, 27 Jul 2023 14:56:42 +0200 Subject: [PATCH] abiility to skip scan and retrieve cached data --- app/.babelrc | 5 ++++ app/App.tsx | 58 ++++++++++++++++++++++++++++++------------- app/declarations.d.ts | 1 + app/package.json | 2 ++ app/server/server.ts | 13 +++++++++- app/utils/checks.ts | 5 ++++ app/yarn.lock | 17 +++++++++++++ 7 files changed, 83 insertions(+), 18 deletions(-) create mode 100644 app/.babelrc create mode 100644 app/declarations.d.ts diff --git a/app/.babelrc b/app/.babelrc new file mode 100644 index 000000000..efd56e870 --- /dev/null +++ b/app/.babelrc @@ -0,0 +1,5 @@ +{ + "plugins": [ + ["module:react-native-dotenv"] + ] +} \ No newline at end of file diff --git a/app/App.tsx b/app/App.tsx index 352dd89f8..bf8c15a27 100644 --- a/app/App.tsx +++ b/app/App.tsx @@ -23,18 +23,33 @@ import { } from 'react-native/Libraries/NewAppScreen'; // @ts-ignore import PassportReader from 'react-native-passport-reader'; -import {checkInputs} from './utils/checks'; +import {checkInputs, getFirstName} from './utils/checks'; +import {DEFAULT_PNUMBER, DEFAULT_DOB, DEFAULT_DOE, DEFAULT_ADDRESS} from '@env'; -const CACHE_PASSPORT_DATA = true; +console.log('DEFAULT_PNUMBER', DEFAULT_PNUMBER); + +const CACHE_DATA_IN_LOCAL_SERVER = true; +const SKIP_SCAN = true; + +type PassportData = { + mrzInfo: any; + publicKey: any; + publicKeyPEM: any; + dataGroupHashes: any; + eContent: any; + encryptedDigest: any; + contentBytes: any; + eContentDecomposed: any; +}; function App(): JSX.Element { const isDarkMode = useColorScheme() === 'dark'; - const [passportNumber, setPassportNumber] = useState('19HA34828'); - const [dateOfBirth, setDateOfBirth] = useState('000719'); - const [dateOfExpiry, setDateOfExpiry] = useState('291209'); - const [address, setAddress] = useState(''); + const [passportNumber, setPassportNumber] = useState(DEFAULT_PNUMBER ?? ''); + const [dateOfBirth, setDateOfBirth] = useState(DEFAULT_DOB ?? ''); + const [dateOfExpiry, setDateOfExpiry] = useState(DEFAULT_DOE ?? ''); + const [address, setAddress] = useState(DEFAULT_ADDRESS ?? ''); + const [passportData, setPassportData] = useState(null); const [step, setStep] = useState('enterDetails'); - const [firstName, setFirstName] = useState(''); const backgroundStyle = { backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, @@ -50,6 +65,17 @@ function App(): JSX.Element { }; }, []); + if (SKIP_SCAN && passportData === null) { + console.log('skipping scan step...'); + fetch('http://192.168.1.22:3000/passportData') + .then(response => response.json()) + .then(data => { + console.log('passport data fetched'); + setPassportData(data); + setStep('scanCompleted'); + }); + } + async function handleResponse(response: any) { const { mrzInfo, @@ -82,11 +108,12 @@ function App(): JSX.Element { console.log('contentBytes', passportData.contentBytes); console.log('eContentDecomposed', passportData.eContentDecomposed); - // Stores data in local server to avoid having to scan the passport each time - // For development purposes only + setPassportData(passportData); - if (CACHE_PASSPORT_DATA) { - fetch('http://192.168.1.22:3000/passportData', { + if (CACHE_DATA_IN_LOCAL_SERVER) { + // Caches data in local server to avoid having to scan the passport each time + // For development purposes only + fetch('http://192.168.1.22:3000/post', { method: 'POST', headers: { 'Content-Type': 'application/json', @@ -100,11 +127,6 @@ function App(): JSX.Element { }); } - const firstName = passportData.mrzInfo.secondaryIdentifier.split('<')[0]; - setFirstName( - firstName.charAt(0).toUpperCase() + firstName.slice(1).toLowerCase(), - ); - // 1. Compute the eContent from the dg1File // 2. Format all the data as calldata for the verifier contract @@ -192,7 +214,9 @@ function App(): JSX.Element { {step === 'scanCompleted' ? ( Connection successful - Hi {firstName} + + Hi {getFirstName(passportData?.mrzInfo)} !{' '} + Input your address or ens { +app.post('/post', (req: Request, res: Response) => { const data = req.body; fs.writeFile('passportData.json', JSON.stringify(data, null, 2), err => { if (err) { @@ -19,6 +19,17 @@ app.post('/passportData', (req: Request, res: Response) => { }); }); +app.get('/passportData', (req: Request, res: Response) => { + fs.readFile('passportData.json', (err, data) => { + if (err) { + console.log(err); + res.status(500).json({message: 'An error occurred while reading file'}); + } else { + res.json(JSON.parse(data.toString())); + } + }); +}); + const port = 3000; app.listen(port, () => { console.log(`Server running on http://localhost:${port}`); diff --git a/app/utils/checks.ts b/app/utils/checks.ts index f18a09c3d..9074d13de 100644 --- a/app/utils/checks.ts +++ b/app/utils/checks.ts @@ -14,3 +14,8 @@ export function checkInputs( } return true; } + +export function getFirstName(mrzInfo: any): string { + const firstName = mrzInfo.secondaryIdentifier.split('<')[0]; + return firstName.charAt(0).toUpperCase() + firstName.slice(1).toLowerCase(); +} diff --git a/app/yarn.lock b/app/yarn.lock index a48d057a9..25feb31fc 100644 --- a/app/yarn.lock +++ b/app/yarn.lock @@ -1961,6 +1961,11 @@ resolved "https://registry.yarnpkg.com/@types/range-parser/-/range-parser-1.2.4.tgz#cd667bcfdd025213aafb7ca5915a932590acdcdc" integrity sha512-EEhsLsD6UsDM1yFhAvy0Cjr6VwmpMWqFBCb9w07wVugF7w9nfajxLuVmngTIpgS6svCnm6Vaw+MZhoDCKnOfsw== +"@types/react-native-dotenv@^0.2.0": + version "0.2.0" + resolved "https://registry.yarnpkg.com/@types/react-native-dotenv/-/react-native-dotenv-0.2.0.tgz#32c58422a422c1adf68acce363ed791314d5a8e7" + integrity sha512-ZxX+dU/yoQc0jTk+/NWttkiuXceJyN5FpOSqDl0WynN5GDzxwH7OMruQ47qcY8llo2RD3irjvzJ9BwC8gDiq0A== + "@types/react-test-renderer@^18.0.0": version "18.0.0" resolved "https://registry.npmjs.org/@types/react-test-renderer/-/react-test-renderer-18.0.0.tgz" @@ -2989,6 +2994,11 @@ doctrine@^3.0.0: dependencies: esutils "^2.0.2" +dotenv@^16.3.1: + version "16.3.1" + resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.3.1.tgz#369034de7d7e5b120972693352a3bf112172cc3e" + integrity sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ== + ee-first@1.1.1: version "1.1.1" resolved "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz" @@ -5638,6 +5648,13 @@ react-is@^17.0.1: resolved "https://registry.npmjs.org/react-is/-/react-is-17.0.2.tgz" integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w== +react-native-dotenv@^3.4.9: + version "3.4.9" + resolved "https://registry.yarnpkg.com/react-native-dotenv/-/react-native-dotenv-3.4.9.tgz#621c5b0c1d0c5c7f569bfe5a1d804bec7885c010" + integrity sha512-dbyd+mcy7SUzxEgmt33TRf1FGcNe6swJhXmB0unKkI49F7+pidog9kPtjxMLTAfmKA8gcN2XHQSKltGfGbGCLQ== + dependencies: + dotenv "^16.3.1" + react-native-passport-reader@^1.0.3: version "1.0.3" resolved "https://registry.npmjs.org/react-native-passport-reader/-/react-native-passport-reader-1.0.3.tgz"