From b2ccecfadc41a246e3665444f9eb029e00c5dbee Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Thu, 4 Aug 2022 21:52:28 +0100
Subject: [PATCH] Add CMixManagerRestore functor

---
 .../CmixManager/CMixManager.swift             | 14 +++++-
 .../Functors/CMixManagerRestore.swift         | 46 +++++++++++++++++++
 2 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 Sources/ElixxirDAppsSDK/CmixManager/Functors/CMixManagerRestore.swift

diff --git a/Sources/ElixxirDAppsSDK/CmixManager/CMixManager.swift b/Sources/ElixxirDAppsSDK/CmixManager/CMixManager.swift
index 1763bb3e..1e1b38e5 100644
--- a/Sources/ElixxirDAppsSDK/CmixManager/CMixManager.swift
+++ b/Sources/ElixxirDAppsSDK/CmixManager/CMixManager.swift
@@ -3,6 +3,7 @@ import Bindings
 public struct CMixManager {
   public var hasStorage: CMixManagerHasStorage
   public var create: CMixManagerCreate
+  public var restore: CMixManagerRestore
   public var load: CMixManagerLoad
   public var remove: CMixManagerRemove
 }
@@ -21,7 +22,8 @@ extension CMixManager {
     passwordStorage: PasswordStorage,
     newCMix: NewCMix = .live,
     getCMixParams: GetCMixParams = .liveDefault,
-    loadCMix: LoadCMix = .live
+    loadCMix: LoadCMix = .live,
+    newCMixFromBackup: NewCMixFromBackup = .live
   ) -> CMixManager {
     CMixManager(
       hasStorage: .live(
@@ -39,6 +41,15 @@ extension CMixManager {
         getCMixParams: getCMixParams,
         loadCMix: loadCMix
       ),
+      restore: .live(
+        environment: environment,
+        downloadNDF: downloadNDF,
+        generateSecret: generateSecret,
+        passwordStorage: passwordStorage,
+        directoryPath: directoryPath,
+        fileManager: fileManager,
+        newCMixFromBackup: newCMixFromBackup
+      ),
       load: .live(
         directoryPath: directoryPath,
         passwordStorage: passwordStorage,
@@ -57,6 +68,7 @@ extension CMixManager {
   public static let unimplemented = CMixManager(
     hasStorage: .unimplemented,
     create: .unimplemented,
+    restore: .unimplemented,
     load: .unimplemented,
     remove: .unimplemented
   )
diff --git a/Sources/ElixxirDAppsSDK/CmixManager/Functors/CMixManagerRestore.swift b/Sources/ElixxirDAppsSDK/CmixManager/Functors/CMixManagerRestore.swift
new file mode 100644
index 00000000..466684b8
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/CmixManager/Functors/CMixManagerRestore.swift
@@ -0,0 +1,46 @@
+import Foundation
+import XCTestDynamicOverlay
+
+public struct CMixManagerRestore {
+  public var run: (Data, String) throws -> BackupReport
+
+  public func callAsFunction(
+    backup: Data,
+    passphrase: String
+  ) throws -> BackupReport {
+    try run(backup, passphrase)
+  }
+}
+
+extension CMixManagerRestore {
+  public static func live(
+    environment: Environment,
+    downloadNDF: DownloadAndVerifySignedNdf,
+    generateSecret: GenerateSecret,
+    passwordStorage: PasswordStorage,
+    directoryPath: String,
+    fileManager: FileManager,
+    newCMixFromBackup: NewCMixFromBackup
+  ) -> CMixManagerRestore {
+    CMixManagerRestore { backup, passphrase in
+      let ndfData = try downloadNDF(environment)
+      let password = generateSecret()
+      try passwordStorage.save(password)
+      try? fileManager.removeItem(atPath: directoryPath)
+      try? fileManager.createDirectory(atPath: directoryPath, withIntermediateDirectories: true)
+      return try newCMixFromBackup(
+        ndfJSON: String(data: ndfData, encoding: .utf8)!,
+        storageDir: directoryPath,
+        backupPassphrase: passphrase,
+        sessionPassword: password,
+        backupFileContents: backup
+      )
+    }
+  }
+}
+
+extension CMixManagerRestore {
+  public static let unimplemented = CMixManagerRestore(
+    run: XCTUnimplemented("\(Self.self)")
+  )
+}
-- 
GitLab