diff --git a/bitchat/Nostr/NostrProtocol.swift b/bitchat/Nostr/NostrProtocol.swift index 7e3864e3..c323af36 100644 --- a/bitchat/Nostr/NostrProtocol.swift +++ b/bitchat/Nostr/NostrProtocol.swift @@ -745,11 +745,13 @@ struct NostrProtocol { return sharedSecretData } - /// Randomized `created_at` for seals and gift wraps. Samples uniformly - /// from `[now − maxPast, now]` (never future) so relays cannot correlate - /// envelope timing with the true send time carried on the inner rumor. - /// Matches Android `NostrCrypto.randomizeTimestampUpToPast`. Internal so - /// tests can pin `now` / `maxPast`. + /// Randomized `created_at` for the outer gift wrap (and the seal that + /// sits inside it). Samples uniformly from `[now − maxPast, now]` + /// (never future). Relays only see the wrap timestamp; the seal is + /// encrypted inside the wrap, so randomizing it is not observable to + /// anyone — callers still stamp both layers for parity with Android + /// `NostrCrypto.randomizeTimestampUpToPast`. The true send time lives + /// on the inner rumor. Internal so tests can pin `now` / `maxPast`. static func randomizedEnvelopeTimestamp( now: Date = Date(), maxPast: TimeInterval = TransportConfig.nostrGiftWrapTimestampRandomizationSeconds diff --git a/bitchat/Services/TransportConfig.swift b/bitchat/Services/TransportConfig.swift index 16731cce..1fa441be 100644 --- a/bitchat/Services/TransportConfig.swift +++ b/bitchat/Services/TransportConfig.swift @@ -224,18 +224,23 @@ enum TransportConfig { static let nostrDMSubscribeLookbackSeconds: TimeInterval = 86400 // Tolerated clock skew for client-side Nostr DM timestamp windows. static let nostrDMMaxClockSkewSeconds: TimeInterval = 900 - /// Outer gift-wrap / seal `created_at` randomization floor. Matches - /// Android `NostrCrypto.randomizeTimestampUpToPast` (up to 48h into the - /// past). The actual message time lives on the inner rumor. - static let nostrGiftWrapTimestampRandomizationSeconds: TimeInterval = 172_800 + /// Outer gift-wrap `created_at` randomization floor when *sending*. + /// Cap at lookback − skew so a wrap never lands older than + /// `nostrDMSubscribeLookbackSeconds` (every installed build still + /// subscribes `since: now − 86400`). A deeper draw would silently miss + /// half of DMs/acks to peers that have not yet widened their filter. + /// Android today draws up to 79_200s (`NIP17_DEFAULT_MAX_PAST_SECONDS`); + /// the true send time lives on the inner rumor either way. + static let nostrGiftWrapTimestampRandomizationSeconds: TimeInterval = + nostrDMSubscribeLookbackSeconds - nostrDMMaxClockSkewSeconds - // Outer gift-wrap age ceiling. Android (and NIP-17-style clients) may - // randomize the wrap's created_at up to 48h into the past; relays that - // honor `since` will not deliver those wraps under a 24h filter. The - // accept window is 48h plus skew so a wrap at the randomization floor - // still clears a mildly slow clock. + // Outer gift-wrap age ceiling when *receiving*. Kept at the historical + // NIP-17 48h floor (+ skew) so wraps from peers that still randomize + // that deep (or that Android's 48h subscribe will deliver) are not + // dropped client-side after the relay hands them over. Independent of + // the send-side randomization above — do not derive one from the other. static let nostrGiftWrapMaxAgeSeconds: TimeInterval = - nostrGiftWrapTimestampRandomizationSeconds + nostrDMMaxClockSkewSeconds + 172_800 + nostrDMMaxClockSkewSeconds // A sampled chat message this recent means "a conversation is happening // there" for the empty-timeline nearby-activity hint. static let uiGeohashChatActivityWindowSeconds: TimeInterval = 900 diff --git a/bitchatTests/NostrInboundPipelineTimestampTests.swift b/bitchatTests/NostrInboundPipelineTimestampTests.swift index 74bbd9bc..577f454c 100644 --- a/bitchatTests/NostrInboundPipelineTimestampTests.swift +++ b/bitchatTests/NostrInboundPipelineTimestampTests.swift @@ -64,4 +64,19 @@ struct NostrEnvelopeTimestampRandomizationTests { // Negative past is treated as zero. #expect(NostrProtocol.randomizedEnvelopeTimestamp(now: now, maxPast: -10) == now) } + + @Test("Default send randomization stays inside the 24h subscribe lookback") + func defaultDrawStaysInsideLookback() { + let now = Date(timeIntervalSince1970: 1_700_000_000) + let lookback = TransportConfig.nostrDMSubscribeLookbackSeconds + let maxPast = TransportConfig.nostrGiftWrapTimestampRandomizationSeconds + #expect(maxPast == lookback - TransportConfig.nostrDMMaxClockSkewSeconds) + for _ in 0..<200 { + let ts = NostrProtocol.randomizedEnvelopeTimestamp(now: now) + #expect(ts <= now) + #expect(ts >= now.addingTimeInterval(-maxPast)) + // Relays that honor `since: now − lookback` must still return it. + #expect(ts >= now.addingTimeInterval(-lookback)) + } + } }