Add source-based routing support (#862)

* Add source-based routing support

* include neighbors in ANNOUNCE

---------

Co-authored-by: jack <jackjackbits@users.noreply.github.com>
Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
This commit is contained in:
jack
2025-10-28 12:38:44 +01:00
committed by GitHub
co-authored by jack callebtc
parent 2edbe2bbbd
commit 01256f3041
9 changed files with 504 additions and 26 deletions
+6 -2
View File
@@ -21,8 +21,9 @@ struct BitchatPacket: Codable {
let payload: Data
var signature: Data?
var ttl: UInt8
var route: [Data]?
init(type: UInt8, senderID: Data, recipientID: Data?, timestamp: UInt64, payload: Data, signature: Data?, ttl: UInt8, version: UInt8 = 1) {
init(type: UInt8, senderID: Data, recipientID: Data?, timestamp: UInt64, payload: Data, signature: Data?, ttl: UInt8, version: UInt8 = 1, route: [Data]? = nil) {
self.version = version
self.type = type
self.senderID = senderID
@@ -31,6 +32,7 @@ struct BitchatPacket: Codable {
self.payload = payload
self.signature = signature
self.ttl = ttl
self.route = route
}
// Convenience initializer for new binary format
@@ -53,6 +55,7 @@ struct BitchatPacket: Codable {
self.payload = payload
self.signature = nil
self.ttl = ttl
self.route = nil
}
var data: Data? {
@@ -81,7 +84,8 @@ struct BitchatPacket: Codable {
payload: payload,
signature: nil, // Remove signature for signing
ttl: 0, // Use fixed TTL=0 for signing to ensure relay compatibility
version: version
version: version,
route: route
)
return BinaryProtocol.encode(unsignedPacket)
}
+14
View File
@@ -136,6 +136,20 @@ extension PeerID {
}
}
extension PeerID {
var routingData: Data? {
if let direct = Data(hexString: id), direct.count == 8 { return direct }
if let bareData = Data(hexString: bare), bareData.count == 8 { return bareData }
let short = toShort()
return Data(hexString: short.id)
}
init?(routingData: Data) {
guard routingData.count == 8 else { return nil }
self.init(hexData: routingData)
}
}
// MARK: - Validation
extension PeerID {