diff --git a/Sources/ElixxirDAppsSDK/ConnectionMaker.swift b/Sources/ElixxirDAppsSDK/ConnectionMaker.swift index 4baa7618e129896d093d22a0b40b0dfb7fbf620a..34d7c14b0eaa9f41b5bb959f2fa760e42b271b88 100644 --- a/Sources/ElixxirDAppsSDK/ConnectionMaker.swift +++ b/Sources/ElixxirDAppsSDK/ConnectionMaker.swift @@ -1,12 +1,12 @@ import Bindings public struct ConnectionMaker { - public var connect: (Bool, Data, Data) throws -> Connection + public var connect: (Bool, Data, Identity) throws -> Connection public func callAsFunction( withAuthentication: Bool, recipientContact: Data, - myIdentity: Data + myIdentity: Identity ) throws -> Connection { try connect(withAuthentication, recipientContact, myIdentity) } @@ -15,18 +15,20 @@ public struct ConnectionMaker { extension ConnectionMaker { public static func live(bindingsClient: BindingsClient) -> ConnectionMaker { ConnectionMaker { withAuthentication, recipientContact, myIdentity in + let encoder = JSONEncoder() + let myIdentityData = try encoder.encode(myIdentity) if withAuthentication { return Connection.live( bindingsAuthenticatedConnection: try bindingsClient.connect( withAuthentication: recipientContact, - myIdentity: myIdentity + myIdentity: myIdentityData ) ) } else { return Connection.live( bindingsConnection: try bindingsClient.connect( recipientContact, - myIdentity: myIdentity + myIdentity: myIdentityData ) ) } diff --git a/Sources/ElixxirDAppsSDK/ContactFactsProvider.swift b/Sources/ElixxirDAppsSDK/ContactFactsProvider.swift index e2adebfbbb6b2e7143cc4abe074ee352f8bc400b..2029dc6f306aa74ef1fd48da93ed5038723e5e7b 100644 --- a/Sources/ElixxirDAppsSDK/ContactFactsProvider.swift +++ b/Sources/ElixxirDAppsSDK/ContactFactsProvider.swift @@ -1,9 +1,9 @@ import Bindings public struct ContactFactsProvider { - public var get: (Data) throws -> Data + public var get: (Data) throws -> [Fact] - public func callAsFunction(contact: Data) throws -> Data { + public func callAsFunction(contact: Data) throws -> [Fact] { try get(contact) } } @@ -11,13 +11,15 @@ public struct ContactFactsProvider { extension ContactFactsProvider { public static let live = ContactFactsProvider { contact in var error: NSError? - let facts = BindingsGetFactsFromContact(contact, &error) + let factsData = BindingsGetFactsFromContact(contact, &error) if let error = error { throw error } - guard let facts = facts else { + guard let factsData = factsData else { fatalError("BindingsGetFactsFromContact returned `nil` without providing error") } + let decoder = JSONDecoder() + let facts = try decoder.decode([Fact].self, from: factsData) return facts } } diff --git a/Sources/ElixxirDAppsSDK/ContactFactsSetter.swift b/Sources/ElixxirDAppsSDK/ContactFactsSetter.swift index 0fca1df3f70537edc4e8fe62800d6ae093942f88..8dd0ed98e4ed1e8449b1df3c628c71798784de23 100644 --- a/Sources/ElixxirDAppsSDK/ContactFactsSetter.swift +++ b/Sources/ElixxirDAppsSDK/ContactFactsSetter.swift @@ -1,17 +1,22 @@ import Bindings public struct ContactFactsSetter { - public var set: (Data, Data) throws -> Data + public var set: (Data, [Fact]) throws -> Data - public func callAsFunction(contact: Data, facts: Data) throws -> Data { + public func callAsFunction( + contact: Data, + facts: [Fact] + ) throws -> Data { try set(contact, facts) } } extension ContactFactsSetter { public static let live = ContactFactsSetter { contact, facts in + let encoder = JSONEncoder() + let factsData = try encoder.encode(facts) var error: NSError? - let updatedContact = BindingsSetFactsOnContact(contact, facts, &error) + let updatedContact = BindingsSetFactsOnContact(contact, factsData, &error) if let error = error { throw error } diff --git a/Sources/ElixxirDAppsSDK/ContactFromIdentityProvider.swift b/Sources/ElixxirDAppsSDK/ContactFromIdentityProvider.swift index 1643a0cdc4a6b2979d67f0112e4f5cc0398859da..292f217d8c28f556f6599e4a4bf4fbde6248081c 100644 --- a/Sources/ElixxirDAppsSDK/ContactFromIdentityProvider.swift +++ b/Sources/ElixxirDAppsSDK/ContactFromIdentityProvider.swift @@ -1,16 +1,21 @@ import Bindings public struct ContactFromIdentityProvider { - public var get: (Data) throws -> Data + public var get: (Identity) throws -> Data - public func callAsFunction(identity: Data) throws -> Data { + public func callAsFunction(identity: Identity) throws -> Data { try get(identity) } } extension ContactFromIdentityProvider { public static func live(bindingsClient: BindingsClient) -> ContactFromIdentityProvider { - ContactFromIdentityProvider(get: bindingsClient.getContactFromIdentity(_:)) + ContactFromIdentityProvider { identity in + let encoder = JSONEncoder() + let identityData = try encoder.encode(identity) + let contactData = try bindingsClient.getContactFromIdentity(identityData) + return contactData + } } } diff --git a/Sources/ElixxirDAppsSDK/IdentityMaker.swift b/Sources/ElixxirDAppsSDK/IdentityMaker.swift index 91e7eeed7c4d90d73896c100a8416471581d7564..863e180e8343e87808984d4dee709e9c92fea766 100644 --- a/Sources/ElixxirDAppsSDK/IdentityMaker.swift +++ b/Sources/ElixxirDAppsSDK/IdentityMaker.swift @@ -1,9 +1,9 @@ import Bindings public struct IdentityMaker { - public var make: () throws -> Data + public var make: () throws -> Identity - public func callAsFunction() throws -> Data { + public func callAsFunction() throws -> Identity { try make() } } @@ -11,7 +11,9 @@ public struct IdentityMaker { extension IdentityMaker { public static func live(bindingsClient: BindingsClient) -> IdentityMaker { IdentityMaker { - try bindingsClient.makeIdentity() + let data = try bindingsClient.makeIdentity() + let decoder = JSONDecoder() + return try decoder.decode(Identity.self, from: data) } } } diff --git a/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift b/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift index 92b6ab9673ee1f75910f6919e96dc7d089563228..84c19ce7a14fd55d7cbb28aff665f6b6103c2297 100644 --- a/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift +++ b/Sources/ElixxirDAppsSDK/MessageDeliveryWaiter.swift @@ -2,26 +2,28 @@ import Bindings public struct MessageDeliveryWaiter { public enum Result: Equatable { - case delivered(roundResults: Data) + case delivered(roundResults: [Int]) case notDelivered(timedOut: Bool) } - public var wait: (Data, Int, @escaping (Result) -> Void) throws -> Void + public var wait: (MessageSendReport, Int, @escaping (Result) -> Void) throws -> Void public func callAsFunction( - roundList: Data, + report: MessageSendReport, timeoutMS: Int, callback: @escaping (Result) -> Void - ) throws -> Void { - try wait(roundList, timeoutMS, callback) + ) throws { + try wait(report, timeoutMS, callback) } } extension MessageDeliveryWaiter { public static func live(bindingsClient: BindingsClient) -> MessageDeliveryWaiter { - MessageDeliveryWaiter { roundList, timeoutMS, callback in + MessageDeliveryWaiter { report, timeoutMS, callback in + let encoder = JSONEncoder() + let reportData = try encoder.encode(report) try bindingsClient.wait( - forMessageDelivery: roundList, + forMessageDelivery: reportData, mdc: Callback(onCallback: callback), timeoutMS: timeoutMS ) @@ -38,8 +40,14 @@ private final class Callback: NSObject, BindingsMessageDeliveryCallbackProtocol let onCallback: (MessageDeliveryWaiter.Result) -> Void func eventCallback(_ delivered: Bool, timedOut: Bool, roundResults: Data?) { - if delivered, !timedOut, let roundResults = roundResults { - return onCallback(.delivered(roundResults: roundResults)) + if delivered, !timedOut, let roundResultsData = roundResults { + let decoder = JSONDecoder() + do { + let roundResults = try decoder.decode([Int].self, from: roundResultsData) + return onCallback(.delivered(roundResults: roundResults)) + } catch { + fatalError("BindingsMessageDeliveryCallback roundResults decoding error: \(error)") + } } if !delivered, roundResults == nil { return onCallback(.notDelivered(timedOut: timedOut)) diff --git a/Sources/ElixxirDAppsSDK/MessageListener.swift b/Sources/ElixxirDAppsSDK/MessageListener.swift index bed6b8c10517be2fd6d073f2e32b8d941436a795..dc2bfd1ce9a7c8b1ec55cf29ee457a0c450398a4 100644 --- a/Sources/ElixxirDAppsSDK/MessageListener.swift +++ b/Sources/ElixxirDAppsSDK/MessageListener.swift @@ -1,12 +1,12 @@ import Bindings public struct MessageListener { - public var listen: (Int, String, @escaping (Data) -> Void) -> Void + public var listen: (Int, String, @escaping (Message) -> Void) -> Void public func callAsFunction( messageType: Int, listenerName: String = "MessageListener", - callback: @escaping (Data) -> Void + callback: @escaping (Message) -> Void ) { listen(messageType, listenerName, callback) } @@ -39,18 +39,25 @@ extension MessageListener { } private class Listener: NSObject, BindingsListenerProtocol { - init(listenerName: String, onHear: @escaping (Data) -> Void) { + init(listenerName: String, onHear: @escaping (Message) -> Void) { self.listenerName = listenerName self.onHear = onHear super.init() } let listenerName: String - let onHear: (Data) -> Void + let onHear: (Message) -> Void + let decoder = JSONDecoder() func hear(_ item: Data?) { - guard let item = item else { return } - onHear(item) + guard let item = item else { + fatalError("BindingsListenerProtocol.hear received `nil`") + } + do { + onHear(try decoder.decode(Message.self, from: item)) + } catch { + fatalError("Message decoding failed with error: \(error)") + } } func name() -> String { diff --git a/Sources/ElixxirDAppsSDK/MessageSender.swift b/Sources/ElixxirDAppsSDK/MessageSender.swift index 888159b46e503b74ed83cd02b181793317a960ab..4e493b132666a40997a30f63e52dd58f7b3f63af 100644 --- a/Sources/ElixxirDAppsSDK/MessageSender.swift +++ b/Sources/ElixxirDAppsSDK/MessageSender.swift @@ -1,12 +1,12 @@ import Bindings public struct MessageSender { - public var send: (Int, Data) throws -> Data + public var send: (Int, Data) throws -> MessageSendReport public func callAsFunction( messageType: Int, payload: Data - ) throws -> Data { + ) throws -> MessageSendReport { try send(messageType, payload) } } @@ -28,7 +28,10 @@ extension MessageSender { sendE2E: @escaping (Int, Data) throws -> Data ) -> MessageSender { MessageSender { messageType, payload in - try sendE2E(messageType, payload) + let reportData = try sendE2E(messageType, payload) + let decoder = JSONDecoder() + let report = try decoder.decode(MessageSendReport.self, from: reportData) + return report } } } diff --git a/Sources/ElixxirDAppsSDK/RestlikeRequestSender.swift b/Sources/ElixxirDAppsSDK/RestlikeRequestSender.swift index fda472aa16a9854c2cb4d4376766d24a04151cd6..efe42bc5210035e22e0157e5aec4307d811981a2 100644 --- a/Sources/ElixxirDAppsSDK/RestlikeRequestSender.swift +++ b/Sources/ElixxirDAppsSDK/RestlikeRequestSender.swift @@ -1,13 +1,13 @@ import Bindings public struct RestlikeRequestSender { - public var send: (Int, Int, Data) throws -> Data + public var send: (Int, Int, RestlikeMessage) throws -> RestlikeMessage public func callAsFunction( clientId: Int, connectionId: Int, - request: Data - ) throws -> Data { + request: RestlikeMessage + ) throws -> RestlikeMessage { try send(clientId, connectionId, request) } } @@ -15,20 +15,24 @@ public struct RestlikeRequestSender { extension RestlikeRequestSender { public static func live(authenticated: Bool) -> RestlikeRequestSender { RestlikeRequestSender { clientId, connectionId, request in + let encoder = JSONEncoder() + let requestData = try encoder.encode(request) var error: NSError? - let response: Data? + let responseData: Data? if authenticated { - response = BindingsRestlikeRequestAuth(clientId, connectionId, request, &error) + responseData = BindingsRestlikeRequestAuth(clientId, connectionId, requestData, &error) } else { - response = BindingsRestlikeRequest(clientId, connectionId, request, &error) + responseData = BindingsRestlikeRequest(clientId, connectionId, requestData, &error) } if let error = error { throw error } - guard let response = response else { + guard let responseData = responseData else { let functionName = "BindingsRestlikeRequest\(authenticated ? "Auth" : "")" fatalError("\(functionName) returned `nil` without providing error") } + let decoder = JSONDecoder() + let response = try decoder.decode(RestlikeMessage.self, from: responseData) return response } }