From: Chris Duncan Date: Sun, 20 Oct 2024 05:41:16 +0000 (-0700) Subject: Adjust some variable names to align wallet creation and import a little better and... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=35a1eb88a0720c16c36589862eb4233b017d6953;p=nemo-wallet.git Adjust some variable names to align wallet creation and import a little better and reuse some code. Create handler for closing dialog forms. --- diff --git a/app/pages/nemo-wallet.html b/app/pages/nemo-wallet.html index c30255d..dafe433 100644 --- a/app/pages/nemo-wallet.html +++ b/app/pages/nemo-wallet.html @@ -48,8 +48,8 @@ document.querySelector(newHash).classList.add('show') } } - function getWalletType (type) { - switch (type) { + function getWalletClass (algorithm) { + switch (algorithm) { case 'blake2b': { return Blake2bWallet } @@ -108,38 +108,34 @@ async function createWallet () { const walletSelect = document.querySelector('#wallet') - const form = document.querySelector('#wallet-form') - const name = form.querySelector('#wallet-form-name x-input').value + const form = document.querySelector('#wallet-new') + const name = form.querySelector('#wallet-new-name x-input').value if (name === '_ledger') { - walletSelect.value = null notify.warn(`Invalid wallet name "${name}".`) return } - const type = form.querySelector('#wallet-form-wallet-type x-radios').value - const walletType = getWalletType(type) - const salt = form.querySelector('#wallet-form-salt x-input')?.value - const password = form.querySelector('#wallet-form-password x-input')?.value + const algorithm = form.querySelector('#wallet-new-algorithm x-radios').value + const walletClass = getWalletClass(algorithm) + const salt = form.querySelector('#wallet-new-salt x-input')?.value + const password = form.querySelector('#wallet-new-password x-input')?.value if (!password) { - walletSelect.value = null notify.error('Password is required') return } let wallet try { - wallet = await walletType.create(password, salt) + wallet = await walletClass.create(password, salt) const walletData = { id: wallet.id, name: name || wallet.id, - type + algorithm } await addToStorage(`wallets`, JSON.stringify(walletData)) const menu = document.querySelector('#wallet > x-menu') const menuitem = document.createElement('x-menuitem') menuitem.value = walletData.id - if (menu.childElementCount >= 0) { - menu.appendChild(menuitem) - } + menu.appendChild(menuitem) const label = document.createElement('x-label') label.innerText = walletData.name menuitem.appendChild(label) @@ -147,7 +143,6 @@ } catch (err) { label?.remove() menuitem?.remove() - walletSelect.value = null notify.error(err.msg) } finally { await form.close() @@ -174,20 +169,40 @@ } }) } - function updateWalletForm () { - const form = document.querySelector('#wallet-form') - const walletTypeContainer = document.querySelector('#wallet-form-wallet-type') - const walletType = walletTypeContainer.firstChild - const importTypeContainer = document.querySelector('#wallet-form-import-type') - const importType = importTypeContainer.firstChild - const saltContainer = document.querySelector('#wallet-form-salt') - if (walletType.value === 'bip44' && importType.value !== 'seed') { + + function updateWalletForm (id) { + const form = document.querySelector(id) + if (!form) { + notify.error(`Form ID not found, contact developer`) + return + } + const algorithmContainer = document.querySelector(`${id}-algorithm`) + const algorithm = document.querySelector(`${id}-algorithm x-radios`) + const typeContainer = document.querySelector(`${id}-type`) + const type = document.querySelector(`${id}-type x-select`) + const saltContainer = document.querySelector(`${id}-salt`) + const salt = document.querySelector(`${id}-salt x-input`) + if (algorithm.value === 'bip44' && type?.value !== 'seed') { saltContainer.classList.remove('hide') } else { saltContainer.classList.add('hide') } - document.querySelector('#wallet-form-import-value x-label').innerText = importType.value + const importValue = document.querySelector(`${id}-value x-input`) + const importValueLabel = document.querySelector(`${id}-value x-label`) + if (importValueLabel) { + importValueLabel.innerText = type.value + } } + + async function closeForm (id) { + const form = document.querySelector(id) + const inputs = form.querySelector('x-input') + for (const input of inputs) { + input.value = null + } + await form.close() + } + async function showWallets () { const card = document.querySelector('#walletCard') const storedWalletData = JSON.parse(sessionStorage.getItem('wallets') ?? '[]') @@ -196,7 +211,7 @@ const w = JSON.parse(walletData) console.dir(w) if (w.id && w.name && w.type) { - const walletType = getWalletType(w.type) + const walletType = getWalletClass(w.type) wallets.push(await walletType.restore(w.id)) } } @@ -327,11 +342,12 @@ - +