From 9067f3442a7ab3d335cb13bbf0c2b092283b532e Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 31 Aug 2024 11:55:57 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinOMEMO.swift | 112 ++++++++++++------ 1 file changed, 73 insertions(+), 39 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index cb298e5..9b96b22 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -17,6 +17,25 @@ final class ClientMartinOMEMO { signalStorage.setup(withContext: signalContext) return (signalStorage, signalContext) } + // func regenerateKeys(wipe: Bool = false) -> Bool { + // if wipe { + // wipeSignedPreKeys() + // } + // } + // if wipe { + // DBOMEMOStore.instance.wipe(forAccount: context!.sessionObject.userBareJid!) + // } + // + // let hasKeyPair = identityKeyStore.keyPair() != nil + // if wipe || identityKeyStore.localRegistrationId() == 0 || !hasKeyPair { + // let regId: UInt32 = signalContext.generateRegistrationId() + // AccountSettings.omemoRegistrationId(for: context!.sessionObject.userBareJid!, value: regId) + // + // let keyPair = SignalIdentityKeyPair.generateKeyPair(context: signalContext) + // if !identityKeyStore.save(identity: SignalAddress(name: context!.sessionObject.userBareJid!.stringValue, deviceId: Int32(identityKeyStore.localRegistrationId())), key: keyPair) {} + // } + // return true + // } } // MARK: - Session @@ -362,26 +381,6 @@ extension ClientMartinOMEMO: SignalSignedPreKeyStoreProtocol { // MARK: - Identity extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { - // func regenerateKeys(wipe: Bool = false) -> Bool { - // if wipe { - // wipeSignedPreKeys() - // } - // } - // if wipe { - // DBOMEMOStore.instance.wipe(forAccount: context!.sessionObject.userBareJid!) - // } - // - // let hasKeyPair = identityKeyStore.keyPair() != nil - // if wipe || identityKeyStore.localRegistrationId() == 0 || !hasKeyPair { - // let regId: UInt32 = signalContext.generateRegistrationId() - // AccountSettings.omemoRegistrationId(for: context!.sessionObject.userBareJid!, value: regId) - // - // let keyPair = SignalIdentityKeyPair.generateKeyPair(context: signalContext) - // if !identityKeyStore.save(identity: SignalAddress(name: context!.sessionObject.userBareJid!.stringValue, deviceId: Int32(identityKeyStore.localRegistrationId())), key: keyPair) {} - // } - // return true - // } - func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? { // guard let deviceId = localRegistrationId(forAccount: account) else { // return nil @@ -400,6 +399,12 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { } func localRegistrationId() -> UInt32 { + // + // + // + // + // + // 0 } @@ -408,19 +413,17 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { return false } - func isTrusted(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool { - print(identity, key) - return false - } - func save(identity: MartinOMEMO.SignalAddress, publicKeyData: Data?) -> Bool { print(identity, publicKeyData) return false } - func isTrusted(identity: MartinOMEMO.SignalAddress, publicKeyData: Data?) -> Bool { - print(identity, publicKeyData) - return false + func isTrusted(identity _: MartinOMEMO.SignalAddress, key _: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool { + true + } + + func isTrusted(identity _: MartinOMEMO.SignalAddress, publicKeyData _: Data?) -> Bool { + true } func setStatus(_ status: MartinOMEMO.IdentityStatus, forIdentity: MartinOMEMO.SignalAddress) -> Bool { @@ -433,26 +436,57 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { return false } - func identities(forName: String) -> [MartinOMEMO.Identity] { - print(forName) - return [] + func identities(forName name: String) -> [MartinOMEMO.Identity] { + do { + return try Database.shared.dbQueue.read { db in + try Row.fetchAll( + db, + sql: "SELECT * FROM omemo_identities WHERE account = :account AND name = :name", + arguments: ["account": credentials.bareJid, "name": name] + ) + }.compactMap { row in + guard + let fingerprint = row["fingerprint"] as? String, + let statusInt = row["status"] as? Int, + let status = MartinOMEMO.IdentityStatus(rawValue: statusInt), + let deviceId = row["device_id"] as? Int32, + let own = row["own"] as? Int, + let key = row["key"] as? Data + else { + return nil + } + return MartinOMEMO.Identity(address: MartinOMEMO.SignalAddress(name: name, deviceId: deviceId), status: status, fingerprint: fingerprint, key: key, own: own > 0) + } + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return [] + } } func identityFingerprint(forAddress address: MartinOMEMO.SignalAddress) -> String? { - print(address) - return nil + do { + let data = try Database.shared.dbQueue.read { db in + try Row.fetchOne( + db, + sql: "SELECT fingerprint FROM omemo_identities WHERE account = :account AND name = :name AND device_id = :deviceId", + arguments: ["account": credentials.bareJid, "name": address.name, "deviceId": address.deviceId] + ) + } + return data?["fingerprint"] + } catch { + logIt(.error, "Error fetching chats: \(error.localizedDescription)") + return nil + } } } // MARK: - SenderKey extension ClientMartinOMEMO: SignalSenderKeyStoreProtocol { - func storeSenderKey(_ key: Data, address: MartinOMEMO.SignalAddress?, groupId: String?) -> Bool { - print(key, address, groupId) - return false + func storeSenderKey(_: Data, address _: MartinOMEMO.SignalAddress?, groupId _: String?) -> Bool { + false } - func loadSenderKey(forAddress: MartinOMEMO.SignalAddress?, groupId: String?) -> Data? { - print(forAddress, groupId) - return nil + func loadSenderKey(forAddress _: MartinOMEMO.SignalAddress?, groupId _: String?) -> Data? { + nil } }