Skip to content

Commit 90c9eff

Browse files
author
Alex Vlasov
committed
convert the rest of structures to swift 4 style deserialization
1 parent a8b0bbe commit 90c9eff

12 files changed

+405
-41
lines changed

web3swift.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@
145145
81A1824320D7AA750016741F /* Promise+Web3+Eth+SendTransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824120D7AA750016741F /* Promise+Web3+Eth+SendTransaction.swift */; };
146146
81A1824520D7B91B0016741F /* Promise+Web3+Intermediate+Send.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824420D7B91B0016741F /* Promise+Web3+Intermediate+Send.swift */; };
147147
81A1824620D7B91B0016741F /* Promise+Web3+Intermediate+Send.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824420D7B91B0016741F /* Promise+Web3+Intermediate+Send.swift */; };
148+
81A1824820D7DDA20016741F /* Promise+Web3+Personal+Sign.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824720D7DDA20016741F /* Promise+Web3+Personal+Sign.swift */; };
149+
81A1824920D7DDA20016741F /* Promise+Web3+Personal+Sign.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824720D7DDA20016741F /* Promise+Web3+Personal+Sign.swift */; };
150+
81A1824B20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824A20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift */; };
151+
81A1824C20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81A1824A20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift */; };
148152
81BEDE1D20852F75002CD87D /* Web3+PersonalOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81BEDE1C20852F75002CD87D /* Web3+PersonalOperations.swift */; };
149153
81BEDE1E20852F75002CD87D /* Web3+PersonalOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81BEDE1C20852F75002CD87D /* Web3+PersonalOperations.swift */; };
150154
81C0FCF220440EB500D82FAF /* Web3+Protocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C0FCF120440EB500D82FAF /* Web3+Protocols.swift */; };
@@ -311,6 +315,8 @@
311315
81A1823F20D79FDB0016741F /* Promise+Web3+Eth+EstimateGas.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+Eth+EstimateGas.swift"; sourceTree = "<group>"; };
312316
81A1824120D7AA750016741F /* Promise+Web3+Eth+SendTransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+Eth+SendTransaction.swift"; sourceTree = "<group>"; };
313317
81A1824420D7B91B0016741F /* Promise+Web3+Intermediate+Send.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+Intermediate+Send.swift"; sourceTree = "<group>"; };
318+
81A1824720D7DDA20016741F /* Promise+Web3+Personal+Sign.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+Personal+Sign.swift"; sourceTree = "<group>"; };
319+
81A1824A20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Promise+Web3+Personal+UnlockAccount.swift"; sourceTree = "<group>"; };
314320
81BEDE1C20852F75002CD87D /* Web3+PersonalOperations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+PersonalOperations.swift"; sourceTree = "<group>"; };
315321
81C0FCEF20440C3600D82FAF /* Web3+Wallet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Web3+Wallet.swift"; sourceTree = "<group>"; };
316322
81C0FCF120440EB500D82FAF /* Web3+Protocols.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+Protocols.swift"; sourceTree = "<group>"; };
@@ -633,6 +639,8 @@
633639
81A1823F20D79FDB0016741F /* Promise+Web3+Eth+EstimateGas.swift */,
634640
81A1824120D7AA750016741F /* Promise+Web3+Eth+SendTransaction.swift */,
635641
81A1824420D7B91B0016741F /* Promise+Web3+Intermediate+Send.swift */,
642+
81A1824720D7DDA20016741F /* Promise+Web3+Personal+Sign.swift */,
643+
81A1824A20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift */,
636644
);
637645
path = Classes;
638646
sourceTree = "<group>";
@@ -1048,6 +1056,7 @@
10481056
8125F0662047D9FF00A0F2FE /* ABIEncoder.swift in Sources */,
10491057
8113D2C61FD7E1590074282C /* LibSecp256k1Extension.swift in Sources */,
10501058
81EB1E4B208173D7003BD47F /* Web3+Personal.swift in Sources */,
1059+
81A1824B20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift in Sources */,
10511060
81A1823A20D79AD40016741F /* Promise+Web3+Eth+SendRawTransaction.swift in Sources */,
10521061
81D7D97520A3240900A193EC /* EthereumFilterEncodingExtensions.swift in Sources */,
10531062
817EBB122004FE2F00E02EAA /* BIP32HDNode.swift in Sources */,
@@ -1091,6 +1100,7 @@
10911100
81166665204565AC008D8AD0 /* Web3+BrowserFunctions.swift in Sources */,
10921101
81531AA01FE7C07A002192CC /* EthereumKeystoreV3.swift in Sources */,
10931102
8103BBBA20764A6900499769 /* Web3+DataFetchOperation.swift in Sources */,
1103+
81A1824820D7DDA20016741F /* Promise+Web3+Personal+Sign.swift in Sources */,
10941104
8123E1C9200CBAF800B6D3AB /* Data+Extension.swift in Sources */,
10951105
81C5DA1B2072633300424CD6 /* ABIv2ParameterTypes.swift in Sources */,
10961106
81FB21EE2077D98A007F9A83 /* SynchronizedQueue.swift in Sources */,
@@ -1190,6 +1200,7 @@
11901200
81FB21F22078142D007F9A83 /* Web3+Deprecated.swift in Sources */,
11911201
41948131203630530065A83B /* BIP32KeystoreJSONStructure.swift in Sources */,
11921202
41948132203630530065A83B /* BIP32HDNode.swift in Sources */,
1203+
81A1824920D7DDA20016741F /* Promise+Web3+Personal+Sign.swift in Sources */,
11931204
81A1822620D678590016741F /* Promise+Web3+Eth+GetGasPrice.swift in Sources */,
11941205
41948133203630530065A83B /* BIP39.swift in Sources */,
11951206
81C5DA2F2074EBF500424CD6 /* ContractABIv2.swift in Sources */,
@@ -1209,6 +1220,7 @@
12091220
81A1822920D678BF0016741F /* Promise+Web3+Eth+GetAccounts.swift in Sources */,
12101221
81C5DA322074EC1E00424CD6 /* ContractProtocol.swift in Sources */,
12111222
81C5DA232072DFE600424CD6 /* ABIv2TypeParser.swift in Sources */,
1223+
81A1824C20D7DF1B0016741F /* Promise+Web3+Personal+UnlockAccount.swift in Sources */,
12121224
41948139203630530065A83B /* LibSecp256k1Extension.swift in Sources */,
12131225
4194813A203630530065A83B /* DictionaryLiteralJSONSerializer.swift in Sources */,
12141226
4194813B203630530065A83B /* NSRegularExpressionExtension.swift in Sources */,

