aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/nip17.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/nip17.ts')
-rw-r--r--src/lib/nip17.ts80
1 files changed, 6 insertions, 74 deletions
diff --git a/src/lib/nip17.ts b/src/lib/nip17.ts
index d0b32d78..0398c426 100644
--- a/src/lib/nip17.ts
+++ b/src/lib/nip17.ts
@@ -5,88 +5,20 @@ import { SimplePool } from "nostr-tools/pool";
import { Relay } from "nostr-tools/relay";
import { unwrapEvent } from "nostr-tools/nip17";
-import { relaysSupporting } from "$lib/nip11";
-import { expired, expiration } from "$lib/nip40";
-import { encrypt, u } from "$lib/nip44";
-import { eventToSign, signer } from "$lib/store";
-import { get } from "svelte/store";
+import { ensureSigner } from '$lib/nip07';
+import { relaysSupporting } from '$lib/nip11';
+import { expired, expiration } from '$lib/nip40';
+import { encrypt, u } from '$lib/nip44';
const TWO_DAYS = 2 * 24 * 60 * 60;
const randomTimestamp = () => Math.floor(Date.now() / 1000 - Math.random() * TWO_DAYS);
const pool = new SimplePool();
let relayListCache = new Map();
-import { PUBLIC_DM_RELAYS } from "$env/static/public";
-const DM_RELAYS_LIST = PUBLIC_DM_RELAYS.split(",");
+import { PUBLIC_DM_RELAYS } from '$env/static/public';
+const DM_RELAYS_LIST = PUBLIC_DM_RELAYS.split(',');
const DM_FETCH_LIMIT = 65536;
-// 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.
-const ensureSigner = async (userPubkey: string): Promise<Uint8Array | null> => {
- // Check if NIP-07 extension is available and matches user
- if ((window as any).nostr) {
- try {
- const extPubkey = await (window as any).nostr.getPublicKey();
- if (extPubkey === userPubkey) {
- return null; // Use NIP-07
- }
- } catch {
- // Extension didn't work, fall through to signer
- }
- }
-
- // Check if signer already has a valid sk (64 hex chars = 32 bytes)
- const currentSigner: any = get(signer);
- if (currentSigner?.ready && currentSigner?.params?.sk && currentSigner.params.sk.length === 64) {
- return hexToBytes(currentSigner.params.sk);
- }
-
- // Clear invalid signer state before triggering modal
- if (
- currentSigner?.ready &&
- (!currentSigner?.params?.sk || currentSigner.params.sk.length !== 64)
- ) {
- signer.set(undefined);
- }
-
- // Trigger the signer modal with a dummy event
- const dummyEvent = {
- kind: 14,
- content: "DM encryption",
- tags: [],
- created_at: Math.floor(Date.now() / 1000),
- };
- eventToSign.set(dummyEvent);
-
- // Wait for signer to be ready
- const signerValue = await new Promise<any>((resolve, reject) => {
- let unsubscribe: () => void;
- unsubscribe = signer.subscribe((v: any) => {
- if (v === "cancel") {
- setTimeout(() => unsubscribe?.());
- eventToSign.set(null);
- reject(new Error("cancelled"));
- }
- if (v?.ready) {
- setTimeout(() => unsubscribe?.());
- eventToSign.set(null);
- resolve(v);
- }
- });
- });
-
- if (signerValue?.params?.sk) {
- return hexToBytes(signerValue.params.sk);
- }
-
- // If extension method was selected
- if (signerValue?.method === "extension") {
- return null; // Use NIP-07
- }
-
- throw new Error("No signing method available");
-};
-
// Create a NIP-17 message using a provided sender secret key.
// If wrapPK is specified and different than receiverPK,
// the message will be gift-wrapped to wrapPK