This commit is contained in:
despiegk 2025-04-21 13:17:56 +02:00
parent c47f67b901
commit 6573a01d75
2 changed files with 259 additions and 87 deletions

View File

@ -33,17 +33,107 @@ function hexToBuffer(hex) {
return bytes; return bytes;
} }
// LocalStorage functions for Ethereum wallets // IndexedDB setup for Ethereum wallets
const ETH_WALLET_PREFIX = 'eth_wallet_'; const DB_NAME = 'EthWalletDB';
const DB_VERSION = 1;
const STORE_NAME = 'ethWallets';
// Save Ethereum wallet to localStorage // Initialize the database
function saveEthWalletToStorage(address, privateKey) { function initDatabase() {
localStorage.setItem(`${ETH_WALLET_PREFIX}${address}`, privateKey); return new Promise((resolve, reject) => {
const request = indexedDB.open(DB_NAME, DB_VERSION);
request.onerror = (event) => {
console.error('Error opening Ethereum wallet database:', event.target.error);
reject('Error opening database: ' + event.target.error);
};
request.onsuccess = (event) => {
const db = event.target.result;
resolve(db);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
// Create object store for Ethereum wallets if it doesn't exist
if (!db.objectStoreNames.contains(STORE_NAME)) {
const store = db.createObjectStore(STORE_NAME, { keyPath: 'address' });
store.createIndex('address', 'address', { unique: true });
}
};
});
} }
// Get Ethereum wallet from localStorage // Get database connection
function getEthWalletFromStorage(address) { function getDB() {
return localStorage.getItem(`${ETH_WALLET_PREFIX}${address}`); return initDatabase();
}
// Save Ethereum wallet to IndexedDB
async function saveEthWalletToStorage(address, privateKey) {
try {
const db = await getDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readwrite');
const store = transaction.objectStore(STORE_NAME);
const wallet = {
address: address,
privateKey: privateKey,
created: new Date()
};
const request = store.put(wallet);
request.onsuccess = () => {
resolve();
};
request.onerror = (event) => {
console.error('Error saving Ethereum wallet:', event.target.error);
reject('Error saving wallet: ' + event.target.error);
};
transaction.oncomplete = () => {
db.close();
};
});
} catch (error) {
console.error('Database error in saveEthWalletToStorage:', error);
}
}
// Get Ethereum wallet from IndexedDB
async function getEthWalletFromStorage(address) {
try {
const db = await getDB();
return new Promise((resolve, reject) => {
const transaction = db.transaction([STORE_NAME], 'readonly');
const store = transaction.objectStore(STORE_NAME);
const request = store.get(address);
request.onsuccess = (event) => {
const wallet = event.target.result;
if (wallet) {
resolve(wallet.privateKey);
} else {
resolve(null);
}
};
request.onerror = (event) => {
console.error('Error retrieving Ethereum wallet:', event.target.error);
reject('Error retrieving wallet: ' + event.target.error);
};
transaction.oncomplete = () => {
db.close();
};
});
} catch (error) {
console.error('Database error in getEthWalletFromStorage:', error);
return null;
}
} }
// Session state // Session state
@ -147,13 +237,20 @@ async function performCreateEthereumWallet() {
} }
try { try {
// Show loading state
document.getElementById('ethereum-wallet-result').textContent = 'Creating wallet...';
// Create Ethereum wallet // Create Ethereum wallet
console.log('Creating Ethereum wallet from keypair:', selectedKeypair);
const result = create_ethereum_wallet(); const result = create_ethereum_wallet();
console.log('Create Ethereum wallet result:', result);
if (result === 0) { if (result === 0) {
hasEthereumWallet = true; hasEthereumWallet = true;
// Get and display Ethereum address // Get and display Ethereum address
const address = get_ethereum_address(); const address = get_ethereum_address();
console.log('Generated Ethereum address:', address);
document.getElementById('ethereum-address-value').textContent = address; document.getElementById('ethereum-address-value').textContent = address;
// Get and display private key // Get and display private key
@ -163,14 +260,22 @@ async function performCreateEthereumWallet() {
// Show the wallet info // Show the wallet info
document.getElementById('ethereum-wallet-info').classList.remove('hidden'); document.getElementById('ethereum-wallet-info').classList.remove('hidden');
// Save the wallet to localStorage try {
saveEthWalletToStorage(address, privateKey); // Save the wallet to IndexedDB
console.log('Saving wallet to IndexedDB:', address);
document.getElementById('ethereum-wallet-result').textContent = 'Successfully created Ethereum wallet'; await saveEthWalletToStorage(address, privateKey);
console.log('Wallet saved successfully');
document.getElementById('ethereum-wallet-result').textContent = 'Successfully created Ethereum wallet';
} catch (saveError) {
console.error('Error saving wallet to IndexedDB:', saveError);
document.getElementById('ethereum-wallet-result').textContent = 'Wallet created but failed to save to storage';
}
} else { } else {
document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`; document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`;
} }
} catch (e) { } catch (e) {
console.error('Error in performCreateEthereumWallet:', e);
document.getElementById('ethereum-wallet-result').textContent = `Error: ${e}`; document.getElementById('ethereum-wallet-result').textContent = `Error: ${e}`;
} }
} }
@ -190,13 +295,20 @@ async function performCreateEthereumWalletFromName() {
} }
try { try {
// Show loading state
document.getElementById('ethereum-wallet-result').textContent = 'Creating wallet...';
// Create Ethereum wallet from name // Create Ethereum wallet from name
console.log('Creating Ethereum wallet from name:', name);
const result = create_ethereum_wallet_from_name(name); const result = create_ethereum_wallet_from_name(name);
console.log('Create Ethereum wallet from name result:', result);
if (result === 0) { if (result === 0) {
hasEthereumWallet = true; hasEthereumWallet = true;
// Get and display Ethereum address // Get and display Ethereum address
const address = get_ethereum_address(); const address = get_ethereum_address();
console.log('Generated Ethereum address:', address);
document.getElementById('ethereum-address-value').textContent = address; document.getElementById('ethereum-address-value').textContent = address;
// Get and display private key // Get and display private key
@ -206,14 +318,22 @@ async function performCreateEthereumWalletFromName() {
// Show the wallet info // Show the wallet info
document.getElementById('ethereum-wallet-info').classList.remove('hidden'); document.getElementById('ethereum-wallet-info').classList.remove('hidden');
// Save the wallet to localStorage try {
saveEthWalletToStorage(address, privateKey); // Save the wallet to IndexedDB
console.log('Saving wallet to IndexedDB:', address);
document.getElementById('ethereum-wallet-result').textContent = `Successfully created Ethereum wallet from name "${name}"`; await saveEthWalletToStorage(address, privateKey);
console.log('Wallet saved successfully');
document.getElementById('ethereum-wallet-result').textContent = `Successfully created Ethereum wallet from name "${name}"`;
} catch (saveError) {
console.error('Error saving wallet to IndexedDB:', saveError);
document.getElementById('ethereum-wallet-result').textContent = 'Wallet created but failed to save to storage';
}
} else { } else {
document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`; document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`;
} }
} catch (e) { } catch (e) {
console.error('Error in performCreateEthereumWalletFromName:', e);
document.getElementById('ethereum-wallet-result').textContent = `Error: ${e}`; document.getElementById('ethereum-wallet-result').textContent = `Error: ${e}`;
} }
} }
@ -228,13 +348,20 @@ async function performCreateEthereumWalletFromPrivateKey() {
} }
try { try {
// Show loading state
document.getElementById('ethereum-wallet-result').textContent = 'Creating wallet...';
// Create Ethereum wallet from private key // Create Ethereum wallet from private key
console.log('Creating Ethereum wallet from private key');
const result = create_ethereum_wallet_from_private_key(privateKey); const result = create_ethereum_wallet_from_private_key(privateKey);
console.log('Create Ethereum wallet from private key result:', result);
if (result === 0) { if (result === 0) {
hasEthereumWallet = true; hasEthereumWallet = true;
// Get and display Ethereum address // Get and display Ethereum address
const address = get_ethereum_address(); const address = get_ethereum_address();
console.log('Generated Ethereum address:', address);
document.getElementById('ethereum-address-value').textContent = address; document.getElementById('ethereum-address-value').textContent = address;
// Get and display private key // Get and display private key
@ -244,14 +371,22 @@ async function performCreateEthereumWalletFromPrivateKey() {
// Show the wallet info // Show the wallet info
document.getElementById('ethereum-wallet-info').classList.remove('hidden'); document.getElementById('ethereum-wallet-info').classList.remove('hidden');
// Save the wallet to localStorage try {
saveEthWalletToStorage(address, displayPrivateKey); // Save the wallet to IndexedDB
console.log('Saving wallet to IndexedDB:', address);
document.getElementById('ethereum-wallet-result').textContent = 'Successfully imported Ethereum wallet from private key'; await saveEthWalletToStorage(address, displayPrivateKey);
console.log('Wallet saved successfully');
document.getElementById('ethereum-wallet-result').textContent = 'Successfully imported Ethereum wallet from private key';
} catch (saveError) {
console.error('Error saving wallet to IndexedDB:', saveError);
document.getElementById('ethereum-wallet-result').textContent = 'Wallet imported but failed to save to storage';
}
} else { } else {
document.getElementById('ethereum-wallet-result').textContent = `Error importing Ethereum wallet: ${result}`; document.getElementById('ethereum-wallet-result').textContent = `Error importing Ethereum wallet: ${result}`;
} }
} catch (e) { } catch (e) {
console.error('Error in performCreateEthereumWalletFromPrivateKey:', e);
document.getElementById('ethereum-wallet-result').textContent = `Error: ${e}`; document.getElementById('ethereum-wallet-result').textContent = `Error: ${e}`;
} }
} }

