From ec01d39f27a23d12ce0732d48c96c25edce81223 Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Wed, 16 Oct 2024 09:02:05 +0200 Subject: [PATCH] check that correct card is tapped --- src/Main.tsx | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Main.tsx b/src/Main.tsx index d938945..24a2e19 100644 --- a/src/Main.tsx +++ b/src/Main.tsx @@ -27,6 +27,7 @@ enum Step { LoginError, FactoryReset, NotAuthentic, + WrongCard } const Main = () => { @@ -44,6 +45,7 @@ const Main = () => { const pinRef = useRef(""); const mnemonicRef = useRef(""); const walletKey = useRef(""); + const keyUID = useRef(""); const sessionRef = useRef("") const challengeRef = useRef("") @@ -76,6 +78,20 @@ const Main = () => { return null } + const rememberCard = async (uid) => { + walletKey.current = await Keycard.exportKeyWithPath(pinRef.current, WALLET_DERIVATION_PATH); + keyUID.current = uid; + await AsyncStorage.setItem("wallet-key", walletKey.current); + await AsyncStorage.setItem("key-uid", keyUID.current); + } + + const forgetCard = () => { + walletKey.current = ""; + keyUID.current = "" + AsyncStorage.removeItem("wallet-key"); + AsyncStorage.removeItem("key-uid"); + } + const loginRequest = async () => { var req = {'session-id': sessionRef.current}; @@ -130,22 +146,23 @@ const Main = () => { break; case Step.Loading: await Keycard.saveMnemonic(mnemonicRef.current, pinRef.current); - walletKey.current = await Keycard.exportKeyWithPath(pinRef.current, WALLET_DERIVATION_PATH); - await AsyncStorage.setItem("wallet-key", walletKey.current); + await rememberCard(appInfo["key-uid"]); setStep(Step.LoadSuccess) break; case Step.Authentication: - walletKey.current = await Keycard.exportKeyWithPath(pinRef.current, WALLET_DERIVATION_PATH); - await AsyncStorage.setItem("wallet-key", walletKey.current); + await rememberCard(appInfo["key-uid"]); setStep(Step.Home); break; case Step.Home: - loginReq = await loginRequest(); + if (appInfo["key-uid"] != keyUID.current) { + setStep(Step.WrongCard); + } else { + loginReq = await loginRequest(); + } break; case Step.FactoryReset: await Keycard.factoryReset(); - walletKey.current = ""; - AsyncStorage.removeItem("wallet-key"); + forgetCard(); setStep(Step.Discovery); break; default: @@ -177,9 +194,7 @@ const Main = () => { await stopNFC(); - if (loginReq) { - console.log(loginReq); - + if (loginReq) { try { const resp = await fetch(LOGIN_ENDPOINT, { method: 'POST', @@ -216,8 +231,11 @@ const Main = () => { const loadData = async () => { await Keycard.setPairings(await getPairings()); await Keycard.setCertificationAuthorities(["029ab99ee1e7a71bdf45b3f9c58c99866ff1294d2c1e304e228a86e10c3343501c"]); - const tmp = await AsyncStorage.getItem("wallet-key"); + + let tmp = await AsyncStorage.getItem("wallet-key"); walletKey.current = tmp !== null ? tmp : ""; + tmp = await AsyncStorage.getItem("key-uid"); + keyUID.current = tmp !== null ? tmp : ""; if (walletKey.current) { setStep(Step.Home); @@ -282,8 +300,7 @@ const Main = () => { } const cancel = () => { - walletKey.current = ""; - AsyncStorage.removeItem("wallet-key"); + forgetCard(); setStep(Step.Discovery); } @@ -328,6 +345,7 @@ const Main = () => { {step == Step.LoginSuccess && } {step == Step.LoginError && } {step == Step.NotAuthentic && } + {step == Step.WrongCard && } );