Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix crash for toChecksum when "String index is out of bounds" #865

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading