diff --git a/Sources/Web3Core/Contract/ContractProtocol.swift b/Sources/Web3Core/Contract/ContractProtocol.swift index c532a1934..5a2ab8863 100755 --- a/Sources/Web3Core/Contract/ContractProtocol.swift +++ b/Sources/Web3Core/Contract/ContractProtocol.swift @@ -356,7 +356,7 @@ extension DefaultContractProtocol { throw Web3Error.inputError(desc: "Make sure ABI you use contains error that can match signature: 0x\(selector.toHexString())") } default: - throw Web3Error.inputError(desc: "Invalid data count") + throw Web3Error.inputError(desc: "Given data has invalid bytes count.") } } @@ -395,7 +395,7 @@ extension DefaultContractProtocol { @discardableResult public func callStatic(_ method: String, parameters: [Any], provider: Web3Provider) async throws -> [String: Any] { guard let address = address else { - throw Web3Error.inputError(desc: "address field is missing") + throw Web3Error.inputError(desc: "RPC failed: contract is missing an address.") } guard let data = self.method(method, parameters: parameters, extraData: nil) else { throw Web3Error.dataError diff --git a/Sources/Web3Core/Utility/String+Extension.swift b/Sources/Web3Core/Utility/String+Extension.swift index 16e6ff66b..778558166 100755 --- a/Sources/Web3Core/Utility/String+Extension.swift +++ b/Sources/Web3Core/Utility/String+Extension.swift @@ -137,7 +137,17 @@ extension String { } public var isHex: Bool { - stripHexPrefix().reduce(true, { $0 && $1.isHexDigit } ) + var _str = self.trim() + if _str.isEmpty { + return false + } + _str = _str.stripHexPrefix() + for char in _str { + if !char.isHexDigit { + return false + } + } + return true } /// Splits a string into groups of `every` n characters, grouping from left-to-right by default. If `backwards` is true, right-to-left. diff --git a/Tests/web3swiftTests/localTests/String+ExtensionTests.swift.swift b/Tests/web3swiftTests/localTests/String+ExtensionTests.swift.swift new file mode 100644 index 000000000..1a6686178 --- /dev/null +++ b/Tests/web3swiftTests/localTests/String+ExtensionTests.swift.swift @@ -0,0 +1,31 @@ +// +// String+ExtensionTests.swift +// +// Created by JeneaVranceanu on 26.11.2023. +// + +import Foundation +import XCTest + +class StringExtensionsTest: XCTestCase { + + func testIsHex() throws { + XCTAssertTrue("0x".isHex) + XCTAssertTrue("0xF".isHex) + XCTAssertTrue("F".isHex) + XCTAssertTrue("0xFF".isHex) + XCTAssertTrue("0x0123456789abcdefABCDEF".isHex) + XCTAssertTrue("0123456789abcdefABCDEF".isHex) + XCTAssertTrue("0123456789abcdefABCDEF ".isHex) + XCTAssertTrue(" 0123456789abcdefABCDEF ".isHex) + XCTAssertTrue(" 0123456789abcdefABCDEF".isHex) + + XCTAssertFalse("".isHex) + XCTAssertFalse("-".isHex) + XCTAssertFalse("xyz".isHex) + XCTAssertFalse("0xCAFEQ".isHex) + XCTAssertFalse("R0123456789abcdefABCDEF ".isHex) + XCTAssertFalse(" R0123456789abcdefABCDEF ".isHex) + } + +}