aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/account
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-16 13:17:43 -0800
committerAdam Soltys <[email protected]>2026-02-16 13:17:43 -0800
commitac06683100e089ad589661b14ebaf5dc9582a256 (patch)
treef561f90aa09f3a6a3747cd703984043f028f9067 /src/routes/(app)/account
parente39bf4f977bba2952a7de4a307b14c5ec862a3a7 (diff)
downloadcoinos-ui-ac06683100e089ad589661b14ebaf5dc9582a256.tar.gz
coinos-ui-ac06683100e089ad589661b14ebaf5dc9582a256.zip
passkey tweaks
Diffstat (limited to 'src/routes/(app)/account')
-rw-r--r--src/routes/(app)/account/[id]/+page.svelte16
-rw-r--r--src/routes/(app)/account/ark/+page.svelte103
-rw-r--r--src/routes/(app)/account/pass/+page.svelte43
-rw-r--r--src/routes/(app)/account/seed/+page.svelte46
4 files changed, 200 insertions, 8 deletions
diff --git a/src/routes/(app)/account/[id]/+page.svelte b/src/routes/(app)/account/[id]/+page.svelte
index 157dd096..cfcb6dea 100644
--- a/src/routes/(app)/account/[id]/+page.svelte
+++ b/src/routes/(app)/account/[id]/+page.svelte
@@ -11,7 +11,8 @@
import { tick } from "svelte";
import { entropyToMnemonic, mnemonicToSeed } from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english.js";
- import { getRememberedWalletPassword, forgetWalletPassword } from "$lib/passwordCache";
+ import { getRememberedWalletPassword, forgetWalletPassword, getCachedPrfKey } from "$lib/passwordCache";
+ import { isPrfEncrypted, prfDecrypt } from "$lib/crypto";
let { data } = $props();
let account = $derived(data.account);
@@ -70,6 +71,18 @@
let isCustodial = $derived(!seed && account.type !== "ark");
+ let revealWithPrfKey = async () => {
+ const prfKey = getCachedPrfKey();
+ if (!prfKey || !seed || !isPrfEncrypted(seed)) return false;
+ try {
+ const entropy = await prfDecrypt(prfKey, seed);
+ mnemonic = entropyToMnemonic(entropy, wordlist);
+ return true;
+ } catch (e) {
+ return false;
+ }
+ };
+
let revealWithCachedPassword = async () => {
const cached = getRememberedWalletPassword();
if (!cached) return false;
@@ -86,6 +99,7 @@
};
let requestMnemonic = async () => {
+ if (await revealWithPrfKey()) return;
if (await revealWithCachedPassword()) return;
toggle();
};
diff --git a/src/routes/(app)/account/ark/+page.svelte b/src/routes/(app)/account/ark/+page.svelte
index ce843e11..7725f81a 100644
--- a/src/routes/(app)/account/ark/+page.svelte
+++ b/src/routes/(app)/account/ark/+page.svelte
@@ -17,12 +17,17 @@
rememberWalletPassword,
forgetWalletPassword,
getRememberedWalletPassword,
+ getCachedPrfKey,
+ rememberPrfKey,
} from "$lib/passwordCache";
+ import { isPrfEncrypted, prfDecrypt } from "$lib/crypto";
+ import { loginWithPasskey } from "$lib/passkey";
let { data } = $props();
let user = $derived(data.user);
let hasMasterSeed = $derived(!!user.seed);
+ let hasPrfSeed = $derived(!!user.seed && isPrfEncrypted(user.seed));
let privateKey = $state("");
let arkAddress = $state("");
@@ -30,6 +35,7 @@
let showNsec = $state(false);
let confirmed = $state(false);
let submitting = $state(false);
+ let needsPasskeyUnlock = $state(false);
let password = $state("");
let confirm = $state("");
@@ -40,9 +46,51 @@
let name = "Ark Vault";
let type = "ark";
+ let deriveFromPrfKey = async (prfKey: ArrayBuffer) => {
+ try {
+ const { SingleKey, Wallet } = await import("@arkade-os/sdk");
+ const entropy = await prfDecrypt(prfKey, user.seed);
+ const mnemonic = entropyToMnemonic(entropy, wordlist);
+ const seed = await mnemonicToSeed(mnemonic);
+ const master = HDKey.fromMasterSeed(seed, versions);
+ const arkChild = master.derive("m/86'/0'/0'/0/0");
+ const arkHex = bytesToHex(arkChild.privateKey!);
+ const identity = SingleKey.fromHex(arkHex);
+ const wallet = await Wallet.create({ identity, arkServerUrl });
+ arkAddress = await wallet.getAddress();
+ confirmed = true;
+ } catch (e: any) {
+ console.log("PRF seed derivation failed", e);
+ fail("Failed to decrypt seed");
+ }
+ };
+
+ let unlockWithPasskey = async () => {
+ try {
+ const { prfKey } = await loginWithPasskey();
+ if (prfKey) {
+ rememberPrfKey(prfKey, defaultRememberForMs);
+ await deriveFromPrfKey(prfKey);
+ needsPasskeyUnlock = false;
+ } else {
+ fail("Passkey does not support PRF");
+ }
+ } catch (e: any) {
+ if (e.name !== "NotAllowedError") {
+ fail(e.message || "Passkey unlock failed");
+ }
+ }
+ };
+
onMount(async () => {
- if (hasMasterSeed) {
- // Try cached password first
+ if (hasPrfSeed) {
+ const prfKey = getCachedPrfKey();
+ if (prfKey) {
+ await deriveFromPrfKey(prfKey);
+ } else {
+ needsPasskeyUnlock = true;
+ }
+ } else if (hasMasterSeed) {
const cached = getRememberedWalletPassword();
if (cached) {
password = cached;
@@ -96,6 +144,25 @@
};
let submit = async () => {
+ if (hasPrfSeed) {
+ submitting = true;
+ await tick();
+ try {
+ if (!arkAddress) {
+ const prfKey = getCachedPrfKey();
+ if (prfKey) await deriveFromPrfKey(prfKey);
+ else throw new Error("PRF key not available");
+ }
+ await post("/account", { name, type, arkAddress });
+ goto(`/${user.username}`);
+ } catch (e: any) {
+ console.log(e);
+ fail(e.message);
+ }
+ submitting = false;
+ return;
+ }
+
if (hasMasterSeed) {
if (!password) {
fail("Password required");
@@ -162,7 +229,37 @@
</div>
<div class="container w-full mx-auto text-lg px-4 max-w-xl space-y-5">
- {#if hasMasterSeed}
+ {#if hasPrfSeed}
+ {#if needsPasskeyUnlock}
+ <div class="space-y-5">
+ <p class="text-secondary">
+ Your Ark wallet will be derived from your master seed. Unlock with your passkey to continue.
+ </p>
+ <button type="button" class="btn btn-accent" onclick={unlockWithPasskey}>
+ <iconify-icon noobserver icon="ph:fingerprint-bold" width="24"></iconify-icon>
+ Unlock with Passkey
+ </button>
+ </div>
+ {:else}
+ <form onsubmit={preventDefault(submit)} class="space-y-5">
+ <p class="text-secondary">
+ Your Ark wallet will be derived from your passkey-protected master seed.
+ </p>
+ <div class="flex gap-2">
+ <a href={`/${user.username}`} class="contents">
+ <button type="button" class="btn !w-auto grow">Back</button>
+ </a>
+ <button disabled={submitting} type="submit" class="btn btn-accent !w-auto grow">
+ {#if submitting}
+ <Spinner />
+ {:else}
+ Create Wallet
+ {/if}
+ </button>
+ </div>
+ </form>
+ {/if}
+ {:else if hasMasterSeed}
<form onsubmit={preventDefault(submit)} class="space-y-5">
<p class="text-secondary">
Your Ark wallet will be derived from your master seed. Enter your wallet password to continue.
diff --git a/src/routes/(app)/account/pass/+page.svelte b/src/routes/(app)/account/pass/+page.svelte
index 348e7538..ba1e878c 100644
--- a/src/routes/(app)/account/pass/+page.svelte
+++ b/src/routes/(app)/account/pass/+page.svelte
@@ -15,7 +15,9 @@
defaultRememberForMs,
rememberWalletPassword,
forgetWalletPassword,
+ getCachedPrfKey,
} from "$lib/passwordCache";
+ import { isPrfEncrypted, prfEncrypt } from "$lib/crypto";
let { data }: any = $props();
@@ -28,8 +30,45 @@
revealConfirm = $state();
let rememberForMs = $state(defaultRememberForMs);
- onMount(() => {
- if (!$mnemonic) goto("/account/new");
+ onMount(async () => {
+ if (!$mnemonic) {
+ goto("/account/new");
+ return;
+ }
+
+ // PRF users: encrypt seed and create account immediately, skip password
+ const prfKey = getCachedPrfKey();
+ if (prfKey) {
+ submitting = true;
+ await tick();
+ try {
+ const entropy = mnemonicToEntropy($mnemonic as string, wordlist);
+ const encryptedSeed = await prfEncrypt(prfKey, entropy);
+ await post("/post/user", { seed: encryptedSeed });
+
+ const master = HDKey.fromMasterSeed(
+ await mnemonicToSeed($mnemonic as string),
+ versions,
+ );
+ const child = master.derive("m/84'/0'/0'");
+ const pubkey = child.publicExtendedKey;
+ const fingerprint = child.fingerprint.toString(16).padStart(8, "0");
+ await post("/account", {
+ fingerprint,
+ pubkey,
+ name: $t("accounts.savings"),
+ type: "bitcoin",
+ accountIndex: 0,
+ });
+
+ $mnemonic = "";
+ goto(`/${user.username}`);
+ } catch (e: any) {
+ console.log(e);
+ fail(e.message);
+ }
+ submitting = false;
+ }
});
let submit = async () => {
diff --git a/src/routes/(app)/account/seed/+page.svelte b/src/routes/(app)/account/seed/+page.svelte
index 76d457f6..994a4422 100644
--- a/src/routes/(app)/account/seed/+page.svelte
+++ b/src/routes/(app)/account/seed/+page.svelte
@@ -2,17 +2,53 @@
import Mnemonic from "$comp/Mnemonic.svelte";
import { browser } from "$app/environment";
import { tick, onMount } from "svelte";
- import { copy } from "$lib/utils";
+ import { copy, versions, fail, post } from "$lib/utils";
import { t } from "$lib/translations";
import { mnemonic } from "$lib/store";
- import { generateMnemonic } from "@scure/bip39";
+ import { generateMnemonic, mnemonicToSeed, entropyToMnemonic } from "@scure/bip39";
import { wordlist } from "@scure/bip39/wordlists/english.js";
+ import { HDKey } from "@scure/bip32";
+ import { goto } from "$app/navigation";
+ import { isPrfEncrypted, prfDecrypt } from "$lib/crypto";
+ import { getCachedPrfKey } from "$lib/passwordCache";
+ import Spinner from "$comp/Spinner.svelte";
let { data } = $props();
let user = $derived(data.user);
+ let creating = $state(false);
onMount(async () => {
if (!browser) return;
+ if (user.seed && isPrfEncrypted(user.seed)) {
+ const prfKey = getCachedPrfKey();
+ if (prfKey) {
+ creating = true;
+ try {
+ const entropy = await prfDecrypt(prfKey, user.seed);
+ const mn = entropyToMnemonic(entropy, wordlist);
+ const seed = await mnemonicToSeed(mn);
+ const master = HDKey.fromMasterSeed(seed, versions);
+ const child = master.derive("m/84'/0'/0'");
+ const pubkey = child.publicExtendedKey;
+ const fingerprint = child.fingerprint.toString(16).padStart(8, "0");
+ await post("/account", {
+ fingerprint,
+ pubkey,
+ name: $t("accounts.savings"),
+ type: "bitcoin",
+ accountIndex: 0,
+ });
+ goto(`/${user.username}`);
+ } catch (e: any) {
+ console.log(e);
+ fail(e.message);
+ creating = false;
+ }
+ } else {
+ goto(`/${user.username}`);
+ }
+ return;
+ }
if (!$mnemonic) await generate();
});
@@ -21,6 +57,11 @@
};
</script>
+{#if creating}
+ <div class="flex justify-center py-20">
+ <Spinner />
+ </div>
+{:else}
<div class="space-y-5">
<h1 class="text-center text-3xl font-semibold">{$t("accounts.seedPhrase")}</h1>
@@ -44,3 +85,4 @@
</div>
</div>
</div>
+{/if}