diff options
| author | Adam Soltys <[email protected]> | 2026-03-01 14:30:39 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-03-01 14:30:39 -0800 |
| commit | f3d2029a61138d69437c1815a41fa6db3fd6198a (patch) | |
| tree | 149ed8baa69561d4d82436388049189fa411ea23 /src/lib/nip07.ts | |
| parent | 0eb128fdd59cd550beda23549a47df75442d997f (diff) | |
| download | coinos-ui-f3d2029a61138d69437c1815a41fa6db3fd6198a.tar.gz coinos-ui-f3d2029a61138d69437c1815a41fa6db3fd6198a.zip | |
dm test
Diffstat (limited to 'src/lib/nip07.ts')
| -rw-r--r-- | src/lib/nip07.ts | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/lib/nip07.ts b/src/lib/nip07.ts index ee31ee58..ce1cddaf 100644 --- a/src/lib/nip07.ts +++ b/src/lib/nip07.ts @@ -1,11 +1,34 @@ import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js"; -import { finalizeEvent } from "nostr-tools/pure"; +import { finalizeEvent, getPublicKey } from "nostr-tools/pure"; import * as libnip44 from "$lib/nip44"; import { u } from "$lib/nip44"; import { eventToSign, signer } from "$lib/store"; import { get } from "svelte/store"; +// Try to load sk from localStorage nsec and populate the signer store. +const tryNsecFromStorage = async (): Promise<Uint8Array | null> => { + const nsec = localStorage.getItem("nsec"); + if (!nsec) return null; + + try { + const { nip19 } = await import("nostr-tools"); + const { type, data } = nip19.decode(nsec); + if (type !== "nsec" || !(data instanceof Uint8Array)) return null; + + const sk = data; + const pk = getPublicKey(sk); + signer.set({ + method: "nsec", + params: { sk: bytesToHex(sk), pk }, + ready: true, + }); + return sk; + } catch { + return null; + } +}; + // Ensures we have access to a secret key, triggering the signer modal if needed. // Returns the sk as Uint8Array, or null if using NIP-07 extension. export const ensureSigner = async (userPubkey: string): Promise<Uint8Array | null> => { @@ -27,6 +50,10 @@ export const ensureSigner = async (userPubkey: string): Promise<Uint8Array | nul return hexToBytes(currentSigner.params.sk); } + // Try to load from localStorage nsec before triggering the modal + const skFromStorage = await tryNsecFromStorage(); + if (skFromStorage) return skFromStorage; + // Clear invalid signer state before triggering modal if ( currentSigner?.ready && |