View File

@ -140,32 +140,37 @@ async function saveSpaceToStorage(spaceName, encryptedData) {
// Get encrypted space from IndexedDB // Get encrypted space from IndexedDB
async function getSpaceFromStorage(spaceName) { async function getSpaceFromStorage(spaceName) {
const db = await getDB(); try {
return new Promise((resolve, reject) => { const db = await getDB();
const transaction = db.transaction([STORE_NAME], 'readonly'); return new Promise((resolve, reject) => {
const store = transaction.objectStore(STORE_NAME); const transaction = db.transaction([STORE_NAME], 'readonly');
const request = store.get(spaceName); const store = transaction.objectStore(STORE_NAME);
const request = store.get(spaceName);
request.onsuccess = (event) => {
const space = event.target.result; request.onsuccess = (event) => {
if (space) { const space = event.target.result;
// Update last accessed timestamp if (space) {
updateLastAccessed(spaceName).catch(console.error); // Update last accessed timestamp
resolve(space.encryptedData); updateLastAccessed(spaceName).catch(console.error);
} else { resolve(space.encryptedData);
resolve(null); } else {
} resolve(null);
}; }
};
request.onerror = (event) => {
console.error('Error retrieving space:', event.target.error); request.onerror = (event) => {
reject('Error retrieving space: ' + event.target.error); console.error('Error retrieving space:', event.target.error);
}; reject('Error retrieving space: ' + event.target.error);
};
transaction.oncomplete = () => {
db.close(); transaction.oncomplete = () => {
}; db.close();
}); };
});
} catch (error) {
console.error('Database error in getSpaceFromStorage:', error);
return null;
}
} }
// Update last accessed timestamp // Update last accessed timestamp
@ -253,7 +258,7 @@ let currentSpace = null;
let selectedKeypair = null; let selectedKeypair = null;
// Update UI based on login state // Update UI based on login state
function updateLoginUI() { async function updateLoginUI() {
const loginForm = document.getElementById('login-form'); const loginForm = document.getElementById('login-form');
const logoutForm = document.getElementById('logout-form'); const logoutForm = document.getElementById('logout-form');
const loginStatus = document.getElementById('login-status'); const loginStatus = document.getElementById('login-status');
@ -274,7 +279,11 @@ function updateLoginUI() {
} }
// Update the spaces list // Update the spaces list
updateSpacesList(); try {
await updateSpacesList();
} catch (e) {
console.error('Error updating spaces list in UI:', e);
}
} }
// Update the spaces dropdown list // Update the spaces dropdown list
@ -323,24 +332,33 @@ async function performLogin() {
return; return;
} }
// Decrypt the space console.log('Retrieved space from IndexedDB:', { spaceName, encryptedDataLength: encryptedSpace.length });
const result = decrypt_key_space(encryptedSpace, password);
if (result === 0) { try {
isLoggedIn = true; // Decrypt the space - this is a synchronous WebAssembly function
currentSpace = spaceName; const result = decrypt_key_space(encryptedSpace, password);
updateLoginUI(); console.log('Decrypt result:', result);
updateKeypairsList();
document.getElementById('space-result').textContent = `Successfully logged in to space "${spaceName}"`;
// Setup auto-logout if (result === 0) {
updateActivity(); isLoggedIn = true;
setupAutoLogout(); currentSpace = spaceName;
await updateLoginUI();
// Add activity listeners updateKeypairsList();
document.addEventListener('click', updateActivity); document.getElementById('space-result').textContent = `Successfully logged in to space "${spaceName}"`;
document.addEventListener('keypress', updateActivity);
} else { // Setup auto-logout
document.getElementById('space-result').textContent = `Error logging in: ${result}`; updateActivity();
setupAutoLogout();
// Add activity listeners
document.addEventListener('click', updateActivity);
document.addEventListener('keypress', updateActivity);
} else {
document.getElementById('space-result').textContent = `Error logging in: ${result}`;
}
} catch (decryptErr) {
console.error('Decryption error:', decryptErr);
document.getElementById('space-result').textContent = `Decryption error: ${decryptErr}`;
} }
} catch (e) { } catch (e) {
console.error('Login error:', e); console.error('Login error:', e);
@ -369,31 +387,50 @@ async function performCreateSpace() {
return; return;
} }
// Create new space try {
const result = create_key_space(spaceName); // Create new space
if (result === 0) { console.log('Creating new space:', spaceName);
// Encrypt and save the space const result = create_key_space(spaceName);
const encryptedSpace = encrypt_key_space(password); console.log('Create space result:', result);
await saveSpaceToStorage(spaceName, encryptedSpace);
isLoggedIn = true; if (result === 0) {
currentSpace = spaceName; try {
updateLoginUI(); // Encrypt and save the space
updateKeypairsList(); console.log('Encrypting space with password');
document.getElementById('space-result').textContent = `Successfully created space "${spaceName}"`; const encryptedSpace = encrypt_key_space(password);
console.log('Encrypted space length:', encryptedSpace.length);
// Setup auto-logout
updateActivity(); // Save to IndexedDB
setupAutoLogout(); console.log('Saving to IndexedDB');
await saveSpaceToStorage(spaceName, encryptedSpace);
// Add activity listeners console.log('Save completed');
document.addEventListener('click', updateActivity);
document.addEventListener('keypress', updateActivity); isLoggedIn = true;
} else { currentSpace = spaceName;
document.getElementById('space-result').textContent = `Error creating space: ${result}`; await updateLoginUI();
updateKeypairsList();
document.getElementById('space-result').textContent = `Successfully created space "${spaceName}"`;
// Setup auto-logout
updateActivity();
setupAutoLogout();
// Add activity listeners
document.addEventListener('click', updateActivity);
document.addEventListener('keypress', updateActivity);
} catch (encryptError) {
console.error('Error encrypting or saving space:', encryptError);
document.getElementById('space-result').textContent = `Error saving space: ${encryptError}`;
}
} else {
document.getElementById('space-result').textContent = `Error creating space: ${result}`;
}
} catch (createError) {
console.error('Error in WebAssembly create_key_space:', createError);
document.getElementById('space-result').textContent = `Error creating key space: ${createError}`;
} }
} catch (e) { } catch (e) {
console.error('Error creating space:', e); console.error('Error checking existing space:', e);
document.getElementById('space-result').textContent = `Error: ${e}`; document.getElementById('space-result').textContent = `Error: ${e}`;
} }
} }