From cb50cbe1373629795dd524dbf03213891afb0f72 Mon Sep 17 00:00:00 2001 From: sam Date: Sun, 19 Oct 2025 18:58:13 +0700 Subject: [PATCH] give 1059s a fuzzed time --- src/contexts/DMContext.tsx | 28 +++++++++++++++------------- src/hooks/useSendDM.ts | 14 +++++++++++--- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/contexts/DMContext.tsx b/src/contexts/DMContext.tsx index 909d73f..3501510 100644 --- a/src/contexts/DMContext.tsx +++ b/src/contexts/DMContext.tsx @@ -371,10 +371,10 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, } } else if (msg.kind === 1059) { const { processedMessage } = await processNIP17GiftWrap(msg); + // Use the real message (kind 14) with real timestamp, not gift wrap return { - ...msg, - decryptedContent: processedMessage.decryptedContent, - error: processedMessage.error, + ...processedMessage, + content: msg.content, // Keep original encrypted content } as NostrEvent & { decryptedContent?: string; error?: string }; } return msg; @@ -480,13 +480,14 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, for (const giftWrap of messages) { const { processedMessage, conversationPartner } = await processNIP17GiftWrap(giftWrap); + // Use the real message (kind 14) timestamp, not the randomized gift wrap timestamp const messageWithAnimation: DecryptedMessage = { - ...giftWrap, - decryptedContent: processedMessage.decryptedContent, - error: processedMessage.error, + ...processedMessage, + content: giftWrap.content, // Keep original encrypted content for storage }; - const messageAge = Date.now() - (giftWrap.created_at * 1000); + // Use real message timestamp for recency check + const messageAge = Date.now() - (processedMessage.created_at * 1000); if (messageAge < 5000) { messageWithAnimation.clientFirstSeen = Date.now(); } @@ -774,13 +775,14 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, const { processedMessage, conversationPartner } = await processNIP17GiftWrap(event); + // Use the real message (kind 14) timestamp, not the randomized gift wrap timestamp const messageWithAnimation: DecryptedMessage = { - ...event, - decryptedContent: processedMessage.decryptedContent, - error: processedMessage.error, + ...processedMessage, + content: event.content, // Keep original encrypted content for storage }; - const messageAge = Date.now() - (event.created_at * 1000); + // Use real message timestamp for recency check + const messageAge = Date.now() - (processedMessage.created_at * 1000); if (messageAge < 5000) { messageWithAnimation.clientFirstSeen = Date.now(); } @@ -1083,9 +1085,9 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, const optimisticId = `optimistic-${Date.now()}-${Math.random()}`; const optimisticMessage: DecryptedMessage = { id: optimisticId, - kind: protocol === MESSAGE_PROTOCOL.NIP04 ? 4 : 1059, + kind: protocol === MESSAGE_PROTOCOL.NIP04 ? 4 : 14, // Use kind 14 for NIP-17 (the real message kind) pubkey: userPubkey, - created_at: Math.floor(Date.now() / 1000), + created_at: Math.floor(Date.now() / 1000), // Real timestamp tags: [['p', recipientPubkey]], content: '', decryptedContent: content, diff --git a/src/hooks/useSendDM.ts b/src/hooks/useSendDM.ts index b8c6d16..5004475 100644 --- a/src/hooks/useSendDM.ts +++ b/src/hooks/useSendDM.ts @@ -134,6 +134,14 @@ export function useSendDM(): UseSendDMReturn { // Step 1: Create the inner Kind 14 Private Direct Message const now = Math.floor(Date.now() / 1000); + // Generate randomized timestamps for gift wraps (NIP-59 metadata privacy) + // Randomize within ±2 days to hide actual send time from relays + const randomizeTimestamp = (baseTime: number) => { + const twoDaysInSeconds = 2 * 24 * 60 * 60; + const randomOffset = Math.floor(Math.random() * twoDaysInSeconds * 2) - twoDaysInSeconds; + return baseTime + randomOffset; + }; + // Prepare content with file URLs const messageContent = prepareMessageContent(content, attachments); @@ -176,19 +184,19 @@ export function useSendDM(): UseSendDMReturn { const recipientRandomSigner = new NSecSigner(generateSecretKey()); const senderRandomSigner = new NSecSigner(generateSecretKey()); - // Sign both gift wraps with random keys + // Sign both gift wraps with random keys and randomized timestamps const [recipientGiftWrap, senderGiftWrap] = await Promise.all([ recipientRandomSigner.sign({ kind: 1059, pubkey: getPublicKey(recipientRandomSigner.privateKey), - created_at: now, + created_at: randomizeTimestamp(now), // Randomized to hide real send time tags: [['p', recipientPubkey]], content: await recipientRandomSigner.nip44!.encrypt(recipientPubkey, JSON.stringify(recipientSeal)), }), senderRandomSigner.sign({ kind: 1059, pubkey: getPublicKey(senderRandomSigner.privateKey), - created_at: now, + created_at: randomizeTimestamp(now), // Randomized to hide real send time tags: [['p', user.pubkey]], content: await senderRandomSigner.nip44!.encrypt(user.pubkey, JSON.stringify(senderSeal)), }),