clean code, and use newly created PortalBtcWallet library

This commit is contained in:
Danny Morabito 2025-07-09 18:51:42 +02:00
parent 9825c53c1c
commit 45dfe1a1c7
Signed by: dannym
GPG key ID: 7CC8056A5A04557E
22 changed files with 503 additions and 1087 deletions

View file

@ -1,26 +1,24 @@
<script lang="ts">
import { decryptMnemonic } from "$lib/wallet.svelte";
import {
decryptMnemonic,
hasMnemonic,
MNEMONIC_KEY,
openWallet,
} from "$lib/wallet.svelte";
import { browser } from "$app/environment";
import { onMount } from "svelte";
let {
open = $bindable(),
unlock,
}: { open: boolean; unlock: (pw: string) => void } = $props();
let { onunlock }: { onunlock: () => void } = $props();
let dialogEl: HTMLDialogElement | null = $state(null);
let password = $state("");
let error = $state("");
let isValidating = $state(false);
$effect(() => {
if (open) dialogEl?.showModal();
else dialogEl?.close();
onMount(() => {
dialogEl?.showModal();
});
function closeDialog() {
open = false;
}
async function attemptUnlock() {
if (!password.trim()) return;
@ -29,55 +27,52 @@
try {
if (!browser) throw new Error("Not in browser");
const encryptedSeed = localStorage.getItem("seed");
if (!encryptedSeed) throw new Error("No encrypted seed found");
await decryptMnemonic(encryptedSeed, password);
unlock(password);
password = "";
error = "";
if (!hasMnemonic()) throw new Error("No encrypted seed found");
const encryptedMnemonic = localStorage.getItem(MNEMONIC_KEY)!;
const mnemonic = await decryptMnemonic(encryptedMnemonic, password);
await openWallet(mnemonic);
onunlock();
dialogEl?.close();
} catch (err) {
error = "Incorrect password";
console.error("Password validation failed:", err);
} finally {
isValidating = false;
}
}
</script>
{#if open}
<dialog bind:this={dialogEl} onclose={closeDialog}>
<h2>Unlock Wallet</h2>
<p>Enter your wallet password to decrypt your seed.</p>
{#if error}
<p
style="color: var(--error-color); font-size: 0.7rem; margin-bottom: 1rem;"
>
{error}
</p>
{/if}
<input
type="password"
class="retro-input"
bind:value={password}
placeholder="Password"
<dialog bind:this={dialogEl}>
<h2>Unlock Wallet</h2>
<p>Enter your wallet password to decrypt your seed.</p>
{#if error}
<p
style="color: var(--error-color); font-size: 0.7rem; margin-bottom: 1rem;"
>
{error}
</p>
{/if}
<input
type="password"
class="retro-input"
bind:value={password}
placeholder="Password"
disabled={isValidating}
onkeydown={(e) => {
if (e.key === "Enter" && !isValidating) {
attemptUnlock();
}
}}
/>
<div style="margin-top:1rem;text-align:center;">
<button
class="retro-btn primary"
disabled={isValidating}
onkeydown={(e) => {
if (e.key === "Enter" && !isValidating) {
attemptUnlock();
}
}}
/>
<div style="margin-top:1rem;text-align:center;">
<button
class="retro-btn primary"
disabled={isValidating}
onclick={() => {
attemptUnlock();
}}>{isValidating ? "Validating..." : "Unlock"}</button
>
</div>
</dialog>
{/if}
onclick={() => {
attemptUnlock();
}}>{isValidating ? "Validating..." : "Unlock"}</button
>
</div>
</dialog>
<style>
dialog {