]> zoso.dev Git - nemo-wallet.git/commitdiff
Refactor wallet function to save wallet IDs for later. Fix weird function naming...
authorChris Duncan <chris@zoso.dev>
Mon, 14 Oct 2024 10:51:17 +0000 (03:51 -0700)
committerChris Duncan <chris@zoso.dev>
Mon, 14 Oct 2024 10:51:17 +0000 (03:51 -0700)
app/pages/nemo-wallet.html

index a3ad90662c1a689f7d07cf6bb4b77f4d09e5d1bc..f92c95edb02407873dadf9a264abe762a2a3cfe8 100644 (file)
       Bip44Wallet = libnemo.Bip44Wallet
       console.log('loaded libnemo')
     }
-    async function wallet () {
+    async function createWallet () {
       const wallet = await Bip44Wallet.create('password')
-      await wallet.unlock('password')
+      const bip44WalletIds = JSON.parse(sessionStorage.getItem('Bip44Wallets') ?? '[]')
+      bip44WalletIds.push(wallet.id)
+      sessionStorage.setItem('Bip44Wallets', JSON.stringify(bip44WalletIds))
       return wallet
     }
     async function showWallets () {
+      console.log(`showWallets()`)
       const card = document.getElementById('wallet')
-      for (let i = 0; i < sessionStorage.length; i++) {
-        const name = sessionStorage.key(i)
-        const item = sessionStorage.getItem(name)
-        console.log(item)
+      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 walletElement = document.createElement('x-label')
-        walletElement.innerText = item
+        await wallet.unlock('password')
+        console.log(`wallet: ${wallet}`)
+        walletElement.innerText = wallet.mnemonic
+        wallet.lock('password')
         card.appendChild(walletElement)
       }
     }
-    function clear () {
+    async function clearStorage () {
+      console.log('clear()')
       sessionStorage.clear()
     }
   </script>
 
 <body onload="load()">
   <x-box id="main" style="visibility:hidden;">
-    <x-button onclick="wallet().then(w => {console.log(w);showWallets()})">
+    <x-button onclick="createWallet().then(w => { console.log(w);showWallets() })">
       <x-label>Create</x-label>
     </x-button>
     <x-card id="wallet">
     </x-card>
-    <x-button onclick="clear()">
+    <x-button onclick="clearStorage()">
       <x-label>Clear</x-label>
     </x-button>
   </x-box>