From 8569bb4bd85b0ed8151b9d4f6a2242ab9d85e5a0 Mon Sep 17 00:00:00 2001 From: despiegk Date: Mon, 21 Apr 2025 11:57:11 +0200 Subject: [PATCH] ... --- www/ethereum.html | 59 ++---------- www/js/ethereum.js | 224 +++++---------------------------------------- 2 files changed, 32 insertions(+), 251 deletions(-) diff --git a/www/ethereum.html b/www/ethereum.html index ce33fdf..90497fa 100644 --- a/www/ethereum.html +++ b/www/ethereum.html @@ -125,60 +125,21 @@ Ethereum Demo - -
-

Key Space Management

+
Note: You must first login and create a keypair in the Main Crypto Demo page.
+ + +
+

Select Keypair

Status: Not logged in
-
-
- - -
- -
- - -
- -
- - -
-
- - - -
Result will appear here
-
- - -
-

Keypair Management

- -
-
- - - -
- -
- - -
+
+ +
Result will appear here
diff --git a/www/js/ethereum.js b/www/js/ethereum.js index ad6bad3..0880ef1 100644 --- a/www/js/ethereum.js +++ b/www/js/ethereum.js @@ -33,20 +33,9 @@ function hexToBuffer(hex) { return bytes; } -// LocalStorage functions for key spaces -const STORAGE_PREFIX = 'crypto_space_'; +// LocalStorage functions for Ethereum wallets const ETH_WALLET_PREFIX = 'eth_wallet_'; -// Save encrypted space to localStorage -function saveSpaceToStorage(spaceName, encryptedData) { - localStorage.setItem(`${STORAGE_PREFIX}${spaceName}`, encryptedData); -} - -// Get encrypted space from localStorage -function getSpaceFromStorage(spaceName) { - return localStorage.getItem(`${STORAGE_PREFIX}${spaceName}`); -} - // Save Ethereum wallet to localStorage function saveEthWalletToStorage(address, privateKey) { localStorage.setItem(`${ETH_WALLET_PREFIX}${address}`, privateKey); @@ -58,30 +47,34 @@ function getEthWalletFromStorage(address) { } // Session state -let isLoggedIn = false; -let currentSpace = null; let selectedKeypair = null; let hasEthereumWallet = false; // Update UI based on login state function updateLoginUI() { - const loginForm = document.getElementById('login-form'); - const logoutForm = document.getElementById('logout-form'); const loginStatus = document.getElementById('login-status'); - const currentSpaceName = document.getElementById('current-space-name'); - if (isLoggedIn) { - loginForm.classList.add('hidden'); - logoutForm.classList.remove('hidden'); - loginStatus.textContent = 'Status: Logged in'; - loginStatus.className = 'status logged-in'; - currentSpaceName.textContent = currentSpace; - } else { - loginForm.classList.remove('hidden'); - logoutForm.classList.add('hidden'); - loginStatus.textContent = 'Status: Not logged in'; + try { + // Try to list keypairs to check if logged in + const keypairs = list_keypairs(); + + if (keypairs && keypairs.length > 0) { + loginStatus.textContent = 'Status: Logged in'; + loginStatus.className = 'status logged-in'; + + // Update keypairs list + updateKeypairsList(); + } else { + loginStatus.textContent = 'Status: Not logged in. Please login in the Main Crypto Demo page first.'; + loginStatus.className = 'status logged-out'; + + // Hide Ethereum wallet info when logged out + document.getElementById('ethereum-wallet-info').classList.add('hidden'); + hasEthereumWallet = false; + } + } catch (e) { + loginStatus.textContent = 'Status: Not logged in. Please login in the Main Crypto Demo page first.'; loginStatus.className = 'status logged-out'; - currentSpaceName.textContent = ''; // Hide Ethereum wallet info when logged out document.getElementById('ethereum-wallet-info').classList.add('hidden'); @@ -119,133 +112,8 @@ function updateKeypairsList() { } } -// Login to a space -async function performLogin() { - const spaceName = document.getElementById('space-name').value.trim(); - const password = document.getElementById('space-password').value; - - if (!spaceName || !password) { - document.getElementById('space-result').textContent = 'Please enter both space name and password'; - return; - } - - try { - // Get encrypted space from localStorage - const encryptedSpace = getSpaceFromStorage(spaceName); - if (!encryptedSpace) { - document.getElementById('space-result').textContent = `Space "${spaceName}" not found`; - return; - } - - // Decrypt the space - const result = decrypt_key_space(encryptedSpace, password); - if (result === 0) { - isLoggedIn = true; - currentSpace = spaceName; - updateLoginUI(); - updateKeypairsList(); - document.getElementById('space-result').textContent = `Successfully logged in to space "${spaceName}"`; - } else { - document.getElementById('space-result').textContent = `Error logging in: ${result}`; - } - } catch (e) { - document.getElementById('space-result').textContent = `Error: ${e}`; - } -} - -// Create a new space -async function performCreateSpace() { - const spaceName = document.getElementById('space-name').value.trim(); - const password = document.getElementById('space-password').value; - - if (!spaceName || !password) { - document.getElementById('space-result').textContent = 'Please enter both space name and password'; - return; - } - - // Check if space already exists - if (getSpaceFromStorage(spaceName)) { - document.getElementById('space-result').textContent = `Space "${spaceName}" already exists`; - return; - } - - try { - // Create new space - const result = create_key_space(spaceName); - if (result === 0) { - // Encrypt and save the space - const encryptedSpace = encrypt_key_space(password); - saveSpaceToStorage(spaceName, encryptedSpace); - - isLoggedIn = true; - currentSpace = spaceName; - updateLoginUI(); - updateKeypairsList(); - document.getElementById('space-result').textContent = `Successfully created space "${spaceName}"`; - } else { - document.getElementById('space-result').textContent = `Error creating space: ${result}`; - } - } catch (e) { - document.getElementById('space-result').textContent = `Error: ${e}`; - } -} - -// Logout from current space -function performLogout() { - logout(); - clear_ethereum_wallets(); - isLoggedIn = false; - currentSpace = null; - selectedKeypair = null; - hasEthereumWallet = false; - updateLoginUI(); - document.getElementById('space-result').textContent = 'Logged out successfully'; -} - -// Create a new keypair -async function performCreateKeypair() { - if (!isLoggedIn) { - document.getElementById('keypair-management-result').textContent = 'Please login first'; - return; - } - - const keypairName = document.getElementById('keypair-name').value.trim(); - - if (!keypairName) { - document.getElementById('keypair-management-result').textContent = 'Please enter a keypair name'; - return; - } - - try { - // Create new keypair - const result = create_keypair(keypairName); - if (result === 0) { - document.getElementById('keypair-management-result').textContent = `Successfully created keypair "${keypairName}"`; - - // Update keypairs list - updateKeypairsList(); - - // Select the new keypair - selectedKeypair = keypairName; - document.getElementById('select-keypair').value = keypairName; - - // Save the updated space to localStorage - saveCurrentSpace(); - } else { - document.getElementById('keypair-management-result').textContent = `Error creating keypair: ${result}`; - } - } catch (e) { - document.getElementById('keypair-management-result').textContent = `Error: ${e}`; - } -} - // Select a keypair async function performSelectKeypair() { - if (!isLoggedIn) { - document.getElementById('keypair-management-result').textContent = 'Please login first'; - return; - } - const keypairName = document.getElementById('select-keypair').value; if (!keypairName) { @@ -271,31 +139,8 @@ async function performSelectKeypair() { } } -// Save the current space to localStorage -function saveCurrentSpace() { - if (!isLoggedIn || !currentSpace) return; - - try { - const password = document.getElementById('space-password').value; - if (!password) { - console.error('Password not available for saving space'); - return; - } - - const encryptedSpace = encrypt_key_space(password); - saveSpaceToStorage(currentSpace, encryptedSpace); - } catch (e) { - console.error('Error saving space:', e); - } -} - // Create an Ethereum wallet from the selected keypair async function performCreateEthereumWallet() { - if (!isLoggedIn) { - document.getElementById('ethereum-wallet-result').textContent = 'Please login first'; - return; - } - if (!selectedKeypair) { document.getElementById('ethereum-wallet-result').textContent = 'Please select a keypair first'; return; @@ -322,9 +167,6 @@ async function performCreateEthereumWallet() { saveEthWalletToStorage(address, privateKey); document.getElementById('ethereum-wallet-result').textContent = 'Successfully created Ethereum wallet'; - - // Save the updated space to localStorage - saveCurrentSpace(); } else { document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`; } @@ -335,11 +177,6 @@ async function performCreateEthereumWallet() { // Create an Ethereum wallet from a name and the selected keypair async function performCreateEthereumWalletFromName() { - if (!isLoggedIn) { - document.getElementById('ethereum-wallet-result').textContent = 'Please login first'; - return; - } - if (!selectedKeypair) { document.getElementById('ethereum-wallet-result').textContent = 'Please select a keypair first'; return; @@ -373,9 +210,6 @@ async function performCreateEthereumWalletFromName() { saveEthWalletToStorage(address, privateKey); document.getElementById('ethereum-wallet-result').textContent = `Successfully created Ethereum wallet from name "${name}"`; - - // Save the updated space to localStorage - saveCurrentSpace(); } else { document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`; } @@ -386,11 +220,6 @@ async function performCreateEthereumWalletFromName() { // Create an Ethereum wallet from a private key async function performCreateEthereumWalletFromPrivateKey() { - if (!isLoggedIn) { - document.getElementById('ethereum-wallet-result').textContent = 'Please login first'; - return; - } - const privateKey = document.getElementById('private-key').value.trim(); if (!privateKey) { @@ -419,9 +248,6 @@ async function performCreateEthereumWalletFromPrivateKey() { saveEthWalletToStorage(address, displayPrivateKey); document.getElementById('ethereum-wallet-result').textContent = 'Successfully imported Ethereum wallet from private key'; - - // Save the updated space to localStorage - saveCurrentSpace(); } else { document.getElementById('ethereum-wallet-result').textContent = `Error importing Ethereum wallet: ${result}`; } @@ -493,13 +319,7 @@ async function run() { console.log('WebAssembly crypto module initialized!'); - // Set up the login/space management - document.getElementById('login-button').addEventListener('click', performLogin); - document.getElementById('create-space-button').addEventListener('click', performCreateSpace); - document.getElementById('logout-button').addEventListener('click', performLogout); - - // Set up the keypair management - document.getElementById('create-keypair-button').addEventListener('click', performCreateKeypair); + // Set up the keypair selection document.getElementById('select-keypair').addEventListener('change', performSelectKeypair); // Set up the Ethereum wallet management