Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.03 KB | None | 0 0
  1. open func inAppUsage(identity: String, password: String) throws {
  2. // Derive separate passwords for login and backup from single one
  3. let backupPassword = try EThree.derivePasswords(from: password).backupPassword
  4.  
  5. self.initUser(backupPassword: backupPassword).start { _, error in
  6. guard error == nil else {
  7. // Error handling here
  8. if let error = error as? EThreeError, error == .wrongPassword {
  9. // Wrong password case
  10. }
  11. else {
  12. // Unknown error handling
  13. }
  14. return
  15. }
  16. // User is initialized!
  17. }
  18. }
  19.  
  20. /// Initializes user on current device
  21. /// - Parameters:
  22. /// - identity: identity of user
  23. /// - password: user password
  24. open func initUser(backupPassword: String) -> GenericOperation<Void> {
  25. return CallbackOperation { _, completion in
  26. do {
  27. // Clean up local Private Key if exists
  28. if try self.ethree.hasLocalPrivateKey() {
  29. try self.ethree.cleanUp()
  30. }
  31.  
  32. do {
  33. let selfCard = try self.ethree.findUser(with: self.ethree.identity, forceReload: true)
  34. .startSync()
  35. .get()
  36.  
  37. // Self Card found, current user exists on Virgil Cloud
  38. try self.restoreUser(backupPassword: backupPassword, selfCard: selfCard)
  39. }
  40. catch FindUsersError.cardWasNotFound {
  41. // Self Card was not found, user is not registered on VirgilCloud
  42. try self.createUser(backupPassword: backupPassword)
  43. }
  44.  
  45. completion((), nil)
  46. } catch {
  47. completion(nil, error)
  48. }
  49. }
  50. }
  51.  
  52. /// Registeres user on Virgil Cloud and backs up Private Key
  53. /// - Parameter password: backup password
  54. open func createUser(backupPassword: String) throws {
  55. do {
  56. try self.ethree.register().startSync().get()
  57.  
  58. try self.ethree.backupPrivateKey(password: backupPassword).startSync().get()
  59. }
  60. catch CloudKeyStorageError.entryAlreadyExists {
  61. // For some reason Private Key backup already exists.
  62. // We should reset it and back up new one
  63. try self.ethree.resetPrivateKeyBackup().startSync().get()
  64.  
  65. // This sleep prevents throttling on Virgil Pythia service
  66. sleep(2)
  67.  
  68. try self.ethree.backupPrivateKey(password: backupPassword).startSync().get()
  69. }
  70. }
  71.  
  72. /// Restores user Private Key
  73. /// - Parameters:
  74. /// - password: backup password
  75. /// - selfCard: Self Card
  76. open func restoreUser(backupPassword: String, selfCard: Card) throws {
  77. do {
  78. try self.ethree.restorePrivateKey(password: backupPassword).startSync().get()
  79.  
  80. // Checking if restored Private Key matches active Self Card
  81. if try !self.isKeyValid(card: selfCard) {
  82. // Reset not valid backup
  83. try self.ethree.resetPrivateKeyBackup().startSync().get()
  84.  
  85. // This sleep prevents throttling on Virgil Pythia service
  86. sleep(2)
  87.  
  88. try self.rotateKey(backupPassword: backupPassword)
  89. }
  90. }
  91. catch CloudKeyStorageError.entryNotFound {
  92. // This sleep prevents throttling on Virgil Pythia service
  93. sleep(2)
  94.  
  95. try self.rotateKey(backupPassword: backupPassword)
  96. }
  97. }
  98.  
  99. /// Checks if restored Private Key matches active Self Card
  100. /// - Parameter card: Self Card to check
  101. open func isKeyValid(card: Card) throws -> Bool {
  102. // Retrieve local Private Key
  103. let params = try KeychainStorageParams.makeKeychainStorageParams()
  104. let keychain = KeychainStorage(storageParams: params)
  105. let entry = try keychain.retrieveEntry(withName: self.ethree.identity)
  106. let keyPair = try self.crypto.importPrivateKey(from: entry.data)
  107.  
  108. // Check that id of Self Card Public Key matches local Private Key one
  109. return card.publicKey.identifier == keyPair.identifier
  110. }
  111.  
  112. /// Performs rotate operation
  113. /// - Parameter password: backup password
  114. open func rotateKey(backupPassword: String) throws {
  115. // Clean up local Private Key if exists
  116. if try self.ethree.hasLocalPrivateKey() {
  117. try self.ethree.cleanUp()
  118. }
  119.  
  120. try self.ethree.rotatePrivateKey().startSync().get()
  121. try self.ethree.backupPrivateKey(password: backupPassword).startSync().get()
  122.  
  123. // You need to notify other contacts that they need to find this user
  124. // with forceReload=true to update cached card of this user
  125. try self.notifyContactsAboutRotate()
  126. }
  127.  
  128. open func notifyContactsAboutRotate() throws {
  129. print("key was rotated")
  130. // TODO: Fill me
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement