-
Notifications
You must be signed in to change notification settings - Fork 11
Tx input parsing #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
bejitono
wants to merge
12
commits into
Safari-Wallet:main
Choose a base branch
from
bejitono:tx-input-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Tx input parsing #16
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7b182e1
Defines tx input model.
bejitono d5393fb
Implements transaction decoder.
bejitono 7f9cc27
Implements transaction input business logic.
bejitono 1ae1747
Fixes issues with some contracts not being fetched, and contracts bei…
bejitono b12019b
Implements transaction parser.
bejitono d6eefeb
Displays tx input descriptions on tx list.
bejitono 0dee230
Implements transaction decoder tests.
bejitono 65b140b
Merge branch 'main' of https://github.com/Safari-Wallet/ui into tx-in…
bejitono f4f42a0
Clean up.
bejitono 05bf7bf
Makes function names title cased.
bejitono 551d683
Refactors transaction decoding.
bejitono de9e48b
Implements simple string value parsing.
bejitono File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,36 @@ | ||
| // | ||
| // Method.swift | ||
| // Wallet | ||
| // | ||
| // Created by Stefano on 21.12.21. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| struct MethodInfo { | ||
| let contractAddress: String | ||
| let methodId: String | ||
| let description: MethodDescription | ||
| } | ||
|
|
||
| struct MethodDescription { | ||
| let stringFormat: String | ||
| let arguments: [String] | ||
| } | ||
|
|
||
| extension MethodInfo: Decodable { | ||
|
|
||
| enum CodingKeys : String, CodingKey { | ||
| case contractAddress = "Address" | ||
| case methodId = "MethodId" | ||
| case description = "Description" | ||
| } | ||
| } | ||
|
|
||
| extension MethodDescription: Decodable { | ||
|
|
||
| enum CodingKeys : String, CodingKey { | ||
| case stringFormat = "StringFormat" | ||
| case arguments = "Arguments" | ||
| } | ||
| } |
This file contains hidden or 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,21 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <array> | ||
| <dict> | ||
| <key>Description</key> | ||
| <dict> | ||
| <key>StringFormat</key> | ||
| <string>Registered %@</string> | ||
| <key>Arguments</key> | ||
| <array> | ||
| <string>name</string> | ||
| </array> | ||
| </dict> | ||
| <key>Address</key> | ||
| <string>0x283af0b28c62c092c9727f1ee09c02ca627eb7f5</string> | ||
| <key>MethodId</key> | ||
| <string>f7a16963</string> | ||
| </dict> | ||
| </array> | ||
| </plist> | ||
This file contains hidden or 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
This file contains hidden or 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,32 @@ | ||
| // | ||
| // TransactionInput.swift | ||
| // Wallet | ||
| // | ||
| // Created by Stefano on 17.12.21. | ||
| // | ||
|
|
||
| import MEWwalletKit | ||
|
|
||
| typealias InputName = String | ||
|
|
||
| struct TransactionInput { | ||
| let method: Method | ||
| let inputData: [InputName: InputData] | ||
| } | ||
|
|
||
| struct Method { | ||
| let name: String | ||
| let signature: String | ||
| let hash: String | ||
| let inputs: [InputParameter] | ||
| } | ||
|
|
||
| struct InputParameter { | ||
| let name: InputName | ||
| let type: ABI.Element.ParameterType | ||
| } | ||
|
|
||
| struct InputData { | ||
| let parameter: InputParameter | ||
| let data: Any | ||
| } |
This file contains hidden or 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
This file contains hidden or 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,63 @@ | ||
| // | ||
| // TransactionDecoder.swift | ||
| // Wallet | ||
| // | ||
| // Created by Stefano on 21.12.21. | ||
| // | ||
|
|
||
| import Foundation | ||
| import MEWwalletKit | ||
| import UIKit | ||
|
|
||
| protocol TransactionDecodable { | ||
| func decode(input: String, with contract: Contract) -> TransactionInput? | ||
| } | ||
|
|
||
| struct TransactionDecoder: TransactionDecodable { | ||
|
|
||
| func decode(input: String, with contract: Contract) -> TransactionInput? { | ||
| guard let data = contract.abi.data(using: .utf8), | ||
| let abiRecords = try? JSONDecoder().decode([ABI.Record].self, from: data) else { | ||
| return nil | ||
| } | ||
| for record in abiRecords { | ||
| let element = try? record.parse() | ||
| if case .function(let function) = element { | ||
| let signature = function.signature | ||
| let decodedMethodHash = function.methodString | ||
| let data = Data(hex: input) | ||
| guard !data.isEmpty, data.count > 4 else { return nil } | ||
| let methodHash = data[0 ..< 4].dataToHexString() | ||
| if methodHash == decodedMethodHash, let decodedInput = element?.decodeInputData(data) { | ||
|
DimitarNestorov marked this conversation as resolved.
|
||
| let parameters = getInputParameters(of: function) | ||
| let inputData = mapToInputDataFrom(parameters: parameters, input: decodedInput) | ||
| return TransactionInput( | ||
| method: Method( | ||
| name: function.name ?? "", | ||
| signature: signature, | ||
| hash: methodHash, | ||
| inputs: parameters | ||
| ), | ||
| inputData: inputData | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| private func getInputParameters(of function: ABI.Element.Function) -> [InputParameter] { | ||
| function.inputs.map { InputParameter(name: $0.name, type: $0.type) } | ||
| } | ||
|
|
||
| private func mapToInputDataFrom(parameters: [InputParameter], input: [String: Any]) -> [InputName: InputData] { | ||
| let inputData = parameters.compactMap { param -> (InputName, InputData)? in | ||
| guard let data = input[param.name] else { return nil } | ||
| return (param.name, InputData(parameter: param, data: data)) | ||
| } | ||
| return Dictionary( | ||
| inputData, | ||
| uniquingKeysWith: { (first, _) in first } | ||
| ) | ||
| } | ||
| } | ||
This file contains hidden or 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,75 @@ | ||
| // | ||
| // TransactionInputParser.swift | ||
| // Wallet | ||
| // | ||
| // Created by Stefano on 21.12.21. | ||
| // | ||
|
|
||
| import BigInt | ||
| import Foundation | ||
| import MEWwalletKit | ||
|
|
||
| protocol TransactionInputParseable { | ||
| func parse(input: [String: String], methodHash: String, contractAddress: RawAddress) -> String? | ||
| func parseToStringValues(input: TransactionInput) -> [InputName: String] | ||
| } | ||
|
|
||
| typealias MethodID = String | ||
|
|
||
| final class TransactionInputParser: TransactionInputParseable { | ||
|
|
||
| private lazy var methodInfoLookup: [RawAddress: [MethodID: MethodInfo]] = { | ||
| let decoder = PropertyListDecoder() | ||
|
|
||
| guard let url = Bundle.main.url(forResource: "MethodInfo", withExtension: "plist"), | ||
| let data = try? Data(contentsOf: url), | ||
| let methodInfo = try? decoder.decode([MethodInfo].self, from: data) else { return [:] } | ||
|
|
||
| return methodInfo.reduce([RawAddress: [MethodID: MethodInfo]]()) { partialResult, info in | ||
| var methodInfo = partialResult | ||
| if methodInfo[info.contractAddress] != nil { | ||
| methodInfo[info.contractAddress]?[info.methodId] = info | ||
| } else { | ||
| methodInfo[info.contractAddress] = [info.methodId: info] | ||
| } | ||
| return methodInfo | ||
| } | ||
| }() | ||
|
|
||
| func parse(input: [String: String], methodHash: String, contractAddress: RawAddress) -> String? { | ||
| guard let methodInfo = methodInfoLookup[contractAddress]?[methodHash] else { return nil } | ||
| let arguments = methodInfo.description.arguments.compactMap { input[$0] } | ||
| return String(format: methodInfo.description.stringFormat, arguments: arguments) | ||
| } | ||
|
|
||
| func parseToStringValues(input: TransactionInput) -> [InputName: String] { | ||
| input.inputData.reduce([InputName: String]()) { partialResult, input in | ||
| var stringValues = partialResult | ||
| stringValues[input.key] = convertRawValueToString(inputData: input.value) | ||
| return stringValues | ||
| } | ||
| } | ||
|
|
||
| private func convertRawValueToString(inputData: InputData) -> String? { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just simple conversion to string values right now so we can display it on the tx history detail page for demo purposes. |
||
| switch inputData.parameter.type { | ||
| case .uint: | ||
| return (inputData.data as? BigUInt).flatMap(String.init) | ||
| case .int: | ||
| return (inputData.data as? BigInt).flatMap(String.init) | ||
| case .address: | ||
| return (inputData.data as? Address).flatMap { $0.address } | ||
| case .function: | ||
| return "n/a" | ||
| case .bool: | ||
| return (inputData.data as? Bool).flatMap(String.init) | ||
| case .bytes, .dynamicBytes: | ||
| return ABIEncoder.convertToData(inputData.data as AnyObject)?.toHexString() | ||
| case .string: | ||
| return inputData.data as? String | ||
| case .array: | ||
| return "n/a" | ||
| case .tuple: | ||
| return "n/a" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you guys think of this approach? Maintaining a list of string formats per contract/methodId so we can display the tx input in a descriptive way, instead of dumping the method name and its arguments? But it's something we'd have to maintain over time...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maintaining a list is a good idea. While it involves more upfront work, and the ongoing maintenance cost, it's the best approach we have so far to provide really clear, user-friendly descriptions of transactions.
One question: why a
plist? Would it not be better to put this in Swift directly, perhaps a map fromaddress+methodto a callback? This way, we can not only return text, but also other pieces of UI (logos, links, perhaps even in-wallet actions?)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to our discussions, this approach is still a bit limited as it assumes that the parameter value’s string representation can be readily displayed. However, in some cases we will need to do some further formatting, e.g. when the input parameter is a timestamp, we would like to format it in a readable way. One way we could handle this is to add an additional type argument which tells the client how to format the value. wdyt?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, I agree. What about using
String(format:_:), or another string formatting API, or a library? https://riptutorial.com/swift/example/6342/formatting-stringsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right! So in the current PR
String(format:arguments:)is used (seeTransactionInputParser). So we should be able to handle different format specifiers. But if we wanted to do date formatting for example then string formats wouldn't be sufficient anymore.There are also a couple of other things to consider:
It's easy for errors to creep in here. When the list contains errors in the string format or the supplied argument names then it will be difficult to catch. We will need to think about how we can add further validation perhaps, and/or how to properly test updates in the string format list.