...
This commit is contained in:
parent
67cbb35156
commit
8569bb4bd8
@ -125,60 +125,21 @@
|
|||||||
<a href="ethereum.html">Ethereum Demo</a>
|
<a href="ethereum.html">Ethereum Demo</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Login/Space Management Section -->
|
<div class="note">Note: You must first login and create a keypair in the <a href="index.html">Main Crypto Demo</a> page.</div>
|
||||||
<div class="container" id="login-container">
|
|
||||||
<h2>Key Space Management</h2>
|
<!-- Keypair Selection Section -->
|
||||||
|
<div class="container" id="keypair-selection-container">
|
||||||
|
<h2>Select Keypair</h2>
|
||||||
|
|
||||||
<div id="login-status" class="status logged-out">
|
<div id="login-status" class="status logged-out">
|
||||||
Status: Not logged in
|
Status: Not logged in
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="login-form">
|
<div class="form-group">
|
||||||
<div class="form-group">
|
<label for="select-keypair">Select Keypair:</label>
|
||||||
<label for="space-name">Space Name:</label>
|
<select id="select-keypair">
|
||||||
<input type="text" id="space-name" placeholder="Enter space name" />
|
<option value="">-- Select a keypair --</option>
|
||||||
</div>
|
</select>
|
||||||
|
|
||||||
<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>
|
</div>
|
||||||
|
|
||||||
<div class="result" id="keypair-management-result">Result will appear here</div>
|
<div class="result" id="keypair-management-result">Result will appear here</div>
|
||||||
|
@ -33,20 +33,9 @@ function hexToBuffer(hex) {
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
// LocalStorage functions for key spaces
|
// LocalStorage functions for Ethereum wallets
|
||||||
const STORAGE_PREFIX = 'crypto_space_';
|
|
||||||
const ETH_WALLET_PREFIX = 'eth_wallet_';
|
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
|
// Save Ethereum wallet to localStorage
|
||||||
function saveEthWalletToStorage(address, privateKey) {
|
function saveEthWalletToStorage(address, privateKey) {
|
||||||
localStorage.setItem(`${ETH_WALLET_PREFIX}${address}`, privateKey);
|
localStorage.setItem(`${ETH_WALLET_PREFIX}${address}`, privateKey);
|
||||||
@ -58,30 +47,34 @@ function getEthWalletFromStorage(address) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Session state
|
// Session state
|
||||||
let isLoggedIn = false;
|
|
||||||
let currentSpace = null;
|
|
||||||
let selectedKeypair = null;
|
let selectedKeypair = null;
|
||||||
let hasEthereumWallet = false;
|
let hasEthereumWallet = false;
|
||||||
|
|
||||||
// Update UI based on login state
|
// Update UI based on login state
|
||||||
function updateLoginUI() {
|
function updateLoginUI() {
|
||||||
const loginForm = document.getElementById('login-form');
|
|
||||||
const logoutForm = document.getElementById('logout-form');
|
|
||||||
const loginStatus = document.getElementById('login-status');
|
const loginStatus = document.getElementById('login-status');
|
||||||
const currentSpaceName = document.getElementById('current-space-name');
|
|
||||||
|
|
||||||
if (isLoggedIn) {
|
try {
|
||||||
loginForm.classList.add('hidden');
|
// Try to list keypairs to check if logged in
|
||||||
logoutForm.classList.remove('hidden');
|
const keypairs = list_keypairs();
|
||||||
loginStatus.textContent = 'Status: Logged in';
|
|
||||||
loginStatus.className = 'status logged-in';
|
if (keypairs && keypairs.length > 0) {
|
||||||
currentSpaceName.textContent = currentSpace;
|
loginStatus.textContent = 'Status: Logged in';
|
||||||
} else {
|
loginStatus.className = 'status logged-in';
|
||||||
loginForm.classList.remove('hidden');
|
|
||||||
logoutForm.classList.add('hidden');
|
// Update keypairs list
|
||||||
loginStatus.textContent = 'Status: Not logged in';
|
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';
|
loginStatus.className = 'status logged-out';
|
||||||
currentSpaceName.textContent = '';
|
|
||||||
|
|
||||||
// Hide Ethereum wallet info when logged out
|
// Hide Ethereum wallet info when logged out
|
||||||
document.getElementById('ethereum-wallet-info').classList.add('hidden');
|
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
|
// Select a keypair
|
||||||
async function performSelectKeypair() {
|
async function performSelectKeypair() {
|
||||||
if (!isLoggedIn) {
|
|
||||||
document.getElementById('keypair-management-result').textContent = 'Please login first';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const keypairName = document.getElementById('select-keypair').value;
|
const keypairName = document.getElementById('select-keypair').value;
|
||||||
|
|
||||||
if (!keypairName) {
|
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
|
// Create an Ethereum wallet from the selected keypair
|
||||||
async function performCreateEthereumWallet() {
|
async function performCreateEthereumWallet() {
|
||||||
if (!isLoggedIn) {
|
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Please login first';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!selectedKeypair) {
|
if (!selectedKeypair) {
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Please select a keypair first';
|
document.getElementById('ethereum-wallet-result').textContent = 'Please select a keypair first';
|
||||||
return;
|
return;
|
||||||
@ -322,9 +167,6 @@ async function performCreateEthereumWallet() {
|
|||||||
saveEthWalletToStorage(address, privateKey);
|
saveEthWalletToStorage(address, privateKey);
|
||||||
|
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Successfully created Ethereum wallet';
|
document.getElementById('ethereum-wallet-result').textContent = 'Successfully created Ethereum wallet';
|
||||||
|
|
||||||
// Save the updated space to localStorage
|
|
||||||
saveCurrentSpace();
|
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`;
|
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
|
// Create an Ethereum wallet from a name and the selected keypair
|
||||||
async function performCreateEthereumWalletFromName() {
|
async function performCreateEthereumWalletFromName() {
|
||||||
if (!isLoggedIn) {
|
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Please login first';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!selectedKeypair) {
|
if (!selectedKeypair) {
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Please select a keypair first';
|
document.getElementById('ethereum-wallet-result').textContent = 'Please select a keypair first';
|
||||||
return;
|
return;
|
||||||
@ -373,9 +210,6 @@ async function performCreateEthereumWalletFromName() {
|
|||||||
saveEthWalletToStorage(address, privateKey);
|
saveEthWalletToStorage(address, privateKey);
|
||||||
|
|
||||||
document.getElementById('ethereum-wallet-result').textContent = `Successfully created Ethereum wallet from name "${name}"`;
|
document.getElementById('ethereum-wallet-result').textContent = `Successfully created Ethereum wallet from name "${name}"`;
|
||||||
|
|
||||||
// Save the updated space to localStorage
|
|
||||||
saveCurrentSpace();
|
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('ethereum-wallet-result').textContent = `Error creating Ethereum wallet: ${result}`;
|
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
|
// Create an Ethereum wallet from a private key
|
||||||
async function performCreateEthereumWalletFromPrivateKey() {
|
async function performCreateEthereumWalletFromPrivateKey() {
|
||||||
if (!isLoggedIn) {
|
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Please login first';
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const privateKey = document.getElementById('private-key').value.trim();
|
const privateKey = document.getElementById('private-key').value.trim();
|
||||||
|
|
||||||
if (!privateKey) {
|
if (!privateKey) {
|
||||||
@ -419,9 +248,6 @@ async function performCreateEthereumWalletFromPrivateKey() {
|
|||||||
saveEthWalletToStorage(address, displayPrivateKey);
|
saveEthWalletToStorage(address, displayPrivateKey);
|
||||||
|
|
||||||
document.getElementById('ethereum-wallet-result').textContent = 'Successfully imported Ethereum wallet from private key';
|
document.getElementById('ethereum-wallet-result').textContent = 'Successfully imported Ethereum wallet from private key';
|
||||||
|
|
||||||
// Save the updated space to localStorage
|
|
||||||
saveCurrentSpace();
|
|
||||||
} else {
|
} else {
|
||||||
document.getElementById('ethereum-wallet-result').textContent = `Error importing Ethereum wallet: ${result}`;
|
document.getElementById('ethereum-wallet-result').textContent = `Error importing Ethereum wallet: ${result}`;
|
||||||
}
|
}
|
||||||
@ -493,13 +319,7 @@ async function run() {
|
|||||||
|
|
||||||
console.log('WebAssembly crypto module initialized!');
|
console.log('WebAssembly crypto module initialized!');
|
||||||
|
|
||||||
// Set up the login/space management
|
// Set up the keypair selection
|
||||||
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);
|
|
||||||
document.getElementById('select-keypair').addEventListener('change', performSelectKeypair);
|
document.getElementById('select-keypair').addEventListener('change', performSelectKeypair);
|
||||||
|
|
||||||
// Set up the Ethereum wallet management
|
// Set up the Ethereum wallet management
|
||||||
|
Loading…
Reference in New Issue
Block a user