Skip to content

Commit 883ecaa

Browse files
author
Alex Vlasov
committed
input data decoding without method name
1 parent cac6c6a commit 883ecaa

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

web3swift/Contract/Classes/Contract.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import BigInt
1111

1212
@available(*, deprecated)
1313
public struct Contract:ContractProtocol {
14+
1415
public var allEvents: [String] {
1516
return events.keys.flatMap({ (s) -> String in
1617
return s
@@ -210,4 +211,8 @@ public struct Contract:ContractProtocol {
210211
public func decodeInputData(_ method: String, data: Data) -> [String : Any]? {
211212
return nil
212213
}
214+
215+
public func decodeInputData(_ data: Data) -> [String : Any]? {
216+
return nil
217+
}
213218
}

web3swift/Contract/Classes/ContractABIv2.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ public struct ContractV2:ContractProtocol {
232232

233233
public func decodeInputData(_ method: String, data: Data) -> [String : Any]? {
234234
if method == "fallback" {
235-
return [String:Any]()
235+
return nil
236236
}
237237
guard let function = methods[method] else {return nil}
238238
switch function {
@@ -241,7 +241,25 @@ public struct ContractV2:ContractProtocol {
241241
case .constructor(_):
242242
return function.decodeInputData(data)
243243
default:
244-
return [String:Any]()
244+
return nil
245+
}
246+
}
247+
248+
public func decodeInputData(_ data: Data) -> [String:Any]? {
249+
guard data.count % 32 == 4 else {return nil}
250+
let methodSignature = data[0..<4]
251+
let foundFunction = self._abi.filter { (m) -> Bool in
252+
switch m {
253+
case .function(let function):
254+
return function.methodEncoding == methodSignature
255+
default:
256+
return false
257+
}
258+
}
259+
guard foundFunction.count == 1 else {
260+
return nil
245261
}
262+
let function = foundFunction[0]
263+
return function.decodeInputData(Data(data[4 ..< data.count]))
246264
}
247265
}

web3swift/Contract/Classes/ContractProtocol.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public protocol ContractProtocol {
1919
init?(_ abiString: String, at: EthereumAddress?)
2020
func decodeReturnData(_ method:String, data: Data) -> [String:Any]?
2121
func decodeInputData(_ method:String, data: Data) -> [String:Any]?
22+
func decodeInputData(_ data: Data) -> [String:Any]?
2223
func parseEvent(_ eventLog: EventLog) -> (eventName:String?, eventData:[String:Any]?)
2324
func testBloomForEventPrecence(eventName: String, bloom: EthereumBloomFilter) -> Bool?
2425
// func allEvents() -> [String: [String: Any]?]

web3swiftTests/web3swiftTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,13 @@ class web3swiftTests: XCTestCase {
21482148
XCTAssert(decoded!["_to"] as? EthereumAddress == EthereumAddress("0xcdd45864e794fe5e3e1b0045b77e62f4c43b8bd9"))
21492149
}
21502150

2151+
func testDecodeInputDataWithoutMethodName() {
2152+
let contract = ContractV2.init(Web3.Utils.erc20ABI)!
2153+
let dataToDecode = Data.fromHex("0xa9059cbb000000000000000000000000cdd45864e794fe5e3e1b0045b77e62f4c43b8bd9000000000000000000000000000000000000000000000224b5f018c3e30142d5")!
2154+
let decoded = contract.decodeInputData(dataToDecode)
2155+
XCTAssert(decoded!["_to"] as? EthereumAddress == EthereumAddress("0xcdd45864e794fe5e3e1b0045b77e62f4c43b8bd9"))
2156+
}
2157+
21512158
func testPerformanceExample() {
21522159
// This is an example of a performance test case.
21532160
self.measure {

0 commit comments

Comments
 (0)