Skip to content

Commit

Permalink
Return hash as string
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelBCarter committed Nov 23, 2024
1 parent 2fee8bb commit f17c7a0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Sources/XyoClient/BoundWitness/BoundWitnessBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ public class BoundWitnessBuilder {

public func payload<T: Payload>(_ schema: String, _ payload: T) throws -> BoundWitnessBuilder {
_payloads.append(payload)
_payload_hashes.append(try payload.hash().toHex())
_payload_hashes.append(try payload.hash())
_payload_schemas.append(schema)
return self
}

public func payloads(_ payloads: [Payload]) throws -> BoundWitnessBuilder {
_payloads.append(contentsOf: payloads)
_payload_hashes.append(
contentsOf: try payloads.map { payload in try payload.hash().toHex() })
contentsOf: try payloads.map { payload in try payload.hash() })
_payload_schemas.append(contentsOf: payloads.map { payload in payload.schema })
return self
}

public func query(_ payload: Payload) throws -> BoundWitnessBuilder {
self._query = try payload.hash().toHex()
self._query = try payload.hash()
let _ = try self.payload(payload.schema, payload)
return self
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/XyoClient/Payload/Payload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension Payload {
/// 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 {
public func hash() throws -> String {
let encoder = JSONEncoder()
encoder.outputFormatting = .sortedKeys

Expand All @@ -38,11 +38,11 @@ extension Payload {
let filteredData = try JSONSerialization.data(withJSONObject: filteredJSON, options: [.sortedKeys])

// Convert the JSON data into a string
guard let jsonString = String(data: filteredData, encoding: .utf16) else {
guard let jsonString = String(data: filteredData, encoding: .utf8) else {
throw BoundWitnessBuilderError.encodingError
}

// Hash the JSON string
return try jsonString.sha256()
return try jsonString.sha256().toHex()
}
}

0 comments on commit f17c7a0

Please sign in to comment.