random signers for 1059s

This commit is contained in:
sam committed 2025-10-19 18:16:09 +07:00
1 parent 04ae3d89c0
commit 79b03dee38
1 file changed
+25 -14
+25 -14
View File
@@ -2,7 +2,9 @@ import { useMutation, type UseMutationResult } from '@tanstack/react-query';
import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useNostrPublish } from '@/hooks/useNostrPublish'; import { useNostrPublish } from '@/hooks/useNostrPublish';
import { useToast } from '@/hooks/useToast'; import { useToast } from '@/hooks/useToast';
import type { NostrEvent } from '@nostrify/nostrify'; import { useNostr } from '@nostrify/react';
import { NSecSigner, type NostrEvent } from '@nostrify/nostrify';
import { generateSecretKey, getPublicKey } from 'nostr-tools';
// ============================================================================ // ============================================================================
// Types // Types
@@ -75,6 +77,7 @@ function createImetaTags(attachments: FileAttachment[] = []): string[][] {
export function useSendDM(): UseSendDMReturn { export function useSendDM(): UseSendDMReturn {
const { user } = useCurrentUser(); const { user } = useCurrentUser();
const { mutateAsync: createEvent } = useNostrPublish(); const { mutateAsync: createEvent } = useNostrPublish();
const { nostr } = useNostr();
const { toast } = useToast(); const { toast } = useToast();
// Send NIP-04 Message // Send NIP-04 Message
@@ -166,29 +169,37 @@ export function useSendDM(): UseSendDMReturn {
}; };
// Step 3: Create TWO Kind 1059 Gift Wrap events // Step 3: Create TWO Kind 1059 Gift Wrap events
// NOTE: This implementation uses the user's key for signing. // Per NIP-17/NIP-59: Gift wraps MUST be signed with random, ephemeral keys
// todo: For maximum privacy, this should use a random key (see NIP-59). // to hide the sender's identity and provide - some - metadata privacy
// That fix will be implemented separately.
// Encrypt the seals first // Create random signers for each gift wrap
const recipientGiftWrapContent = await user.signer.nip44.encrypt(recipientPubkey, JSON.stringify(recipientSeal)); const recipientRandomSigner = new NSecSigner(generateSecretKey());
const myGiftWrapContent = await user.signer.nip44.encrypt(user.pubkey, JSON.stringify(senderSeal)); const senderRandomSigner = new NSecSigner(generateSecretKey());
// Publish both gift wraps in parallel // Sign both gift wraps with random keys
const [recipientGiftWrap] = await Promise.all([ const [recipientGiftWrap, senderGiftWrap] = await Promise.all([
createEvent({ recipientRandomSigner.sign({
kind: 1059, kind: 1059,
content: recipientGiftWrapContent, pubkey: getPublicKey(recipientRandomSigner.privateKey),
created_at: now,
tags: [['p', recipientPubkey]], tags: [['p', recipientPubkey]],
content: await recipientRandomSigner.nip44!.encrypt(recipientPubkey, JSON.stringify(recipientSeal)),
}), }),
createEvent({ senderRandomSigner.sign({
kind: 1059, kind: 1059,
content: myGiftWrapContent, pubkey: getPublicKey(senderRandomSigner.privateKey),
created_at: now,
tags: [['p', user.pubkey]], tags: [['p', user.pubkey]],
content: await senderRandomSigner.nip44!.encrypt(user.pubkey, JSON.stringify(senderSeal)),
}), }),
]); ]);
// Return the recipient's gift wrap as the "result" // Publish both to relays
await Promise.all([
nostr.event(recipientGiftWrap),
nostr.event(senderGiftWrap),
]);
return recipientGiftWrap; return recipientGiftWrap;
}, },
onError: (error) => { onError: (error) => {