Fix timestamp bug causing peer discovery failures

- Fixed BitchatPacket convenience initializer to use milliseconds (was using seconds)
- Fixed BitchatMessage binary encoding to use milliseconds for consistency
- Updated decoding logic to properly handle millisecond timestamps
- This resolves the issue where packets were being dropped due to timestamp validation failures

All timestamps are now consistently in milliseconds throughout the codebase.
This commit is contained in:
jack
2025-07-04 16:07:49 +02:00
parent 6023ff8af0
commit 9ef9ef8056
3 changed files with 7 additions and 7 deletions
+5 -5
View File
@@ -196,11 +196,11 @@ extension BitchatMessage {
data.append(flags)
// Timestamp
let timestampSeconds = UInt64(timestamp.timeIntervalSince1970)
// Timestamp (in milliseconds)
let timestampMillis = UInt64(timestamp.timeIntervalSince1970 * 1000)
// Encode as 8 bytes, big-endian
for i in (0..<8).reversed() {
data.append(UInt8((timestampSeconds >> (i * 8)) & 0xFF))
data.append(UInt8((timestampMillis >> (i * 8)) & 0xFF))
}
// ID
@@ -290,11 +290,11 @@ extension BitchatMessage {
return nil
}
let timestampData = dataCopy[offset..<offset+8]
let timestampSeconds = timestampData.reduce(0) { result, byte in
let timestampMillis = timestampData.reduce(0) { result, byte in
(result << 8) | UInt64(byte)
}
offset += 8
let timestamp = Date(timeIntervalSince1970: TimeInterval(timestampSeconds))
let timestamp = Date(timeIntervalSince1970: TimeInterval(timestampMillis) / 1000.0)
// ID
guard offset < dataCopy.count else {
+1 -1
View File
@@ -95,7 +95,7 @@ struct BitchatPacket: Codable {
self.type = type
self.senderID = senderID.data(using: .utf8)!
self.recipientID = nil
self.timestamp = UInt64(Date().timeIntervalSince1970)
self.timestamp = UInt64(Date().timeIntervalSince1970 * 1000) // milliseconds
self.payload = payload
self.signature = nil
self.ttl = ttl
+1 -1
View File
@@ -1053,7 +1053,7 @@ class BluetoothMeshService: NSObject {
let currentTime = UInt64(Date().timeIntervalSince1970 * 1000) // milliseconds
let timeDiff = abs(Int64(currentTime) - Int64(packet.timestamp))
if timeDiff > 300000 { // 5 minutes in milliseconds
print("[SECURITY] Dropping packet with timestamp too far from current time: \(timeDiff/1000) seconds")
print("[SECURITY] Dropping packet from \(peerID) type:\(packet.type) - timestamp diff: \(timeDiff/1000)s (packet:\(packet.timestamp) vs current:\(currentTime))")
return
}