Skip to content

Commit bce743e

Browse files
Merge pull request #756 from JeneaVranceanu/chore/swift-lint-cleanup
chore: swift lint cleanup
2 parents 9bd3042 + 809d1d8 commit bce743e

24 files changed

+370
-254
lines changed

Sources/Web3Core/EthereumABI/ABIElements.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ extension ABI.Element.Function {
215215

216216
extension ABI.Element.Event {
217217
public func decodeReturnedLogs(eventLogTopics: [Data], eventLogData: Data) -> [String: Any]? {
218-
guard let eventContent = ABIDecoder.decodeLog(event: self, eventLogTopics: eventLogTopics, eventLogData: eventLogData) else {return nil}
218+
guard let eventContent = ABIDecoder.decodeLog(event: self, eventLogTopics: eventLogTopics, eventLogData: eventLogData) else { return nil }
219219
return eventContent
220220
}
221221
}
@@ -467,7 +467,6 @@ extension ABIDecoder {
467467
}
468468

469469
var returnArray = [String: Any]()
470-
471470
if data.count == 0 && inputs.count == 1 {
472471
let name = "0"
473472
let value = inputs[0].type.emptyValue

Sources/Web3Core/KeystoreManager/EthereumKeystoreV3.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import Foundation
77
import CryptoSwift
88

9+
// swiftlint:disable cyclomatic_complexity
910
public class EthereumKeystoreV3: AbstractKeystore {
1011
// Protocol
1112
public var isHDKeystore: Bool = false

Sources/Web3Core/Structure/SECP256k1.swift

Lines changed: 64 additions & 59 deletions
Large diffs are not rendered by default.

Sources/web3swift/Tokens/ERC1155/Web3+ERC1155.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ public class ERC1155: IERC1155 {
4141
public var abi: String
4242

4343
lazy var contract: Web3.Contract = {
44+
// swiftlint:disable force_unwrapping
4445
let contract = self.web3.contract(self.abi, at: self.address, abiVersion: 2)
4546
precondition(contract != nil)
4647
return contract!
48+
// swiftlint:enable force_unwrapping
4749
}()
4850

4951
public init(web3: Web3, provider: Web3Provider, address: EthereumAddress, abi: String = Web3.Utils.erc1155ABI, transaction: CodableTransaction = .emptyTransaction) {
@@ -67,11 +69,11 @@ public class ERC1155: IERC1155 {
6769
if self._hasReadProperties {
6870
return
6971
}
70-
guard contract.contract.address != nil else {return}
72+
guard contract.contract.address != nil else { return }
7173

72-
guard let tokenIdPromise = try await contract.createReadOperation("id")?.callContractMethod() else {return}
74+
guard let tokenIdPromise = try await contract.createReadOperation("id")?.callContractMethod() else { return }
7375

74-
guard let tokenId = tokenIdPromise["0"] as? BigUInt else {return}
76+
guard let tokenId = tokenIdPromise["0"] as? BigUInt else { return }
7577
self._tokenId = tokenId
7678

7779
self._hasReadProperties = true
@@ -94,7 +96,7 @@ public class ERC1155: IERC1155 {
9496
let result = try await contract
9597
.createReadOperation("balanceOf", parameters: [account, id])!
9698
.callContractMethod()
97-
guard let res = result["0"] as? BigUInt else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
99+
guard let res = result["0"] as? BigUInt else { throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node") }
98100
return res
99101
}
100102

@@ -107,14 +109,14 @@ public class ERC1155: IERC1155 {
107109
public func isApprovedForAll(owner: EthereumAddress, operator user: EthereumAddress, scope: Data) async throws -> Bool {
108110
let result = try await contract.createReadOperation("isApprovedForAll", parameters: [owner, user, scope])!.callContractMethod()
109111

110-
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
112+
guard let res = result["0"] as? Bool else { throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node") }
111113
return res
112114
}
113115

114116
public func supportsInterface(interfaceID: String) async throws -> Bool {
115117
let result = try await contract.createReadOperation("supportsInterface", parameters: [interfaceID])!.callContractMethod()
116118

117-
guard let res = result["0"] as? Bool else {throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node")}
119+
guard let res = result["0"] as? Bool else { throw Web3Error.processingError(desc: "Failed to get result of expected type from the Ethereum node") }
118120
return res
119121
}
120122
}

Sources/web3swift/Tokens/ERC721/Web3+ERC721.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ public class ERC721: IERC721 {
6363
public var address: EthereumAddress
6464

6565
lazy var contract: Web3.Contract = {
66+
// swiftlint:disable force_unwrapping
6667
let contract = self.web3.contract(Web3.Utils.erc721ABI, at: self.address, abiVersion: 2)
6768
precondition(contract != nil)
6869
return contract!
70+
// swiftlint:enable force_unwrapping
6971
}()
7072

7173
public init(web3: Web3, provider: Web3Provider, address: EthereumAddress, transaction: CodableTransaction = .emptyTransaction) {
@@ -88,12 +90,12 @@ public class ERC721: IERC721 {
8890
if self._hasReadProperties {
8991
return
9092
}
91-
guard contract.contract.address != nil else {return}
93+
guard contract.contract.address != nil else { return }
9294

9395
async let tokenIdPromise = contract.createReadOperation("tokenId")?.callContractMethod()
9496

95-
guard let tokenIdResult = try await tokenIdPromise else {return}
96-
guard let tokenId = tokenIdResult["0"] as? BigUInt else {return}
97+
guard let tokenIdResult = try await tokenIdPromise else { return }
98+
guard let tokenId = tokenIdResult["0"] as? BigUInt else { return }
9799
self._tokenId = tokenId
98100

99101
self._hasReadProperties = true

Sources/web3swift/Tokens/ERC721x/Web3+ERC721x.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ public class ERC721x: IERC721x {
4747
public var abi: String
4848

4949
lazy var contract: Web3.Contract = {
50+
// swiftlint:disable force_unwrapping
5051
let contract = self.web3.contract(self.abi, at: self.address, abiVersion: 2)
5152
precondition(contract != nil)
5253
return contract!
54+
// swiftlint:enable force_unwrapping
5355
}()
5456

5557
public init(web3: Web3, provider: Web3Provider, address: EthereumAddress, abi: String = Web3.Utils.erc721xABI, transaction: CodableTransaction = .emptyTransaction) {
@@ -73,12 +75,12 @@ public class ERC721x: IERC721x {
7375
if self._hasReadProperties {
7476
return
7577
}
76-
guard contract.contract.address != nil else {return}
78+
guard contract.contract.address != nil else { return }
7779
transaction.callOnBlock = .latest
7880

79-
guard let tokenIdPromise = try await contract.createReadOperation("tokenId")?.callContractMethod() else {return}
81+
guard let tokenIdPromise = try await contract.createReadOperation("tokenId")?.callContractMethod() else { return }
8082

81-
guard let tokenId = tokenIdPromise["0"] as? BigUInt else {return}
83+
guard let tokenId = tokenIdPromise["0"] as? BigUInt else { return }
8284
self._tokenId = tokenId
8385

8486
self._hasReadProperties = true

Sources/web3swift/Utils/EIP/EIP4361.swift

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ private let uriPattern = "(([^:?#\\s]+):)?(([^?#\\s]*))?([^?#\\s]*)(\\?([^#\\s]*
4646
public final class EIP4361 {
4747

4848
public enum EIP4361Field: String {
49-
case domain = "domain"
50-
case address = "address"
51-
case statement = "statement"
52-
case uri = "uri"
53-
case version = "version"
54-
case chainId = "chainId"
55-
case nonce = "nonce"
56-
case issuedAt = "issuedAt"
57-
case expirationTime = "expirationTime"
58-
case notBefore = "notBefore"
59-
case requestId = "requestId"
60-
case resources = "resources"
49+
case domain
50+
case address
51+
case statement
52+
case uri
53+
case version
54+
case chainId
55+
case nonce
56+
case issuedAt
57+
case expirationTime
58+
case notBefore
59+
case requestId
60+
case resources
6161
}
6262

6363
private static let domain = "(?<\(EIP4361Field.domain.rawValue)>([^?#]*)) wants you to sign in with your Ethereum account:"
@@ -77,9 +77,9 @@ public final class EIP4361 {
7777
"^\(domain)\(address)\(statementParagraph)\(uri)\(version)\(chainId)\(nonce)\(issuedAt)\(expirationTime)\(notBefore)\(requestId)\(resourcesParagraph)$"
7878
}
7979

80-
private static var _eip4361OptionalPattern: String!
80+
private static var _eip4361OptionalPattern: String?
8181
private static var eip4361OptionalPattern: String {
82-
guard _eip4361OptionalPattern == nil else { return _eip4361OptionalPattern! }
82+
if let _eip4361OptionalPattern = _eip4361OptionalPattern { return _eip4361OptionalPattern }
8383

8484
let domain = "(?<\(EIP4361Field.domain.rawValue)>(.*)) wants you to sign in with your Ethereum account:"
8585
let address = "\\n(?<\(EIP4361Field.address.rawValue)>.*)\\n\\n"
@@ -106,20 +106,27 @@ public final class EIP4361 {
106106
"\(requestId)",
107107
"\(resourcesParagraph)$"]
108108

109-
_eip4361OptionalPattern = patternParts.joined()
110-
return _eip4361OptionalPattern!
109+
let eip4361OptionalPattern = patternParts.joined()
110+
_eip4361OptionalPattern = eip4361OptionalPattern
111+
return eip4361OptionalPattern
111112
}
112113

113114
public static func validate(_ message: String) -> EIP4361ValidationResponse {
115+
// swiftlint:disable force_try
114116
let siweConstantMessageRegex = try! NSRegularExpression(pattern: "^\(domain)\\n")
115117
guard siweConstantMessageRegex.firstMatch(in: message, range: message.fullNSRange) != nil else {
116118
return EIP4361ValidationResponse(isEIP4361: false, eip4361: nil, capturedFields: [:])
117119
}
118120

119121
let eip4361Regex = try! NSRegularExpression(pattern: eip4361OptionalPattern)
122+
// swiftlint:enable force_try
120123
var capturedFields: [EIP4361Field: String] = [:]
121124
for (key, value) in eip4361Regex.captureGroups(string: message) {
125+
/// We are using EIP4361Field.rawValue to create regular expression.
126+
/// These values must decode back from raw representation always.
127+
// swiftlint:disable force_unwrapping
122128
capturedFields[.init(rawValue: key)!] = value
129+
// swiftlint:enable force_unwrapping
123130
}
124131
return EIP4361ValidationResponse(isEIP4361: true,
125132
eip4361: EIP4361(message),
@@ -153,7 +160,9 @@ public final class EIP4361 {
153160
public let resources: [URL]?
154161

155162
public init?(_ message: String) {
163+
// swiftlint:disable force_try
156164
let eip4361Regex = try! NSRegularExpression(pattern: EIP4361.eip4361Pattern)
165+
// swiftlint:enable force_try
157166
let groups = eip4361Regex.captureGroups(string: message)
158167
let dateFormatter = ISO8601DateFormatter()
159168
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]

Sources/web3swift/Utils/EIP/EIP67Code.swift

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension Web3 {
2525
public var parameters: [(ABI.Element.ParameterType, Any)]
2626

2727
public func toString() -> String? {
28-
let encoding = method + "(" + parameters.map({ el -> String in
28+
let encoding = method + "(" + parameters.map { el -> String in
2929
if let string = el.1 as? String {
3030
return el.0.abiRepresentation + " " + string
3131
} else if let number = el.1 as? BigUInt {
@@ -36,7 +36,7 @@ extension Web3 {
3636
return el.0.abiRepresentation + " " + data.toHexString().addHexPrefix()
3737
}
3838
return ""
39-
}).joined(separator: ", ") + ")"
39+
}.joined(separator: ", ") + ")"
4040
return encoding
4141
}
4242
}
@@ -46,7 +46,7 @@ extension Web3 {
4646
}
4747

4848
public init? (address: String) {
49-
guard let addr = EthereumAddress(address) else {return nil}
49+
guard let addr = EthereumAddress(address) else { return nil }
5050
self.address = addr
5151
}
5252

@@ -97,40 +97,41 @@ extension Web3 {
9797

9898
public struct EIP67CodeParser {
9999
public static func parse(_ data: Data) -> EIP67Code? {
100-
guard let string = String(data: data, encoding: .utf8) else {return nil}
100+
guard let string = String(data: data, encoding: .utf8) else { return nil }
101101
return parse(string)
102102
}
103103

104104
public static func parse(_ string: String) -> EIP67Code? {
105-
guard string.hasPrefix("ethereum:") else {return nil}
105+
guard string.hasPrefix("ethereum:") else { return nil }
106106
let striped = string.components(separatedBy: "ethereum:")
107-
guard striped.count == 2 else {return nil}
108-
guard let encoding = striped[1].removingPercentEncoding else {return nil}
109-
guard let url = URL.init(string: encoding) else {return nil}
110-
guard let address = EthereumAddress(url.lastPathComponent) else {return nil}
107+
guard striped.count == 2,
108+
let encoding = striped[1].removingPercentEncoding,
109+
let url = URL.init(string: encoding),
110+
let address = EthereumAddress(url.lastPathComponent) else { return nil }
111111
var code = EIP67Code(address: address)
112-
guard let components = URLComponents(string: encoding)?.queryItems else {return code}
112+
parseEncodingComponents(&code, encoding)
113+
return code
114+
}
115+
116+
private static func parseEncodingComponents(_ code: inout EIP67Code, _ encoding: String) {
117+
guard let components = URLComponents(string: encoding)?.queryItems else { return }
113118
for comp in components {
114119
switch comp.name {
115120
case "value":
116-
guard let value = comp.value else {return nil}
117-
guard let val = BigUInt(value, radix: 10) else {return nil}
121+
guard let value = comp.value, let val = BigUInt(value, radix: 10) else { return }
118122
code.amount = val
119123
case "gas":
120-
guard let value = comp.value else {return nil}
121-
guard let val = BigUInt(value, radix: 10) else {return nil}
124+
guard let value = comp.value, let val = BigUInt(value, radix: 10) else { return }
122125
code.gasLimit = val
123126
case "data":
124-
guard let value = comp.value else {return nil}
125-
guard let data = Data.fromHex(value) else {return nil}
127+
guard let value = comp.value, let data = Data.fromHex(value) else { return }
126128
code.data = EIP67Code.DataType.data(data)
127129
case "function":
128130
continue
129131
default:
130132
continue
131133
}
132134
}
133-
return code
134135
}
135136
}
136137
}

Sources/web3swift/Utils/EIP/EIP712.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public struct EIP712Domain: EIP712Hashable {
2727

2828
public extension EIP712.Address {
2929
static var zero: Self {
30+
// swiftlint:disable force_unwrapping
3031
EthereumAddress(Data(count: 20))!
32+
// swiftlint:enable force_unwrapping
3133
}
3234
}
3335

Sources/web3swift/Utils/ENS/ENSReverseRegistrar.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ public extension ENS {
1515
public let address: EthereumAddress
1616

1717
lazy var contract: Web3.Contract = {
18+
// swiftlint:disable force_unwrapping
1819
let contract = self.web3.contract(Web3.Utils.reverseRegistrarABI, at: self.address, abiVersion: 2)
1920
precondition(contract != nil)
2021
return contract!
22+
// swiftlint:enable force_unwrapping
2123
}()
2224

2325
lazy var defaultTransaction: CodableTransaction = {
@@ -53,15 +55,15 @@ public extension ENS {
5355
public func getReverseRecordName(address: EthereumAddress) async throws -> Data {
5456
guard let transaction = self.contract.createReadOperation("node", parameters: [address]) else { throw Web3Error.transactionSerializationError }
5557

56-
guard let result = try? await transaction.callContractMethod() else {throw Web3Error.processingError(desc: "Can't call transaction")}
58+
guard let result = try? await transaction.callContractMethod() else { throw Web3Error.processingError(desc: "Can't call transaction") }
5759
guard let name = result["0"] as? Data else { throw Web3Error.processingError(desc: "Can't get answer") }
5860
return name
5961
}
6062

6163
public func getDefaultResolver() async throws -> EthereumAddress {
6264
guard let transaction = self.contract.createReadOperation("defaultResolver") else { throw Web3Error.transactionSerializationError }
6365

64-
guard let result = try? await transaction.callContractMethod() else {throw Web3Error.processingError(desc: "Can't call transaction")}
66+
guard let result = try? await transaction.callContractMethod() else { throw Web3Error.processingError(desc: "Can't call transaction") }
6567
guard let address = result["0"] as? EthereumAddress else { throw Web3Error.processingError(desc: "Can't get answer") }
6668
return address
6769
}

0 commit comments

Comments
 (0)