mv-experiment #1
|
@ -11,7 +11,6 @@ final class ClientMartinOMEMO {
|
||||||
|
|
||||||
init(_ credentials: Credentials) {
|
init(_ credentials: Credentials) {
|
||||||
self.credentials = credentials
|
self.credentials = credentials
|
||||||
_ = regenerateKeys(wipe: false)
|
|
||||||
print("ClientMartinOMEMO init")
|
print("ClientMartinOMEMO init")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,27 +23,60 @@ final class ClientMartinOMEMO {
|
||||||
// swiftlint:disable:next force_unwrapping
|
// swiftlint:disable:next force_unwrapping
|
||||||
let signalContext = SignalContext(withStorage: signalStorage)!
|
let signalContext = SignalContext(withStorage: signalStorage)!
|
||||||
signalStorage.setup(withContext: signalContext)
|
signalStorage.setup(withContext: signalContext)
|
||||||
|
|
||||||
|
_ = regenerateKeys(wipe: false, context: signalContext)
|
||||||
|
|
||||||
return (signalStorage, signalContext)
|
return (signalStorage, signalContext)
|
||||||
}
|
}
|
||||||
|
|
||||||
private func regenerateKeys(wipe: Bool = false) -> Bool {
|
private func regenerateKeys(wipe: Bool = false, context: SignalContext) -> Bool {
|
||||||
if wipe {
|
if wipe {
|
||||||
OMEMOSession.wipe(account: credentials.bareJid)
|
OMEMOSession.wipe(account: credentials.bareJid)
|
||||||
OMEMOPreKey.wipe(account: credentials.bareJid)
|
OMEMOPreKey.wipe(account: credentials.bareJid)
|
||||||
OMEMOSignedPreKey.wipe(account: credentials.bareJid)
|
OMEMOSignedPreKey.wipe(account: credentials.bareJid)
|
||||||
OMEMOIdentity.wipe(account: credentials.bareJid)
|
OMEMOIdentity.wipe(account: credentials.bareJid)
|
||||||
|
Settings.getFor(credentials.bareJid)?.wipeOmemoRegId()
|
||||||
}
|
}
|
||||||
|
|
||||||
let hasKeyPair = keyPair() != nil
|
let hasKeyPair = keyPair() != nil
|
||||||
if wipe || localRegistrationId() == 0 || !hasKeyPair {
|
if wipe || localRegistrationId() == 0 || !hasKeyPair {
|
||||||
// let regId: UInt32 = signalContext.generateRegistrationId()
|
let regId: UInt32 = context.generateRegistrationId()
|
||||||
// AccountSettings.omemoRegistrationId(for: context!.sessionObject.userBareJid!, value: regId)
|
|
||||||
//
|
var settings = Settings.getFor(credentials.bareJid)
|
||||||
// let keyPair = SignalIdentityKeyPair.generateKeyPair(context: signalContext)
|
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) {}
|
// if !identityKeyStore.save(identity: SignalAddress(name: context!.sessionObject.userBareJid!.stringValue, deviceId: Int32(identityKeyStore.localRegistrationId())), key: keyPair) {}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func fingerprint(publicKey: Data) -> String {
|
||||||
|
publicKey.map { byte -> String in
|
||||||
|
String(format: "%02x", byte)
|
||||||
|
}.joined()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Session
|
// MARK: - Session
|
||||||
|
@ -132,10 +164,11 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol {
|
||||||
// MARK: - Identity
|
// MARK: - Identity
|
||||||
extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
|
extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
|
||||||
func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? {
|
func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? {
|
||||||
// guard let deviceId = localRegistrationId(forAccount: account) else {
|
let deviceId = localRegistrationId()
|
||||||
// return nil
|
guard deviceId != 0 else {
|
||||||
// }
|
return nil
|
||||||
//
|
}
|
||||||
|
|
||||||
// guard
|
// guard
|
||||||
// let data = try! Database.main.reader({ database in
|
// let data = try! Database.main.reader({ database in
|
||||||
// try database.select(query: .omemoKeyPairForAccount, params: ["account": account, "name": account.stringValue, "deviceId": deviceId]).mapFirst { $0.data(for: "key") }
|
// try database.select(query: .omemoKeyPairForAccount, params: ["account": account, "name": account.stringValue, "deviceId": deviceId]).mapFirst { $0.data(for: "key") }
|
||||||
|
@ -145,17 +178,15 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// return SignalIdentityKeyPair(fromKeyPairData: data)
|
// return SignalIdentityKeyPair(fromKeyPairData: data)
|
||||||
nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func localRegistrationId() -> UInt32 {
|
func localRegistrationId() -> UInt32 {
|
||||||
//
|
if let settings = Settings.getFor(credentials.bareJid) {
|
||||||
//
|
return UInt32(settings.omemoRegId)
|
||||||
//
|
} else {
|
||||||
//
|
return 0
|
||||||
//
|
}
|
||||||
//
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {
|
func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {
|
||||||
|
|
49
ConversationsClassic/AppData/Model/Settings.swift
Normal file
49
ConversationsClassic/AppData/Model/Settings.swift
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
import Foundation
|
||||||
|
import GRDB
|
||||||
|
|
||||||
|
struct Settings: DBStorable {
|
||||||
|
static let databaseTableName = "settings"
|
||||||
|
|
||||||
|
let bareJid: String
|
||||||
|
var omemoRegId: Int
|
||||||
|
|
||||||
|
var id: String {
|
||||||
|
bareJid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension Settings {
|
||||||
|
static func getFor(_ bareJid: String) -> Settings? {
|
||||||
|
do {
|
||||||
|
return try Database.shared.dbQueue.read { db in
|
||||||
|
let settings = try Settings.filter(Column("bareJid") == bareJid).fetchOne(db)
|
||||||
|
return settings
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
logIt(.error, "Settings not exists for \(bareJid)")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func wipeOmemoRegId() {
|
||||||
|
do {
|
||||||
|
_ = try Database.shared.dbQueue.write { db in
|
||||||
|
try Settings
|
||||||
|
.filter(Column("bareJid") == bareJid)
|
||||||
|
.updateAll(db, Column("omemoRegId").set(to: 0))
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
logIt(.error, "Failed to wipe omemoRegId for \(bareJid)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func save() {
|
||||||
|
do {
|
||||||
|
try Database.shared.dbQueue.write { db in
|
||||||
|
try self.insert(db)
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
logIt(.error, "Failed to save settings for \(bareJid)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -103,6 +103,13 @@ extension Database {
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
migrator.registerMigration("Add settings table") { db in
|
||||||
|
try db.create(table: "settings", options: [.ifNotExists]) { table in
|
||||||
|
table.column("bareJid", .text).notNull().primaryKey().unique(onConflict: .replace)
|
||||||
|
table.column("omemoRegId", .integer).notNull()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// return migrator
|
// return migrator
|
||||||
return migrator
|
return migrator
|
||||||
}()
|
}()
|
||||||
|
|
Loading…
Reference in a new issue