This commit is contained in:
fmodf 2024-10-23 17:07:30 +02:00
parent 841387aab7
commit e39dbb2e49
5 changed files with 70 additions and 50 deletions

View file

@ -27,14 +27,15 @@ final class ClientMartinOMEMO {
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)
UserSettings.omemoDeviceId = 0 UserSettings.set(omemoDeviceId: 0, for: credentials.bareJid)
} }
let hasKeyPair = keyPair() != nil let hasKeyPair = keyPair() != nil
if wipe || UserSettings.omemoDeviceId == 0 || !hasKeyPair { let deviceId = UserSettings.get(omemoDeviceIdFor: credentials.bareJid)
if wipe || deviceId == 0 || !hasKeyPair {
let regId: UInt32 = context.generateRegistrationId() let regId: UInt32 = context.generateRegistrationId()
let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId)) let address = SignalAddress(name: credentials.bareJid, deviceId: Int32(regId))
UserSettings.omemoDeviceId = regId UserSettings.set(omemoDeviceId: regId, for: credentials.bareJid)
guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else { guard let keyPair = SignalIdentityKeyPair.generateKeyPair(context: context), let publicKey = keyPair.publicKey else {
return false return false
@ -45,7 +46,6 @@ final class ClientMartinOMEMO {
return save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized()) return save(address: address, fingerprint: fingerprint, own: true, data: keyPair.serialized())
} }
print("omemoDeviceId \(UserSettings.omemoDeviceId)")
return true return true
} }
@ -160,7 +160,7 @@ extension ClientMartinOMEMO: SignalSessionStoreProtocol {
// MARK: - Identity // MARK: - Identity
extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol { extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? { func keyPair() -> (any MartinOMEMO.SignalIdentityKeyPairProtocol)? {
let deviceId = UserSettings.omemoDeviceId let deviceId = UserSettings.get(omemoDeviceIdFor: credentials.bareJid)
guard deviceId != 0 else { guard deviceId != 0 else {
return nil return nil
} }
@ -183,7 +183,7 @@ extension ClientMartinOMEMO: SignalIdentityKeyStoreProtocol {
} }
func localRegistrationId() -> UInt32 { func localRegistrationId() -> UInt32 {
UserSettings.omemoDeviceId UserSettings.get(omemoDeviceIdFor: credentials.bareJid)
} }
func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool { func save(identity: MartinOMEMO.SignalAddress, key: (any MartinOMEMO.SignalIdentityKeyProtocol)?) -> Bool {

View file

@ -121,7 +121,7 @@ extension Client {
} }
func disconnect() { func disconnect() {
_ = connection.disconnect() _ = connection.disconnect(true)
} }
} }

View file

@ -69,14 +69,14 @@ extension Database {
} }
try db.create(table: "omemo_identities", options: [.ifNotExists]) { table in try db.create(table: "omemo_identities", options: [.ifNotExists]) { table in
table.column("account", .text).notNull() table.column("account", .text).notNull().collate(.nocase)
table.column("name", .text).notNull() table.column("name", .text).notNull()
table.column("deviceId", .integer).notNull() table.column("deviceId", .integer).notNull()
table.column("fingerprint", .text).notNull() table.column("fingerprint", .text).notNull()
table.column("key", .blob).notNull() table.column("key", .blob).notNull()
table.column("own", .integer).notNull() table.column("own", .integer).notNull()
table.column("status", .integer).notNull() table.column("status", .integer).notNull()
table.primaryKey(["account", "name", "fingerprint"], onConflict: .ignore) table.uniqueKey(["account", "name", "fingerprint"], onConflict: .ignore)
} }
try db.create(table: "omemo_pre_keys", options: [.ifNotExists]) { table in try db.create(table: "omemo_pre_keys", options: [.ifNotExists]) { table in

View file

@ -0,0 +1,61 @@
import Foundation
// Wrapper
@propertyWrapper
private struct Storage<T> {
private let key: String
private let defaultValue: T
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
// Read value from UserDefaults
UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
}
set {
// Set value to UserDefaults
UserDefaults.standard.set(newValue, forKey: key)
UserDefaults.standard.synchronize()
}
}
}
// Storage
private let kBase = "conversations.classic.user.defaults"
private let kOmemoDevicesIds = "\(kBase).omemoDevicesIds"
private let kSecureChatsByDefault = "\(kBase).secureChatsByDefault"
enum UserSettings {
@Storage(key: kOmemoDevicesIds, defaultValue: [:])
private static var omemoDevicesIds: [String: UInt32]
@Storage(key: kSecureChatsByDefault, defaultValue: false)
private static var vSecureChatsByDefault: Bool
}
// Public
extension UserSettings {
static func reset() {
omemoDevicesIds = [:]
vSecureChatsByDefault = false
}
static func set(omemoDeviceId: UInt32, for account: String) {
var dict = UserSettings.omemoDevicesIds
dict[account] = omemoDeviceId
UserSettings.omemoDevicesIds = dict
}
static func get(omemoDeviceIdFor account: String) -> UInt32 {
UserSettings.omemoDevicesIds[account] ?? 0
}
static var secureChatsByDefault: Bool {
get { UserSettings.vSecureChatsByDefault }
set { UserSettings.vSecureChatsByDefault = newValue }
}
}

View file

@ -1,41 +0,0 @@
import Foundation
// Wrapper
@propertyWrapper
struct Storage<T> {
private let key: String
private let defaultValue: T
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
// Read value from UserDefaults
UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
}
set {
// Set value to UserDefaults
UserDefaults.standard.set(newValue, forKey: key)
}
}
}
// Storage
private let kOmemoDeviceId = "conversations.classic.user.defaults.omemoDeviceId"
private let kSecureChatsByDefault = "conversations.classic.user.defaults.secureChatsByDefault"
enum UserSettings {
@Storage(key: kOmemoDeviceId, defaultValue: 0)
static var omemoDeviceId: UInt32
@Storage(key: kSecureChatsByDefault, defaultValue: false)
static var secureChatsByDefault: Bool
static func reset() {
omemoDeviceId = 0
secureChatsByDefault = false
}
}