...
This commit is contained in:
parent
67cbb35156
commit
8569bb4bd8
@ -125,60 +125,21 @@
|
||||
<a href="ethereum.html">Ethereum Demo</a>
|
||||
</div>
|
||||
|
||||
<!-- Login/Space Management Section -->
|
||||
<div class="container" id="login-container">
|
||||
<h2>Key Space Management</h2>
|
||||
<div class="note">Note: You must first login and create a keypair in the <a href="index.html">Main Crypto Demo</a> page.</div>
|
||||
|
||||
<!-- Keypair Selection Section -->
|
||||
<div class="container" id="keypair-selection-container">
|
||||
<h2>Select Keypair</h2>
|
||||
|
||||
<div id="login-status" class="status logged-out">
|
||||
Status: Not logged in
|
||||
</div>
|
||||
|
||||
<div id="login-form">
|
||||
<div class="form-group">
|
||||
<label for="space-name">Space Name:</label>
|
||||
<input type="text" id="space-name" placeholder="Enter space name" />
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="space-password">Password:</label>
|
||||
<input type="password" id="space-password" placeholder="Enter password" />
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<button id="login-button">Login</button>
|
||||
<button id="create-space-button">Create New Space</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="logout-form" class="hidden">
|
||||
<div class="form-group">
|
||||
<label>Current Space: <span id="current-space-name"></span></label>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<button id="logout-button" class="danger">Logout</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="result" id="space-result">Result will appear here</div>
|
||||
</div>
|
||||
|
||||
<!-- Keypair Management Section -->
|
||||
<div class="container" id="keypair-management-container">
|
||||
<h2>Keypair Management</h2>
|
||||
|
||||
<div id="keypair-form">
|
||||
<div class="form-group">
|
||||
<label for="keypair-name">Keypair Name:</label>
|
||||
<input type="text" id="keypair-name" placeholder="Enter keypair name" />
|
||||
<button id="create-keypair-button">Create Keypair</button>
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<label for="select-keypair">Select Keypair:</label>
|
||||
<select id="select-keypair">
|
||||
<option value="">-- Select a keypair --</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="select-keypair">Select Keypair:</label>
|
||||
<select id="select-keypair">
|
||||
<option value="">-- Select a keypair --</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="result" id="keypair-management-result">Result will appear here</div>
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user