From d950c4c9078690bffdf6834752821462cfcf370b Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 31 Aug 2024 15:23:21 +0200 Subject: [PATCH] wip --- .../AppData/Client/Client+MartinOMEMO.swift | 68 ++++++++++++------- 1 file changed, 42 insertions(+), 26 deletions(-) diff --git a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift index 1fb4a34..1309126 100644 --- a/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift +++ b/ConversationsClassic/AppData/Client/Client+MartinOMEMO.swift @@ -54,31 +54,35 @@ final class ClientMartinOMEMO { String(format: "%02x", byte) }.joined() - guard !OMEMOIdentity.existsFor(account: credentials.bareJid, name: credentials.bareJid, fingerprint: fingerprint) else { - return false - } - - 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 save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized()) } return true } + + private func save(address: SignalAddress, fingerprint: String, own: Bool, data: Data) -> Bool { + guard !OMEMOIdentity.existsFor(account: credentials.bareJid, name: address.name, fingerprint: fingerprint) else { + return false + } + + do { + _ = try Database.shared.dbQueue.write { db in + try OMEMOIdentity( + account: credentials.bareJid, + name: address.name, + deviceId: Int(address.deviceId), + fingerprint: fingerprint, + key: data, + own: own, + status: MartinOMEMO.IdentityStatus.trustedActive.rawValue + ) + .insert(db) + } + return true + } catch { + logIt(.error, "Error storing identity key: \(error.localizedDescription)") + return false + } + } } // MARK: - Session @@ -197,13 +201,25 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { } func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool { - print(identity, key) - return false + guard let key = key as SignalIdentityKeyProtocol?, let publicKey = key.publicKey else { + return false + } + let fingerprint = publicKey.map { byte -> String in + String(format: "%02x", byte) + }.joined() + + return save(address: identity, fingerprint: fingerprint, own: true, data: key.serialized()) } func save(identity: MartinOMEMO.SignalAddress, publicKeyData: Data?) -> Bool { - print(identity, publicKeyData) - return false + guard let publicKeyData = publicKeyData else { + return false + } + let fingerprint = publicKeyData.map { byte -> String in + String(format: "%02x", byte) + }.joined() + + return save(address: identity, fingerprint: fingerprint, own: false, data: publicKeyData) } func isTrusted(identity _: MartinOMEMO.SignalAddress, key _: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {