Skip to content

Commit

Permalink
fix crash for String index is out of bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
karthuszY committed Sep 14, 2024
1 parent 710451b commit 5660280
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/Web3Core/EthereumAddress/EthereumAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
6 changes: 6 additions & 0 deletions Tests/web3swiftTests/localTests/UncategorizedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5660280

Please sign in to comment.