diff --git a/FlowCrypt/Functionality/Services/Local Pub Key Services/ContactsService.swift b/FlowCrypt/Functionality/Services/Local Pub Key Services/ContactsService.swift index 0b1495552..8e9647791 100644 --- a/FlowCrypt/Functionality/Services/Local Pub Key Services/ContactsService.swift +++ b/FlowCrypt/Functionality/Services/Local Pub Key Services/ContactsService.swift @@ -44,18 +44,16 @@ struct ContactsService: ContactsServiceType { extension ContactsService: ContactsProviderType { func searchContact(with email: String) -> Promise { guard let contact = localContactsProvider.searchRecipient(with: email) else { - return searchRemote(for: email) + return pubLookup.lookup(with: email).then { recipient in + localContactsProvider.save(recipient: recipient) + } + } + pubLookup.lookup(with: email).then { recipient in + localContactsProvider.updateKeys(for: recipient) } return Promise(contact) } - private func searchRemote(for email: String) -> Promise { - pubLookup - .lookup(with: email) - .then { recipient in - self.localContactsProvider.save(recipient: recipient) - } - } } extension ContactsService: PublicKeyProvider { diff --git a/FlowCrypt/Functionality/Services/Local Pub Key Services/LocalContactsProvider.swift b/FlowCrypt/Functionality/Services/Local Pub Key Services/LocalContactsProvider.swift index c47f5c768..0c67b184b 100644 --- a/FlowCrypt/Functionality/Services/Local Pub Key Services/LocalContactsProvider.swift +++ b/FlowCrypt/Functionality/Services/Local Pub Key Services/LocalContactsProvider.swift @@ -15,6 +15,7 @@ protocol LocalContactsProviderType: PublicKeyProvider { func searchRecipient(with email: String) -> RecipientWithPubKeys? func save(recipient: RecipientWithPubKeys) func remove(recipient: RecipientWithPubKeys) + func updateKeys(for recipient: RecipientWithPubKeys) func getAllRecipients() -> [RecipientWithPubKeys] } @@ -56,6 +57,22 @@ extension LocalContactsProvider: LocalContactsProviderType { ) } + func updateKeys(for recipient: RecipientWithPubKeys) { + guard let recipientObject = find(with: recipient.email) else { + localContactsCache.save(RecipientObject(recipient)) + return + } + + recipient.pubKeys + .forEach { pubKey in + if let index = recipientObject.pubKeys.firstIndex(where: { $0.primaryFingerprint == pubKey.fingerprint }) { + update(pubKey: pubKey, for: recipientObject, at: index) + } else { + add(pubKey: pubKey, to: recipientObject) + } + } + } + func searchRecipient(with email: String) -> RecipientWithPubKeys? { guard let recipientObject = find(with: email) else { return nil } return RecipientWithPubKeys(recipientObject) @@ -76,7 +93,7 @@ extension LocalContactsProvider: LocalContactsProviderType { func removePubKey(with fingerprint: String, for email: String) { find(with: email)? .pubKeys - .filter { $0.fingerprint == fingerprint } + .filter { $0.primaryFingerprint == fingerprint } .forEach { key in try? localContactsCache.realm.write { localContactsCache.realm.delete(key) @@ -90,4 +107,22 @@ extension LocalContactsProvider { localContactsCache.realm.object(ofType: RecipientObject.self, forPrimaryKey: email) } + + private func add(pubKey: PubKey, to recipient: RecipientObject) { + guard let pubKeyObject = try? PubKeyObject(pubKey) else { return } + try? localContactsCache.realm.write { + recipient.pubKeys.append(pubKeyObject) + } + } + + private func update(pubKey: PubKey, for recipient: RecipientObject, at index: Int) { + guard let existingKeyLastSig = recipient.pubKeys[index].lastSig, + let updateKeyLastSig = pubKey.lastSig, + updateKeyLastSig > existingKeyLastSig + else { return } + + try? localContactsCache.realm.write { + recipient.pubKeys[index].update(from: pubKey) + } + } } diff --git a/FlowCrypt/Models/Realm Models/PubKeyObject.swift b/FlowCrypt/Models/Realm Models/PubKeyObject.swift index 2d888b6f6..a2d8ad069 100644 --- a/FlowCrypt/Models/Realm Models/PubKeyObject.swift +++ b/FlowCrypt/Models/Realm Models/PubKeyObject.swift @@ -38,7 +38,7 @@ final class PubKeyObject: Object { self.expiresOn = expiresOn self.created = created - self.longids.append(objectsIn: fingerprints) + self.longids.append(objectsIn: longids) self.fingerprints.append(objectsIn: fingerprints) guard let primaryFingerprint = self.fingerprints.first else { @@ -64,5 +64,19 @@ extension PubKeyObject { } extension PubKeyObject { - var fingerprint: String? { fingerprints.first } + func update(from key: PubKey) { + self.armored = key.armored + self.lastSig = key.lastSig + self.lastChecked = key.lastChecked + self.expiresOn = key.expiresOn + self.created = key.created + + let longids = List() + longids.append(objectsIn: key.longids) + self.longids = longids + + let fingerprints = List() + fingerprints.append(objectsIn: key.fingerprints) + self.fingerprints = fingerprints + } } diff --git a/Podfile.lock b/Podfile.lock index 4e43b47a0..d4c396aa3 100644 --- a/Podfile.lock +++ b/Podfile.lock @@ -15,8 +15,8 @@ PODS: - PINRemoteImage/PINCache (3.0.3): - PINCache (~> 3.0.3) - PINRemoteImage/Core - - SwiftFormat/CLI (0.48.16) - - SwiftLint (0.44.0) + - SwiftFormat/CLI (0.48.17) + - SwiftLint (0.45.0) - SwiftyRSA (1.7.0): - SwiftyRSA/ObjC (= 1.7.0) - SwiftyRSA/ObjC (1.7.0) @@ -64,8 +64,8 @@ SPEC CHECKSUMS: PINCache: 7a8fc1a691173d21dbddbf86cd515de6efa55086 PINOperation: 00c935935f1e8cf0d1e2d6b542e75b88fc3e5e20 PINRemoteImage: f1295b29f8c5e640e25335a1b2bd9d805171bd01 - SwiftFormat: b8a60419c4a8680f67faa80e2da710fd9dc85ca5 - SwiftLint: e96c0a8c770c7ebbc4d36c55baf9096bb65c4584 + SwiftFormat: 0a9044eb365d74d4a0a2cefa5fe44a4cbef382a7 + SwiftLint: e5c7f1fba68eccfc51509d5b2ce1699f5502e0c7 SwiftyRSA: 8c6dd1ea7db1b8dc4fb517a202f88bb1354bc2c6 Texture: 2e8ab2519452515f7f5a520f5a8f7e0a413abfa3