diff --git a/Sources/ElixxirDAppsSDK/Models/ChannelDef.swift b/Sources/ElixxirDAppsSDK/Models/ChannelDef.swift new file mode 100644 index 0000000000000000000000000000000000000000..3a5a501a512285c37c1692961c5c01a1b784a404 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Models/ChannelDef.swift @@ -0,0 +1,37 @@ +import Foundation + +public struct ChannelDef: Equatable { + public init( + name: String, + description: String, + salt: Data, + pubKey: Data + ) { + self.name = name + self.description = description + self.salt = salt + self.pubKey = pubKey + } + + public var name: String + public var description: String + public var salt: Data + public var pubKey: Data +} + +extension ChannelDef: Codable { + enum CodingKeys: String, CodingKey { + case name = "Name" + case description = "Description" + case salt = "Salt" + case pubKey = "PubKey" + } + + public static func decode(_ data: Data) throws -> Self { + try JSONDecoder().decode(Self.self, from: data) + } + + public func encode() throws -> Data { + try JSONEncoder().encode(self) + } +} diff --git a/Tests/ElixxirDAppsSDKTests/Models/ChannelDefTests.swift b/Tests/ElixxirDAppsSDKTests/Models/ChannelDefTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..0c3bdc9066b9cebdbd17911a165f35eb7dfaaf1b --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/Models/ChannelDefTests.swift @@ -0,0 +1,34 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class ChannelDefTests: XCTestCase { + func testCoding() throws { + let name = "My broadcast channel" + let description = "A broadcast channel for me to test things" + let saltB64 = "gpUqW7N22sffMXsvPLE7BA==" + let pubKeyB64 = "LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1DZ0NJUUN2YkZVckJKRFpqT3Y0Y0MvUHZZdXNvQkFtUTFkb3Znb044aHRuUjA2T3F3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0=" + let jsonString = """ + { + "Name": "\(name)", + "Description": "\(description)", + "Salt": "\(saltB64)", + "PubKey": "\(pubKeyB64)" + } + """ + let jsonData = jsonString.data(using: .utf8)! + let model = try ChannelDef.decode(jsonData) + + XCTAssertNoDifference(model, ChannelDef( + name: name, + description: description, + salt: Data(base64Encoded: saltB64)!, + pubKey: Data(base64Encoded: pubKeyB64)! + )) + + let encodedModel = try model.encode() + let decodedModel = try ChannelDef.decode(encodedModel) + + XCTAssertNoDifference(decodedModel, model) + } +}