From b22c813f9fb3b4d69dade75d22e90e6779919f07 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 16 Feb 2026 20:56:39 -0800 Subject: nostr key derivation --- src/routes/(app)/account/[id]/+page.svelte | 103 ++++++++++++++++++----- src/routes/(app)/account/ark/+page.svelte | 92 +++++++++++++------- src/routes/(app)/account/bitcoin/+page.server.ts | 5 ++ src/routes/(app)/account/import/+page.svelte | 2 +- src/routes/(app)/account/new/+page.server.ts | 8 +- src/routes/(app)/account/pass/+page.svelte | 15 ++-- src/routes/(app)/account/seed/+page.svelte | 16 ++-- src/routes/(app)/account/setup/+page.svelte | 2 +- 8 files changed, 167 insertions(+), 76 deletions(-) create mode 100644 src/routes/(app)/account/bitcoin/+page.server.ts (limited to 'src/routes/(app)/account') diff --git a/src/routes/(app)/account/[id]/+page.svelte b/src/routes/(app)/account/[id]/+page.svelte index 0bab9c1b..674cb067 100644 --- a/src/routes/(app)/account/[id]/+page.svelte +++ b/src/routes/(app)/account/[id]/+page.svelte @@ -9,9 +9,12 @@ import { enhance } from "$app/forms"; import { t } from "$lib/translations"; import { tick } from "svelte"; - import { entropyToMnemonic, mnemonicToSeed } from "@scure/bip39"; + import { entropyToMnemonic, mnemonicToSeed, validateMnemonic, mnemonicToEntropy } from "@scure/bip39"; import { wordlist } from "@scure/bip39/wordlists/english.js"; + import { HDKey } from "@scure/bip32"; + import { versions } from "$lib/utils"; import { getRememberedWalletPassword, forgetWalletPassword, getCachedPrfKey } from "$lib/passwordCache"; + import Spinner from "$comp/Spinner.svelte"; let { data } = $props(); let account = $derived(data.account); @@ -70,8 +73,45 @@ let isCustodial = $derived(!seed && account.type !== "ark"); + let importing = $state(false); + let importText = $state(""); + let importSubmitting = $state(false); + + let importSeed = async () => { + let text = importText.replace(/,/g, " ").trim(); + if (!validateMnemonic(text, wordlist)) { + fail($t("accounts.invalidSeedPhrase")); + return; + } + + importSubmitting = true; + try { + let s = await mnemonicToSeed(text); + let master = HDKey.fromMasterSeed(s, versions); + let child = master.derive(`m/84'/0'/${account.accountIndex ?? 0}'`); + let pubkey = child.publicExtendedKey; + let fp = child.fingerprint.toString(16).padStart(8, "0"); + + await post(`/post/account/${id}`, { + fingerprint: fp, + pubkey, + }); + + importing = false; + importText = ""; + goto(`/${user.username}`, { invalidateAll: true }); + } catch (e: any) { + fail(e.message || $t("accounts.importFailed")); + } + importSubmitting = false; + }; + let revealWithPrfKey = async () => { - const prfKey = getCachedPrfKey(); + let prfKey = getCachedPrfKey(); + if (!prfKey) { + const { getWalletEntropy } = await import("$lib/walletEntropy"); + prfKey = await getWalletEntropy(); + } if (!prfKey) return false; try { const entropy = new Uint8Array(prfKey); @@ -110,7 +150,7 @@ let entropy = await decrypt(seed, password as string); mnemonic = entropyToMnemonic(entropy, wordlist); } catch (e: any) { - fail("Invalid password, try again"); + fail($t("accounts.invalidPasswordTryAgain")); passwordPrompt = true; } }); @@ -158,6 +198,7 @@ + {#if isCustodial}
@@ -192,23 +233,13 @@

{$t("user.settings.thresholdDesc")}

- -
- - -

{$t("user.settings.reserveDesc")}

-
{/if} + +
{#if mnemonic} @@ -224,17 +255,45 @@ {/if} + {#if account.type === "bitcoin"} + {#if importing} +
+ +
+ + +
+
+ {:else} + + {/if} + {/if} + {#if seed || account.type === "ark"} {/if} +
- @@ -254,7 +313,7 @@ bind:submit={doneReserve} bind:element={thresholdEl} /> - + {/if} @@ -270,7 +329,7 @@ bind:submit={doneReserve} bind:element={reserveEl} /> - + {/if} diff --git a/src/routes/(app)/account/ark/+page.svelte b/src/routes/(app)/account/ark/+page.svelte index f2bc8991..b6bec5b7 100644 --- a/src/routes/(app)/account/ark/+page.svelte +++ b/src/routes/(app)/account/ark/+page.svelte @@ -26,7 +26,8 @@ let user = $derived(data.user); let hasMasterSeed = $derived(!!user.seed); - let hasPrfSeed = $derived(!!getCachedPrfKey()); + let hasPrfSeed = $state(!!getCachedPrfKey()); + let hasSigner = $state(false); let privateKey = $state(""); let arkAddress = $state(""); @@ -42,7 +43,7 @@ let revealConfirm = $state(false); let rememberForMs = $state(defaultRememberForMs); - let name = "Savings"; + let name = $t("accounts.savings"); let type = "ark"; let deriveFromPrfKey = async (prfKey: ArrayBuffer) => { @@ -60,7 +61,19 @@ confirmed = true; } catch (e: any) { console.log("PRF seed derivation failed", e); - fail("Failed to decrypt seed"); + fail($t("accounts.failedToDecryptSeed")); + } + }; + + let unlockWithNostr = async () => { + try { + const { deriveNostrEntropy } = await import("$lib/walletEntropy"); + const entropy = await deriveNostrEntropy(); + hasPrfSeed = true; + await deriveFromPrfKey(entropy); + needsPasskeyUnlock = false; + } catch (e: any) { + fail(e.message || $t("accounts.passkeyUnlockFailed")); } }; @@ -72,16 +85,29 @@ await deriveFromPrfKey(prfKey); needsPasskeyUnlock = false; } else { - fail("Passkey does not support PRF"); + fail($t("accounts.passkeyNoPrf")); } } catch (e: any) { if (e.name !== "NotAllowedError") { - fail(e.message || "Passkey unlock failed"); + fail(e.message || $t("accounts.passkeyUnlockFailed")); } } }; onMount(async () => { + hasSigner = !!localStorage.getItem("signer"); + + // Try wallet entropy (covers both cached prfKey and silent nsec derivation) + if (!hasPrfSeed) { + const { getWalletEntropy } = await import("$lib/walletEntropy"); + const entropy = await getWalletEntropy(); + if (entropy) { + hasPrfSeed = true; + await deriveFromPrfKey(entropy); + return; + } + } + if (hasPrfSeed) { const prfKey = getCachedPrfKey(); if (prfKey) { @@ -117,7 +143,7 @@ confirmed = true; } catch (e: any) { console.log("master seed derivation failed", e); - fail("Invalid password"); + fail($t("accounts.invalidPassword")); } }; @@ -164,7 +190,7 @@ if (hasMasterSeed) { if (!password) { - fail("Password required"); + fail($t("accounts.passwordRequired")); return; } @@ -190,7 +216,7 @@ } if (!confirmed) { - fail("Please backup your key first"); + fail($t("accounts.pleaseBackupFirst")); return; } @@ -224,7 +250,7 @@
-

Create Savings Account

+

{$t("accounts.createSavingsAccount")}

@@ -232,27 +258,33 @@ {#if needsPasskeyUnlock}

- Your Ark wallet will be derived from your master seed. Unlock with your passkey to continue. + {$t("accounts.arkUnlockWithPasskey")}

+ {#if hasSigner} + + {/if}
{:else}

- Your Ark wallet will be derived from your passkey-protected master seed. + {$t("accounts.arkDerivedFromPasskey")}

- +
@@ -261,7 +293,7 @@ {:else if hasMasterSeed}

- Your Ark wallet will be derived from your master seed. Enter your wallet password to continue. + {$t("accounts.arkEnterPassword")}

{/if} diff --git a/src/routes/(app)/account/setup/+page.svelte b/src/routes/(app)/account/setup/+page.svelte index faab0938..35c0c9d9 100644 --- a/src/routes/(app)/account/setup/+page.svelte +++ b/src/routes/(app)/account/setup/+page.svelte @@ -17,7 +17,7 @@
- +
Savings account
-- cgit v1.2.3