mirror of https://github.com/oxen-io/session-ios
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1.7 KiB
Swift
47 lines
1.7 KiB
Swift
7 years ago
|
//
|
||
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import PromiseKit
|
||
|
|
||
|
@objc(SSKCreatePreKeysOperation)
|
||
|
public class CreatePreKeysOperation: OWSOperation {
|
||
|
private var accountManager: AccountManager {
|
||
|
return AccountManager.shared
|
||
|
}
|
||
|
private var primaryStorage: OWSPrimaryStorage {
|
||
|
return OWSPrimaryStorage.shared()
|
||
|
}
|
||
|
|
||
|
private var identityKeyManager: OWSIdentityManager {
|
||
|
return OWSIdentityManager.shared()
|
||
|
}
|
||
|
|
||
|
public override func run() {
|
||
|
Logger.debug("")
|
||
|
|
||
|
if self.identityKeyManager.identityKeyPair() == nil {
|
||
|
self.identityKeyManager.generateNewIdentityKey()
|
||
|
}
|
||
|
let identityKey: Data = self.identityKeyManager.identityKeyPair()!.publicKey
|
||
|
let signedPreKeyRecord: SignedPreKeyRecord = self.primaryStorage.generateRandomSignedRecord()
|
||
|
let preKeyRecords: [PreKeyRecord] = self.primaryStorage.generatePreKeyRecords()
|
||
|
|
||
|
firstly {
|
||
|
self.primaryStorage.storeSignedPreKey(signedPreKeyRecord.id, signedPreKeyRecord: signedPreKeyRecord)
|
||
|
self.primaryStorage.storePreKeyRecords(preKeyRecords)
|
||
|
|
||
|
return self.accountManager.setPreKeys(identityKey: identityKey, signedPreKeyRecord: signedPreKeyRecord, preKeyRecords: preKeyRecords)
|
||
|
}.then { () -> Void in
|
||
|
signedPreKeyRecord.markAsAcceptedByService()
|
||
|
self.primaryStorage.setCurrentSignedPrekeyId(signedPreKeyRecord.id)
|
||
|
}.then { () -> Void in
|
||
|
Logger.debug("done")
|
||
|
self.reportSuccess()
|
||
|
}.catch { error in
|
||
|
self.reportError(error)
|
||
|
}.retainUntilComplete()
|
||
|
}
|
||
|
}
|