From 16da2cb00d16f87d02535bfdae815f83f19f251f Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 27 Feb 2026 15:24:13 -0800 Subject: fix race condition --- src/components/Account.svelte | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) (limited to 'src/components') diff --git a/src/components/Account.svelte b/src/components/Account.svelte index 45ffdd8b..ff9218fe 100644 --- a/src/components/Account.svelte +++ b/src/components/Account.svelte @@ -18,8 +18,8 @@ let arkBalance = $state(0); let arkLoading = $state(false); - let tryUnlockArk = async () => { - const deriveArkKey = async (key: ArrayBuffer) => { + let tryUnlockArk = async (): Promise => { + const deriveArkKey = async (key: ArrayBuffer): Promise => { const [{ HDKey }, { entropyToMnemonic, mnemonicToSeed }, { wordlist }] = await Promise.all([ import("@scure/bip32"), import("@scure/bip39"), @@ -36,8 +36,7 @@ const s = await mnemonicToSeed(mnemonicStr); const master = HDKey.fromMasterSeed(s, versions); const arkChild = master.derive("m/86'/0'/0'/0/0"); - $arkkey = bytesToHex(arkChild.privateKey!); - $arkaid = id; + return bytesToHex(arkChild.privateKey!); }; // Try cached PRF key or silent nsec @@ -45,8 +44,7 @@ const entropy = await getWalletEntropy(); if (entropy) { try { - await deriveArkKey(entropy); - return true; + return await deriveArkKey(entropy); } catch (e) { console.log("Wallet entropy unlock failed", e); } @@ -55,8 +53,7 @@ // Try interactive nostr derivation try { const interactiveEntropy = await deriveNostrEntropy(); - await deriveArkKey(interactiveEntropy); - return true; + return await deriveArkKey(interactiveEntropy); } catch (e) { console.log("Interactive nostr unlock failed", e); return false; @@ -69,9 +66,14 @@ document.cookie = `aid=${id}; path=/; max-age=86400`; if (isArk) { if ($arkkey && $arkaid === id) { - goto(url); - } else if (await tryUnlockArk()) { - goto(url); + await goto(url); + } else { + const key = await tryUnlockArk(); + if (key) { + await goto(url); + $arkkey = key; + $arkaid = id; + } } } else { $arkkey = undefined; -- cgit v1.2.3