From 68b0dd5d64ea1bbe12c027b5272750691563ce4f Mon Sep 17 00:00:00 2001 From: albertopeam Date: Fri, 25 Nov 2022 14:24:38 +0100 Subject: [PATCH 1/6] removed warnings --- .../Envelope/EIP1559Envelope.swift | 4 +-- .../Envelope/EIP2930Envelope.swift | 4 +-- .../Transaction/Envelope/LegacyEnvelope.swift | 8 ++--- .../Web3Core/Utility/Array+Extension.swift | 9 ++++++ .../localTests/ArrayExtensionTests.swift | 30 +++++++++++++++++++ 5 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 Tests/web3swiftTests/localTests/ArrayExtensionTests.swift diff --git a/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift b/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift index 673af57ce..483c9fb74 100644 --- a/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift @@ -250,9 +250,9 @@ extension EIP1559Envelope { switch type { case .transaction: - fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject] + fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s].toAnyObject() case .signature: - fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list] as [AnyObject] + fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list].toAnyObject() } guard var result = RLP.encode(fields) else { return nil } result.insert(UInt8(self.type.rawValue), at: 0) diff --git a/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift b/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift index fe4bcfb46..023cb3981 100644 --- a/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift @@ -211,9 +211,9 @@ extension EIP2930Envelope { switch type { case .transaction: - fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s] as [AnyObject] + fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s].toAnyObject() case .signature: - fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list] as [AnyObject] + fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list].toAnyObject() } guard var result = RLP.encode(fields) else { return nil } result.insert(UInt8(self.type.rawValue), at: 0) diff --git a/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift b/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift index b1c2c7e4c..ee8a850b4 100644 --- a/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift @@ -195,12 +195,12 @@ extension LegacyEnvelope { let fields: [AnyObject] switch type { case .transaction: - fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s] as [AnyObject] + fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s].toAnyObject() case .signature: - if let chainID = chainID, chainID != 0 { - fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)] as [AnyObject] + if let chainID = self.chainID, chainID != 0 { + fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)].toAnyObject() } else { - fields = [nonce, gasPrice, gasLimit, to.addressData, value, data] as [AnyObject] + fields = [nonce, gasPrice, gasLimit, to.addressData, value, data].toAnyObject() } } return RLP.encode(fields) diff --git a/Sources/Web3Core/Utility/Array+Extension.swift b/Sources/Web3Core/Utility/Array+Extension.swift index 1b0e78ce8..2a520f8f6 100755 --- a/Sources/Web3Core/Utility/Array+Extension.swift +++ b/Sources/Web3Core/Utility/Array+Extension.swift @@ -106,3 +106,12 @@ extension Array where Element: BinaryInteger { return sorted_data[index] } } + +//MARK: - Conversion + +/// Transforms an Array of Any? into an Array of AnyObject +extension Array where Element == Any? { + func toAnyObject() -> [AnyObject] { + self.map{ $0 as AnyObject } + } +} diff --git a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift new file mode 100644 index 000000000..00ae854ff --- /dev/null +++ b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift @@ -0,0 +1,30 @@ +// +// ArrayExtensionTests.swift +// Created by albertopeam on 25/11/22. +// + +@testable import Core +import XCTest + +final class ArrayExtensionTests: XCTestCase { + func testToAnyObjectEmpty() { + let result = [].toAnyObject() + XCTAssertEqual(result.count, 0) + } + + func testToAnyObjectNils() throws { + let result = [nil, nil].toAnyObject() + XCTAssertEqual(result.count, 2) + XCTAssertTrue(result.first is NSNull) + XCTAssertTrue(result.dropFirst().first is NSNull) + } + + func testToAnyObjectNilAndNonNils() throws { + let result = [1, nil, "", NSNull()].toAnyObject() + XCTAssertEqual(result.count, 4) + XCTAssertEqual(result.first as? Int, 1) + XCTAssertTrue(result.dropFirst().first is NSNull) + XCTAssertNil(result.dropFirst().first as? String, "2") + XCTAssertTrue(result.dropFirst().first is NSNull) + } +} From dea600c7851224b320d9da957714c6c6f399c770 Mon Sep 17 00:00:00 2001 From: albertopeam Date: Fri, 25 Nov 2022 22:49:13 +0100 Subject: [PATCH 2/6] improve doc. improve & fix tests --- .../Web3Core/Utility/Array+Extension.swift | 4 ++-- .../localTests/ArrayExtensionTests.swift | 23 +++++++++++++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/Sources/Web3Core/Utility/Array+Extension.swift b/Sources/Web3Core/Utility/Array+Extension.swift index 2a520f8f6..9a6c3cdba 100755 --- a/Sources/Web3Core/Utility/Array+Extension.swift +++ b/Sources/Web3Core/Utility/Array+Extension.swift @@ -107,9 +107,9 @@ extension Array where Element: BinaryInteger { } } -//MARK: - Conversion +// MARK: - Conversion -/// Transforms an Array of Any? into an Array of AnyObject +/// Transforms `[Any?]` into `[AnyObject]` extension Array where Element == Any? { func toAnyObject() -> [AnyObject] { self.map{ $0 as AnyObject } diff --git a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift index 00ae854ff..7e2673baa 100644 --- a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift +++ b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift @@ -5,6 +5,7 @@ @testable import Core import XCTest +import BigInt final class ArrayExtensionTests: XCTestCase { func testToAnyObjectEmpty() { @@ -20,11 +21,23 @@ final class ArrayExtensionTests: XCTestCase { } func testToAnyObjectNilAndNonNils() throws { - let result = [1, nil, "", NSNull()].toAnyObject() - XCTAssertEqual(result.count, 4) + let result = [1, + nil, + "2", + NSNull(), + Data(hex: "FA"), + BigInt(3), + BigUInt(4), + EthereumAddress(Data(count: 20)) + ].toAnyObject() + XCTAssertEqual(result.count, 8) XCTAssertEqual(result.first as? Int, 1) - XCTAssertTrue(result.dropFirst().first is NSNull) - XCTAssertNil(result.dropFirst().first as? String, "2") - XCTAssertTrue(result.dropFirst().first is NSNull) + XCTAssertTrue(result.dropFirst(1).first is NSNull) + XCTAssertEqual(result.dropFirst(2).first as? String, "2") + XCTAssertTrue(result.dropFirst(3).first is NSNull) + XCTAssertEqual(result.dropFirst(4).first as? Data, Data(hex: "FA")) + XCTAssertEqual(result.dropFirst(5).first as? BigInt, BigInt(3)) + XCTAssertEqual(result.dropFirst(6).first as? BigUInt, BigUInt(4)) + XCTAssertEqual(result.dropFirst(7).first as? EthereumAddress, EthereumAddress(Data(count: 20))) } } From 076daf0114498f05fbc89b72f8b0c78ca1bd2d46 Mon Sep 17 00:00:00 2001 From: albertopeam Date: Thu, 29 Dec 2022 19:59:33 +0100 Subject: [PATCH 3/6] replaced import --- Tests/web3swiftTests/localTests/ArrayExtensionTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift index 7e2673baa..49c2b30fe 100644 --- a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift +++ b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift @@ -3,7 +3,7 @@ // Created by albertopeam on 25/11/22. // -@testable import Core +@testable import Web3Core import XCTest import BigInt From 1381c98c2afb0411a275cac30c4986b3f3c10d87 Mon Sep 17 00:00:00 2001 From: albertopeam Date: Thu, 29 Dec 2022 20:03:42 +0100 Subject: [PATCH 4/6] removed swiftlint warnings --- .../Web3Core/Utility/Array+Extension.swift | 2 +- .../localTests/ArrayExtensionTests.swift | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Sources/Web3Core/Utility/Array+Extension.swift b/Sources/Web3Core/Utility/Array+Extension.swift index 9a6c3cdba..4dbcd868c 100755 --- a/Sources/Web3Core/Utility/Array+Extension.swift +++ b/Sources/Web3Core/Utility/Array+Extension.swift @@ -112,6 +112,6 @@ extension Array where Element: BinaryInteger { /// Transforms `[Any?]` into `[AnyObject]` extension Array where Element == Any? { func toAnyObject() -> [AnyObject] { - self.map{ $0 as AnyObject } + self.map { $0 as AnyObject } } } diff --git a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift index 49c2b30fe..72d121d4a 100644 --- a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift +++ b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift @@ -12,23 +12,24 @@ final class ArrayExtensionTests: XCTestCase { let result = [].toAnyObject() XCTAssertEqual(result.count, 0) } - + func testToAnyObjectNils() throws { let result = [nil, nil].toAnyObject() XCTAssertEqual(result.count, 2) XCTAssertTrue(result.first is NSNull) XCTAssertTrue(result.dropFirst().first is NSNull) } - + func testToAnyObjectNilAndNonNils() throws { - let result = [1, - nil, - "2", - NSNull(), - Data(hex: "FA"), - BigInt(3), - BigUInt(4), - EthereumAddress(Data(count: 20)) + let result = [ + 1, + nil, + "2", + NSNull(), + Data(hex: "FA"), + BigInt(3), + BigUInt(4), + EthereumAddress(Data(count: 20)) ].toAnyObject() XCTAssertEqual(result.count, 8) XCTAssertEqual(result.first as? Int, 1) From 68d6bb047918160e71df7a3031aeb9b9e2eec4bd Mon Sep 17 00:00:00 2001 From: Jenea Vranceanu <36865532+JeneaVranceanu@users.noreply.github.com> Date: Tue, 10 Jan 2023 14:45:55 +0200 Subject: [PATCH 5/6] chore: removed redundant `self.` use --- Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift b/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift index ee8a850b4..fa221d3c6 100644 --- a/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift @@ -197,7 +197,7 @@ extension LegacyEnvelope { case .transaction: fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s].toAnyObject() case .signature: - if let chainID = self.chainID, chainID != 0 { + if let chainID = chainID, chainID != 0 { fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)].toAnyObject() } else { fields = [nonce, gasPrice, gasLimit, to.addressData, value, data].toAnyObject() From efd6672fbf419e1943f8a6714049861019555da4 Mon Sep 17 00:00:00 2001 From: albertopeam Date: Tue, 10 Jan 2023 22:00:53 +0100 Subject: [PATCH 6/6] replaced [AnyObject] with [Any?] to handle any data type: classes or structs --- Sources/Web3Core/RLP/RLP.swift | 4 +- .../Envelope/EIP1559Envelope.swift | 6 +-- .../Envelope/EIP2930Envelope.swift | 6 +-- .../Transaction/Envelope/LegacyEnvelope.swift | 8 ++-- .../Web3Core/Utility/Array+Extension.swift | 9 ---- .../localTests/ArrayExtensionTests.swift | 44 ------------------- 6 files changed, 12 insertions(+), 65 deletions(-) delete mode 100644 Tests/web3swiftTests/localTests/ArrayExtensionTests.swift diff --git a/Sources/Web3Core/RLP/RLP.swift b/Sources/Web3Core/RLP/RLP.swift index 588432db7..c60a0afde 100755 --- a/Sources/Web3Core/RLP/RLP.swift +++ b/Sources/Web3Core/RLP/RLP.swift @@ -113,10 +113,10 @@ public struct RLP { } // FIXME: Make encode generic to avoid casting it's argument to [AnyObject] - internal static func encode(_ elements: [AnyObject]) -> Data? { + internal static func encode(_ elements: [Any?]) -> Data? { var encodedData = Data() for e in elements { - if let encoded = encode(e) { + if let encoded = encode(e as AnyObject) { encodedData.append(encoded) } else { guard let asArray = e as? [AnyObject] else {return nil} diff --git a/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift b/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift index 483c9fb74..052b931d9 100644 --- a/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/EIP1559Envelope.swift @@ -245,14 +245,14 @@ extension EIP1559Envelope { // } public func encode(for type: EncodeType = .transaction) -> Data? { - let fields: [AnyObject] + let fields: [Any?] let list = accessList.map { $0.encodeAsList() as AnyObject } switch type { case .transaction: - fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s].toAnyObject() + fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list, v, r, s] case .signature: - fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list].toAnyObject() + fields = [chainID, nonce, maxPriorityFeePerGas, maxFeePerGas, gasLimit, to.addressData, value, data, list] } guard var result = RLP.encode(fields) else { return nil } result.insert(UInt8(self.type.rawValue), at: 0) diff --git a/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift b/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift index 023cb3981..93cfcbfb7 100644 --- a/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/EIP2930Envelope.swift @@ -206,14 +206,14 @@ extension EIP2930Envelope { } public func encode(for type: EncodeType = .transaction) -> Data? { - let fields: [AnyObject] + let fields: [Any?] let list = accessList.map { $0.encodeAsList() as AnyObject } switch type { case .transaction: - fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s].toAnyObject() + fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list, v, r, s] case .signature: - fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list].toAnyObject() + fields = [chainID, nonce, gasPrice, gasLimit, to.addressData, value, data, list] } guard var result = RLP.encode(fields) else { return nil } result.insert(UInt8(self.type.rawValue), at: 0) diff --git a/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift b/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift index fa221d3c6..121ecb63b 100644 --- a/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift +++ b/Sources/Web3Core/Transaction/Envelope/LegacyEnvelope.swift @@ -192,15 +192,15 @@ extension LegacyEnvelope { // } public func encode(for type: EncodeType = .transaction) -> Data? { - let fields: [AnyObject] + let fields: [Any?] switch type { case .transaction: - fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s].toAnyObject() + fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, v, r, s] case .signature: if let chainID = chainID, chainID != 0 { - fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)].toAnyObject() + fields = [nonce, gasPrice, gasLimit, to.addressData, value, data, chainID, BigUInt(0), BigUInt(0)] } else { - fields = [nonce, gasPrice, gasLimit, to.addressData, value, data].toAnyObject() + fields = [nonce, gasPrice, gasLimit, to.addressData, value, data] } } return RLP.encode(fields) diff --git a/Sources/Web3Core/Utility/Array+Extension.swift b/Sources/Web3Core/Utility/Array+Extension.swift index 4dbcd868c..1b0e78ce8 100755 --- a/Sources/Web3Core/Utility/Array+Extension.swift +++ b/Sources/Web3Core/Utility/Array+Extension.swift @@ -106,12 +106,3 @@ extension Array where Element: BinaryInteger { return sorted_data[index] } } - -// MARK: - Conversion - -/// Transforms `[Any?]` into `[AnyObject]` -extension Array where Element == Any? { - func toAnyObject() -> [AnyObject] { - self.map { $0 as AnyObject } - } -} diff --git a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift b/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift deleted file mode 100644 index 72d121d4a..000000000 --- a/Tests/web3swiftTests/localTests/ArrayExtensionTests.swift +++ /dev/null @@ -1,44 +0,0 @@ -// -// ArrayExtensionTests.swift -// Created by albertopeam on 25/11/22. -// - -@testable import Web3Core -import XCTest -import BigInt - -final class ArrayExtensionTests: XCTestCase { - func testToAnyObjectEmpty() { - let result = [].toAnyObject() - XCTAssertEqual(result.count, 0) - } - - func testToAnyObjectNils() throws { - let result = [nil, nil].toAnyObject() - XCTAssertEqual(result.count, 2) - XCTAssertTrue(result.first is NSNull) - XCTAssertTrue(result.dropFirst().first is NSNull) - } - - func testToAnyObjectNilAndNonNils() throws { - let result = [ - 1, - nil, - "2", - NSNull(), - Data(hex: "FA"), - BigInt(3), - BigUInt(4), - EthereumAddress(Data(count: 20)) - ].toAnyObject() - XCTAssertEqual(result.count, 8) - XCTAssertEqual(result.first as? Int, 1) - XCTAssertTrue(result.dropFirst(1).first is NSNull) - XCTAssertEqual(result.dropFirst(2).first as? String, "2") - XCTAssertTrue(result.dropFirst(3).first is NSNull) - XCTAssertEqual(result.dropFirst(4).first as? Data, Data(hex: "FA")) - XCTAssertEqual(result.dropFirst(5).first as? BigInt, BigInt(3)) - XCTAssertEqual(result.dropFirst(6).first as? BigUInt, BigUInt(4)) - XCTAssertEqual(result.dropFirst(7).first as? EthereumAddress, EthereumAddress(Data(count: 20))) - } -}