mv-experiment #1
|
@ -40,42 +40,44 @@ 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)
|
||||
|
||||
// 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) {}
|
||||
guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private func fingerprint(publicKey: Data) -> String {
|
||||
publicKey.map { byte -> String in
|
||||
let fingerprint = publicKey.map { byte -> String in
|
||||
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 true
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue