From 0b1734b80bc907bc61286925736f1302f04116f0 Mon Sep 17 00:00:00 2001 From: Joel Carter Date: Sat, 23 Nov 2024 09:14:15 -0600 Subject: [PATCH] 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() } }