From 8d56fcfe90d2fd38f1eb0f46b1e51cdc393fe198 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Wed, 1 Jun 2022 11:51:43 +0200
Subject: [PATCH] Add ClientCreator

---
 Sources/ElixxirDAppsSDK/ClientCreator.swift | 32 +++++++++++++++++++++
 Sources/ElixxirDAppsSDK/Errors.swift        |  4 +++
 2 files changed, 36 insertions(+)
 create mode 100644 Sources/ElixxirDAppsSDK/ClientCreator.swift

diff --git a/Sources/ElixxirDAppsSDK/ClientCreator.swift b/Sources/ElixxirDAppsSDK/ClientCreator.swift
new file mode 100644
index 00000000..e048227e
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/ClientCreator.swift
@@ -0,0 +1,32 @@
+import Bindings
+
+public struct ClientCreator {
+  public var create: (URL, Data, Data) throws -> Void
+
+  public func callAsFunction(directoryURL: URL, ndf: Data, password: Data) throws {
+    try create(directoryURL, ndf, password)
+  }
+}
+
+extension ClientCreator {
+  public static let live = ClientCreator { directoryURL, ndf, password in
+    var error: NSError?
+    let network = String(data: ndf, encoding: .utf8)!
+    let created = BindingsNewClient(network, directoryURL.path, password, nil, &error)
+    if let error = error {
+      throw error
+    }
+    if !created {
+      throw BindingsNewClientUnknownError()
+    }
+  }
+}
+
+#if DEBUG
+extension ClientCreator {
+  public static let failing = ClientCreator { _, _, _ in
+    struct NotImplemented: Error {}
+    throw NotImplemented()
+  }
+}
+#endif
diff --git a/Sources/ElixxirDAppsSDK/Errors.swift b/Sources/ElixxirDAppsSDK/Errors.swift
index 0f113897..94f93f98 100644
--- a/Sources/ElixxirDAppsSDK/Errors.swift
+++ b/Sources/ElixxirDAppsSDK/Errors.swift
@@ -9,3 +9,7 @@ public struct BindingsGenerateSecretUnknownError: Error, Equatable {
 public struct PasswordStorageMissingPasswordError: Error, Equatable {
   public init() {}
 }
+
+public struct BindingsNewClientUnknownError: Error, Equatable {
+  public init() {}
+}
-- 
GitLab