web3swift/Promises/Classes/Promise+Web3+Eth+GetBlockByHash.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ extension web3.Eth {
2121
let rp = web3.dispatch(request)
2222
let queue = web3.requestDispatcher.queue
2323
return rp.map(on: queue ) { response in
24-
guard let value: [String: AnyObject] = response.getValue() else {
24+
guard let value: Block = response.getValue() else {
2525
throw Web3Error.nodeError("Invalid value from Ethereum node")
2626
}
27-
let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
28-
let details = try JSONDecoder().decode(Block.self, from: reencoded)
29-
return details
27+
return value
28+
// let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
29+
// let details = try JSONDecoder().decode(Block.self, from: reencoded)
30+
// return details
3031
}
3132
}
3233
}

web3swift/Promises/Classes/Promise+Web3+Eth+GetBlockByNumber.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ extension web3.Eth {
2626
let rp = web3.dispatch(request)
2727
let queue = web3.requestDispatcher.queue
2828
return rp.map(on: queue ) { response in
29-
guard let value: [String: AnyObject] = response.getValue() else {
29+
guard let value: Block = response.getValue() else {
3030
throw Web3Error.nodeError("Invalid value from Ethereum node")
3131
}
32-
let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
33-
let details = try JSONDecoder().decode(Block.self, from: reencoded)
34-
return details
32+
return value
33+
// let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
34+
// let details = try JSONDecoder().decode(Block.self, from: reencoded)
35+
// return details
3536
}
3637
}
3738
}

