Skip to content

Commit 192b955

Browse files
Fix encoding version in typed data Domain (#230)
* Change `StarknetTypedData` version type to `String` * Remove dummy changes * Add `Domain` encoding * Apply code review suggestion * Little refactor * Little refactor * Remove unused code * Fix version encoding * Fix tests
1 parent c112768 commit 192b955

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

Sources/Starknet/Data/TypedData/StarknetTypedData.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ public extension StarknetTypedData {
399399
public let chainId: Element
400400
public let revision: Revision
401401

402+
fileprivate enum CodingKeys: CodingKey {
403+
case name
404+
case version
405+
case chainId
406+
case revision
407+
}
408+
402409
public var separatorName: String {
403410
switch revision {
404411
case .v0: "StarkNetDomain"
@@ -413,6 +420,14 @@ public extension StarknetTypedData {
413420
self.chainId = try container.decode(Element.self, forKey: .chainId)
414421
self.revision = try container.decodeIfPresent(Revision.self, forKey: .revision) ?? .v0
415422
}
423+
424+
public func encode(to encoder: Encoder) throws {
425+
var container = encoder.container(keyedBy: CodingKeys.self)
426+
try container.encode(name, forKey: .name)
427+
try container.encode(String(describing: version), forKey: .version)
428+
try container.encode(chainId, forKey: .chainId)
429+
try container.encode(revision, forKey: .revision)
430+
}
416431
}
417432

418433
enum Revision: String, Codable, Equatable {
@@ -704,6 +719,29 @@ extension StarknetTypedData {
704719
}
705720
}
706721

722+
extension StarknetTypedData.Element: CustomStringConvertible {
723+
public var description: String {
724+
switch self {
725+
case let .string(s):
726+
s
727+
case let .decimal(n):
728+
String(n)
729+
case let .signedDecimal(n):
730+
String(n)
731+
case let .felt(f):
732+
f.toHex()
733+
case let .signedFelt(f):
734+
f.toHex()
735+
case let .bool(b):
736+
String(b)
737+
case .object:
738+
String(describing: self)
739+
case .array:
740+
String(describing: self)
741+
}
742+
}
743+
}
744+
707745
private extension String {
708746
func strippingPointer() -> String {
709747
if self.isArray() {

Tests/StarknetTests/Data/TypedDataTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ final class TypedDataTests: XCTestCase {
4747
static let exampleDomainV0 = """
4848
{
4949
"name": "DomainV0",
50-
"version": 1,
50+
"version": "1",
5151
"chainId": 2137,
5252
}
5353
"""
5454
static let exampleDomainV1 = """
5555
{
5656
"name": "DomainV1",
57-
"version": 2,
57+
"version": "2",
5858
"chainId": "2137",
5959
"revision": 1
6060
}

0 commit comments

Comments
 (0)