PeerID 15/n: Bitchat Message & Packet accept in init (#754)

This commit is contained in:
Islam
2025-10-06 17:16:11 +02:00
committed by GitHub
parent 01ec4573f8
commit 5f44c56a90
10 changed files with 45 additions and 34 deletions
+16 -4
View File
@@ -42,7 +42,19 @@ final class BitchatMessage: Codable {
case isPrivate, recipientNickname, senderPeerID, mentions, deliveryStatus
}
init(id: String? = nil, sender: String, content: String, timestamp: Date, isRelay: Bool, originalSender: String? = nil, isPrivate: Bool = false, recipientNickname: String? = nil, senderPeerID: String? = nil, mentions: [String]? = nil, deliveryStatus: DeliveryStatus? = nil) {
init(
id: String? = nil,
sender: String,
content: String,
timestamp: Date,
isRelay: Bool,
originalSender: String? = nil,
isPrivate: Bool = false,
recipientNickname: String? = nil,
senderPeerID: PeerID? = nil,
mentions: [String]? = nil,
deliveryStatus: DeliveryStatus? = nil
) {
self.id = id ?? UUID().uuidString
self.sender = sender
self.content = content
@@ -51,7 +63,7 @@ final class BitchatMessage: Codable {
self.originalSender = originalSender
self.isPrivate = isPrivate
self.recipientNickname = recipientNickname
self.senderPeerID = PeerID(str: senderPeerID)
self.senderPeerID = senderPeerID
self.mentions = mentions
self.deliveryStatus = deliveryStatus ?? (isPrivate ? .sending : nil)
}
@@ -264,11 +276,11 @@ extension BitchatMessage {
}
}
var senderPeerID: String?
var senderPeerID: PeerID?
if hasSenderPeerID && offset < dataCopy.count {
let length = Int(dataCopy[offset]); offset += 1
if offset + length <= dataCopy.count {
senderPeerID = String(data: dataCopy[offset..<offset+length], encoding: .utf8)
senderPeerID = PeerID(data: dataCopy[offset..<offset+length])
offset += length
}
}
+2 -2
View File
@@ -34,12 +34,12 @@ struct BitchatPacket: Codable {
}
// Convenience initializer for new binary format
init(type: UInt8, ttl: UInt8, senderID: String, payload: Data) {
init(type: UInt8, ttl: UInt8, senderID: PeerID, payload: Data) {
self.version = 1
self.type = type
// Convert hex string peer ID to binary data (8 bytes)
var senderData = Data()
var tempID = senderID
var tempID = senderID.id
while tempID.count >= 2 {
let hexByte = String(tempID.prefix(2))
if let byte = UInt8(hexByte, radix: 16) {
+1 -2
View File
@@ -69,8 +69,7 @@ extension PeerID {
/// Convenience init to create PeerID by converting Data to String
init?(data: Data) {
guard let str = String(data: data, encoding: .utf8) else { return nil }
self.init(str: str)
self.init(str: String(data: data, encoding: .utf8))
}
/// Convenience init to "hide" hex-encoding implementation detail