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"
)
return;
if(response?.hasKeyPair){
setExtstate("authenticated");
setExtstate("authenticated");
} else {
setExtstate("wallet-dropped");
}
}
})
.catch((error) => {

View File

@ -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
);
}
}
}

View File

@ -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<string> => {
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 <T = any>(key: string): Promise<T> => {
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 {