show password field when already has wallet

This commit is contained in:
PhilReact 2024-10-31 12:39:15 +02:00
parent 0879abdce2
commit 8b14717b53
3 changed files with 60 additions and 23 deletions

View File

@ -762,8 +762,12 @@ function App() {
holdRefExtState.current === "web-app-request-buy-order" holdRefExtState.current === "web-app-request-buy-order"
) )
return; return;
if(response?.hasKeyPair){
setExtstate("authenticated");
setExtstate("authenticated"); } else {
setExtstate("wallet-dropped");
}
} }
}) })
.catch((error) => { .catch((error) => {

View File

@ -78,7 +78,7 @@ export async function getWalletInfoCase(request, event) {
{ {
requestId: request.requestId, requestId: request.requestId,
action: "getWalletInfo", action: "getWalletInfo",
payload: { walletInfo }, payload: { walletInfo, hasKeyPair: true },
type: "backgroundMessageResponse", type: "backgroundMessageResponse",
}, },
event.origin event.origin
@ -108,15 +108,41 @@ export async function getWalletInfoCase(request, event) {
} }
} catch (error) { } catch (error) {
event.source.postMessage( try {
{ const walletInfo = await getData('walletInfo').catch((error)=> null)
requestId: request.requestId, if(walletInfo){
action: "getWalletInfo", event.source.postMessage(
error: error?.message, {
type: "backgroundMessageResponse", requestId: request.requestId,
}, action: "getWalletInfo",
event.origin 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
);
}
} }
} }

View File

@ -3,6 +3,8 @@ import { SecureStoragePlugin } from '@evva/capacitor-secure-storage-plugin';
let inMemoryKey: CryptoKey | null = null; let inMemoryKey: CryptoKey | null = null;
let inMemoryIV: Uint8Array | null = null; let inMemoryIV: Uint8Array | null = null;
const keysToEncrypt = ['keyPair'];
async function initializeKeyAndIV() { async function initializeKeyAndIV() {
if (!inMemoryKey) { if (!inMemoryKey) {
inMemoryKey = await generateKey(); // Generates the key in memory 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<string> => { export const storeData = async (key: string, payload: any): Promise<string> => {
await initializeKeyAndIV(); 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); const { iv, encryptedData } = await encryptData(JSON.stringify(payload), inMemoryKey);
// Combine IV and encrypted data into a single string // Combine IV and encrypted data into a single string
const combinedData = new Uint8Array([...iv, ...new Uint8Array(encryptedData)]); const combinedData = new Uint8Array([...iv, ...new Uint8Array(encryptedData)]);
await SecureStoragePlugin.set({ key, value: btoa(String.fromCharCode(...combinedData)) }); await SecureStoragePlugin.set({ key, value: btoa(String.fromCharCode(...combinedData)) });
return "Data saved successfully";
} else { } 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 // Retrieve data, split the IV and encrypted data, then decrypt
export const getData = async <T = any>(key: string): Promise<T> => { export const getData = async <T = any>(key: string): Promise<T> => {
await initializeKeyAndIV(); await initializeKeyAndIV();
if (!inMemoryKey) throw new Error("Encryption key is not initialized");
const storedDataBase64 = await SecureStoragePlugin.get({ key }); const storedDataBase64 = await SecureStoragePlugin.get({ key });
if (storedDataBase64.value) { if (storedDataBase64.value) {
const combinedData = atob(storedDataBase64.value).split("").map((c) => c.charCodeAt(0)); if (keysToEncrypt.includes(key) && inMemoryKey) {
const iv = new Uint8Array(combinedData.slice(0, 12)); // First 12 bytes are the IV // If the key is in keysToEncrypt, decrypt the data
const encryptedData = new Uint8Array(combinedData.slice(12)).buffer; // The rest is encrypted 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); const decryptedData = await decryptData(encryptedData, inMemoryKey, iv);
return JSON.parse(decryptedData) as T; 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 { } else {
throw new Error(`No data found for key: ${key}`); throw new Error(`No data found for key: ${key}`);
} }
}; };
// Remove keys from storage and log out // Remove keys from storage and log out
export async function removeKeysAndLogout(keys: string[], event: MessageEvent, request: any) { export async function removeKeysAndLogout(keys: string[], event: MessageEvent, request: any) {
try { try {