From bfd7e5182a737c7129ac63438251f2de5fc77d6f Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Wed, 27 Jul 2022 14:29:09 +0100
Subject: [PATCH] Add ChannelDef model

---
 .../ElixxirDAppsSDK/Models/ChannelDef.swift   | 37 +++++++++++++++++++
 .../Models/ChannelDefTests.swift              | 34 +++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 Sources/ElixxirDAppsSDK/Models/ChannelDef.swift
 create mode 100644 Tests/ElixxirDAppsSDKTests/Models/ChannelDefTests.swift

diff --git a/Sources/ElixxirDAppsSDK/Models/ChannelDef.swift b/Sources/ElixxirDAppsSDK/Models/ChannelDef.swift
new file mode 100644
index 00000000..3a5a501a
--- /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 00000000..0c3bdc90
--- /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)
+  }
+}
-- 
GitLab