From 40d2fe6383ca0b54256c985953fd2e40f32d35bf Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 31 Aug 2024 14:56:36 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinOMEMO.swift | 56 ++++++++++--------- .../AppData/Model/OMEMO.swift | 14 +++++ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 114b4b3..d975206 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -40,43 +40,45 @@ final class ClientMartinOMEMO { let hasKeyPair = keyPair() != nil if wipe || localRegistrationId() == 0 || !hasKeyPair { - let regId: UInt32 = context.generateRegistrationId() + let regId = context.generateRegistrationId() + let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId)) var settings = Settings.getFor(credentials.bareJid) settings?.omemoRegId = Int(regId) settings?.save() - let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context) + guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else { + return false + } + let fingerprint = publicKey.map { byte -> String in + String(format: "%02x", byte) + }.joined() - // do { - // _ = try Database.shared.dbQueue.write { db in - // try OMEMOIdentity( - // account: credentials.bareJid, - // name: credentials.bareJid, - // deviceId: regId, - // key: keyPair.publicKey, - // fingerprint: fingerprint(publicKey: keyPair.publicKey), - // status: MartinOMEMO.IdentityStatus.trusted.rawValue, - // own: 1 - // ) - // .insert(db) - // } - // } catch { - // logIt(.error, "Error storing identity key: \(error.localizedDescription)") - // return false - // } + guard !OMEMOIdentity.existsFor(account: credentials.bareJid, name: credentials.bareJid, fingerprint: fingerprint) else { + return false + } - // 102 - // if !identityKeyStore.save(identity: SignalAddress(name: context!.sessionObject.userBareJid!.stringValue, deviceId: Int32(identityKeyStore.localRegistrationId())), key: keyPair) {} + do { + _ = try Database.shared.dbQueue.write { db in + try OMEMOIdentity( + account: credentials.bareJid, + name: address.name, + deviceId: Int(address.deviceId), + fingerprint: fingerprint, + key: keyPair.serialized(), + own: true, + status: MartinOMEMO.IdentityStatus.trustedActive.rawValue + ) + .insert(db) + } + return true + } catch { + logIt(.error, "Error storing identity key: \(error.localizedDescription)") + return false + } } return true } - - private func fingerprint(publicKey: Data) -> String { - publicKey.map { byte -> String in - String(format: "%02x", byte) - }.joined() - } } // MARK: - Session diff --git a/ConversationsClassic/AppData/Model/OMEMO.swift b/ConversationsClassic/AppData/Model/OMEMO.swift index 65ff9ef..75a5450 100644 --- a/ConversationsClassic/AppData/Model/OMEMO.swift +++ b/ConversationsClassic/AppData/Model/OMEMO.swift @@ -112,6 +112,20 @@ extension OMEMOIdentity { logIt(.error, "Failed to wipe OMEMO identity: \(error)") } } + + static func existsFor(account: String, name: String, fingerprint: String) -> Bool { + do { + return try Database.shared.dbQueue.read { db in + try OMEMOIdentity + .filter(Column("account") == account) + .filter(Column("name") == name) + .filter(Column("fingerprint") == fingerprint) + .fetchOne(db) != nil + } + } catch { + return false + } + } } // MARK: - PreKey