Cap gift-wrap timestamp draw at lookback minus skew

48h randomization put half of wraps older than the 24h relay
subscribe window every installed build still uses, so DMs and acks
to not-yet-updated peers never arrived. Keep the 48h+skew accept
gate for peers that still stamp that deep.
This commit is contained in:
Taksh committed 2026-09-25 15:43:31 +05:30
1 parent ad348693ac
commit 7b9d319e4f
3 files changed
+37 -15

No files matched your search

+7 -5
View File
@@ -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
+15 -10
View File
@@ -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
@@ -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))
}
}
}