diff --git a/src/App.tsx b/src/App.tsx index 38cca7b..de1da01 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -762,8 +762,12 @@ function App() { holdRefExtState.current === "web-app-request-buy-order" ) return; + if(response?.hasKeyPair){ + setExtstate("authenticated"); - setExtstate("authenticated"); + } else { + setExtstate("wallet-dropped"); + } } }) .catch((error) => { diff --git a/src/background-cases.ts b/src/background-cases.ts index dff5ed9..a56df7e 100644 --- a/src/background-cases.ts +++ b/src/background-cases.ts @@ -78,7 +78,7 @@ export async function getWalletInfoCase(request, event) { { requestId: request.requestId, action: "getWalletInfo", - payload: { walletInfo }, + payload: { walletInfo, hasKeyPair: true }, type: "backgroundMessageResponse", }, event.origin @@ -108,15 +108,41 @@ export async function getWalletInfoCase(request, event) { } } catch (error) { - event.source.postMessage( - { - requestId: request.requestId, - action: "getWalletInfo", - error: error?.message, - type: "backgroundMessageResponse", - }, - event.origin - ); + try { + const walletInfo = await getData('walletInfo').catch((error)=> null) + if(walletInfo){ + event.source.postMessage( + { + requestId: request.requestId, + action: "getWalletInfo", + payload: { walletInfo, hasKeyPair: false }, + type: "backgroundMessageResponse", + }, + event.origin + ); + } else { + event.source.postMessage( + { + requestId: request.requestId, + action: "getWalletInfo", + error: "Wallet not authenticated", + type: "backgroundMessageResponse", + }, + event.origin + ); + } + } catch (error) { + event.source.postMessage( + { + requestId: request.requestId, + action: "getWalletInfo", + error: "Wallet not authenticated", + type: "backgroundMessageResponse", + }, + event.origin + ); + } + } } diff --git a/src/utils/chromeStorage.ts b/src/utils/chromeStorage.ts index 44d306f..ab1d050 100644 --- a/src/utils/chromeStorage.ts +++ b/src/utils/chromeStorage.ts @@ -3,6 +3,8 @@ import { SecureStoragePlugin } from '@evva/capacitor-secure-storage-plugin'; let inMemoryKey: CryptoKey | null = null; let inMemoryIV: Uint8Array | null = null; +const keysToEncrypt = ['keyPair']; + async function initializeKeyAndIV() { if (!inMemoryKey) { inMemoryKey = await generateKey(); // Generates the key in memory @@ -57,39 +59,44 @@ async function decryptData(encryptedData: ArrayBuffer, key: CryptoKey, iv: Uint8 export const storeData = async (key: string, payload: any): Promise => { await initializeKeyAndIV(); - if (inMemoryKey) { + if (keysToEncrypt.includes(key) && inMemoryKey) { + // Encrypt the payload if the key is in keysToEncrypt const { iv, encryptedData } = await encryptData(JSON.stringify(payload), inMemoryKey); // Combine IV and encrypted data into a single string const combinedData = new Uint8Array([...iv, ...new Uint8Array(encryptedData)]); await SecureStoragePlugin.set({ key, value: btoa(String.fromCharCode(...combinedData)) }); - - return "Data saved successfully"; } else { - throw new Error("Key is not initialized in memory"); + // Store data in plain text if not in keysToEncrypt + await SecureStoragePlugin.set({ key, value: JSON.stringify(payload) }); } + + return "Data saved successfully"; }; // Retrieve data, split the IV and encrypted data, then decrypt export const getData = async (key: string): Promise => { await initializeKeyAndIV(); - if (!inMemoryKey) throw new Error("Encryption key is not initialized"); - const storedDataBase64 = await SecureStoragePlugin.get({ key }); if (storedDataBase64.value) { - const combinedData = atob(storedDataBase64.value).split("").map((c) => c.charCodeAt(0)); - const iv = new Uint8Array(combinedData.slice(0, 12)); // First 12 bytes are the IV - const encryptedData = new Uint8Array(combinedData.slice(12)).buffer; // The rest is encrypted data + if (keysToEncrypt.includes(key) && inMemoryKey) { + // If the key is in keysToEncrypt, decrypt the data + const combinedData = atob(storedDataBase64.value).split("").map((c) => c.charCodeAt(0)); + const iv = new Uint8Array(combinedData.slice(0, 12)); // First 12 bytes are the IV + const encryptedData = new Uint8Array(combinedData.slice(12)).buffer; // The rest is encrypted data - const decryptedData = await decryptData(encryptedData, inMemoryKey, iv); - return JSON.parse(decryptedData) as T; + const decryptedData = await decryptData(encryptedData, inMemoryKey, iv); + return JSON.parse(decryptedData) as T; + } else { + // If the key is not in keysToEncrypt, parse data as plain text + return JSON.parse(storedDataBase64.value) as T; + } } else { throw new Error(`No data found for key: ${key}`); } }; - // Remove keys from storage and log out export async function removeKeysAndLogout(keys: string[], event: MessageEvent, request: any) { try {