From d8b78e4e6d69ea9b6561214d91cf84b256686c35 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Thu, 2 Jun 2022 23:02:17 +0200
Subject: [PATCH] Add ContactPubkeyProvider

---
 .../ContactPubkeyProvider.swift               | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
 create mode 100644 Sources/ElixxirDAppsSDK/ContactPubkeyProvider.swift

diff --git a/Sources/ElixxirDAppsSDK/ContactPubkeyProvider.swift b/Sources/ElixxirDAppsSDK/ContactPubkeyProvider.swift
new file mode 100644
index 00000000..4f97eed0
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/ContactPubkeyProvider.swift
@@ -0,0 +1,32 @@
+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