diff --git a/Package.swift b/Package.swift index 4cf232779c9d62e35c3e58dc592c3c0ddf1b95bc..5f0bf3edb9c89e60b400bac60b999d01736bd514 100644 --- a/Package.swift +++ b/Package.swift @@ -26,6 +26,12 @@ let package = Package( targets: ["ElixxirDAppsSDK"] ), ], + dependencies: [ + .package( + url: "https://github.com/pointfreeco/swift-custom-dump.git", + .upToNextMajor(from: "0.4.0") + ), + ], targets: [ .target( name: "ElixxirDAppsSDK", @@ -37,7 +43,11 @@ let package = Package( .testTarget( name: "ElixxirDAppsSDKTests", dependencies: [ - .target(name: "ElixxirDAppsSDK") + .target(name: "ElixxirDAppsSDK"), + .product( + name: "CustomDump", + package: "swift-custom-dump" + ), ], swiftSettings: swiftSettings ), diff --git a/Sources/ElixxirDAppsSDK/Identity.swift b/Sources/ElixxirDAppsSDK/Identity.swift new file mode 100644 index 0000000000000000000000000000000000000000..116fea14ada72e0bca8fe1881c775d1360259463 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/Identity.swift @@ -0,0 +1,27 @@ +import Foundation + +public struct Identity: Equatable, Codable { + enum CodingKeys: String, CodingKey { + case id = "ID" + case rsaPrivatePem = "RSAPrivatePem" + case salt = "Salt" + case dhKeyPrivate = "DHKeyPrivate" + } + + public init( + id: Data, + rsaPrivatePem: Data, + salt: Data, + dhKeyPrivate: Data + ) { + self.id = id + self.rsaPrivatePem = rsaPrivatePem + self.salt = salt + self.dhKeyPrivate = dhKeyPrivate + } + + public var id: Data + public var rsaPrivatePem: Data + public var salt: Data + public var dhKeyPrivate: Data +} diff --git a/Tests/ElixxirDAppsSDKTests/IdentityTests.swift b/Tests/ElixxirDAppsSDKTests/IdentityTests.swift new file mode 100644 index 0000000000000000000000000000000000000000..1a4fb85f7a6a12bf63f8ecc26f871a2da11c25e0 --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/IdentityTests.swift @@ -0,0 +1,38 @@ +import CustomDump +import XCTest +@testable import ElixxirDAppsSDK + +final class IdentityTests: XCTestCase { + func testCoding() throws { + let userId = secureRandomData(count: 32) + let rsaPrivateKey = secureRandomData(count: 32) + let salt = secureRandomData(count: 32) + let dhKeyPrivate = secureRandomData(count: 32) + let jsonString = """ + { + "ID": \(userId.jsonEncodedBase64()), + "RSAPrivatePem": \(rsaPrivateKey.jsonEncodedBase64()), + "Salt": \(salt.jsonEncodedBase64()), + "DHKeyPrivate": \(dhKeyPrivate.jsonEncodedBase64()) + } + """ + let jsonData = jsonString.data(using: .utf8)! + let decoder = JSONDecoder() + decoder.dataDecodingStrategy = .base64 + let identity = try decoder.decode(Identity.self, from: jsonData) + + XCTAssertNoDifference(identity, Identity( + id: userId, + rsaPrivatePem: rsaPrivateKey, + salt: salt, + dhKeyPrivate: dhKeyPrivate + )) + + let encoder = JSONEncoder() + encoder.dataEncodingStrategy = .base64 + let encodedIdentity = try encoder.encode(identity) + let decodedIdentity = try decoder.decode(Identity.self, from: encodedIdentity) + + XCTAssertNoDifference(decodedIdentity, identity) + } +} diff --git a/Tests/ElixxirDAppsSDKTests/TestHelpers.swift b/Tests/ElixxirDAppsSDKTests/TestHelpers.swift new file mode 100644 index 0000000000000000000000000000000000000000..ff545db09d89f2cec2435fa7f28b82f0a6b11be2 --- /dev/null +++ b/Tests/ElixxirDAppsSDKTests/TestHelpers.swift @@ -0,0 +1,17 @@ +import Foundation + +func secureRandomData(count: Int) -> Data { + var bytes = [Int8](repeating: 0, count: count) + let status = SecRandomCopyBytes(kSecRandomDefault, count, &bytes) + assert(status == errSecSuccess) + return Data(bytes: bytes, count: count) +} + +extension Data { + func jsonEncodedBase64() -> String { + let encoder = JSONEncoder() + encoder.dataEncodingStrategy = .base64 + let data = try! encoder.encode(self) + return String(data: data, encoding: .utf8)! + } +}