Skip to content

Commit 15a5e35

Browse files
authored
Merge pull request #98 from BANKEX/develop
Input parameters encoding for contracts
2 parents 2e37d03 + 883ecaa commit 15a5e35

27 files changed

+569
-42
lines changed

web3swift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@
144144
81C5DA2F2074EBF500424CD6 /* ContractABIv2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C5DA2D2074EBF500424CD6 /* ContractABIv2.swift */; };
145145
81C5DA312074EC1E00424CD6 /* ContractProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C5DA302074EC1E00424CD6 /* ContractProtocol.swift */; };
146146
81C5DA322074EC1E00424CD6 /* ContractProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C5DA302074EC1E00424CD6 /* ContractProtocol.swift */; };
147+
81D7D97220A31FB700A193EC /* ComparisonExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81D7D97120A31FB700A193EC /* ComparisonExtensions.swift */; };
148+
81D7D97320A31FB700A193EC /* ComparisonExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81D7D97120A31FB700A193EC /* ComparisonExtensions.swift */; };
149+
81D7D97520A3240900A193EC /* EthereumStringEncodingExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81D7D97420A3240900A193EC /* EthereumStringEncodingExtensions.swift */; };
150+
81D7D97620A3240900A193EC /* EthereumStringEncodingExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81D7D97420A3240900A193EC /* EthereumStringEncodingExtensions.swift */; };
147151
81DDECCF1FDF004E0063684A /* Web3.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81DDECCE1FDF004E0063684A /* Web3.swift */; };
148152
81EB1E4B208173D7003BD47F /* Web3+Personal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81EB1E4A208173D7003BD47F /* Web3+Personal.swift */; };
149153
81EB1E4C208173D7003BD47F /* Web3+Personal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81EB1E4A208173D7003BD47F /* Web3+Personal.swift */; };
@@ -275,6 +279,8 @@
275279
81C5DA2A2074CA1400424CD6 /* ABIv2Decoding.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ABIv2Decoding.swift; sourceTree = "<group>"; };
276280
81C5DA2D2074EBF500424CD6 /* ContractABIv2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContractABIv2.swift; sourceTree = "<group>"; };
277281
81C5DA302074EC1E00424CD6 /* ContractProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContractProtocol.swift; sourceTree = "<group>"; };
282+
81D7D97120A31FB700A193EC /* ComparisonExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComparisonExtensions.swift; sourceTree = "<group>"; };
283+
81D7D97420A3240900A193EC /* EthereumStringEncodingExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EthereumStringEncodingExtensions.swift; sourceTree = "<group>"; };
278284
81DDECCE1FDF004E0063684A /* Web3.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Web3.swift; sourceTree = "<group>"; };
279285
81EB1E4A208173D7003BD47F /* Web3+Personal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+Personal.swift"; sourceTree = "<group>"; };
280286
81FA43F32044097000EE14D5 /* web3swift-macOS_Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "web3swift-macOS_Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -539,6 +545,8 @@
539545
818EABD91FDC9A5C00E013FC /* Contract.swift */,
540546
81C5DA2D2074EBF500424CD6 /* ContractABIv2.swift */,
541547
81C5DA302074EC1E00424CD6 /* ContractProtocol.swift */,
548+
81D7D97120A31FB700A193EC /* ComparisonExtensions.swift */,
549+
81D7D97420A3240900A193EC /* EthereumStringEncodingExtensions.swift */,
542550
);
543551
path = Classes;
544552
sourceTree = "<group>";
@@ -1020,13 +1028,15 @@
10201028
8125F0662047D9FF00A0F2FE /* ABIEncoder.swift in Sources */,
10211029
8113D2C61FD7E1590074282C /* LibSecp256k1Extension.swift in Sources */,
10221030
81EB1E4B208173D7003BD47F /* Web3+Personal.swift in Sources */,
1031+
81D7D97520A3240900A193EC /* EthereumStringEncodingExtensions.swift in Sources */,
10231032
817EBB122004FE2F00E02EAA /* BIP32HDNode.swift in Sources */,
10241033
8113D2C41FD7E1590074282C /* CryptoExtensions.swift in Sources */,
10251034
8103BBB72076391F00499769 /* Web3+Concurrency.swift in Sources */,
10261035
81C5DA252072E14E00424CD6 /* ABIv2Encoding.swift in Sources */,
10271036
817EBB2920075D2E00E02EAA /* BIP39.swift in Sources */,
10281037
815630022007B53C00A0EC2F /* BIP32Keystore.swift in Sources */,
10291038
818ABD5B1FE95F8F002657BB /* Web3+Instance.swift in Sources */,
1039+
81D7D97220A31FB700A193EC /* ComparisonExtensions.swift in Sources */,
10301040
817EBB27200673D100E02EAA /* KeystoreV3JSONStructure.swift in Sources */,
10311041
81BEDE1D20852F75002CD87D /* Web3+PersonalOperations.swift in Sources */,
10321042
8103BBBD207654B900499769 /* Web3+ConversionOperations.swift in Sources */,
@@ -1107,13 +1117,15 @@
11071117
8125F0672047D9FF00A0F2FE /* ABIEncoder.swift in Sources */,
11081118
41948125203630530065A83B /* Web3+Methods.swift in Sources */,
11091119
81EB1E4C208173D7003BD47F /* Web3+Personal.swift in Sources */,
1120+
81D7D97620A3240900A193EC /* EthereumStringEncodingExtensions.swift in Sources */,
11101121
41948126203630530065A83B /* Web3+Eth.swift in Sources */,
11111122
8103BBB82076391F00499769 /* Web3+Concurrency.swift in Sources */,
11121123
81C5DA262072E14E00424CD6 /* ABIv2Encoding.swift in Sources */,
11131124
41948128203630530065A83B /* ABIDecoder.swift in Sources */,
11141125
41948129203630530065A83B /* ABITypes.swift in Sources */,
11151126
4194812B203630530065A83B /* RLP.swift in Sources */,
11161127
4194812C203630530065A83B /* KeystoreManager.swift in Sources */,
1128+
81D7D97320A31FB700A193EC /* ComparisonExtensions.swift in Sources */,
11171129
8103BBBE207654B900499769 /* Web3+ConversionOperations.swift in Sources */,
11181130
81BEDE1E20852F75002CD87D /* Web3+PersonalOperations.swift in Sources */,
11191131
81C5DA12207254F600424CD6 /* ABIv2.swift in Sources */,

