aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/account/ark
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-02-05 23:05:32 -0800
committerAdam Soltys <[email protected]>2026-02-11 15:34:33 -0800
commita418477c3ee9bdd567cb1625f23abbd94758983d (patch)
tree258b7fead7f23a7a6d5f3361096824d56cb89312 /src/routes/(app)/account/ark
parent806baacb2f126191068a4a7e3fccf9684a339d07 (diff)
downloadcoinos-ui-a418477c3ee9bdd567cb1625f23abbd94758983d.tar.gz
coinos-ui-a418477c3ee9bdd567cb1625f23abbd94758983d.zip
use master seed for ark accounts
Diffstat (limited to 'src/routes/(app)/account/ark')
-rw-r--r--src/routes/(app)/account/ark/+page.svelte158
1 files changed, 152 insertions, 6 deletions
diff --git a/src/routes/(app)/account/ark/+page.svelte b/src/routes/(app)/account/ark/+page.svelte
index aee80837..32dbfe13 100644
--- a/src/routes/(app)/account/ark/+page.svelte
+++ b/src/routes/(app)/account/ark/+page.svelte
@@ -5,19 +5,28 @@
import Spinner from "$comp/Spinner.svelte";
import { bytesToHex, randomBytes } from "@noble/hashes/utils.js";
import { hex } from "@scure/base";
- import { focus, fail, post, copy } from "$lib/utils";
+ import { focus, fail, post, copy, versions } from "$lib/utils";
import { goto } from "$app/navigation";
import { arkServerUrl } from "$lib/ark";
+ import { HDKey } from "@scure/bip32";
+ import {
+ entropyToMnemonic,
+ mnemonicToSeed,
+ } from "@scure/bip39";
+ import { wordlist } from "@scure/bip39/wordlists/english.js";
import {
rememberForOptions,
defaultRememberForMs,
rememberWalletPassword,
forgetWalletPassword,
+ getRememberedWalletPassword,
} from "$lib/passwordCache";
let { data } = $props();
let { user } = data;
+ let hasMasterSeed = $derived(!!user.seed);
+
let privateKey = $state("");
let arkAddress = $state("");
let nsec = $state("");
@@ -34,10 +43,40 @@
let name = "Ark Vault";
let type = "ark";
- onMount(() => {
- generateKey();
+ onMount(async () => {
+ if (hasMasterSeed) {
+ // Try cached password first
+ const cached = getRememberedWalletPassword();
+ if (cached) {
+ password = cached;
+ confirm = cached;
+ await deriveFromMasterSeed(cached);
+ }
+ } else {
+ await generateKey();
+ }
});
+ let deriveFromMasterSeed = async (pw) => {
+ try {
+ const { decrypt } = await import("nostr-tools/nip49");
+ const { SingleKey, Wallet } = await import("@arkade-os/sdk");
+ let entropy = await decrypt(user.seed, pw);
+ let mnemonic = entropyToMnemonic(entropy, wordlist);
+ let seed = await mnemonicToSeed(mnemonic, pw);
+ let master = HDKey.fromMasterSeed(seed, versions);
+ let arkChild = master.derive("m/86'/0'/0'/0/0");
+ let arkHex = bytesToHex(arkChild.privateKey);
+ const identity = SingleKey.fromHex(arkHex);
+ const wallet = await Wallet.create({ identity, arkServerUrl });
+ arkAddress = await wallet.getAddress();
+ confirmed = true;
+ } catch (e) {
+ console.log("master seed derivation failed", e);
+ fail("Invalid password");
+ }
+ };
+
let generateKey = async () => {
const { nip19 } = await import("nostr-tools");
const { SingleKey, Wallet } = await import("@arkade-os/sdk");
@@ -46,7 +85,6 @@
showNsec = false;
confirmed = false;
- // Derive the ARK address from the private key
const identity = SingleKey.fromHex(privateKey);
const wallet = await Wallet.create({ identity, arkServerUrl });
arkAddress = await wallet.getAddress();
@@ -61,6 +99,33 @@
};
let submit = async () => {
+ if (hasMasterSeed) {
+ if (!password) {
+ fail("Password required");
+ return;
+ }
+
+ submitting = true;
+ await tick();
+
+ try {
+ if (!arkAddress) await deriveFromMasterSeed(password);
+ await post("/account", { name, type, arkAddress });
+ if (rememberForMs) {
+ rememberWalletPassword(password, rememberForMs);
+ } else {
+ forgetWalletPassword();
+ }
+ goto(`/${user.username}`);
+ } catch (e) {
+ console.log(e);
+ fail(e.message);
+ }
+
+ submitting = false;
+ return;
+ }
+
if (!confirmed) {
fail("Please backup your key first");
return;
@@ -75,7 +140,6 @@
await tick();
try {
- // Encrypt the private key using NIP-49
const { encrypt } = await import("nostr-tools/nip49");
let seed = await encrypt(hex.decode(privateKey), password);
@@ -101,7 +165,89 @@
</div>
<div class="container w-full mx-auto text-lg px-4 max-w-xl space-y-5">
- {#if !confirmed}
+ {#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.
+ </p>
+
+ <label
+ for="password"
+ class="input flex items-center justify-center gap-2 w-full"
+ >
+ {#if revealPassword}
+ <input
+ name="password"
+ type="text"
+ required
+ bind:value={password}
+ autocapitalize="none"
+ class="clean"
+ placeholder="Wallet password"
+ />
+ {:else}
+ <input
+ use:focus
+ name="password"
+ type="password"
+ placeholder="Wallet password"
+ required
+ bind:value={password}
+ autocapitalize="none"
+ class="clean"
+ />
+ {/if}
+
+ <button
+ type="button"
+ class="ml-auto"
+ aria-label="Toggle password visibility"
+ onclick={() => (revealPassword = !revealPassword)}
+ >
+ <iconify-icon
+ noobserver
+ icon={revealPassword ? "ph:eye-bold" : "ph:eye-slash-bold"}
+ width="32"
+ ></iconify-icon>
+ </button>
+ </label>
+
+ <div class="space-y-2">
+ <label for="rememberFor" class="text-sm text-secondary">
+ Remember for
+ </label>
+ <select
+ id="rememberFor"
+ class="w-full"
+ value={rememberForMs}
+ onchange={(e) => (rememberForMs = Number(e.target.value))}
+ >
+ {#each rememberForOptions as option}
+ <option value={option.ms}>{option.label}</option>
+ {/each}
+ </select>
+ </div>
+
+ <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>
+ {:else if !confirmed}
<div class="space-y-4">
<p class="text-secondary">
Your Ark wallet is secured by a private key. Please copy it down somewhere safe.