Skip to content

Commit 5660280

Browse files
committed
fix crash for String index is out of bounds
1 parent 710451b commit 5660280

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

Sources/Web3Core/EthereumAddress/EthereumAddress.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public struct EthereumAddress: Equatable {
6363
/// represented as `ASCII` data. Otherwise, checksummed address is returned with `0x` prefix.
6464
public static func toChecksumAddress(_ addr: String) -> String? {
6565
let address = addr.lowercased().stripHexPrefix()
66-
guard let hash = address.data(using: .ascii)?.sha3(.keccak256).toHexString().stripHexPrefix() else { return nil }
66+
guard address.count == 40,
67+
let hash = address.data(using: .ascii)?.sha3(.keccak256).toHexString().stripHexPrefix() else { return nil }
6768
var ret = "0x"
6869

6970
for (i, char) in address.enumerated() {

Tests/web3swiftTests/localTests/UncategorizedTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ class UncategorizedTests: LocalTestCase {
3535
let output = EthereumAddress.toChecksumAddress(input)
3636
XCTAssert(output == "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359", "Failed to checksum address")
3737
}
38+
39+
func testErrorAddressChecksumAddress() throws {
40+
let input = "ethereum:0xfb6916095ca1df60bb79ce92ce3ea74c37c5d359?chainId=1&action=transfer"
41+
let output = EthereumAddress.toChecksumAddress(input)
42+
XCTAssert(output == nil, "Failed to checksum address")
43+
}
3844

3945
func testChecksumAddressParsing() throws {
4046
let input = "0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359"

0 commit comments

Comments
 (0)