Add Trust Revoke View

This commit is contained in:
Leon Böttger committed 2025-08-23 21:01:31 +02:00
1 parent 448cc15a7e
commit d3dc89f412
10 files changed
+2438 -1467

No files matched your search

+66 -12
View File
@@ -8,26 +8,80 @@
import Foundation
class TrustStore {
class TrustStore: ObservableObject {
static let shared = TrustStore()
@Published private(set) var trustedCertificates: [String: TrustedCertificate] = [:]
private let trustedKeysKey = "com.leonboettger.quickdrop.identity.trustedKeys"
private func getTrustedKeys() -> [String: Data] {
return AppGroup.appGroupUD.dictionary(forKey: trustedKeysKey) as? [String: Data] ?? [:]
private init() {
loadTrustedCertificates()
}
// MARK: - Persistence
private func loadTrustedCertificates() {
guard let dict = AppGroup.appGroupUD.dictionary(forKey: trustedKeysKey) as? [String: Data] else {
trustedCertificates = [:]
return
}
var decoded: [String: TrustedCertificate] = [:]
for (key, data) in dict {
if let cert = try? JSONDecoder().decode(TrustedCertificate.self, from: data) {
decoded[key] = cert
}
}
trustedCertificates = decoded
}
private func saveTrustedCertificates() {
var dict: [String: Data] = [:]
for (key, cert) in trustedCertificates {
if let data = try? JSONEncoder().encode(cert) {
dict[key] = data
}
}
AppGroup.appGroupUD.set(dict, forKey: trustedKeysKey)
}
// MARK: - Public API
func findTrustedKey(for secretIdHash: Data) -> Data? {
return getTrustedKeys()[secretIdHash.hex]
trustedCertificates[secretIdHash.hex]?.certificateData
}
func addTrusted(certificate: Sharing_Nearby_PublicCertificate) {
func addTrusted(certificate: Sharing_Nearby_PublicCertificate, device: RemoteDeviceInfo) {
guard let publicKeyData = try? certificate.serializedData() else { return }
var keys = getTrustedKeys()
keys[certificate.secretID.hex] = publicKeyData
AppGroup.appGroupUD.set(keys, forKey: trustedKeysKey)
trustedCertificates[certificate.secretID.hex] = TrustedCertificate(
device: device,
creationDate: Date(),
certificateData: publicKeyData
)
saveTrustedCertificates()
}
func removeTrusted(secretIdHex: String) {
trustedCertificates.removeValue(forKey: secretIdHex)
saveTrustedCertificates()
}
struct TrustedCertificate: Codable, Equatable {
// warning: do not rename for persistence
let device: RemoteDeviceInfo
let creationDate: Date
let certificateData: Data
}
}