Skip to content

Commit

Permalink
[FIX] EthereumAddress hash
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthMike committed Feb 27, 2023
1 parent 70cbffa commit c7404eb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 11 deletions.
41 changes: 41 additions & 0 deletions web3sTests/Address/EthereumAddressTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// web3.swift
// Copyright © 2023 Argent Labs Limited. All rights reserved.
//

import XCTest
@testable import web3

class EthereumAddressTests: XCTestCase {
private var values: Set<EthereumAddress>!
private let addr1 = EthereumAddress("0x162142f0508F557C02bEB7C473682D7C91Bcef41")
private let addr1Padded = EthereumAddress("0x0162142f0508F557C02bEB7C473682D7C91Bcef41")
private let addr2 = EthereumAddress("0x162142f0508F557C02bEB7C473682D7C91Bcef42")

func testGivenAddress_WhenComparingWithSameAddressString_AddressIsEqual() {
XCTAssertEqual(addr1, addr1)
}

func testGivenAddress_WhenHashingWithSameAddressString_AddressIsEqual() {
values = [addr1]
XCTAssertTrue(values.contains(addr1))
}

func testGivenAddress_WhenComparingWithDifferentAddressString_AddressNotEqual() {
XCTAssertNotEqual(addr1, addr2)
}

func testGivenAddress_WhenComparingWith0PaddedAddress_AddressIsEqual() {
XCTAssertEqual(addr1, addr1Padded)
}

func testGivenAddress_WhenHashingWith0PaddedAddress_AddressIsEqual() {
values = [addr1]
XCTAssertTrue(values.contains(addr1Padded))
}

func testGiven0PaddedAddress_WhenHashingWithNotPaddedAddress_AddressIsEqual() {
values = [addr1Padded]
XCTAssertTrue(values.contains(addr1))
}
}
2 changes: 1 addition & 1 deletion web3swift/src/Client/Models/EthereumAddress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public extension EthereumAddress {
}

func asData() -> Data? {
raw.web3.hexData
raw.web3.hexDataAddingPadding?.web3.strippingZeroesFromBytes
}

func toChecksumAddress() -> String {
Expand Down
9 changes: 9 additions & 0 deletions web3swift/src/Extensions/HexExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ public extension Web3Extensions where Base == String {

return nil
}

var hexDataAddingPadding: Data? {
let noHexPrefix = self.noHexPrefix
if let bytes = try? HexUtil.byteArray(fromHex: noHexPrefix, addingPadding: true) {
return Data(bytes)
}

return nil
}
}
25 changes: 15 additions & 10 deletions web3swift/src/Utils/HexUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ class HexUtil {
}
}

static func byteArray(fromHex string: String) throws -> [UInt8] {
static func byteArray(fromHex string: String, addingPadding addPadding: Bool = false) throws -> [UInt8] {
var iterator = string.unicodeScalars.makeIterator()
var byteArray: [UInt8] = []

while let msn = iterator.next() {
if let lsn = iterator.next() {
do {
let convertedMsn = try convert(hexDigit: msn)
let convertedLsn = try convert(hexDigit: lsn)
byteArray += [convertedMsn << 4 | convertedLsn]
} catch {
throw error
}
} else {
var lsn = iterator.next()
if lsn == nil, addPadding == true {
lsn = "0"
}

guard let lsn else {
throw HexConversionError.stringNotEven
}

do {
let convertedMsn = try convert(hexDigit: msn)
let convertedLsn = try convert(hexDigit: lsn)
byteArray += [convertedMsn << 4 | convertedLsn]
} catch {
throw error
}
}
return byteArray
}
Expand Down

0 comments on commit c7404eb

Please sign in to comment.