Skip to content

Commit 48716c8

Browse files
committed
removed swiftlint warnings
1 parent 29cda62 commit 48716c8

File tree

4 files changed

+102
-102
lines changed

4 files changed

+102
-102
lines changed

Sources/Web3Core/KeystoreManager/BIP44.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import Foundation
88
public protocol BIP44 {
99
/**
1010
Derive an `HDNode` based on the provided `path`. The function will throws `BIP44Error.warning` if it was invoked with `warns` as true and the root key doesn't have a previous child with at least one transaction, using false the child node will be derived directly not throwing. This function needs to query the blockchain history when `warns`is true, so it can throw network errors.
11-
12-
- Parameter path: valid BIP32 path.
13-
- Parameter warns: true to be warned about following BIP44 standard, false otherwise.
14-
- Throws: `BIP44Error.warning` if the child key shouldn't be used according to BIP44 standard.
15-
- Returns: an HDNode child key for the provided `path` if it can be created, otherwise nil
11+
- Parameter path: valid BIP32 path.
12+
- Parameter warns: true to be warned about following BIP44 standard, false otherwise.
13+
- Throws: `BIP44Error.warning` if the child key shouldn't be used according to BIP44 standard.
14+
- Returns: an HDNode child key for the provided `path` if it can be created, otherwise nil
1615
*/
1716
func derive(path: String, warns: Bool, transactionChecker: TransactionChecker) async throws -> HDNode?
1817
}
@@ -24,8 +23,7 @@ public enum BIP44Error: Error, Equatable {
2423

2524
public protocol TransactionChecker {
2625
/**
27-
It verifies if the provided `address` has at least one transaction
28-
26+
It verifies if the provided `address` has at least one transaction
2927
- Parameter address: to be queried
3028
- Throws: any error related to query the blockchain provider
3129
- Returns: true if the `address` has at least one transaction, false otherwise
@@ -79,7 +77,7 @@ extension String {
7977
return false
8078
}
8179
}
82-
80+
8381
/// Returns the account from the path if the string contains a well formed BIP44 path
8482
var accountFromPath: Int? {
8583
guard isBip44Path else {
@@ -93,7 +91,7 @@ extension String {
9391
}
9492
return account
9593
}
96-
94+
9795
/**
9896
Transforms a bip44 path into a new one changing `account` & `index`. The resulting one will have the change value equal to `0` to represent external chain. Format `m/44'/coin_type'/account'/change/address_index`
9997
- Parameter account: the new `account` to use

Sources/Web3Core/KeystoreManager/EtherscanTransactionChecker.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import _Concurrency
99
public struct EtherscanTransactionChecker: TransactionChecker {
1010
private let urlSession: URLSessionProxy
1111
private let apiKey: String
12-
12+
1313
public init(urlSession: URLSessionProxy, apiKey: String) {
1414
self.urlSession = urlSession
1515
self.apiKey = apiKey
1616
}
17-
17+
1818
public func hasTransactions(address: String) async throws -> Bool {
1919
let urlString = "https://api.etherscan.io/api?module=account&action=txlist&address=\(address)&startblock=0&page=1&offset=1&sort=asc&apikey=\(apiKey)"
2020
guard let url = URL(string: urlString) else {

Tests/web3swiftTests/localTests/BIP44Tests.swift

Lines changed: 77 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -11,163 +11,165 @@ final class BIP44Tests: XCTestCase {
1111
private var accountZeroScannedAddresses: [String]!
1212
private var accountZeroAndOneScannedAddresses: [String]!
1313
private var mockTransactionChecker: MockTransactionChecker!
14-
14+
1515
override func setUpWithError() throws {
1616
try super.setUpWithError()
1717
mockTransactionChecker = MockTransactionChecker()
18-
accountZeroScannedAddresses = ["0x31a4aD7593D06D049b3Cc07aB5430264Bf7e069f",
19-
"0x2b4fb04d485446ade5889e77b0cbC2c71075209c",
20-
"0x93DDC6583D4BF6e9b309cfBdC681A78F8B5f37Ff",
21-
"0xab2bBC1392f957F7A5DDCE89b64f30064D39C08b",
22-
"0x5Ae1794fFD14bebF34e0BA65815dF9DCB0FD11a8",
23-
"0x4894C017C7fEfB53A9dc3Cf707d098EBCFD8BdF1",
24-
"0x29cC28Cd30e21e73B51389792453818DaCe33f65",
25-
"0x6B3cB8CFBC89ab7A1D9Ccb53537020c53dD4f6E0",
26-
"0xD5FD55fcB93a47Ef176062ac8265E28A5f09887D",
27-
"0xa8A99549A522aF52a2050e081100ef3D42228B55",
28-
"0x2007f83D32cd82b013b9d0d33Ac9e5Ae725367C5",
29-
"0x80a9A6Dd42D67Dd2EEC5c3D6568Fd16e7c964948",
30-
"0xC7781cd86F6336CfE56Fc243f1a9544595dC984E",
31-
"0x7E3eDEB0201D5A5cAF2b50749a7C7843374c312F",
32-
"0x800853194B31Bf5D621Be0b402E8c2b3b402a2Ed",
33-
"0x73BE98d0a3702E8279ca087B2564b6977389C242",
34-
"0x3eFC4765C5BaB65947864fDf4669b7fb8073d89B",
35-
"0xd521A57ea2bAA6396AE916aD2bC4972a9b3635EB",
36-
"0x561192570145C499f0951dEc0a4Df80D0D0A96bb",
37-
"0x4DdBe17BB1b0056941A1425739978e44D462D7DD"]
38-
accountZeroAndOneScannedAddresses = ["0x31a4aD7593D06D049b3Cc07aB5430264Bf7e069f",
39-
"0x3C7b0FadC415d0be5EBa971DC7Dcc39DdDcd4AF7",
40-
"0x73C13e421eF367c4F55BBC02a8e2a2b12e82f717",
41-
"0xE9D8f89452CF0a0d501B9C798cE696C3a1BAE535",
42-
"0x662e78FD3C77A9B8e693f5DC75398C9c0E7233a6",
43-
"0xBEDF61A3466b40f2591702c91cF888843C81e576",
44-
"0xb406aD2666D36716a847c27BAA6d742ECdA85F23",
45-
"0x069c7bF73d17aeb7b8Ff490177A6eefB7aCcb4a8",
46-
"0xa9dbD111007cAfF0804b98195F7f9231bcBEdf86",
47-
"0x2DDDf0447Eb85ae4B16815B010a7007cd30f0A64",
48-
"0x35ff1f3dcb02B6F137A654a419bFb66FE74dFDFE",
49-
"0xd3A77dE492A58386129546469D0E3D3C67Dd520E",
50-
"0x1c011fEfb24210EB1415DD87C161591f5040d71A",
51-
"0x6C289DCE390863ed58bBd56948950f4D96c7Ab8f",
52-
"0xbB13176bf7571D15E1600077F4da6eD22075676b",
53-
"0x618c1ddD96a3Dc2Bd1E90F7053bCc48986A412f7",
54-
"0x5220836980697693fE2137b64e545f926856feAe",
55-
"0xC49D7d886CA02C438c413ceabE6C1f8138ED6ef8",
56-
"0x049e9466CD2417A615e98DD7233eeec4Fcf5632D",
57-
"0x111FbB56b0B5c97F2896Ee722A917b261bCC77fC",
58-
"0xF3F66e5C119620eBDbD7Fb48B4b5d365De5c9750"]
18+
accountZeroScannedAddresses = [
19+
"0x31a4aD7593D06D049b3Cc07aB5430264Bf7e069f",
20+
"0x2b4fb04d485446ade5889e77b0cbC2c71075209c",
21+
"0x93DDC6583D4BF6e9b309cfBdC681A78F8B5f37Ff",
22+
"0xab2bBC1392f957F7A5DDCE89b64f30064D39C08b",
23+
"0x5Ae1794fFD14bebF34e0BA65815dF9DCB0FD11a8",
24+
"0x4894C017C7fEfB53A9dc3Cf707d098EBCFD8BdF1",
25+
"0x29cC28Cd30e21e73B51389792453818DaCe33f65",
26+
"0x6B3cB8CFBC89ab7A1D9Ccb53537020c53dD4f6E0",
27+
"0xD5FD55fcB93a47Ef176062ac8265E28A5f09887D",
28+
"0xa8A99549A522aF52a2050e081100ef3D42228B55",
29+
"0x2007f83D32cd82b013b9d0d33Ac9e5Ae725367C5",
30+
"0x80a9A6Dd42D67Dd2EEC5c3D6568Fd16e7c964948",
31+
"0xC7781cd86F6336CfE56Fc243f1a9544595dC984E",
32+
"0x7E3eDEB0201D5A5cAF2b50749a7C7843374c312F",
33+
"0x800853194B31Bf5D621Be0b402E8c2b3b402a2Ed",
34+
"0x73BE98d0a3702E8279ca087B2564b6977389C242",
35+
"0x3eFC4765C5BaB65947864fDf4669b7fb8073d89B",
36+
"0xd521A57ea2bAA6396AE916aD2bC4972a9b3635EB",
37+
"0x561192570145C499f0951dEc0a4Df80D0D0A96bb",
38+
"0x4DdBe17BB1b0056941A1425739978e44D462D7DD"]
39+
accountZeroAndOneScannedAddresses = [
40+
"0x31a4aD7593D06D049b3Cc07aB5430264Bf7e069f",
41+
"0x3C7b0FadC415d0be5EBa971DC7Dcc39DdDcd4AF7",
42+
"0x73C13e421eF367c4F55BBC02a8e2a2b12e82f717",
43+
"0xE9D8f89452CF0a0d501B9C798cE696C3a1BAE535",
44+
"0x662e78FD3C77A9B8e693f5DC75398C9c0E7233a6",
45+
"0xBEDF61A3466b40f2591702c91cF888843C81e576",
46+
"0xb406aD2666D36716a847c27BAA6d742ECdA85F23",
47+
"0x069c7bF73d17aeb7b8Ff490177A6eefB7aCcb4a8",
48+
"0xa9dbD111007cAfF0804b98195F7f9231bcBEdf86",
49+
"0x2DDDf0447Eb85ae4B16815B010a7007cd30f0A64",
50+
"0x35ff1f3dcb02B6F137A654a419bFb66FE74dFDFE",
51+
"0xd3A77dE492A58386129546469D0E3D3C67Dd520E",
52+
"0x1c011fEfb24210EB1415DD87C161591f5040d71A",
53+
"0x6C289DCE390863ed58bBd56948950f4D96c7Ab8f",
54+
"0xbB13176bf7571D15E1600077F4da6eD22075676b",
55+
"0x618c1ddD96a3Dc2Bd1E90F7053bCc48986A412f7",
56+
"0x5220836980697693fE2137b64e545f926856feAe",
57+
"0xC49D7d886CA02C438c413ceabE6C1f8138ED6ef8",
58+
"0x049e9466CD2417A615e98DD7233eeec4Fcf5632D",
59+
"0x111FbB56b0B5c97F2896Ee722A917b261bCC77fC",
60+
"0xF3F66e5C119620eBDbD7Fb48B4b5d365De5c9750"]
5961
}
60-
62+
6163
override func tearDownWithError() throws {
6264
try super.tearDownWithError()
6365
mockTransactionChecker = nil
6466
accountZeroScannedAddresses = nil
6567
accountZeroAndOneScannedAddresses = nil
6668
}
67-
69+
6870
func testDeriveNoWarn() async throws {
6971
let rootNode = try rootNode()
70-
72+
7173
let childNode = try await rootNode.derive(path: "m/44'/60'/8096'/0/1", warns: false, transactionChecker: mockTransactionChecker)
72-
74+
7375
XCTAssertEqual(try XCTUnwrap(childNode).publicKey.toHexString(), "035785d4918449c87892371c0f9ccf6e4eda40a7fb0f773f1254c064d3bba64026")
7476
XCTAssertEqual(mockTransactionChecker.addresses.count, 0)
7577
}
7678

7779
func testDeriveInvalidPath() async throws {
7880
let rootNode = try rootNode()
79-
81+
8082
let childNode = try? await rootNode.derive(path: "", warns: true, transactionChecker: mockTransactionChecker)
81-
83+
8284
XCTAssertNil(childNode)
8385
XCTAssertEqual(mockTransactionChecker.addresses.count, 0)
8486
}
85-
86-
//MARK: - address
87-
87+
88+
// MARK: - address
89+
8890
func testAccountZeroCanBeDerivedAlways() async throws {
8991
let rootNode = try rootNode()
90-
92+
9193
let childNode = try await rootNode.derive(path: "m/44'/60'/0'/0/255", warns: true, transactionChecker: mockTransactionChecker)
92-
94+
9395
XCTAssertEqual(try XCTUnwrap(childNode).publicKey.toHexString(), "0262fba1af8f149258123265318114066decf50d16c1222a9d657b7de2296c2734")
9496
XCTAssertEqual(mockTransactionChecker.addresses.count, 0)
9597
}
96-
98+
9799
func testAccountOneWithoutTransactionsInAccountZeroWarns() async throws {
98100
do {
99101
let rootNode = try rootNode()
100102
let path = "m/44'/60'/1'/0/0"
101103
var results = false.times(n: 20)
102104
results.append(true)
103105
mockTransactionChecker.results = results
104-
105-
let _ = try await rootNode.derive(path: path, warns: true, transactionChecker: mockTransactionChecker)
106-
106+
107+
_ = try await rootNode.derive(path: path, warns: true, transactionChecker: mockTransactionChecker)
108+
107109
XCTFail("Child must not be created usign warns true for the path: \(path)")
108110
} catch BIP44Error.warning {
109111
XCTAssertTrue(true)
110112
XCTAssertEqual(mockTransactionChecker.addresses, accountZeroScannedAddresses)
111113
}
112114
}
113-
115+
114116
func testAccountOneWithTransactionsInAccountZeroNotWarns() async throws {
115117
do {
116118
let rootNode = try rootNode()
117119
let path = "m/44'/60'/1'/0/0"
118120
var results = false.times(n: 19)
119121
results.append(true)
120122
mockTransactionChecker.results = results
121-
123+
122124
let childNode = try await rootNode.derive(path: path, warns: true, transactionChecker: mockTransactionChecker)
123-
125+
124126
XCTAssertEqual(try XCTUnwrap(childNode).publicKey.toHexString(), "036cd8f1bad46fa7caf7a80d48528b90db2a3b7a5c9a18d74d61b286e03850abf4")
125127
XCTAssertEqual(mockTransactionChecker.addresses, accountZeroScannedAddresses)
126128
} catch BIP44Error.warning {
127129
XCTFail("BIP44Error.warning must not be thrown")
128130
}
129131
}
130-
132+
131133
func testAccountTwoWithTransactionsInAccountZeroButNotInOneWarns() async throws {
132134
do {
133135
let rootNode = try rootNode()
134136
let path = "m/44'/60'/2'/0/0"
135-
var results: Array<Bool> = .init()
137+
var results: [Bool] = .init()
136138
results.append(true)
137139
results.append(contentsOf: false.times(n: 20))
138140
mockTransactionChecker.results = results
139-
140-
let _ = try await rootNode.derive(path: path, warns: true, transactionChecker: mockTransactionChecker)
141-
141+
142+
_ = try await rootNode.derive(path: path, warns: true, transactionChecker: mockTransactionChecker)
143+
142144
XCTFail("Child must not be created usign warns true for the path: \(path)")
143145
} catch BIP44Error.warning {
144146
XCTAssertTrue(true)
145147
XCTAssertEqual(mockTransactionChecker.addresses, accountZeroAndOneScannedAddresses)
146148
XCTAssertEqual(mockTransactionChecker.addresses.count, 21)
147149
}
148150
}
149-
150-
//MARK: - change + addressIndex
151-
151+
152+
// MARK: - change + addressIndex
153+
152154
func testAccountOneAndInternalAndNotZeroAddressIndexWithTransactionsInAccountZeroNotWarns() async throws {
153155
do {
154156
let rootNode = try rootNode()
155157
let path = "m/44'/60'/1'/1/128"
156158
var results = false.times(n: 19)
157159
results.append(true)
158160
mockTransactionChecker.results = results
159-
161+
160162
let childNode = try await rootNode.derive(path: path, warns: true, transactionChecker: mockTransactionChecker)
161-
163+
162164
XCTAssertEqual(try XCTUnwrap(childNode).publicKey.toHexString(), "0282134e44d4c040a4b4c1a780d8302955096cf1d5e738b161c83f0ce1b863c14e")
163165
XCTAssertEqual(mockTransactionChecker.addresses, accountZeroScannedAddresses)
164166
} catch BIP44Error.warning {
165167
XCTFail("BIP44Error.warning must not be thrown")
166168
}
167169
}
168-
170+
169171
// MARK: - private
170-
172+
171173
private func rootNode() throws -> HDNode {
172174
let mnemonic = "fruit wave dwarf banana earth journey tattoo true farm silk olive fence"
173175
let seed = try XCTUnwrap(BIP39.seedFromMmemonics(mnemonic, password: ""))
@@ -176,8 +178,8 @@ final class BIP44Tests: XCTestCase {
176178
}
177179

178180
extension Bool {
179-
func times(n: Int) -> Array<Bool> {
180-
var array: Array<Bool> = .init()
181+
func times(n: Int) -> [Bool] {
182+
var array: [Bool] = .init()
181183
(0..<n).forEach { _ in
182184
array.append(self)
183185
}
@@ -188,7 +190,7 @@ extension Bool {
188190
private final class MockTransactionChecker: TransactionChecker {
189191
var addresses: [String] = .init()
190192
var results: [Bool] = .init()
191-
193+
192194
func hasTransactions(address: String) async throws -> Bool {
193195
addresses.append(address)
194196
return results.removeFirst()

Tests/web3swiftTests/remoteTests/EtherscanTransactionCheckerTests.swift

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ final class EtherscanTransactionCheckerTests: XCTestCase {
1010
private var testApiKey: String!
1111
private var vitaliksAddress: String!
1212
private var emptyAddress: String!
13-
13+
1414
override func setUpWithError() throws {
1515
try super.setUpWithError()
1616
testApiKey = "4HVPVMV1PN6NGZDFXZIYKEZRP53IA41KVC"
1717
vitaliksAddress = "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B"
1818
emptyAddress = "0x1BeY3KhtHpfATH5Yqxz9d8Z1XbqZFSXtK7"
1919
}
20-
20+
2121
override func tearDownWithError() throws {
2222
try super.tearDownWithError()
2323
testApiKey = nil
@@ -27,41 +27,41 @@ final class EtherscanTransactionCheckerTests: XCTestCase {
2727

2828
func testHasTransactions() async throws {
2929
let sut = EtherscanTransactionChecker(urlSession: URLSession.shared, apiKey: testApiKey)
30-
30+
3131
let result = try await sut.hasTransactions(address: vitaliksAddress)
32-
32+
3333
XCTAssertTrue(result)
3434
}
35-
35+
3636
func testHasNotTransactions() async throws {
3737
let sut = EtherscanTransactionChecker(urlSession: URLSession.shared, apiKey: testApiKey)
38-
38+
3939
let result = try await sut.hasTransactions(address: emptyAddress)
40-
40+
4141
XCTAssertFalse(result)
4242
}
43-
43+
4444
func testNetworkError() async throws {
4545
do {
4646
let urlSessionMock = URLSessionMock()
4747
urlSessionMock.response = (Data(), try XCTUnwrap(HTTPURLResponse(url: try XCTUnwrap(URL(string: "https://")), statusCode: 500, httpVersion: nil, headerFields: nil)))
4848
let sut = EtherscanTransactionChecker(urlSession: urlSessionMock, apiKey: testApiKey)
49-
49+
5050
_ = try await sut.hasTransactions(address: vitaliksAddress)
51-
52-
XCTFail()
51+
52+
XCTFail("Network must throw an error")
5353
} catch {
5454
XCTAssertTrue(true)
5555
}
5656
}
57-
57+
5858
func testWrongApiKey() async throws {
5959
do {
6060
let sut = EtherscanTransactionChecker(urlSession: URLSession.shared, apiKey: "")
61-
61+
6262
_ = try await sut.hasTransactions(address: "")
63-
64-
XCTFail()
63+
64+
XCTFail("API not returns a valid response")
6565
} catch DecodingError.typeMismatch {
6666
XCTAssertTrue(true)
6767
}
@@ -70,7 +70,7 @@ final class EtherscanTransactionCheckerTests: XCTestCase {
7070

7171
final class URLSessionMock: URLSessionProxy {
7272
var response: (Data, HTTPURLResponse) = (Data(), HTTPURLResponse())
73-
73+
7474
func data(request: URLRequest) async throws -> (Data, HTTPURLResponse) {
7575
return response
7676
}

0 commit comments

Comments
 (0)