-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use enums for basic and preset types in
StarknetTypedData
(#180)
- Loading branch information
Showing
3 changed files
with
77 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
|
||
extension StarknetTypedData { | ||
enum BasicType: String, CaseIterable { | ||
case felt | ||
case bool | ||
case selector | ||
case string | ||
case u128 | ||
case i128 | ||
case `enum` | ||
case merkletree | ||
case contractAddress = "ContractAddress" | ||
case classHash = "ClassHash" | ||
case timestamp | ||
case shortstring | ||
|
||
static func cases(revision: Revision) -> [BasicType] { | ||
switch revision { | ||
case .v0: [.felt, .bool, .selector, .string, .merkletree] | ||
case .v1: allCases | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import Foundation | ||
|
||
public extension StarknetTypedData { | ||
enum PresetType: String, Equatable, CaseIterable { | ||
case u256 | ||
case tokenAmount = "TokenAmount" | ||
case nftId = "NftId" | ||
|
||
public var params: [TypeDeclarationWrapper] { | ||
switch self { | ||
case .u256: | ||
[TypeDeclarationWrapper.standard(.init(name: "low", type: BasicType.u128.rawValue)), TypeDeclarationWrapper.standard(.init(name: "high", type: BasicType.u128.rawValue))] | ||
case .tokenAmount: | ||
[TypeDeclarationWrapper.standard(.init(name: "token_address", type: BasicType.contractAddress.rawValue)), TypeDeclarationWrapper.standard(.init(name: "amount", type: Self.u256.rawValue))] | ||
case .nftId: | ||
[TypeDeclarationWrapper.standard(.init(name: "collection_address", type: BasicType.contractAddress.rawValue)), TypeDeclarationWrapper.standard(.init(name: "token_id", type: Self.u256.rawValue))] | ||
} | ||
} | ||
|
||
fileprivate enum CodingKeys: CodingKey { | ||
case u256 | ||
case tokenAmount | ||
case nftId | ||
} | ||
|
||
static func cases(revision: Revision) -> [PresetType] { | ||
switch revision { | ||
case .v0: [] | ||
case .v1: allCases | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters