From: Chris Duncan Date: Wed, 16 Oct 2024 05:53:05 +0000 (-0700) Subject: Generalize wallet creation. Start adding wallet form dialog. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=84d763600c7f65de7648d34bcc3af26c2cd5ff44;p=nemo-wallet.git Generalize wallet creation. Start adding wallet form dialog. --- diff --git a/app/pages/nemo-wallet.html b/app/pages/nemo-wallet.html index 207456b..5a4d1d8 100644 --- a/app/pages/nemo-wallet.html +++ b/app/pages/nemo-wallet.html @@ -32,25 +32,40 @@ const notification = document.getElementById('notification') observer.observe(notification, { attributes: true }) } - async function createBip44Wallet () { - const wallet = await Bip44Wallet.create('password') - try { - addToStorage('Bip44Wallets', wallet.id) - } catch (err) { - notify.error(err.msg) + function getWalletType (type) { + switch (type) { + case 'blake2b': { + return Blake2bWallet + } + case 'bip44': + default: { + return Bip44Wallet + } } - return wallet } - async function createBlake2bWallet () { - const wallet = await Blake2bWallet.create('password') + async function createWallet (el, type) { + const label = el.firstElementChild + const spinner = document.createElement('x-throbber') + spinner.size = 'small' + spinner.style.width = `${label.scrollWidth}px` + el.replaceChildren(spinner) + const walletType = getWalletType(type) + let wallet try { - addToStorage('Blake2bWallets', wallet.id) + wallet = await walletType.create('password') + const walletData = { + id: wallet.id, + type + } + addToStorage(`wallets`, JSON.stringify(walletData)) } catch (err) { notify.error(err.msg) + } finally { + el.replaceChildren(label) + return wallet } - return wallet } - async function addToStorage (key, value) { + function addToStorage (key, value) { if (typeof value !== 'string') { throw new TypeError(`Cannot add ${typeof value} to storage`) } @@ -62,20 +77,26 @@ sessionStorage.setItem(key, JSON.stringify(item)) } async function showWallets () { - console.log(`showWallets()`) - const card = document.getElementById('wallets') - const bip44WalletIds = JSON.parse(sessionStorage.getItem('Bip44Wallets') ?? '[]') - console.log(`bip44WalletIds: ${bip44WalletIds}`) - for (const id of bip44WalletIds) { - console.log(`id: ${id}`) - const wallet = await Bip44Wallet.restore(id) + const card = document.getElementById('walletCard') + const storedWalletData = JSON.parse(sessionStorage.getItem('wallets') ?? '[]') + const wallets = [] + for (const walletData of storedWalletData) { + const w = JSON.parse(walletData) + console.dir(w) + if (w.id && w.type) { + const walletType = getWalletType(w.type) + wallets.push(await walletType.restore(w.id)) + } + } + const walletElements = [] + for (const wallet of wallets) { const walletElement = document.createElement('x-label') await wallet.unlock('password') - console.log(`wallet: ${wallet}`) walletElement.innerText = wallet.mnemonic - wallet.lock('password') - card.appendChild(walletElement) + await wallet.lock('password') + walletElements.push(walletElement) } + card.replaceChildren(...walletElements) } async function clearStorage () { sessionStorage.clear() @@ -121,10 +142,36 @@ - - Create BIP-44 Wallet - - + + + Wallets + + + + New + + + Type + BIP-44 + BLAKE2b + + Password + + + + + + + Import + + + + + + + + Clear Storage