From 876a227ee2c2f9c02615f2409089edf1a5711899 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 08:29:45 -0600 Subject: [PATCH 1/3] De-smurf protocol --- ...dWitnessBodyProtocol.swift => BoundWitnessBody.swift} | 2 +- .../BoundWitness/Body/BoundWitnessBodyJson.swift | 2 +- Sources/XyoClient/BoundWitness/BoundWitness.swift | 2 +- Sources/XyoClient/BoundWitness/BoundWitnessJson.swift | 2 -- Tests/XyoClientTests/BoundWitness.swift | 9 +++++++++ 5 files changed, 12 insertions(+), 5 deletions(-) rename Sources/XyoClient/BoundWitness/Body/{BoundWitnessBodyProtocol.swift => BoundWitnessBody.swift} (83%) diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyProtocol.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift similarity index 83% rename from Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyProtocol.swift rename to Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift index ed5f3da..8722a72 100644 --- a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyProtocol.swift +++ b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift @@ -1,6 +1,6 @@ import Foundation -public protocol XyoBoundWitnessBodyProtocol { +public protocol BoundWitnessBody { var addresses: [String] { get set } var payload_hashes: [String] { get set } var payload_schemas: [String] { get set } diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift index 41d5eaf..2c7301f 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, Decodable { +public class XyoBoundWitnessBodyJson: BoundWitnessBody, Encodable, Decodable { enum CodingKeys: String, CodingKey { case addresses case payload_hashes diff --git a/Sources/XyoClient/BoundWitness/BoundWitness.swift b/Sources/XyoClient/BoundWitness/BoundWitness.swift index a671629..6e8dffe 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitness.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitness.swift @@ -2,7 +2,7 @@ import Foundation let BoundWitnessSchema = "network.xyo.boundwitness" -public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitnessMetaProtocol, +public class BoundWitness: Payload, BoundWitnessBody, XyoBoundWitnessMetaProtocol, Decodable { diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift b/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift index cf31514..a8f2419 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 _previous_hash case _signatures case addresses case payload_hashes @@ -17,7 +16,6 @@ public class XyoBoundWitnessJson: XyoBoundWitnessBodyJson, XyoBoundWitnessMetaPr public var _client: String? public var _hash: String? public var _signatures: [String]? - public var _previous_hash: String? public var _query: String? func encodeMetaFields(_ container: inout KeyedEncodingContainer) throws { diff --git a/Tests/XyoClientTests/BoundWitness.swift b/Tests/XyoClientTests/BoundWitness.swift index e3de485..b2d052a 100644 --- a/Tests/XyoClientTests/BoundWitness.swift +++ b/Tests/XyoClientTests/BoundWitness.swift @@ -33,4 +33,13 @@ final class BoundWitnessTests: XCTestCase { let (bwJson, _) = try bw.build() XCTAssertEqual(bwJson._hash, "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35") } + + func testPayload_hash_returnsExpectedHashWhenNested() throws { + let hash = try BoundWitnessBuilder.hash(testPayload2) + XCTAssertEqual(hash, testPayload2Hash) + let address = Account.fromPrivateKey(key: testVectorPrivateKey.hexToData()) + let bw = try BoundWitnessBuilder().signer(address).payload("network.xyo.test", testPayload2) + let (bwJson, _) = try bw.build() + XCTAssertEqual(bwJson._hash, "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35") + } } From 9985c745ee3fa7ba393c08cbbc1c8316fd018bc7 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 08:42:58 -0600 Subject: [PATCH 2/3] De-smurf protocols --- Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift | 1 + .../XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift | 2 +- Sources/XyoClient/BoundWitness/BoundWitness.swift | 2 +- Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift | 4 ++-- Sources/XyoClient/BoundWitness/BoundWitnessJson.swift | 2 +- ...{BoundWitnessMetaProtocol.swift => BoundWitnessMeta.swift} | 2 +- 6 files changed, 7 insertions(+), 6 deletions(-) rename Sources/XyoClient/BoundWitness/Meta/{BoundWitnessMetaProtocol.swift => BoundWitnessMeta.swift} (74%) diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift index 8722a72..a19a2f0 100644 --- a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift +++ b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBody.swift @@ -6,4 +6,5 @@ public protocol BoundWitnessBody { var payload_schemas: [String] { get set } var previous_hashes: [String?] { get set } var query: String? { get set } + var schema: String { get set } } diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift index 2c7301f..0d972ac 100644 --- a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift +++ b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift @@ -1,6 +1,6 @@ import Foundation -public class XyoBoundWitnessBodyJson: BoundWitnessBody, Encodable, Decodable { +public class BoundWitnessBodyJson: BoundWitnessBody, Encodable, Decodable { enum CodingKeys: String, CodingKey { case addresses case payload_hashes diff --git a/Sources/XyoClient/BoundWitness/BoundWitness.swift b/Sources/XyoClient/BoundWitness/BoundWitness.swift index 6e8dffe..2616cb4 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitness.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitness.swift @@ -2,7 +2,7 @@ import Foundation let BoundWitnessSchema = "network.xyo.boundwitness" -public class BoundWitness: Payload, BoundWitnessBody, XyoBoundWitnessMetaProtocol, +public class BoundWitness: Payload, BoundWitnessBody, BoundWitnessMeta, Decodable { diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift index e67af28..1773233 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift @@ -30,8 +30,8 @@ public class BoundWitnessBuilder { return self } - private func hashableFields() -> XyoBoundWitnessBodyJson { - return XyoBoundWitnessBodyJson( + private func hashableFields() -> BoundWitnessBodyJson { + return BoundWitnessBodyJson( addresses: _accounts.map { witness in witness.address }, payload_hashes: _payload_hashes, payload_schemas: _payload_schemas, diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift b/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift index a8f2419..f214901 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitnessJson.swift @@ -1,6 +1,6 @@ import Foundation -public class XyoBoundWitnessJson: XyoBoundWitnessBodyJson, XyoBoundWitnessMetaProtocol { +public class XyoBoundWitnessJson: BoundWitnessBodyJson, BoundWitnessMeta { enum CodingKeys: String, CodingKey { case _client case _hash diff --git a/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift b/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMeta.swift similarity index 74% rename from Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift rename to Sources/XyoClient/BoundWitness/Meta/BoundWitnessMeta.swift index 989b906..46a20a0 100644 --- a/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMetaProtocol.swift +++ b/Sources/XyoClient/BoundWitness/Meta/BoundWitnessMeta.swift @@ -1,6 +1,6 @@ import Foundation -public protocol XyoBoundWitnessMetaProtocol { +public protocol BoundWitnessMeta { var _client: String? { get set } var _hash: String? { get set } var _signatures: [String]? { get set } From 0b1734b80bc907bc61286925736f1302f04116f0 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 09:14:15 -0600 Subject: [PATCH 3/3] Remove keys prefixed with `_` when hashing --- Sources/XyoClient/Payload/Payload.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Sources/XyoClient/Payload/Payload.swift b/Sources/XyoClient/Payload/Payload.swift index 3caf114..c935ca9 100644 --- a/Sources/XyoClient/Payload/Payload.swift +++ b/Sources/XyoClient/Payload/Payload.swift @@ -23,11 +23,26 @@ extension Payload { public func hash() throws -> Data { let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeys + + // Encode `self` to JSON data let data = try encoder.encode(self) - guard let str = String(data: data, encoding: .utf8) else { + // Decode the JSON into a dictionary and filter keys + guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else { throw BoundWitnessBuilderError.encodingError } - return try str.sha256() + + let filteredJSON = jsonObject.filter { !$0.key.hasPrefix("_") } + + // Encode the filtered dictionary back into JSON data + let filteredData = try JSONSerialization.data(withJSONObject: filteredJSON, options: [.sortedKeys]) + + // Convert the JSON data into a string + guard let jsonString = String(data: filteredData, encoding: .utf8) else { + throw BoundWitnessBuilderError.encodingError + } + + // Hash the JSON string + return try jsonString.sha256() } }