1// DHTStorage.swift2// SophaxChatCore3//4// Persists the KBucketTable snapshot to an AES-256-GCM encrypted blob5// via MessageStore.saveEncryptedBlob — same directory and key as message storage.6// Survives app restarts so we don't cold-bootstrap every time.78import Foundation910public final class DHTStorage:@unchecked Sendable {1112 private let messageStore: MessageStore13 private static let fileName ="dht_kbuckets"1415 public init(messageStore: MessageStore){16 self.messageStore = messageStore17}1819// MARK: - Persist2021/// Saves the k-bucket snapshot (flat array of DHTContact arrays) to encrypted storage.22 public func save(snapshot:[[DHTContact]]){23 guard let data = try? JSONEncoder().encode(snapshot) else { return }24 try? messageStore.saveEncryptedBlob(data, fileName: Self.fileName)25}2627/// Loads and decodes the previously saved k-bucket snapshot.28/// Returns an empty array if no snapshot exists or decoding fails.29 public func load()->[DHTContact]{30 guard let data = messageStore.loadEncryptedBlob(fileName: Self.fileName),31 let snapshot = try? JSONDecoder().decode([[DHTContact]].self, from: data)32 else { return []}33 return snapshot.flatMap { $0 }34}35}