From c7e2383736d3ef99f069226bbafad9fbddfeb030 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 1 Mar 2026 18:51:16 -0800 Subject: messages ui --- src/lib/seed.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/lib/seed.ts (limited to 'src/lib/seed.ts') diff --git a/src/lib/seed.ts b/src/lib/seed.ts new file mode 100644 index 00000000..3d58bcb5 --- /dev/null +++ b/src/lib/seed.ts @@ -0,0 +1,41 @@ +import { getWalletEntropy } from "$lib/walletEntropy"; +import { signer } from "$lib/store"; + +export async function deriveNostrKey(entropy: ArrayBuffer) { + const [{ entropyToMnemonic }, { wordlist }, { privateKeyFromSeedWords }, { getPublicKey }] = + await Promise.all([ + import("@scure/bip39"), + import("@scure/bip39/wordlists/english.js"), + import("nostr-tools/nip06"), + import("nostr-tools"), + ]); + + const ent = new Uint8Array(entropy).slice(0, 16); + const mnemonic = entropyToMnemonic(ent, wordlist); + const sk = privateKeyFromSeedWords(mnemonic); + const pubkey = getPublicKey(sk); + return { sk, pubkey }; +} + +export async function autoUnlockNostr(userPubkey: string) { + if (localStorage.getItem("nsec")) return; + + const entropy = await getWalletEntropy(); + if (!entropy) return; + + try { + const { sk, pubkey } = await deriveNostrKey(entropy); + if (pubkey !== userPubkey) return; + + const { nip19 } = await import("nostr-tools"); + const { bytesToHex } = await import("@noble/hashes/utils.js"); + localStorage.setItem("nsec", nip19.nsecEncode(sk)); + signer.set({ + method: "nsec", + params: { sk: bytesToHex(sk), pk: pubkey }, + ready: true, + }); + } catch (e) { + console.error("Nostr auto-unlock failed:", e); + } +} -- cgit v1.2.3