web3swift/Promises/Classes/Promise+Web3+Eth+GetTransactionDetails.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ extension web3.Eth {
2121
let rp = web3.dispatch(request)
2222
let queue = web3.requestDispatcher.queue
2323
return rp.map(on: queue ) { response in
24-
guard let value: [String: AnyObject] = response.getValue() else {
24+
guard let value: TransactionDetails = response.getValue() else {
2525
throw Web3Error.nodeError("Invalid value from Ethereum node")
2626
}
27+
return value
2728
// guard let details = TransactionDetails(value) else {
2829
// throw Web3Error.processingError("Can not deserialize transaction details")
2930
// }
3031
// return details
31-
let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
32-
let details = try JSONDecoder().decode(TransactionDetails.self, from: reencoded)
33-
return details
32+
// let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
33+
// let details = try JSONDecoder().decode(TransactionDetails.self, from: reencoded)
34+
// return details
3435
}
3536
}
3637
}

web3swift/Promises/Classes/Promise+Web3+Eth+GetTransactionReceipt.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ extension web3.Eth {
2121
let rp = web3.dispatch(request)
2222
let queue = web3.requestDispatcher.queue
2323
return rp.map(on: queue ) { response in
24-
guard let value: [String: AnyObject] = response.getValue() else {
24+
guard let value: TransactionReceipt = response.getValue() else {
2525
throw Web3Error.nodeError("Invalid value from Ethereum node")
2626
}
27+
return value
2728
// guard let details = TransactionReceipt(value) else {
2829
// throw Web3Error.processingError("Can not deserialize transaction details")
2930
// }
3031
// return details
31-
let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
32-
let details = try JSONDecoder().decode(TransactionReceipt.self, from: reencoded)
33-
return details
32+
// let reencoded = try JSONSerialization.data(withJSONObject: value, options: JSONSerialization.WritingOptions(rawValue: 0))
33+
// let details = try JSONDecoder().decode(TransactionReceipt.self, from: reencoded)
34+
// return details
3435
}
3536
}
3637
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Promise+Web3+Personal+Sign.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 18.06.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import BigInt
11+
import PromiseKit
12+
13+
extension web3.Personal {
14+
15+
func sendPersonalMessagePromise(message: Data, from: EthereumAddress, password:String = "BANKEXFOUNDATION") -> Promise<Data> {
16+
let queue = web3.requestDispatcher.queue
17+
do {
18+
if self.web3.provider.attachedKeystoreManager == nil {
19+
let hexData = message.toHexString().addHexPrefix()
20+
let request = JSONRPCRequestFabric.prepareRequest(.personalSign, parameters: [from.address.lowercased(), hexData])
21+
return self.web3.dispatch(request).map(on: queue) {response in
22+
guard let value: Data = response.getValue() else {
23+
throw Web3Error.nodeError("Invalid value from Ethereum node")
24+
}
25+
return value
26+
}
27+
}
28+
guard let signature = try Web3Signer.signPersonalMessage(message, keystore: self.web3.provider.attachedKeystoreManager!, account: from, password: password) else { throw Web3Error.inputError("Failed to locally sign a message") }
29+
let returnPromise = Promise<Data>.pending()
30+
queue.async {
31+
returnPromise.resolver.fulfill(signature)
32+
}
33+
return returnPromise.promise
34+
} catch {
35+
let returnPromise = Promise<Data>.pending()
36+
queue.async {
37+
returnPromise.resolver.reject(error)
38+
}
39+
return returnPromise.promise
40+
}
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// Promise+Web3+Personal+UnlockAccount.swift
3+
// web3swift
4+
//
5+
// Created by Alexander Vlasov on 18.06.2018.
6+
// Copyright © 2018 Bankex Foundation. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import BigInt
11+
import PromiseKit
12+
13+
extension web3.Personal {
14+
func unlockAccountPromise(account: EthereumAddress, password:String = "BANKEXFOUNDATION", seconds: UInt64 = 300) -> Promise<Bool> {
15+
let addr = account.address
16+
return unlockAccountPromise(account: addr, password: password, seconds: seconds)
17+
}
18+
19+
20+
func unlockAccountPromise(account: String, password:String = "BANKEXFOUNDATION", seconds: UInt64 = 300) -> Promise<Bool> {
21+
let queue = web3.requestDispatcher.queue
22+
do {
23+
if self.web3.provider.attachedKeystoreManager == nil {
24+
let request = JSONRPCRequestFabric.prepareRequest(.unlockAccount, parameters: [account.lowercased(), password, seconds])
25+
return self.web3.dispatch(request).map(on: queue) {response in
26+
guard let value: Bool = response.getValue() else {
27+
throw Web3Error.nodeError("Invalid value from Ethereum node")
28+
}
29+
return value
30+
}
31+
}
32+
throw Web3Error.inputError("Can not unlock a local keystore")
33+
} catch {
34+
let returnPromise = Promise<Bool>.pending()
35+
queue.async {
36+
returnPromise.resolver.reject(error)
37+
}
38+
return returnPromise.promise
39+
}
40+
}
41+
}

web3swift/Web3/Classes/Web3+EventParser.swift

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
//
88

99
import Foundation
10-
import Result
10+
import enum Result.Result
1111
import BigInt
12+
import PromiseKit
13+
fileprivate typealias PromiseResult = PromiseKit.Result
1214

1315
extension web3.web3contract {
1416
public struct EventParser: EventParserProtocol {
@@ -147,3 +149,70 @@ extension web3.web3contract {
147149
}
148150
}
149151
}
152+
153+
extension web3.web3contract {
154+
public func getIndexedEventsPromise(eventName: String?, filter: EventFilter, joinWithReceipts: Bool = false) -> Promise<[EventParserResultProtocol]> {
155+
let queue = self.web3.requestDispatcher.queue
156+
do {
157+
guard let rawContract = self.contract as? ContractV2 else {
158+
throw Web3Error.nodeError("ABIv1 is not supported for this method")
159+
}
160+
guard let preEncoding = encodeTopicToGetLogs(contract: rawContract, eventName: eventName, filter: filter) else {
161+
throw Web3Error.processingError("Failed to encode topic for request")
162+
}
163+
// var event: ABIv2.Element.Event? = nil
164+
if eventName != nil {
165+
guard let _ = rawContract.events[eventName!] else {
166+
throw Web3Error.processingError("No such event in a contract")
167+
}
168+
// event = ev
169+
}
170+
let request = JSONRPCRequestFabric.prepareRequest(.getLogs, parameters: [preEncoding])
171+
let fetchLogsPromise = self.web3.dispatch(request).map(on: queue) {response throws -> [EventParserResult] in
172+
guard let value: [EventLog] = response.getValue() else {
173+
throw Web3Error.nodeError("Empty or malformed response")
174+
}
175+
let allLogs = value
176+
let decodedLogs = allLogs.compactMap({ (log) -> EventParserResult? in
177+
let (n, d) = self.contract.parseEvent(log)
178+
guard let evName = n, let evData = d else {return nil}
179+
var res = EventParserResult(eventName: evName, transactionReceipt: nil, contractAddress: log.address, decodedResult: evData)
180+
res.transactionHash = log.transactionHash
181+
return res
182+
}).filter{ (res:EventParserResult?) -> Bool in
183+
if eventName != nil {
184+
if res != nil && res?.eventName == eventName && res?.transactionHash != nil {
185+
return true
186+
}
187+
} else {
188+
if res != nil && res?.transactionHash != nil {
189+
return true
190+
}
191+
}
192+
return false
193+
}
194+
return decodedLogs
195+
}
196+
if (!joinWithReceipts) {
197+
return fetchLogsPromise.mapValues(on: queue) {res -> EventParserResultProtocol in
198+
return res as EventParserResultProtocol
199+
}
200+
}
201+
return fetchLogsPromise.thenMap(on:queue) {singleEvent in
202+
return self.web3.eth.getTransactionReceiptPromise(singleEvent.transactionHash!).map(on: queue) { receipt in
203+
var joinedEvent = singleEvent
204+
joinedEvent.transactionReceipt = receipt
205+
return joinedEvent as EventParserResultProtocol
206+
}
207+
}
208+
} catch {
209+
let returnPromise = Promise<[EventParserResultProtocol]>.pending()
210+
queue.async {
211+
returnPromise.resolver.reject(error)
212+
}
213+
return returnPromise.promise
214+
}
215+
}
216+
}
217+
218+

0 commit comments

Comments
 (0)