web3swift/ABI/Classes/ABIEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ extension ABIElement {
202202
switch self {
203203
case .constructor(let constructor):
204204
guard parameters.count == constructor.inputs.count else {return nil}
205-
let allTypes = constructor.inputs.flatMap({ (input) -> ABIElement.ParameterType in
205+
let allTypes = constructor.inputs.compactMap({ (input) -> ABIElement.ParameterType in
206206
return input.type
207207
})
208208
guard let data = TypesEncoder.encode(types: allTypes, parameters: parameters) else {return nil}
@@ -213,7 +213,7 @@ extension ABIElement {
213213
return nil
214214
case .function(let function):
215215
guard parameters.count == function.inputs.count else {return nil}
216-
let allTypes = function.inputs.flatMap({ (input) -> ABIElement.ParameterType in
216+
let allTypes = function.inputs.compactMap({ (input) -> ABIElement.ParameterType in
217217
return input.type
218218
})
219219
let signature = function.methodEncoding

web3swift/ABIv2/Classes/ABIv2Decoding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct ABIv2Decoder {
1515

1616
extension ABIv2Decoder {
1717
public static func decode(types: [ABIv2.Element.InOut], data: Data) -> [AnyObject]? {
18-
let params = types.flatMap { (el) -> ABIv2.Element.ParameterType in
18+
let params = types.compactMap { (el) -> ABIv2.Element.ParameterType in
1919
return el.type
2020
}
2121
return decode(types: params, data: data)
@@ -222,7 +222,7 @@ extension ABIv2Decoder {
222222
let nonIndexedInputs = event.inputs.filter { (inp) -> Bool in
223223
return !inp.indexed
224224
}
225-
let nonIndexedTypes = nonIndexedInputs.flatMap { (inp) -> ABIv2.Element.ParameterType in
225+
let nonIndexedTypes = nonIndexedInputs.compactMap { (inp) -> ABIv2.Element.ParameterType in
226226
return inp.type
227227
}
228228
guard logs.count == indexedInputs.count + 1 else {return nil}

web3swift/ABIv2/Classes/ABIv2Elements.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,18 @@ extension ABIv2.Element {
142142
}
143143
}
144144

145-
func decodeInputData(_ data: Data) -> [String: Any]? {
145+
func decodeInputData(_ rawData: Data) -> [String: Any]? {
146+
var data = rawData
147+
var sig: Data? = nil
148+
switch rawData.count % 32 {
149+
case 0:
150+
break
151+
case 4:
152+
sig = rawData[0 ..< 4]
153+
data = Data(rawData[4 ..< rawData.count])
154+
default:
155+
return nil
156+
}
146157
switch self {
147158
case .constructor(let function):
148159
if (data.count == 0 && function.inputs.count == 1) {
@@ -174,6 +185,9 @@ extension ABIv2.Element {
174185
case .fallback(_):
175186
return nil
176187
case .function(let function):
188+
if sig != nil && sig != function.methodEncoding {
189+
return nil
190+
}
177191
if (data.count == 0 && function.inputs.count == 1) {
178192
let name = "0"
179193
let value = function.inputs[0].type.emptyValue

web3swift/ABIv2/Classes/ABIv2Encoding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ extension ABIv2Encoder {
138138

139139
public static func encode(types: [ABIv2.Element.InOut], values: [AnyObject]) -> Data? {
140140
guard types.count == values.count else {return nil}
141-
let params = types.flatMap { (el) -> ABIv2.Element.ParameterType in
141+
let params = types.compactMap { (el) -> ABIv2.Element.ParameterType in
142142
return el.type
143143
}
144144
return encode(types: params, values: values)

web3swift/ABIv2/Classes/ABIv2Parsing.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ extension ABIv2.Input {
129129
let name = self.name != nil ? self.name! : ""
130130
let parameterType = try ABIv2TypeParser.parseTypeString(self.type)
131131
if case .tuple(types: _) = parameterType {
132-
let components = try self.components?.flatMap({ (inp: ABIv2.Input) throws -> ABIv2.Element.ParameterType in
132+
let components = try self.components?.compactMap({ (inp: ABIv2.Input) throws -> ABIv2.Element.ParameterType in
133133
let input = try inp.parse()
134134
return input.type
135135
})
@@ -157,7 +157,7 @@ extension ABIv2.Output {
157157
let parameterType = try ABIv2TypeParser.parseTypeString(self.type)
158158
switch parameterType {
159159
case .tuple(types: _):
160-
let components = try self.components?.flatMap({ (inp: ABIv2.Output) throws -> ABIv2.Element.ParameterType in
160+
let components = try self.components?.compactMap({ (inp: ABIv2.Output) throws -> ABIv2.Element.ParameterType in
161161
let input = try inp.parse()
162162
return input.type
163163
})
@@ -167,7 +167,7 @@ extension ABIv2.Output {
167167
case .array(type: let subtype, length: let length):
168168
switch subtype {
169169
case .tuple(types: _):
170-
let components = try self.components?.flatMap({ (inp: ABIv2.Output) throws -> ABIv2.Element.ParameterType in
170+
let components = try self.components?.compactMap({ (inp: ABIv2.Output) throws -> ABIv2.Element.ParameterType in
171171
let input = try inp.parse()
172172
return input.type
173173
})

web3swift/Concurrency/Classes/Web3+Concurrency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class OperationDispatcher {
9090
if self.schedulingOperation != nil {
9191
self.schedulingOperation = nil
9292
}
93-
let allRequests = self.pendingRequests.flatMap { (r) -> Request in
93+
let allRequests = self.pendingRequests.compactMap { (r) -> Request in
9494
return r
9595
}
9696
self.pendingRequests.removeAll()

web3swift/Concurrency/Classes/Web3+EventOperations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ final class ParseTransactionForEventsOperation: Web3Operation {
144144
log.address == contract.address
145145
})
146146
}
147-
let decodedLogs = allLogs.flatMap({ (log) -> EventParserResultProtocol? in
147+
let decodedLogs = allLogs.compactMap({ (log) -> EventParserResultProtocol? in
148148
let (n, d) = contract.parseEvent(log)
149149
guard let evName = n, let evData = d else {return nil}
150150
return EventParserResult(eventName: evName, transactionReceipt: receipt, contractAddress: log.address, decodedResult: evData)
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// ComparisonExtensions.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 09.05.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import BigInt
11+
12+
extension BigUInt: EventFilterComparable {
13+
public func isEqualTo(_ other: AnyObject) -> Bool {
14+
switch other {
15+
case let oth as BigUInt:
16+
return self == oth
17+
case let oth as BigInt:
18+
return self.magnitude == oth.magnitude && self.signum() == oth.signum()
19+
default:
20+
return false
21+
}
22+
}
23+
}
24+
25+
extension BigInt: EventFilterComparable {
26+
public func isEqualTo(_ other: AnyObject) -> Bool {
27+
switch other {
28+
case let oth as BigInt:
29+
return self == oth
30+
case let oth as BigUInt:
31+
return self.magnitude == oth.magnitude && self.signum() == oth.signum()
32+
default:
33+
return false
34+
}
35+
}
36+
}
37+
38+
extension String: EventFilterComparable {
39+
public func isEqualTo(_ other: AnyObject) -> Bool {
40+
switch other {
41+
case let oth as String:
42+
guard let data = self.data(using: .utf8) else {return false}
43+
guard let otherData = oth.data(using: .utf8) else {return false}
44+
let hash = data.sha3(.keccak256)
45+
let otherHash = otherData.sha3(.keccak256)
46+
return hash == otherHash
47+
case let oth as Data:
48+
guard let data = self.data(using: .utf8) else {return false}
49+
let hash = data.sha3(.keccak256)
50+
let otherHash = oth.sha3(.keccak256)
51+
return hash == otherHash
52+
default:
53+
return false
54+
}
55+
}
56+
}
57+
58+
extension Data: EventFilterComparable {
59+
public func isEqualTo(_ other: AnyObject) -> Bool {
60+
switch other {
61+
case let oth as String:
62+
guard let data = Data.fromHex(oth) else {return false}
63+
if self == data {
64+
return true
65+
}
66+
let hash = data.sha3(.keccak256)
67+
return self == hash
68+
case let oth as Data:
69+
if self == oth {
70+
return true
71+
}
72+
let hash = oth.sha3(.keccak256)
73+
return self == hash
74+
default:
75+
return false
76+
}
77+
}
78+
}
79+
80+
extension EthereumAddress: EventFilterComparable {
81+
public func isEqualTo(_ other: AnyObject) -> Bool {
82+
switch other {
83+
case let oth as String:
84+
let addr = EthereumAddress(oth)
85+
return self == addr
86+
case let oth as Data:
87+
let addr = EthereumAddress(oth)
88+
return self == addr
89+
case let oth as EthereumAddress:
90+
return self == oth
91+
default:
92+
return false
93+
}
94+
}
95+
}
96+

web3swift/Contract/Classes/Contract.swift

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

12+
@available(*, deprecated)
1213
public struct Contract:ContractProtocol {
1314

1415
public var allEvents: [String] {
@@ -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
}

0 commit comments

Comments
 (0)