From 6830f3ef243c3df71cf2c4e9e8adec608295c976 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Wed, 20 Jul 2022 16:43:35 +0100 Subject: [PATCH] Add AuthCallbacks wrapper --- Sources/ElixxirDAppsSDK/AuthCallbacks.swift | 81 +++++++++++++++++++++ Sources/ElixxirDAppsSDK/Login.swift | 9 ++- 2 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 Sources/ElixxirDAppsSDK/AuthCallbacks.swift diff --git a/Sources/ElixxirDAppsSDK/AuthCallbacks.swift b/Sources/ElixxirDAppsSDK/AuthCallbacks.swift new file mode 100644 index 00000000..51b1748b --- /dev/null +++ b/Sources/ElixxirDAppsSDK/AuthCallbacks.swift @@ -0,0 +1,81 @@ +import Bindings +import XCTestDynamicOverlay + +public enum AuthCallback { + case confirm(contact: Data, receptionId: Data, ephemeralId: Int64, roundId: Int64) + case request(contact: Data, receptionId: Data, ephemeralId: Int64, roundId: Int64) + case reset(contact: Data, receptionId: Data, ephemeralId: Int64, roundId: Int64) +} + +public struct AuthCallbacks { + public init(handle: @escaping (AuthCallback) -> Void) { + self.handle = handle + } + + public var handle: (AuthCallback) -> Void +} + +extension AuthCallbacks { + public static let unimplemented = AuthCallbacks( + handle: XCTUnimplemented("\(Self.self)") + ) +} + +extension AuthCallbacks { + func makeBindingsAuthCallbacks() -> BindingsAuthCallbacksProtocol { + class Handler: NSObject, BindingsAuthCallbacksProtocol { + init(_ callbacks: AuthCallbacks) { + self.callbacks = callbacks + } + + let callbacks: AuthCallbacks + + func confirm(_ contact: Data?, receptionId: Data?, ephemeralId: Int64, roundId: Int64) { + guard let contact = contact else { + fatalError("BindingsAuthCallbacks.confirm received `nil` contact") + } + guard let receptionId = receptionId else { + fatalError("BindingsAuthCallbacks.confirm received `nil` receptionId") + } + callbacks.handle(.confirm( + contact: contact, + receptionId: receptionId, + ephemeralId: ephemeralId, + roundId: roundId + )) + } + + func request(_ contact: Data?, receptionId: Data?, ephemeralId: Int64, roundId: Int64) { + guard let contact = contact else { + fatalError("BindingsAuthCallbacks.request received `nil` contact") + } + guard let receptionId = receptionId else { + fatalError("BindingsAuthCallbacks.request received `nil` receptionId") + } + callbacks.handle(.request( + contact: contact, + receptionId: receptionId, + ephemeralId: ephemeralId, + roundId: roundId + )) + } + + func reset(_ contact: Data?, receptionId: Data?, ephemeralId: Int64, roundId: Int64) { + guard let contact = contact else { + fatalError("BindingsAuthCallbacks.reset received `nil` contact") + } + guard let receptionId = receptionId else { + fatalError("BindingsAuthCallbacks.reset received `nil` receptionId") + } + callbacks.handle(.reset( + contact: contact, + receptionId: receptionId, + ephemeralId: ephemeralId, + roundId: roundId + )) + } + } + + return Handler(self) + } +} diff --git a/Sources/ElixxirDAppsSDK/Login.swift b/Sources/ElixxirDAppsSDK/Login.swift index 7bd30d98..e89fe54d 100644 --- a/Sources/ElixxirDAppsSDK/Login.swift +++ b/Sources/ElixxirDAppsSDK/Login.swift @@ -2,23 +2,24 @@ import Bindings import XCTestDynamicOverlay public struct Login { - public var run: (Int, Data, Data) throws -> E2E + public var run: (Int, AuthCallbacks?, Data, Data) throws -> E2E public func callAsFunction( cmixId: Int, + authCallbacks: AuthCallbacks? = nil, identity: Data, e2eParamsJSON: Data ) throws -> E2E { - try run(cmixId, identity, e2eParamsJSON) + try run(cmixId, authCallbacks, identity, e2eParamsJSON) } } extension Login { - public static let live = Login { cmixId, identity, e2eParamsJSON in + public static let live = Login { cmixId, authCallbacks, identity, e2eParamsJSON in var error: NSError? let bindingsE2E = BindingsLogin( cmixId, - nil, // TODO: pass callbacks + authCallbacks?.makeBindingsAuthCallbacks(), identity, e2eParamsJSON, &error -- GitLab