|
|
|
@ -1,14 +1,12 @@
|
|
|
|
|
import Sodium
|
|
|
|
|
|
|
|
|
|
extension Storage {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: Encryption Key Pairs
|
|
|
|
|
private static func getClosedGroupEncryptionKeyPairCollection(for groupPublicKey: String) -> String {
|
|
|
|
|
return "SNClosedGroupEncryptionKeyPairCollection-\(groupPublicKey)"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static let closedGroupPublicKeyCollection = "SNClosedGroupPublicKeyCollection"
|
|
|
|
|
private static let closedGroupFormationTimestampCollection = "SNClosedGroupFormationTimestampCollection"
|
|
|
|
|
private static let closedGroupZombieMembersCollection = "SNClosedGroupZombieMembersCollection"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public func getClosedGroupEncryptionKeyPairs(for groupPublicKey: String) -> [ECKeyPair] {
|
|
|
|
|
let collection = Storage.getClosedGroupEncryptionKeyPairCollection(for: groupPublicKey)
|
|
|
|
|
var timestampsAndKeyPairs: [(timestamp: Double, keyPair: ECKeyPair)] = []
|
|
|
|
@ -25,10 +23,11 @@ extension Storage {
|
|
|
|
|
return getClosedGroupEncryptionKeyPairs(for: groupPublicKey).last
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func addClosedGroupEncryptionKeyPair(_ keyPair: ECKeyPair, for groupPublicKey: String, using transaction: Any) {
|
|
|
|
|
public func addClosedGroupEncryptionKeyPair(_ keyPair: ECKeyPair, for groupPublicKey: String, using transaction: Any) -> String {
|
|
|
|
|
let collection = Storage.getClosedGroupEncryptionKeyPairCollection(for: groupPublicKey)
|
|
|
|
|
let timestamp = String(Date().timeIntervalSince1970)
|
|
|
|
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(keyPair, forKey: timestamp, inCollection: collection)
|
|
|
|
|
return timestamp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func removeAllClosedGroupEncryptionKeyPairs(for groupPublicKey: String, using transaction: Any) {
|
|
|
|
@ -36,6 +35,19 @@ extension Storage {
|
|
|
|
|
(transaction as! YapDatabaseReadWriteTransaction).removeAllObjects(inCollection: collection)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Authentication Key Pairs
|
|
|
|
|
private static func getClosedGroupAuthenticationKeyPairCollection(for groupPublicKey: String) -> String {
|
|
|
|
|
return "SNClosedGroupAuthenticationKeyPairCollection-\(groupPublicKey)"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public func addClosedGroupAuthenticationKeyPair(_ keyPair: Sign.KeyPair, for groupPublicKey: String, timestamp: String, using transaction: Any) {
|
|
|
|
|
let collection = Storage.getClosedGroupAuthenticationKeyPairCollection(for: groupPublicKey)
|
|
|
|
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(keyPair, forKey: timestamp, inCollection: collection)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Public Keys
|
|
|
|
|
private static let closedGroupPublicKeyCollection = "SNClosedGroupPublicKeyCollection"
|
|
|
|
|
|
|
|
|
|
public func getUserClosedGroupPublicKeys() -> Set<String> {
|
|
|
|
|
var result: Set<String> = []
|
|
|
|
|
Storage.read { transaction in
|
|
|
|
@ -52,6 +64,9 @@ extension Storage {
|
|
|
|
|
(transaction as! YapDatabaseReadWriteTransaction).removeObject(forKey: groupPublicKey, inCollection: Storage.closedGroupPublicKeyCollection)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Formation Timestamps
|
|
|
|
|
private static let closedGroupFormationTimestampCollection = "SNClosedGroupFormationTimestampCollection"
|
|
|
|
|
|
|
|
|
|
public func getClosedGroupFormationTimestamp(for groupPublicKey: String) -> UInt64? {
|
|
|
|
|
var result: UInt64?
|
|
|
|
|
Storage.read { transaction in
|
|
|
|
@ -64,6 +79,9 @@ extension Storage {
|
|
|
|
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(timestamp, forKey: groupPublicKey, inCollection: Storage.closedGroupFormationTimestampCollection)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Zombie Members
|
|
|
|
|
private static let closedGroupZombieMembersCollection = "SNClosedGroupZombieMembersCollection"
|
|
|
|
|
|
|
|
|
|
public func getZombieMembers(for groupPublicKey: String) -> Set<String> {
|
|
|
|
|
var result: Set<String> = []
|
|
|
|
|
Storage.read { transaction in
|
|
|
|
@ -77,7 +95,8 @@ extension Storage {
|
|
|
|
|
public func setZombieMembers(for groupPublicKey: String, to zombies: Set<String>, using transaction: Any) {
|
|
|
|
|
(transaction as! YapDatabaseReadWriteTransaction).setObject(zombies, forKey: groupPublicKey, inCollection: Storage.closedGroupZombieMembersCollection)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MARK: Convenience
|
|
|
|
|
public func isClosedGroup(_ publicKey: String) -> Bool {
|
|
|
|
|
getUserClosedGroupPublicKeys().contains(publicKey)
|
|
|
|
|
}
|
|
|
|
|