This commit is contained in:
fmodf 2024-08-31 14:56:36 +02:00
parent 9ee1f80afa
commit 40d2fe6383
2 changed files with 43 additions and 27 deletions

View file

@ -40,42 +40,44 @@ final class ClientMartinOMEMO {
let hasKeyPair = keyPair() != nil let hasKeyPair = keyPair() != nil
if wipe || localRegistrationId() == 0 || !hasKeyPair { 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) var settings = Settings.getFor(credentials.bareJid)
settings?.omemoRegId = Int(regId) settings?.omemoRegId = Int(regId)
settings?.save() settings?.save()
let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context) guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else {
return false
// 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
// }
// 102
// if !identityKeyStore.save(identity: SignalAddress(name: context!.sessionObject.userBareJid!.stringValue, deviceId: Int32(identityKeyStore.localRegistrationId())), key: keyPair) {}
} }
return true let fingerprint = publicKey.map { byte -> String in
}
private func fingerprint(publicKey: Data) -> String {
publicKey.map { byte -> String in
String(format: "%02x", byte) String(format: "%02x", byte)
}.joined() }.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 true
} }
} }

View file

@ -112,6 +112,20 @@ extension OMEMOIdentity {
logIt(.error, "Failed to wipe OMEMO identity: \(error)") 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 // MARK: - PreKey