Skip to content

Commit

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

Bound Witness Serialization Improvments
  • Loading branch information
JoelBCarter authored Nov 23, 2024
2 parents d03ab66 + a6f3270 commit e93941a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public class XyoBoundWitnessBodyJson: XyoBoundWitnessBodyProtocol, Encodable, De
case schema
}

public var addresses: [String?] = []
public var addresses: [String] = []
public var payload_hashes: [String] = []
public var payload_schemas: [String] = []
public var previous_hashes: [String?] = []
public var query: String?
public var schema: String

init(
_ addresses: [String?],
_ addresses: [String],
_ previous_hashes: [String?],
_ payload_hashes: [String],
_ payload_schemas: [String],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Foundation

public protocol XyoBoundWitnessBodyProtocol {
var addresses: [String?] { get set }
var addresses: [String] { get set }
var payload_hashes: [String] { get set }
var payload_schemas: [String] { get set }
var previous_hashes: [String?] { get set }
var query: String? { get set }
}
17 changes: 6 additions & 11 deletions Sources/XyoClient/BoundWitness/BoundWitness.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitness

public var _hash: String? = nil

public var _signatures: [String?]? = nil
public var _signatures: [String]? = nil

public var _previous_hash: String? = nil

public var addresses: [String?] = []
public var addresses: [String] = []

public var payload_hashes: [String] = []

Expand All @@ -31,7 +29,6 @@ public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitness
enum CodingKeys: String, CodingKey {
case _client
case _hash
case _previous_hash
case _signatures
case addresses
case payload_hashes
Expand All @@ -52,19 +49,17 @@ public class BoundWitness: Payload, XyoBoundWitnessBodyProtocol, XyoBoundWitness
}

func encodeMetaFields(_ container: inout KeyedEncodingContainer<CodingKeys>) throws {
try container.encode(_client, forKey: ._client)
try container.encode(_hash, forKey: ._hash)
try container.encode(_signatures, forKey: ._signatures)
try container.encodeIfNotNil(_client, forKey: ._client)
try container.encodeIfNotNil(_hash, forKey: ._hash)
try container.encodeIfNotNil(_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)
if query != nil {
try container.encode(query, forKey: .query)
}
try container.encodeIfNotNil(query, forKey: .query)
try container.encode(schema, forKey: .schema)
}

Expand Down
9 changes: 4 additions & 5 deletions Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class BoundWitnessBuilder {
public init() {
}

public func signer(_ account: AccountInstance, _ previousHash: String? = nil)
public func signer(_ account: AccountInstance)
-> BoundWitnessBuilder
{
_accounts.append(account)
_previous_hashes.append(previousHash)
_previous_hashes.append(account.previousHash)
return self
}

Expand Down Expand Up @@ -61,20 +61,19 @@ public class BoundWitnessBuilder {
return self
}

public func sign(hash: String) throws -> [String?] {
public func sign(hash: String) throws -> [String] {
return try self._accounts.map {
try $0.sign(hash: hash)
}
}

public func build(_ previousHash: String? = nil) throws -> (BoundWitness, [Payload]) {
public func build() throws -> (BoundWitness, [Payload]) {
let bw = BoundWitness()
let hashable = hashableFields()
let hash = try BoundWitnessBuilder.hash(hashable)
bw._signatures = try self.sign(hash: hash)
bw._hash = hash
bw._client = "swift"
bw._previous_hash = previousHash
bw.addresses = _accounts.map { witness in witness.address }
bw.previous_hashes = _previous_hashes
bw.payload_hashes = _payload_hashes
Expand Down
2 changes: 1 addition & 1 deletion Sources/XyoClient/BoundWitness/BoundWitnessJson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class XyoBoundWitnessJson: XyoBoundWitnessBodyJson, XyoBoundWitnessMetaPr

public var _client: String?
public var _hash: String?
public var _signatures: [String?]?
public var _signatures: [String]?
public var _previous_hash: String?
public var _query: String?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ import Foundation
public protocol XyoBoundWitnessMetaProtocol {
var _client: String? { get set }
var _hash: String? { get set }
var _signatures: [String?]? { get set }
var _previous_hash: String? { get set }
var _signatures: [String]? { get set }
}

0 comments on commit e93941a

Please sign in to comment.