Skip to content

Commit f996438

Browse files
authored
Merge pull request #347 from argentlabs/improve-ethereum-address-matching
Match against strings to improve performance
2 parents 4953e98 + 2eb6469 commit f996438

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

web3swift/src/Client/Models/EthereumAddress.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,21 @@ public struct EthereumAddress: Codable, Hashable {
1313

1414
private let raw: String
1515
private let numberRepresentation: BigUInt?
16+
private let numberRepresentationAsString: String?
1617
public static let zero: Self = "0x0000000000000000000000000000000000000000"
1718

1819
public init(_ value: String) {
1920
self.raw = value.lowercased()
2021
self.numberRepresentation = BigUInt(hex: raw)
22+
self.numberRepresentationAsString = self.numberRepresentation.map(String.init(describing:))
2123
}
2224

2325
public init(from decoder: Decoder) throws {
2426
let container = try decoder.singleValueContainer()
2527
let raw = try container.decode(String.self).lowercased()
2628
self.raw = raw
2729
self.numberRepresentation = BigUInt(hex: raw)
30+
self.numberRepresentationAsString = self.numberRepresentation.map(String.init(describing:))
2831
}
2932

3033
public func encode(to encoder: Encoder) throws {
@@ -33,19 +36,19 @@ public struct EthereumAddress: Codable, Hashable {
3336
}
3437

3538
public func hash(into hasher: inout Hasher) {
36-
if let number = asNumber() {
37-
hasher.combine(number)
39+
if let numberAsString = numberRepresentationAsString {
40+
hasher.combine(numberAsString)
3841
} else {
3942
hasher.combine(asString())
4043
}
4144
}
4245

4346
public static func == (lhs: EthereumAddress, rhs: EthereumAddress) -> Bool {
44-
guard let lhsInt = lhs.asNumber(), let rhsInt = rhs.asNumber() else {
47+
guard let lhs = lhs.numberRepresentationAsString, let rhs = rhs.numberRepresentationAsString else {
4548
return false
4649
}
4750
// Comparing Number representation avoids issues with lowercase and 0-padding
48-
return lhsInt == rhsInt
51+
return lhs == rhs
4952
}
5053
}
5154

0 commit comments

Comments
 (0)