From 2c70afd4789f3352a18c7ece214950af75b3ebb6 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Mon, 25 Jul 2022 18:41:21 +0100
Subject: [PATCH] Add GetPublicKeyFromContact functor

---
 .../GetPublicKeyFromContact.swift             | 30 +++++++++++++++++
 .../Legacy/ContactPubkeyProvider.swift        | 32 -------------------
 2 files changed, 30 insertions(+), 32 deletions(-)
 create mode 100644 Sources/ElixxirDAppsSDK/GetPublicKeyFromContact.swift
 delete mode 100644 Sources/ElixxirDAppsSDK/Legacy/ContactPubkeyProvider.swift

diff --git a/Sources/ElixxirDAppsSDK/GetPublicKeyFromContact.swift b/Sources/ElixxirDAppsSDK/GetPublicKeyFromContact.swift
new file mode 100644
index 00000000..1ec9acaa
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/GetPublicKeyFromContact.swift
@@ -0,0 +1,30 @@
+import Bindings
+import XCTestDynamicOverlay
+
+public struct GetPublicKeyFromContact {
+  public var run: (Data) throws -> Data
+
+  public func callAsFunction(contact: Data) throws -> Data {
+    try run(contact)
+  }
+}
+
+extension GetPublicKeyFromContact {
+  public static let live = GetPublicKeyFromContact { contact in
+    var error: NSError?
+    let key = BindingsGetPubkeyFromContact(contact, &error)
+    if let error = error {
+      throw error
+    }
+    guard let key = key else {
+      fatalError("BindingsGetPubkeyFromContact returned `nil` without providing error")
+    }
+    return key
+  }
+}
+
+extension GetPublicKeyFromContact {
+  public static let unimplemented = GetPublicKeyFromContact(
+    run: XCTUnimplemented("\(Self.self)")
+  )
+}
diff --git a/Sources/ElixxirDAppsSDK/Legacy/ContactPubkeyProvider.swift b/Sources/ElixxirDAppsSDK/Legacy/ContactPubkeyProvider.swift
deleted file mode 100644
index 4f97eed0..00000000
--- a/Sources/ElixxirDAppsSDK/Legacy/ContactPubkeyProvider.swift
+++ /dev/null
@@ -1,32 +0,0 @@
-import Bindings
-
-public struct ContactPubkeyProvider {
-  public var get: (Data) throws -> Data
-
-  public func callAsFunction(contact: Data) throws -> Data {
-    try get(contact)
-  }
-}
-
-extension ContactPubkeyProvider {
-  public static let live = ContactPubkeyProvider { contact in
-    var error: NSError?
-    let pubkey = BindingsGetPubkeyFromContact(contact, &error)
-    if let error = error {
-      throw error
-    }
-    guard let pubkey = pubkey else {
-      fatalError("BindingsGetPubkeyFromContact returned `nil` without providing error")
-    }
-    return pubkey
-  }
-}
-
-#if DEBUG
-extension ContactPubkeyProvider {
-  public static let failing = ContactPubkeyProvider { _ in
-    struct NotImplemented: Error {}
-    throw NotImplemented()
-  }
-}
-#endif
-- 
GitLab