Skip to content

Commit

Permalink
Merge pull request #54 from XYOracleNetwork/feature/bound-witness-has…
Browse files Browse the repository at this point in the history
…hable-fields

BoundWitness Hashable Fields
  • Loading branch information
JoelBCarter authored Nov 23, 2024
2 parents e93941a + 734727a commit 0441ed5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 29 deletions.
14 changes: 6 additions & 8 deletions Sources/XyoClient/BoundWitness/Body/BoundWitnessBodyJson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/XyoClient/BoundWitness/BoundWitness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitness
}

func encodeMetaFields(_ container: inout KeyedEncodingContainer<CodingKeys>) 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<CodingKeys>) throws {
try container.encode(addresses, forKey: .addresses)
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)
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
8 changes: 1 addition & 7 deletions Sources/XyoClient/extensions/KeyedEncodingContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(_ value: T?, forKey key: KeyedEncodingContainer<K>.Key) throws
where T: Encodable {
if let value = value {
try encode(value, forKey: key)
try encodeIfPresent(value, forKey: key)
}
}
}
14 changes: 11 additions & 3 deletions Tests/XyoClientTests/BoundWitness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
2 changes: 1 addition & 1 deletion Tests/XyoClientTests/TestData/TestPayload1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ public class TestPayload1: Payload {
}

let testPayload1 = TestPayload1("network.xyo.test")
let testPayload1Hash: Hash = "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35"
let testPayload1Hash: Hash = "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db"
4 changes: 3 additions & 1 deletion Tests/XyoClientTests/TestData/TestPayload2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -30,4 +32,4 @@ public class TestPayload2: Payload {
}

let testPayload2 = TestPayload2("network.xyo.test")
let testPayload2Hash: Hash = "a5bd50ec40626d390017646296f6a6ac2938ff2e952b2a27b1467a7ef44cdf35"
let testPayload2Hash: Hash = "c915c56dd93b5e0db509d1a63ca540cfb211e11f03039b05e19712267bb8b6db"

0 comments on commit 0441ed5

Please sign in to comment.