session-ios/SessionMessagingKit/Database/Storage+Shared.swift

47 lines
1.7 KiB
Swift
Raw Normal View History

2020-12-07 06:00:21 +01:00
import PromiseKit
2020-12-10 06:12:22 +01:00
import Sodium
2020-11-24 10:09:23 +01:00
extension Storage {
2020-12-07 06:00:21 +01:00
@discardableResult
public func write(with block: @escaping (Any) -> Void) -> Promise<Void> {
Storage.write(with: { block($0) })
}
2020-12-07 06:00:21 +01:00
@discardableResult
public func write(with block: @escaping (Any) -> Void, completion: @escaping () -> Void) -> Promise<Void> {
Storage.write(with: { block($0) }, completion: completion)
}
public func writeSync(with block: @escaping (Any) -> Void) {
Storage.writeSync { block($0) }
2020-11-12 22:41:54 +01:00
}
@objc public func getUserPublicKey() -> String? {
2020-11-26 02:56:08 +01:00
return OWSIdentityManager.shared().identityKeyPair()?.hexEncodedPublicKey
}
2020-11-12 22:41:54 +01:00
public func getUserKeyPair() -> ECKeyPair? {
return OWSIdentityManager.shared().identityKeyPair()
}
2020-12-10 06:12:22 +01:00
public func getUserED25519KeyPair() -> Box.KeyPair? {
let dbConnection = OWSIdentityManager.shared().dbConnection
let collection = OWSPrimaryStorageIdentityKeyStoreCollection
guard let hexEncodedPublicKey = dbConnection.object(forKey: LKED25519PublicKey, inCollection: collection) as? String,
let hexEncodedSecretKey = dbConnection.object(forKey: LKED25519SecretKey, inCollection: collection) as? String else { return nil }
let publicKey = Box.KeyPair.PublicKey(hex: hexEncodedPublicKey)
let secretKey = Box.KeyPair.SecretKey(hex: hexEncodedSecretKey)
return Box.KeyPair(publicKey: publicKey, secretKey: secretKey)
}
2020-11-12 22:41:54 +01:00
2021-02-26 05:56:41 +01:00
@objc public func getUser() -> Contact? {
guard let userPublicKey = getUserPublicKey() else { return nil }
var result: Contact?
Storage.read { transaction in
result = Storage.shared.getContact(with: userPublicKey)
}
return result
2020-11-26 05:16:35 +01:00
}
}