Skip to content

Commit e40c3f8

Browse files
fix: order custom subtypes (no primitives!) in alphabetical order when encoding
1 parent c6da90f commit e40c3f8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Sources/web3swift/Utils/EIP/EIP712/EIP712Parser.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,21 @@ public struct EIP712TypedData {
187187

188188
internal func encodeType(_ type: String, _ typeData: [EIP712TypeProperty], typesCovered: [String] = []) throws -> String {
189189
var typesCovered = typesCovered
190-
var encodedSubtypes: [String] = []
190+
var encodedSubtypes: [String : String] = [:]
191191
let parameters = try typeData.map { attributeType in
192192
if let innerTypes = types[attributeType.coreType], !typesCovered.contains(attributeType.coreType) {
193193
typesCovered.append(attributeType.coreType)
194194
if attributeType.coreType != type {
195-
encodedSubtypes.append(try encodeType(attributeType.coreType, innerTypes))
195+
encodedSubtypes[attributeType.coreType] = try encodeType(attributeType.coreType, innerTypes)
196196
}
197197
}
198198
return "\(attributeType.type) \(attributeType.name)"
199199
}
200-
return type + "(" + parameters.joined(separator: ",") + ")" + encodedSubtypes.joined(separator: "")
200+
return type + "(" + parameters.joined(separator: ",") + ")" + encodedSubtypes.sorted { lhs, rhs in
201+
return lhs.key < rhs.key
202+
}
203+
.map { $0.value }
204+
.joined(separator: "")
201205
}
202206

203207
/// Convenience function for ``encodeData(_:data:)`` that uses ``primaryType`` and ``message`` as values.

0 commit comments

Comments
 (0)