mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 13:45:20 +00:00
* Extract BitchatMessage into a separate file * Convert `fromBinaryPayload` to `convenience init?` * Extract message dedup into an extension * Remove dead `formatMessageContent` * Minor refactor of timestamp and username formatting * Remove dead `getSenderColor` * Extract MessagePadding into a separate file * Extract BitchatPacket into a separate file * Extract ReadReceipt into a separate file * Extract NoisePayload into a separate file * Remove unnecessary import
42 lines
1.1 KiB
Swift
42 lines
1.1 KiB
Swift
//
|
|
// NoisePayload.swift
|
|
// bitchat
|
|
//
|
|
// This is free and unencumbered software released into the public domain.
|
|
// For more information, see <https://unlicense.org>
|
|
//
|
|
|
|
import Foundation
|
|
|
|
/// Helper to create typed Noise payloads
|
|
struct NoisePayload {
|
|
let type: NoisePayloadType
|
|
let data: Data
|
|
|
|
/// Encode payload with type prefix
|
|
func encode() -> Data {
|
|
var encoded = Data()
|
|
encoded.append(type.rawValue)
|
|
encoded.append(data)
|
|
return encoded
|
|
}
|
|
|
|
/// Decode payload from data
|
|
static func decode(_ data: Data) -> NoisePayload? {
|
|
// Ensure we have at least 1 byte for the type
|
|
guard !data.isEmpty else {
|
|
return nil
|
|
}
|
|
|
|
// Safely get the first byte
|
|
let firstByte = data[data.startIndex]
|
|
guard let type = NoisePayloadType(rawValue: firstByte) else {
|
|
return nil
|
|
}
|
|
|
|
// Create a proper Data copy (not a subsequence) for thread safety
|
|
let payloadData = data.count > 1 ? Data(data.dropFirst()) : Data()
|
|
return NoisePayload(type: type, data: payloadData)
|
|
}
|
|
}
|