From 566028066267774e91c13c3af546b0db83c6ac19 Mon Sep 17 00:00:00 2001 From: karthus <675377091@qq.com> Date: Sat, 14 Sep 2024 13:43:04 +0800 Subject: [PATCH] fix crash for String index is out of bounds --- Sources/Web3Core/EthereumAddress/EthereumAddress.swift | 3 ++- Tests/web3swiftTests/localTests/UncategorizedTests.swift | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Sources/Web3Core/EthereumAddress/EthereumAddress.swift b/Sources/Web3Core/EthereumAddress/EthereumAddress.swift index ddc54b963..673cc3ab6 100755 --- a/Sources/Web3Core/EthereumAddress/EthereumAddress.swift +++ b/Sources/Web3Core/EthereumAddress/EthereumAddress.swift @@ -63,7 +63,8 @@ public struct EthereumAddress: Equatable { /// represented as `ASCII` data. Otherwise, checksummed address is returned with `0x` prefix. public static func toChecksumAddress(_ addr: String) -> String? { let address = addr.lowercased().stripHexPrefix() - guard let hash = address.data(using: .ascii)?.sha3(.keccak256).toHexString().stripHexPrefix() else { return nil } + guard address.count == 40, + let hash = address.data(using: .ascii)?.sha3(.keccak256).toHexString().stripHexPrefix() else { return nil } var ret = "0x" for (i, char) in address.enumerated() { diff --git a/Tests/web3swiftTests/localTests/UncategorizedTests.swift b/Tests/web3swiftTests/localTests/UncategorizedTests.swift index fa6bcf4d7..8b0e23dde 100755 --- a/Tests/web3swiftTests/localTests/UncategorizedTests.swift +++ b/Tests/web3swiftTests/localTests/UncategorizedTests.swift @@ -35,6 +35,12 @@ class UncategorizedTests: LocalTestCase { let output = EthereumAddress.toChecksumAddress(input) XCTAssert(output == "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", "Failed to checksum address") } + + func testErrorAddressChecksumAddress() throws { + let input = "ethereum:0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359?chainId=1&action=transfer" + let output = EthereumAddress.toChecksumAddress(input) + XCTAssert(output == nil, "Failed to checksum address") + } func testChecksumAddressParsing() throws { let input = "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"