From b60515de4f64c85339879cbc99b070da5c0b2cb7 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 07:43:48 -0600 Subject: [PATCH 1/5] Use native encoder method --- .../XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift index 4bd4004..e3ad8fb 100644 --- a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift +++ b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift @@ -51,9 +51,7 @@ public class XyoBoundWitnessBodyJson: XyoBoundWitnessBodyProtocol, Encodable, De try container.encode(payload_hashes, forKey: .payload_hashes) try container.encode(payload_schemas, forKey: .payload_schemas) try container.encode(previous_hashes, forKey: .previous_hashes) - if query != nil { - try container.encode(query, forKey: .query) - } + try container.encodeIfPresent(query, forKey: .query) try container.encode(schema, forKey: .schema) } From 227317236f6bf0b812a43160b8986cd5c522e285 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 07:45:43 -0600 Subject: [PATCH 2/5] Use native encoder extensions --- Sources/XyoClient/BoundWitness/BoundWitness.swift | 8 ++++---- Sources/XyoClient/extensions/KeyedEncodingContainer.swift | 8 +------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/Sources/XyoClient/BoundWitness/BoundWitness.swift b/Sources/XyoClient/BoundWitness/BoundWitness.swift index 8f9a858..a671629 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitness.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitness.swift @@ -49,9 +49,9 @@ public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitness } func encodeMetaFields(_ container: inout KeyedEncodingContainer) throws { - try container.encodeIfNotNil(_client, forKey: ._client) - try container.encodeIfNotNil(_hash, forKey: ._hash) - try container.encodeIfNotNil(_signatures, forKey: ._signatures) + try container.encodeIfPresent(_client, forKey: ._client) + try container.encodeIfPresent(_hash, forKey: ._hash) + try container.encodeIfPresent(_signatures, forKey: ._signatures) } func encodeBodyFields(_ container: inout KeyedEncodingContainer) throws { @@ -59,7 +59,7 @@ public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitness try container.encode(payload_hashes, forKey: .payload_hashes) try container.encode(payload_schemas, forKey: .payload_schemas) try container.encode(previous_hashes, forKey: .previous_hashes) - try container.encodeIfNotNil(query, forKey: .query) + try container.encodeIfPresent(query, forKey: .query) try container.encode(schema, forKey: .schema) } diff --git a/Sources/XyoClient/extensions/KeyedEncodingContainer.swift b/Sources/XyoClient/extensions/KeyedEncodingContainer.swift index 6472e4b..4b628b3 100644 --- a/Sources/XyoClient/extensions/KeyedEncodingContainer.swift +++ b/Sources/XyoClient/extensions/KeyedEncodingContainer.swift @@ -3,13 +3,7 @@ extension KeyedEncodingContainer { throws where T: BinaryFloatingPoint & Encodable { if let value = value, !value.isNaN { - try encode(value, forKey: key) - } - } - mutating func encodeIfNotNil(_ value: T?, forKey key: KeyedEncodingContainer.Key) throws - where T: Encodable { - if let value = value { - try encode(value, forKey: key) + try encodeIfPresent(value, forKey: key) } } } From da4ed063b89948ee7576bbcf906e1fbe21a758ed Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 07:59:19 -0600 Subject: [PATCH 3/5] Use memory previous hash store To prevent test accounts from having a previous hash --- Tests/XyoClientTests/BoundWitness.swift | 14 +++++++++++--- Tests/XyoClientTests/TestData/TestPayload1.swift | 2 +- Tests/XyoClientTests/TestData/TestPayload2.swift | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Tests/XyoClientTests/BoundWitness.swift b/Tests/XyoClientTests/BoundWitness.swift index c7cdc0a..e3de485 100644 --- a/Tests/XyoClientTests/BoundWitness.swift +++ b/Tests/XyoClientTests/BoundWitness.swift @@ -8,21 +8,29 @@ final class BoundWitnessTests: XCTestCase { ("testPayload_hash_returnsExpectedHash1", testPayload_hash_returnsExpectedHash1), ("testPayload_hash_returnsExpectedHash2", testPayload_hash_returnsExpectedHash2), ] + + override func setUp() { + super.setUp() + // Ensure previousHash = nil for tests addresses + Account.previousHashStore = MemoryPreviousHashStore() + } func testPayload_hash_returnsExpectedHash1() throws { let hash = try BoundWitnessBuilder.hash(testPayload1) - XCTAssertEqual(hash, "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db") + XCTAssertEqual(hash, testPayload1Hash) let address = Account.fromPrivateKey(key: testVectorPrivateKey.hexToData()) let bw = try BoundWitnessBuilder().signer(address).payload( "network.xyo.test", TestPayload1("network.xyo.test")) let (bwJson, _) = try bw.build() - XCTAssertEqual(bwJson._hash, testPayload1Hash) + XCTAssertEqual(bwJson._hash, "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35") } func testPayload_hash_returnsExpectedHash2() 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, testPayload2Hash) + XCTAssertEqual(bwJson._hash, "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35") } } diff --git a/Tests/XyoClientTests/TestData/TestPayload1.swift b/Tests/XyoClientTests/TestData/TestPayload1.swift index 7f77c90..226ffc7 100644 --- a/Tests/XyoClientTests/TestData/TestPayload1.swift +++ b/Tests/XyoClientTests/TestData/TestPayload1.swift @@ -30,4 +30,4 @@ public class TestPayload1: Payload { } let testPayload1 = TestPayload1("network.xyo.test") -let testPayload1Hash: Hash = "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35" +let testPayload1Hash: Hash = "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db" diff --git a/Tests/XyoClientTests/TestData/TestPayload2.swift b/Tests/XyoClientTests/TestData/TestPayload2.swift index e0e794c..7025e01 100644 --- a/Tests/XyoClientTests/TestData/TestPayload2.swift +++ b/Tests/XyoClientTests/TestData/TestPayload2.swift @@ -30,4 +30,4 @@ public class TestPayload2: Payload { } let testPayload2 = TestPayload2("network.xyo.test") -let testPayload2Hash: Hash = "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35" +let testPayload2Hash: Hash = "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db" From f34ae35927c284fcf5f40159afab3e472dad0720 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 08:10:29 -0600 Subject: [PATCH 4/5] Add optional field for serialization tests --- Tests/XyoClientTests/TestData/TestPayload2.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Tests/XyoClientTests/TestData/TestPayload2.swift b/Tests/XyoClientTests/TestData/TestPayload2.swift index 7025e01..d772b07 100644 --- a/Tests/XyoClientTests/TestData/TestPayload2.swift +++ b/Tests/XyoClientTests/TestData/TestPayload2.swift @@ -3,6 +3,7 @@ import XyoClient public class TestPayload2SubObject: Encodable { var string_value = "yo" var number_value = 2 + var optional_field: String? = nil } public class TestPayload2: Payload { @@ -17,6 +18,7 @@ public class TestPayload2: Payload { case object_field case timestamp case number_field + case optional_field } public override func encode(to encoder: Encoder) throws { From 734727a011c03348ba8fc8516910cf47319e2b3f Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 08:11:50 -0600 Subject: [PATCH 5/5] Named ctor parameters to prevent confusion --- .../BoundWitness/Body/BoundWitnessBodyJson.swift | 10 +++++----- .../XyoClient/BoundWitness/BoundWitnessBuilder.swift | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift index e3ad8fb..41d5eaf 100644 --- a/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift +++ b/Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift @@ -18,11 +18,11 @@ public class XyoBoundWitnessBodyJson: XyoBoundWitnessBodyProtocol, Encodable, De public var schema: String init( - _ addresses: [String], - _ previous_hashes: [String?], - _ payload_hashes: [String], - _ payload_schemas: [String], - _ query: String? = nil + addresses: [String], + payload_hashes: [String], + payload_schemas: [String], + previous_hashes: [String?], + query: String? = nil ) { self.addresses = addresses self.payload_hashes = payload_hashes diff --git a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift index 5aec07d..e67af28 100644 --- a/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift +++ b/Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift @@ -32,11 +32,11 @@ public class BoundWitnessBuilder { private func hashableFields() -> XyoBoundWitnessBodyJson { return XyoBoundWitnessBodyJson( - _accounts.map { witness in witness.address }, - _previous_hashes, - _payload_hashes, - _payload_schemas, - _query + addresses: _accounts.map { witness in witness.address }, + payload_hashes: _payload_hashes, + payload_schemas: _payload_schemas, + previous_hashes: _previous_hashes, + query: _query ) }