From 39b3f3611cca7fc2775d1d00733ec2ed96f754cc Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 07:54:01 -0600 Subject: [PATCH 01/31] Alternatively allow ENV VARS for config --- .../xcshareddata/xcschemes/XyoClient.xcscheme | 91 +++++++++++++++++++ Sources/XyoClient/XyoPanel.swift | 4 +- Tests/XyoClientTests/Panel.swift | 2 +- 3 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 .swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme new file mode 100644 index 0000000..1144813 --- /dev/null +++ b/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme @@ -0,0 +1,91 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index e20f7a3..6df1da1 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -86,8 +86,8 @@ public class XyoPanel { } struct Defaults { - static let apiModule = "Archivist" - static let apiDomain = "https://beta.api.archivist.xyo.network" + static let apiDomain = ProcessInfo.processInfo.environment["API_DOMAIN"] ?? "https://beta.api.archivist.xyo.network" + static let apiModule = ProcessInfo.processInfo.environment["API_MODULE"] ?? "Archivist" } private static var defaultArchivist: XyoArchivistApiClient { diff --git a/Tests/XyoClientTests/Panel.swift b/Tests/XyoClientTests/Panel.swift index 3f51cc1..bf8a372 100644 --- a/Tests/XyoClientTests/Panel.swift +++ b/Tests/XyoClientTests/Panel.swift @@ -23,7 +23,7 @@ final class PanelTests: XCTestCase { } func testPanelReport() throws { - let apiDomain = "https://beta.api.archivist.xyo.network" + let apiDomain = "http://localhost:8080" let archive = "Archivist" _ = XyoAddress() let witness = XyoBasicWitness({ _ in From 0eb1d82315b0d670ebc3acc1b374c8c5573ed33a Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 07:55:28 -0600 Subject: [PATCH 02/31] Add default env vars --- .swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme | 4 ++-- Sources/XyoClient/XyoPanel.swift | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme index 1144813..bbca230 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme @@ -54,12 +54,12 @@ allowLocationSimulation = "YES"> diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index 6df1da1..5606f60 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -86,8 +86,8 @@ public class XyoPanel { } struct Defaults { - static let apiDomain = ProcessInfo.processInfo.environment["API_DOMAIN"] ?? "https://beta.api.archivist.xyo.network" - static let apiModule = ProcessInfo.processInfo.environment["API_MODULE"] ?? "Archivist" + static let apiDomain = ProcessInfo.processInfo.environment["XYO_API_DOMAIN"] ?? "https://beta.api.archivist.xyo.network" + static let apiModule = ProcessInfo.processInfo.environment["XYO_API_MODULE"] ?? "Archivist" } private static var defaultArchivist: XyoArchivistApiClient { From 67b815a748ef4af368e6671b92431766f6eef8fe Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:07:12 -0600 Subject: [PATCH 03/31] Remove hardcoded domains in Panel tests --- Tests/XyoClientTests/Panel.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/XyoClientTests/Panel.swift b/Tests/XyoClientTests/Panel.swift index bf8a372..4fddfe8 100644 --- a/Tests/XyoClientTests/Panel.swift +++ b/Tests/XyoClientTests/Panel.swift @@ -13,8 +13,8 @@ final class PanelTests: XCTestCase { ] func testCreatePanel() throws { - let apiDomain = "https://beta.api.archivist.xyo.network" - let archive = "Archivist" + let apiDomain = XyoPanel.Defaults.apiDomain + let archive = XyoPanel.Defaults.apiModule let address = XyoAddress() let witness = XyoWitness(address) let panel = XyoPanel(archive: archive, apiDomain: apiDomain, witnesses: [witness]) @@ -23,8 +23,8 @@ final class PanelTests: XCTestCase { } func testPanelReport() throws { - let apiDomain = "http://localhost:8080" - let archive = "Archivist" + let apiDomain = XyoPanel.Defaults.apiDomain + let archive = XyoPanel.Defaults.apiModule _ = XyoAddress() let witness = XyoBasicWitness({ _ in let payload = XyoPayload("network.xyo.basic") From 837fbe9f2bac9d98469c8dbbcd824d907c4f919c Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:21:12 -0600 Subject: [PATCH 04/31] Default to localhost for local dev --- .swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme index bbca230..9b8f416 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/XyoClient.xcscheme @@ -55,7 +55,7 @@ Date: Mon, 11 Nov 2024 08:37:01 -0600 Subject: [PATCH 05/31] Use defaults in tests --- Tests/XyoClientTests/Panel.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/XyoClientTests/Panel.swift b/Tests/XyoClientTests/Panel.swift index 4fddfe8..8d1ac70 100644 --- a/Tests/XyoClientTests/Panel.swift +++ b/Tests/XyoClientTests/Panel.swift @@ -23,8 +23,8 @@ final class PanelTests: XCTestCase { } func testPanelReport() throws { - let apiDomain = XyoPanel.Defaults.apiDomain - let archive = XyoPanel.Defaults.apiModule + let apiDomain = XyoPanel.Defaults.apiDomain + let archive = XyoPanel.Defaults.apiModule _ = XyoAddress() let witness = XyoBasicWitness({ _ in let payload = XyoPayload("network.xyo.basic") From e3547b4d397f96bc632fe955caf5e24271653b1d Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:37:10 -0600 Subject: [PATCH 06/31] Decodable BW --- Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift index ec44a6c..01e2927 100644 --- a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift +++ b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift @@ -1,6 +1,6 @@ import Foundation -public class XyoBoundWitnessBodyJson: XyoBoundWitnessBodyProtocol, Encodable { +public class XyoBoundWitnessBodyJson: XyoBoundWitnessBodyProtocol, Encodable, Decodable { enum CodingKeys: String, CodingKey { case addresses case payload_hashes From a2bdf9044040eb6bfcaa23cef792bbcdb5b42399 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:37:24 -0600 Subject: [PATCH 07/31] Return tuple from BW builder --- Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift index ce24a3a..ea1a36e 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift @@ -56,7 +56,7 @@ public class BoundWitnessBuilder { } } - public func build(_ previousHash: String? = nil) throws -> XyoBoundWitnessJson { + public func build(_ previousHash: String? = nil) throws -> (XyoBoundWitnessJson, [XyoPayload]) { let bw = XyoBoundWitnessJson() let hashable = hashableFields() let hash = try BoundWitnessBuilder.hash(hashable) @@ -69,7 +69,7 @@ public class BoundWitnessBuilder { bw.previous_hashes = _previous_hashes bw.payload_hashes = _payload_hashes bw.payload_schemas = _payload_schemas - return bw + return (bw, _payloads) } static func hash(_ json: T) throws -> String { From 8f913b257e9adaad87f002677f8a2d980e6b70cc Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:37:43 -0600 Subject: [PATCH 08/31] Destructure tuple from builders --- Sources/XyoClient/XyoPanel.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index 5606f60..9931aa6 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -60,7 +60,7 @@ public class XyoPanel { let payloads = witnesses.map { witness in witness.observe() } - let bw = try BoundWitnessBuilder() + let (bw, _) = try BoundWitnessBuilder() .payloads(payloads.compactMap { $0 }) .witnesses(witnesses) .build(_previous_hash) @@ -86,7 +86,8 @@ public class XyoPanel { } struct Defaults { - static let apiDomain = ProcessInfo.processInfo.environment["XYO_API_DOMAIN"] ?? "https://beta.api.archivist.xyo.network" + static let apiDomain = + ProcessInfo.processInfo.environment["XYO_API_DOMAIN"] ?? "https://beta.api.archivist.xyo.network" static let apiModule = ProcessInfo.processInfo.environment["XYO_API_MODULE"] ?? "Archivist" } From c006b1d9f3615676e6e82faa9a706e7490f0ecd1 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:38:39 -0600 Subject: [PATCH 09/31] Add archivist insert method --- .../ArchivistApi/ArchivistApiClient.swift | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 1abd2eb..12cbc68 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -26,6 +26,27 @@ public class XyoArchivistApiClient { self.config = config } + public func insert(_ entries: [XyoBoundWitnessJson]) async throws -> XyoBoundWitnessJson { + let url = "\(self.config.apiDomain)/\(self.config.apiModule)" + let body = entries + + // Perform the request and await the result + let responseData = try await AF.request( + url, + method: .post, + parameters: body, + encoder: JSONParameterEncoder.default + ) + .validate() + .serializingData() + .value + + // Attempt to decode the response data into XyoBoundWitnessJson + let decodedResponse = try JSONDecoder().decode([XyoBoundWitnessJson].self, from: responseData) + // TODO: Return payloads + return decodedResponse[0] + } + public func postBoundWitnesses( _ entries: [XyoBoundWitnessJson] ) throws { From 579f786259721540a92490ca50da00b1a55237e9 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:42:48 -0600 Subject: [PATCH 10/31] Update BW tests to handle tuple response format --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 2 +- Tests/XyoClientTests/BoundWitness.swift | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 12cbc68..84da4b0 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -43,7 +43,7 @@ public class XyoArchivistApiClient { // Attempt to decode the response data into XyoBoundWitnessJson let decodedResponse = try JSONDecoder().decode([XyoBoundWitnessJson].self, from: responseData) - // TODO: Return payloads + // TODO: Return payloads instead once they're deserialziable return decodedResponse[0] } diff --git a/Tests/XyoClientTests/BoundWitness.swift b/Tests/XyoClientTests/BoundWitness.swift index 97f06cc..c2f05b2 100644 --- a/Tests/XyoClientTests/BoundWitness.swift +++ b/Tests/XyoClientTests/BoundWitness.swift @@ -84,7 +84,7 @@ final class BoundWitnessTests: XCTestCase { let address = XyoAddress(testVectorPrivateKey.hexToData()) let bw = try BoundWitnessBuilder().witness(address).payload( "network.xyo.test", TestPayload1("network.xyo.test")) - let bwJson = try bw.build() + let (bwJson, _) = try bw.build() XCTAssertEqual(bwJson._hash, knownHash) } @@ -95,7 +95,7 @@ final class BoundWitnessTests: XCTestCase { let bw = try BoundWitnessBuilder().witness(address).payload( "network.xyo.test", TestPayload1("network.xyo.test")) let apiExpectation = expectation(description: "API Call") - let bwJson = try bw.build() + let (bwJson, _) = try bw.build() XCTAssertEqual(bwJson._hash, knownHash) try api.postBoundWitness(bwJson) { error in XCTAssertTrue(error == nil, error!) @@ -110,7 +110,7 @@ final class BoundWitnessTests: XCTestCase { let address = XyoAddress(testVectorPrivateKey.hexToData()) let testPayload2 = TestPayload2("network.xyo.test") let bw = try BoundWitnessBuilder().witness(address).payload("network.xyo.test", testPayload2) - let bwJson = try bw.build() + let (bwJson, _) = try bw.build() XCTAssertEqual(bwJson._hash, knownHash) } @@ -121,7 +121,7 @@ final class BoundWitnessTests: XCTestCase { let bw = try BoundWitnessBuilder().witness(address).payload( "network.xyo.test", TestPayload2("network.xyo.test")) let apiExpectation = expectation(description: "API Call") - let bwJson = try bw.build() + let (bwJson, _) = try bw.build() XCTAssertEqual(bwJson._hash, knownHash) try api.postBoundWitness(bwJson) { error in XCTAssertEqual(error == nil, true) From 7511bd140b8ade7b80fe631685c3370172d9cf14 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:48:45 -0600 Subject: [PATCH 11/31] DRY up URL builder --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 84da4b0..8931c23 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -22,17 +22,20 @@ public class XyoArchivistApiClient { } } + public var url: String { + return "\(self.config.apiDomain)/\(self.config.apiModule)" + } + private init(_ config: XyoArchivistApiConfig) { self.config = config } public func insert(_ entries: [XyoBoundWitnessJson]) async throws -> XyoBoundWitnessJson { - let url = "\(self.config.apiDomain)/\(self.config.apiModule)" let body = entries // Perform the request and await the result let responseData = try await AF.request( - url, + self.url, method: .post, parameters: body, encoder: JSONParameterEncoder.default @@ -59,7 +62,7 @@ public class XyoArchivistApiClient { ) throws { let body = entries AF.request( - "\(self.config.apiDomain)/\(self.config.apiModule)", + self.url, method: .post, parameters: body, encoder: JSONParameterEncoder.default From d6c839f3c34193542de88d78a1ba3a5e59bb36c9 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:50:36 -0600 Subject: [PATCH 12/31] update method signature --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 8931c23..5a2a0d6 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -30,14 +30,12 @@ public class XyoArchivistApiClient { self.config = config } - public func insert(_ entries: [XyoBoundWitnessJson]) async throws -> XyoBoundWitnessJson { - let body = entries - + public func insert(payloads: [XyoBoundWitnessJson]) async throws -> XyoBoundWitnessJson { // Perform the request and await the result let responseData = try await AF.request( self.url, method: .post, - parameters: body, + parameters: payloads, encoder: JSONParameterEncoder.default ) .validate() From 75dffbca199dc4cf03674d8af7d9b34e62324566 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:51:40 -0600 Subject: [PATCH 13/31] Notes on Query Bound Witness --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 5a2a0d6..e9de220 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -31,6 +31,7 @@ public class XyoArchivistApiClient { } public func insert(payloads: [XyoBoundWitnessJson]) async throws -> XyoBoundWitnessJson { + // TODO: Build query bound witness // Perform the request and await the result let responseData = try await AF.request( self.url, From 6978a5475a0b0ab43bfd13edeb3f538d3006f93a Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:58:59 -0600 Subject: [PATCH 14/31] Dump default formatting options to preserve originals where possible --- .swift-format | 68 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/.swift-format b/.swift-format index cfd40dc..64c2b64 100644 --- a/.swift-format +++ b/.swift-format @@ -1,10 +1,70 @@ { - "version": 1, - "lineLength": 120, + "fileScopedDeclarationPrivacy": { + "accessLevel": "private" + }, + "indentConditionalCompilationBlocks": true, + "indentSwitchCaseLabels": false, "indentation": { "spaces": 2 }, + "lineBreakAroundMultilineExpressionChainComponents": false, + "lineBreakBeforeControlFlowKeywords": false, + "lineBreakBeforeEachArgument": false, + "lineBreakBeforeEachGenericRequirement": false, + "lineLength": 100, + "maximumBlankLines": 1, + "multiElementCollectionTrailingCommas": true, + "noAssignmentInExpressions": { + "allowedFunctions": [ + "XCTAssertNoThrow" + ] + }, + "prioritizeKeepingFunctionOutputTogether": false, + "respectsExistingLineBreaks": true, "rules": { - "OrderedImports": true - } + "AllPublicDeclarationsHaveDocumentation": false, + "AlwaysUseLiteralForEmptyCollectionInit": false, + "AlwaysUseLowerCamelCase": true, + "AmbiguousTrailingClosureOverload": true, + "BeginDocumentationCommentWithOneLineSummary": false, + "DoNotUseSemicolons": true, + "DontRepeatTypeInStaticProperties": true, + "FileScopedDeclarationPrivacy": true, + "FullyIndirectEnum": true, + "GroupNumericLiterals": true, + "IdentifiersMustBeASCII": true, + "NeverForceUnwrap": false, + "NeverUseForceTry": false, + "NeverUseImplicitlyUnwrappedOptionals": false, + "NoAccessLevelOnExtensionDeclaration": true, + "NoAssignmentInExpressions": true, + "NoBlockComments": true, + "NoCasesWithOnlyFallthrough": true, + "NoEmptyTrailingClosureParentheses": true, + "NoLabelsInCasePatterns": true, + "NoLeadingUnderscores": false, + "NoParensAroundConditions": true, + "NoPlaygroundLiterals": true, + "NoVoidReturnOnFunctionSignature": true, + "OmitExplicitReturns": false, + "OneCasePerLine": true, + "OneVariableDeclarationPerLine": true, + "OnlyOneTrailingClosureArgument": true, + "OrderedImports": true, + "ReplaceForEachWithForLoop": true, + "ReturnVoidInsteadOfEmptyTuple": true, + "TypeNamesShouldBeCapitalized": true, + "UseEarlyExits": false, + "UseExplicitNilCheckInConditions": true, + "UseLetInEveryBoundCaseVariable": true, + "UseShorthandTypeNames": true, + "UseSingleLinePropertyGetter": true, + "UseSynthesizedInitializer": true, + "UseTripleSlashForDocumentationComments": true, + "UseWhereClausesInForLoops": false, + "ValidateDocumentationComments": false + }, + "spacesAroundRangeFormationOperators": false, + "tabWidth": 8, + "version": 1 } From d570ddca0096f022dc74a4b6dcb3539bc9953814 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 08:59:02 -0600 Subject: [PATCH 15/31] Formatting --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 2 +- Sources/XyoClient/XyoPanel.swift | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index e9de220..1a94c75 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -31,7 +31,7 @@ public class XyoArchivistApiClient { } public func insert(payloads: [XyoBoundWitnessJson]) async throws -> XyoBoundWitnessJson { - // TODO: Build query bound witness + // TODO: Build query bound witness // Perform the request and await the result let responseData = try await AF.request( self.url, diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index 9931aa6..1bc29a4 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -87,7 +87,8 @@ public class XyoPanel { struct Defaults { static let apiDomain = - ProcessInfo.processInfo.environment["XYO_API_DOMAIN"] ?? "https://beta.api.archivist.xyo.network" + ProcessInfo.processInfo.environment["XYO_API_DOMAIN"] + ?? "https://beta.api.archivist.xyo.network" static let apiModule = ProcessInfo.processInfo.environment["XYO_API_MODULE"] ?? "Archivist" } From 495c7c06d5cd88323b729eb0c7f3fb086efa73d6 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 10:32:53 -0600 Subject: [PATCH 16/31] Spelling --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 1a94c75..9e9ddd0 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -45,7 +45,7 @@ public class XyoArchivistApiClient { // Attempt to decode the response data into XyoBoundWitnessJson let decodedResponse = try JSONDecoder().decode([XyoBoundWitnessJson].self, from: responseData) - // TODO: Return payloads instead once they're deserialziable + // TODO: Return payloads instead once they're deserializable return decodedResponse[0] } From 730abdefb8d6966b9a292e9f5ef03542b1f61408 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 12:42:41 -0600 Subject: [PATCH 17/31] Allow for concurrent API requests from SDK --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index 9e9ddd0..bd20007 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -103,6 +103,10 @@ public class XyoArchivistApiClient { } extension XyoArchivistApiClient { - static fileprivate let queue = DispatchQueue(label: "requests.queue", qos: .utility) + static fileprivate let queue = DispatchQueue( + label: "requests.queue", + qos: .utility, + attributes: [.concurrent] + ) static fileprivate let mainQueue = DispatchQueue.main } From ef3e1cb0bc2a7b124d2bdb26fc0cc3bb39df3848 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 12:43:08 -0600 Subject: [PATCH 18/31] Namespace thread queue for identification --- Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift index bd20007..61d860b 100644 --- a/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift +++ b/Sources/XyoClient/ArchivistApi/ArchivistApiClient.swift @@ -104,7 +104,7 @@ public class XyoArchivistApiClient { extension XyoArchivistApiClient { static fileprivate let queue = DispatchQueue( - label: "requests.queue", + label: "network.xyo.requests.queue", qos: .utility, attributes: [.concurrent] ) From 40c808692592a1c32376c6c1e89e9bc3c1fa3d41 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 14:09:49 -0600 Subject: [PATCH 19/31] Remove `_payloads` from BoundWitness --- Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift | 1 - Sources/XyoClient/BoundWitness/BoundWitnessJson.swift | 3 --- .../XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift | 1 - 3 files changed, 5 deletions(-) diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift index ea1a36e..c1afddc 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift @@ -63,7 +63,6 @@ public class BoundWitnessBuilder { bw._signatures = try self.sign(hash) bw._hash = hash bw._client = "swift" - bw._payloads = _payloads bw._previous_hash = previousHash bw.addresses = _witnesses.map { witness in witness?.addressHex! } bw.previous_hashes = _previous_hashes diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift b/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift index 97ec012..da58915 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift @@ -4,7 +4,6 @@ public class XyoBoundWitnessJson: XyoBoundWitnessBodyJson, XyoBoundWitnessMetaPr enum CodingKeys: String, CodingKey { case _client case _hash - case _payloads case _previous_hash case _signatures case addresses @@ -16,14 +15,12 @@ public class XyoBoundWitnessJson: XyoBoundWitnessBodyJson, XyoBoundWitnessMetaPr public var _client: String? public var _hash: String? - public var _payloads: [XyoPayload]? public var _signatures: [String?]? public var _previous_hash: String? func encodeMetaFields(_ container: inout KeyedEncodingContainer) throws { try container.encode(_client, forKey: ._client) try container.encode(_hash, forKey: ._hash) - try container.encode(_payloads, forKey: ._payloads) try container.encode(_signatures, forKey: ._signatures) } diff --git a/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift b/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift index bb7644b..1fe22fe 100644 --- a/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift +++ b/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift @@ -3,7 +3,6 @@ import Foundation public protocol XyoBoundWitnessMetaProtocol { var _client: String? { get set } var _hash: String? { get set } - var _payloads: [XyoPayload]? { get set } var _signatures: [String?]? { get set } var _previous_hash: String? { get set } } From 2f258947ff285686e4137d433d9f7da631d0b04a Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 14:20:03 -0600 Subject: [PATCH 20/31] Remove previous hash from payloads --- Sources/XyoClient/Payload/XyoPayload.swift | 9 ++++----- Sources/XyoClient/XyoPanel.swift | 2 +- Sources/XyoClient/XyoWitness/Event/EventPayload.swift | 4 ++-- .../XyoWitness/SystemInfo/SystemInfoPayload.swift | 5 ++--- .../XyoWitness/SystemInfo/SystemInfoWitness.swift | 2 +- 5 files changed, 10 insertions(+), 12 deletions(-) diff --git a/Sources/XyoClient/Payload/XyoPayload.swift b/Sources/XyoClient/Payload/XyoPayload.swift index 83fff8e..63bf5f8 100644 --- a/Sources/XyoClient/Payload/XyoPayload.swift +++ b/Sources/XyoClient/Payload/XyoPayload.swift @@ -1,14 +1,13 @@ import Foundation open class XyoPayload: Encodable { - - public init(_ schema: String, _ previousHash: String? = nil) { + public init(_ schema: String) { self.schema = schema.lowercased() - self.previousHash = previousHash } - public var schema: String - public var previousHash: String? +} + +extension XyoPayload { public func hash() throws -> Data { let encoder = JSONEncoder() diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index 1bc29a4..fd09300 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -46,7 +46,7 @@ public class XyoPanel { } public func event(_ event: String, _ closure: XyoPanelReportCallback?) throws -> [XyoPayload] { - try report([XyoEventWitness { previousHash in XyoEventPayload(event, previousHash) }], closure) + try report([XyoEventWitness { previousHash in XyoEventPayload(event) }], closure) } public func report( diff --git a/Sources/XyoClient/XyoWitness/Event/EventPayload.swift b/Sources/XyoClient/XyoWitness/Event/EventPayload.swift index 3ccce64..9a90fb4 100644 --- a/Sources/XyoClient/XyoWitness/Event/EventPayload.swift +++ b/Sources/XyoClient/XyoWitness/Event/EventPayload.swift @@ -5,9 +5,9 @@ open class XyoEventPayload: XyoPayload { let time_stamp = Date() let event: String - override init(_ event: String, _ previousHash: String?) { + override init(_ event: String) { self.event = event - super.init("network.xyo.event", previousHash) + super.init("network.xyo.event") } enum CodingKeys: String, CodingKey { diff --git a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoPayload.swift b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoPayload.swift index 9d27483..65c5569 100644 --- a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoPayload.swift +++ b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoPayload.swift @@ -4,9 +4,9 @@ open class XyoSystemInfoPayload: XyoPayload { var wifiInfo: WifiInformation - public init(_ wifiInfo: WifiInformation, _ previousHash: String? = nil) { + public init(_ wifiInfo: WifiInformation) { self.wifiInfo = wifiInfo - super.init("network.xyo.system.info", previousHash) + super.init("network.xyo.system.info") } enum CodingKeys: String, CodingKey { @@ -21,7 +21,6 @@ open class XyoSystemInfoPayload: XyoPayload { try container.encode(XyoSystemInfoDevicePayloadStruct(), forKey: .device) try container.encode(XyoSystemInfoNetworkPayloadStruct(wifiInfo), forKey: .network) try container.encode(XyoSystemInfoOsPayloadStruct(), forKey: .os) - try container.encode(self.previousHash, forKey: .previousHash) try container.encode(self.schema, forKey: .schema) } } diff --git a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift index f1231d9..47450fc 100644 --- a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift +++ b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift @@ -11,7 +11,7 @@ open class XyoSystemInfoWitness: XyoWitness { public typealias ObserverClosure = ((_ previousHash: String?) -> XyoSystemInfoPayload?) override public func observe() -> XyoSystemInfoPayload? { - let payload = XyoSystemInfoPayload(wifiInfo, previousHash) + let payload = XyoSystemInfoPayload(wifiInfo) previousHash = try? payload.hash().toHex() return payload } From 4812161962ec8893370d22bd492222973b3df386 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 14:34:21 -0600 Subject: [PATCH 21/31] Payload extensions --- Sources/XyoClient/Payload/XyoPayload.swift | 14 -------------- Sources/XyoClient/extensions/XyoPayload.swift | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 Sources/XyoClient/extensions/XyoPayload.swift diff --git a/Sources/XyoClient/Payload/XyoPayload.swift b/Sources/XyoClient/Payload/XyoPayload.swift index 63bf5f8..7b19aff 100644 --- a/Sources/XyoClient/Payload/XyoPayload.swift +++ b/Sources/XyoClient/Payload/XyoPayload.swift @@ -6,17 +6,3 @@ open class XyoPayload: Encodable { } public var schema: String } - -extension XyoPayload { - - public func hash() throws -> Data { - let encoder = JSONEncoder() - encoder.outputFormatting = .sortedKeys - let data = try encoder.encode(self) - - guard let str = String(data: data, encoding: .utf8) else { - throw BoundWitnessBuilderError.encodingError - } - return try str.sha256() - } -} diff --git a/Sources/XyoClient/extensions/XyoPayload.swift b/Sources/XyoClient/extensions/XyoPayload.swift new file mode 100644 index 0000000..bb0361d --- /dev/null +++ b/Sources/XyoClient/extensions/XyoPayload.swift @@ -0,0 +1,15 @@ +import Foundation + +extension XyoPayload { + + public func hash() throws -> Data { + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys + let data = try encoder.encode(self) + + guard let str = String(data: data, encoding: .utf8) else { + throw BoundWitnessBuilderError.encodingError + } + return try str.sha256() + } +} From 85dbb33b53c80388284ec745eb3b519dc5db2b48 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 14:35:02 -0600 Subject: [PATCH 22/31] Formatting --- Sources/XyoClient/XyoPanel.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index fd09300..cd29751 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -46,7 +46,7 @@ public class XyoPanel { } public func event(_ event: String, _ closure: XyoPanelReportCallback?) throws -> [XyoPayload] { - try report([XyoEventWitness { previousHash in XyoEventPayload(event) }], closure) + try report([XyoEventWitness { previousHash in XyoEventPayload(event) }], closure) } public func report( From 72ed1ad0189f90704a33a9893eea8a3da452e65c Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 14:36:51 -0600 Subject: [PATCH 23/31] Documentation --- Sources/XyoClient/extensions/XyoPayload.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sources/XyoClient/extensions/XyoPayload.swift b/Sources/XyoClient/extensions/XyoPayload.swift index bb0361d..0daeb8e 100644 --- a/Sources/XyoClient/extensions/XyoPayload.swift +++ b/Sources/XyoClient/extensions/XyoPayload.swift @@ -2,6 +2,17 @@ import Foundation extension XyoPayload { + /// Generates a SHA-256 hash of the encoded representation of the instance. + /// + /// This method serializes the instance using JSON encoding with sorted keys, + /// converts the encoded data into a UTF-8 string, and then applies SHA-256 + /// hashing to generate a hash of the instance's contents. + /// + /// - Throws: + /// - `BoundWitnessBuilderError.encodingError` if the instance cannot be + /// converted to a UTF-8 string after encoding. + /// - Any error thrown by the `sha256()` function if the hashing process fails. + /// - Returns: A `Data` object containing the SHA-256 hash of the encoded instance. public func hash() throws -> Data { let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeys From f93639826a4e6d5f99eea40346fa824067ef157b Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 14:57:58 -0600 Subject: [PATCH 24/31] Move Payload extensions to same file --- Sources/XyoClient/Payload/XyoPayload.swift | 25 ++++++++++++++++++ Sources/XyoClient/extensions/XyoPayload.swift | 26 ------------------- 2 files changed, 25 insertions(+), 26 deletions(-) delete mode 100644 Sources/XyoClient/extensions/XyoPayload.swift diff --git a/Sources/XyoClient/Payload/XyoPayload.swift b/Sources/XyoClient/Payload/XyoPayload.swift index 7b19aff..88489cb 100644 --- a/Sources/XyoClient/Payload/XyoPayload.swift +++ b/Sources/XyoClient/Payload/XyoPayload.swift @@ -6,3 +6,28 @@ open class XyoPayload: Encodable { } public var schema: String } + +extension XyoPayload { + + /// Generates a SHA-256 hash of the encoded representation of the instance. + /// + /// This method serializes the instance using JSON encoding with sorted keys, + /// converts the encoded data into a UTF-8 string, and then applies SHA-256 + /// hashing to generate a hash of the instance's contents. + /// + /// - Throws: + /// - `BoundWitnessBuilderError.encodingError` if the instance cannot be + /// converted to a UTF-8 string after encoding. + /// - Any error thrown by the `sha256()` function if the hashing process fails. + /// - Returns: A `Data` object containing the SHA-256 hash of the encoded instance. + public func hash() throws -> Data { + let encoder = JSONEncoder() + encoder.outputFormatting = .sortedKeys + let data = try encoder.encode(self) + + guard let str = String(data: data, encoding: .utf8) else { + throw BoundWitnessBuilderError.encodingError + } + return try str.sha256() + } +} diff --git a/Sources/XyoClient/extensions/XyoPayload.swift b/Sources/XyoClient/extensions/XyoPayload.swift deleted file mode 100644 index 0daeb8e..0000000 --- a/Sources/XyoClient/extensions/XyoPayload.swift +++ /dev/null @@ -1,26 +0,0 @@ -import Foundation - -extension XyoPayload { - - /// Generates a SHA-256 hash of the encoded representation of the instance. - /// - /// This method serializes the instance using JSON encoding with sorted keys, - /// converts the encoded data into a UTF-8 string, and then applies SHA-256 - /// hashing to generate a hash of the instance's contents. - /// - /// - Throws: - /// - `BoundWitnessBuilderError.encodingError` if the instance cannot be - /// converted to a UTF-8 string after encoding. - /// - Any error thrown by the `sha256()` function if the hashing process fails. - /// - Returns: A `Data` object containing the SHA-256 hash of the encoded instance. - public func hash() throws -> Data { - let encoder = JSONEncoder() - encoder.outputFormatting = .sortedKeys - let data = try encoder.encode(self) - - guard let str = String(data: data, encoding: .utf8) else { - throw BoundWitnessBuilderError.encodingError - } - return try str.sha256() - } -} From 372a9e108217926fefb694e0471ef23027de8793 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 15:18:37 -0600 Subject: [PATCH 25/31] Implement witness protocol with associated types for generic implementation --- Sources/XyoClient/XyoWitness/Witness.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/XyoClient/XyoWitness/Witness.swift b/Sources/XyoClient/XyoWitness/Witness.swift index c746254..7cc94d5 100644 --- a/Sources/XyoClient/XyoWitness/Witness.swift +++ b/Sources/XyoClient/XyoWitness/Witness.swift @@ -1,6 +1,13 @@ import Foundation -open class XyoWitness { +public protocol Witness { + associatedtype TPayload: XyoPayload + var address: XyoAddress? { get } + var previousHash: String? { get } + func observe() -> [TPayload] +} + +open class XyoWitness: Witness { public let address: XyoAddress? public var previousHash: String? @@ -9,7 +16,7 @@ open class XyoWitness { self.previousHash = previousHash } - open func observe() -> XyoPayload? { + open func observe() -> [XyoPayload] { preconditionFailure("This method must be overridden") } } From 26c0a15e53da87d32eb0c61ee65d61fc2ea485da Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 15:19:05 -0600 Subject: [PATCH 26/31] Implement Witness protocol on basic witness --- Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift b/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift index 0472e6e..2b070ef 100644 --- a/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift +++ b/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift @@ -16,9 +16,12 @@ open class XyoBasicWitness: XyoWitness { private let _observer: ObserverClosure - override public func observe() -> XyoPayload? { - let payload = _observer(previousHash) - previousHash = try? payload?.hash().toHex() - return payload + override public func observe() -> [XyoPayload] { + if let payload = _observer(previousHash) { + previousHash = try? payload.hash().toHex() + return [payload] + } else { + return [] + } } } From 86eef8ec9946fdb4cbabcd844bab8e83c77aee85 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 15:19:15 -0600 Subject: [PATCH 27/31] Implement Witness protocol on system info witness --- .../XyoWitness/SystemInfo/SystemInfoWitness.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift index 47450fc..cc751ed 100644 --- a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift +++ b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift @@ -10,9 +10,13 @@ open class XyoSystemInfoWitness: XyoWitness { public typealias ObserverClosure = ((_ previousHash: String?) -> XyoSystemInfoPayload?) - override public func observe() -> XyoSystemInfoPayload? { - let payload = XyoSystemInfoPayload(wifiInfo) - previousHash = try? payload.hash().toHex() - return payload + override public func observe() -> [XyoSystemInfoPayload] { + if let payload = XyoSystemInfoPayload(wifiInfo) { + previousHash = try? payload.hash().toHex() + return [payload] + } else { + return [] + } + } } From 4209bfcac31bfcb01e0eb9674f12c89887477e2e Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 15:19:29 -0600 Subject: [PATCH 28/31] handle witness arrays in panel report --- Sources/XyoClient/XyoPanel.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index cd29751..cca9e37 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -59,9 +59,9 @@ public class XyoPanel { witnesses.append(contentsOf: self._witnesses) let payloads = witnesses.map { witness in witness.observe() - } + }.flatMap({ $0 }) let (bw, _) = try BoundWitnessBuilder() - .payloads(payloads.compactMap { $0 }) + .payloads(payloads) .witnesses(witnesses) .build(_previous_hash) self._previous_hash = bw._hash From fdad24b0180de13099ccdb1263bc023d0497072b Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 15:45:48 -0600 Subject: [PATCH 29/31] Witness with generic type parameters in protocol --- Sources/XyoClient/XyoPanel.swift | 2 +- .../XyoClient/XyoWitness/Basic/BasicWitness.swift | 4 ++-- .../XyoClient/XyoWitness/Event/EventWitness.swift | 14 ++++++++------ .../XyoWitness/SystemInfo/SystemInfoWitness.swift | 14 +++++--------- Sources/XyoClient/XyoWitness/Witness.swift | 12 ++++++++---- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index cca9e37..499e2fe 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -58,7 +58,7 @@ public class XyoPanel { witnesses.append(contentsOf: adhocWitnesses) witnesses.append(contentsOf: self._witnesses) let payloads = witnesses.map { witness in - witness.observe() + witness.observe(payloads: nil) }.flatMap({ $0 }) let (bw, _) = try BoundWitnessBuilder() .payloads(payloads) diff --git a/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift b/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift index 2b070ef..2888924 100644 --- a/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift +++ b/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift @@ -1,6 +1,6 @@ import Foundation -open class XyoBasicWitness: XyoWitness { +open class XyoBasicWitness: XyoWitness { public init(_ observer: @escaping ObserverClosure) { _observer = observer @@ -16,7 +16,7 @@ open class XyoBasicWitness: XyoWitness { private let _observer: ObserverClosure - override public func observe() -> [XyoPayload] { + override public func observe(payloads: [XyoPayload]?) -> [XyoPayload] { if let payload = _observer(previousHash) { previousHash = try? payload.hash().toHex() return [payload] diff --git a/Sources/XyoClient/XyoWitness/Event/EventWitness.swift b/Sources/XyoClient/XyoWitness/Event/EventWitness.swift index dbb2f33..abbe683 100644 --- a/Sources/XyoClient/XyoWitness/Event/EventWitness.swift +++ b/Sources/XyoClient/XyoWitness/Event/EventWitness.swift @@ -1,7 +1,6 @@ import Foundation -open class XyoEventWitness: XyoWitness { - +open class XyoEventWitness: XyoWitness { public init(_ observer: @escaping ObserverClosure) { _observer = observer super.init() @@ -16,9 +15,12 @@ open class XyoEventWitness: XyoWitness { private let _observer: ObserverClosure - override public func observe() -> XyoEventPayload? { - let payload = _observer(previousHash) - previousHash = try? payload?.hash().toHex() - return payload + public func observe(_: [XyoPayload]?) -> [XyoEventPayload] { + if let payload = _observer(previousHash) { + previousHash = try? payload.hash().toHex() + return [payload] + } else { + return [] + } } } diff --git a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift index cc751ed..ac831a5 100644 --- a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift +++ b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift @@ -1,6 +1,6 @@ import Foundation -open class XyoSystemInfoWitness: XyoWitness { +open class XyoSystemInfoWitness: XyoWitness { var wifiInfo: WifiInformation @@ -10,13 +10,9 @@ open class XyoSystemInfoWitness: XyoWitness { public typealias ObserverClosure = ((_ previousHash: String?) -> XyoSystemInfoPayload?) - override public func observe() -> [XyoSystemInfoPayload] { - if let payload = XyoSystemInfoPayload(wifiInfo) { - previousHash = try? payload.hash().toHex() - return [payload] - } else { - return [] - } - + public func observe(_: [XyoPayload]?) -> [XyoSystemInfoPayload] { + let payload = XyoSystemInfoPayload(wifiInfo) + previousHash = try? payload.hash().toHex() + return [payload] } } diff --git a/Sources/XyoClient/XyoWitness/Witness.swift b/Sources/XyoClient/XyoWitness/Witness.swift index 7cc94d5..157c4aa 100644 --- a/Sources/XyoClient/XyoWitness/Witness.swift +++ b/Sources/XyoClient/XyoWitness/Witness.swift @@ -1,13 +1,17 @@ import Foundation public protocol Witness { - associatedtype TPayload: XyoPayload + associatedtype TPayloadIn: XyoPayload + associatedtype TPayloadOut: XyoPayload var address: XyoAddress? { get } var previousHash: String? { get } - func observe() -> [TPayload] + func observe(payloads: [TPayloadIn]?) -> [TPayloadOut] } -open class XyoWitness: Witness { +open class XyoWitness: Witness { + public typealias TPayloadIn = TIn + public typealias TPayloadOut = TOut + public let address: XyoAddress? public var previousHash: String? @@ -16,7 +20,7 @@ open class XyoWitness: Witness { self.previousHash = previousHash } - open func observe() -> [XyoPayload] { + open func observe(payloads: [TPayloadIn]?) -> [TPayloadOut] { preconditionFailure("This method must be overridden") } } From 964258adeb91a947fa520175f3306423b772ef5b Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 16:06:50 -0600 Subject: [PATCH 30/31] Reduced generics for ease --- Sources/XyoClient/XyoPanel.swift | 2 +- .../XyoWitness/Basic/BasicWitness.swift | 12 +++++++----- .../XyoWitness/Event/EventWitness.swift | 8 ++++++-- .../SystemInfo/SystemInfoWitness.swift | 5 ++++- Sources/XyoClient/XyoWitness/Witness.swift | 17 ++++++++--------- Tests/XyoClientTests/Panel.swift | 4 ++-- 6 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index 499e2fe..cca9e37 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -58,7 +58,7 @@ public class XyoPanel { witnesses.append(contentsOf: adhocWitnesses) witnesses.append(contentsOf: self._witnesses) let payloads = witnesses.map { witness in - witness.observe(payloads: nil) + witness.observe() }.flatMap({ $0 }) let (bw, _) = try BoundWitnessBuilder() .payloads(payloads) diff --git a/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift b/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift index 2888924..da64a55 100644 --- a/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift +++ b/Sources/XyoClient/XyoWitness/Basic/BasicWitness.swift @@ -1,22 +1,24 @@ import Foundation -open class XyoBasicWitness: XyoWitness { +open class XyoBasicWitness: XyoWitness { - public init(_ observer: @escaping ObserverClosure) { + public typealias TPayloadOut = XyoPayload + + public init(observer: @escaping ObserverClosure) { _observer = observer super.init() } - public init(_ address: XyoAddress, _ observer: @escaping ObserverClosure) { + public init(address: XyoAddress, observer: @escaping ObserverClosure) { _observer = observer - super.init(address) + super.init(address: address) } public typealias ObserverClosure = ((_ previousHash: String?) -> XyoPayload?) private let _observer: ObserverClosure - override public func observe(payloads: [XyoPayload]?) -> [XyoPayload] { + override public func observe() -> [XyoPayload] { if let payload = _observer(previousHash) { previousHash = try? payload.hash().toHex() return [payload] diff --git a/Sources/XyoClient/XyoWitness/Event/EventWitness.swift b/Sources/XyoClient/XyoWitness/Event/EventWitness.swift index abbe683..2d85cf4 100644 --- a/Sources/XyoClient/XyoWitness/Event/EventWitness.swift +++ b/Sources/XyoClient/XyoWitness/Event/EventWitness.swift @@ -1,6 +1,10 @@ import Foundation -open class XyoEventWitness: XyoWitness { +open class XyoEventWitness: XyoWitness { + + public typealias TPayloadIn = XyoPayload + public typealias TPayloadOut = XyoEventPayload + public init(_ observer: @escaping ObserverClosure) { _observer = observer super.init() @@ -8,7 +12,7 @@ open class XyoEventWitness: XyoWitness { public init(_ address: XyoAddress, _ observer: @escaping ObserverClosure) { _observer = observer - super.init(address) + super.init(address: address) } public typealias ObserverClosure = ((_ previousHash: String?) -> XyoEventPayload?) diff --git a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift index ac831a5..3784539 100644 --- a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift +++ b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift @@ -1,6 +1,9 @@ import Foundation -open class XyoSystemInfoWitness: XyoWitness { +open class XyoSystemInfoWitness: XyoWitness { + + public typealias TPayloadIn = XyoPayload + public typealias TPayloadOut = XyoSystemInfoPayload var wifiInfo: WifiInformation diff --git a/Sources/XyoClient/XyoWitness/Witness.swift b/Sources/XyoClient/XyoWitness/Witness.swift index 157c4aa..ea4bd1f 100644 --- a/Sources/XyoClient/XyoWitness/Witness.swift +++ b/Sources/XyoClient/XyoWitness/Witness.swift @@ -1,26 +1,25 @@ import Foundation public protocol Witness { - associatedtype TPayloadIn: XyoPayload associatedtype TPayloadOut: XyoPayload - var address: XyoAddress? { get } + var address: XyoAddress { get } var previousHash: String? { get } - func observe(payloads: [TPayloadIn]?) -> [TPayloadOut] + // init(address: XyoAddress?, previousHash: String?) + func observe() -> [TPayloadOut] } -open class XyoWitness: Witness { - public typealias TPayloadIn = TIn - public typealias TPayloadOut = TOut +open class XyoWitness: Witness { + public typealias TPayloadOut = XyoPayload - public let address: XyoAddress? + public let address: XyoAddress public var previousHash: String? - public init(_ address: XyoAddress? = nil, previousHash: String? = nil) { + public init(address: XyoAddress? = nil, previousHash: String? = nil) { self.address = address ?? XyoAddress() self.previousHash = previousHash } - open func observe(payloads: [TPayloadIn]?) -> [TPayloadOut] { + open func observe() -> [TPayloadOut] { preconditionFailure("This method must be overridden") } } diff --git a/Tests/XyoClientTests/Panel.swift b/Tests/XyoClientTests/Panel.swift index 8d1ac70..64ea957 100644 --- a/Tests/XyoClientTests/Panel.swift +++ b/Tests/XyoClientTests/Panel.swift @@ -16,7 +16,7 @@ final class PanelTests: XCTestCase { let apiDomain = XyoPanel.Defaults.apiDomain let archive = XyoPanel.Defaults.apiModule let address = XyoAddress() - let witness = XyoWitness(address) + let witness = XyoWitness(address: address) let panel = XyoPanel(archive: archive, apiDomain: apiDomain, witnesses: [witness]) XCTAssertNotNil(address) XCTAssertNotNil(panel) @@ -26,7 +26,7 @@ final class PanelTests: XCTestCase { let apiDomain = XyoPanel.Defaults.apiDomain let archive = XyoPanel.Defaults.apiModule _ = XyoAddress() - let witness = XyoBasicWitness({ _ in + let witness = XyoBasicWitness(observer: { _ in let payload = XyoPayload("network.xyo.basic") return payload }) From b3bb3e24c7e1b96e7b603bfed4c0547cccb288d8 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Mon, 11 Nov 2024 16:18:23 -0600 Subject: [PATCH 31/31] Witnesses matching protocol --- Sources/XyoClient/XyoPanel.swift | 4 ++-- .../XyoClient/XyoWitness/Event/EventWitness.swift | 9 +++------ .../XyoWitness/SystemInfo/SystemInfoWitness.swift | 15 +++++---------- Sources/XyoClient/XyoWitness/Witness.swift | 11 ++++++----- Tests/XyoClientTests/Panel.swift | 2 +- 5 files changed, 17 insertions(+), 24 deletions(-) diff --git a/Sources/XyoClient/XyoPanel.swift b/Sources/XyoClient/XyoPanel.swift index cca9e37..e8fa808 100644 --- a/Sources/XyoClient/XyoPanel.swift +++ b/Sources/XyoClient/XyoPanel.swift @@ -21,7 +21,7 @@ public class XyoPanel { self.init(archivists: [archivist], witnesses: witnesses ?? []) } - public convenience init(observe: ((_ previousHash: String?) -> XyoEventPayload?)?) { + public convenience init(observe: (() -> XyoEventPayload?)?) { if observe != nil { var witnesses = [XyoWitness]() @@ -46,7 +46,7 @@ public class XyoPanel { } public func event(_ event: String, _ closure: XyoPanelReportCallback?) throws -> [XyoPayload] { - try report([XyoEventWitness { previousHash in XyoEventPayload(event) }], closure) + try report([XyoEventWitness { XyoEventPayload(event) }], closure) } public func report( diff --git a/Sources/XyoClient/XyoWitness/Event/EventWitness.swift b/Sources/XyoClient/XyoWitness/Event/EventWitness.swift index 2d85cf4..08a8417 100644 --- a/Sources/XyoClient/XyoWitness/Event/EventWitness.swift +++ b/Sources/XyoClient/XyoWitness/Event/EventWitness.swift @@ -2,9 +2,6 @@ import Foundation open class XyoEventWitness: XyoWitness { - public typealias TPayloadIn = XyoPayload - public typealias TPayloadOut = XyoEventPayload - public init(_ observer: @escaping ObserverClosure) { _observer = observer super.init() @@ -15,12 +12,12 @@ open class XyoEventWitness: XyoWitness { super.init(address: address) } - public typealias ObserverClosure = ((_ previousHash: String?) -> XyoEventPayload?) + public typealias ObserverClosure = (() -> XyoEventPayload?) private let _observer: ObserverClosure - public func observe(_: [XyoPayload]?) -> [XyoEventPayload] { - if let payload = _observer(previousHash) { + public override func observe() -> [XyoPayload] { + if let payload = _observer() { previousHash = try? payload.hash().toHex() return [payload] } else { diff --git a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift index 3784539..a9730f1 100644 --- a/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift +++ b/Sources/XyoClient/XyoWitness/SystemInfo/SystemInfoWitness.swift @@ -2,19 +2,14 @@ import Foundation open class XyoSystemInfoWitness: XyoWitness { - public typealias TPayloadIn = XyoPayload - public typealias TPayloadOut = XyoSystemInfoPayload + var allowPathMonitor: Bool - var wifiInfo: WifiInformation - - public init(_ allowPathMonitor: Bool = false) { - self.wifiInfo = WifiInformation(allowPathMonitor) + public init(allowPathMonitor: Bool = false) { + self.allowPathMonitor = allowPathMonitor } - public typealias ObserverClosure = ((_ previousHash: String?) -> XyoSystemInfoPayload?) - - public func observe(_: [XyoPayload]?) -> [XyoSystemInfoPayload] { - let payload = XyoSystemInfoPayload(wifiInfo) + public override func observe() -> [XyoPayload] { + let payload = XyoSystemInfoPayload(WifiInformation(allowPathMonitor)) previousHash = try? payload.hash().toHex() return [payload] } diff --git a/Sources/XyoClient/XyoWitness/Witness.swift b/Sources/XyoClient/XyoWitness/Witness.swift index ea4bd1f..7146e7f 100644 --- a/Sources/XyoClient/XyoWitness/Witness.swift +++ b/Sources/XyoClient/XyoWitness/Witness.swift @@ -1,14 +1,15 @@ import Foundation -public protocol Witness { - associatedtype TPayloadOut: XyoPayload +public protocol Module { var address: XyoAddress { get } var previousHash: String? { get } - // init(address: XyoAddress?, previousHash: String?) - func observe() -> [TPayloadOut] } -open class XyoWitness: Witness { +public protocol Witness { + func observe() -> [XyoPayload] +} + +open class XyoWitness: Module, Witness { public typealias TPayloadOut = XyoPayload public let address: XyoAddress diff --git a/Tests/XyoClientTests/Panel.swift b/Tests/XyoClientTests/Panel.swift index 64ea957..a54fd8c 100644 --- a/Tests/XyoClientTests/Panel.swift +++ b/Tests/XyoClientTests/Panel.swift @@ -44,7 +44,7 @@ final class PanelTests: XCTestCase { } func testSimplePanelReport() throws { - let panel = XyoPanel { _ in + let panel = XyoPanel { return nil } let panelExpectation = expectation(description: "Panel Report")