Skip to content

Commit c6e3055

Browse files
author
Alex Vlasov
committed
Fix EthereumAddress initialization
Add test for user case
1 parent ad3b3a7 commit c6e3055

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

web3swift/KeystoreManager/Classes/EthereumAddress.swift

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,25 @@ public struct EthereumAddress: Equatable {
8282
if !addressString.hasHexPrefix() {
8383
return nil
8484
}
85-
if (!ignoreChecksum && data.toHexString().uppercased() != addressString.stripHexPrefix()) { // cover edge case if address is not completely UPPERCASED and potentially missing 0x prefix
86-
let checksummedAddress = EthereumAddress.toChecksumAddress(data.toHexString().addHexPrefix())
87-
guard checksummedAddress == addressString else {return nil}
85+
if (!ignoreChecksum) {
86+
// check for checksum
87+
if data.toHexString() == addressString.stripHexPrefix() {
88+
self._address = data.toHexString().addHexPrefix()
89+
self.type = .normal
90+
return
91+
} else if data.toHexString().uppercased() == addressString.stripHexPrefix() {
92+
self._address = data.toHexString().addHexPrefix()
93+
self.type = .normal
94+
return
95+
} else {
96+
let checksummedAddress = EthereumAddress.toChecksumAddress(data.toHexString().addHexPrefix())
97+
guard checksummedAddress == addressString else {return nil}
98+
self._address = data.toHexString().addHexPrefix()
99+
self.type = .normal
100+
return
101+
}
88102
}
89-
if (!ignoreChecksum && data.toHexString().addHexPrefix() != addressString.stripHexPrefix()) { // cover edge case if address not completely lowercased and potentially missing 0x prefix
90-
let checksummedAddress = EthereumAddress.toChecksumAddress(data.toHexString().addHexPrefix())
91-
guard checksummedAddress == addressString else {return nil}
92-
}
93-
self._address = data.toHexString().addHexPrefix()
94-
self.type = .normal
103+
return nil
95104
case .contractDeployment:
96105
self._address = "0x"
97106
self.type = .contractDeployment

web3swiftTests/web3swiftTests.swift

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,26 @@ class web3swiftTests: XCTestCase {
24202420
let value: BigUInt? = rpcResponse.getValue()
24212421
XCTAssert(value == 1)
24222422
}
2423-
2423+
2424+
func testPublicMappingsAccess() {
2425+
do {
2426+
let jsonString = "[{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"users\",\"outputs\":[{\"name\":\"name\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"userDeviceCount\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalUsers\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]"
2427+
let web3 = Web3.InfuraRinkebyWeb3()
2428+
guard let addr = EthereumAddress("0xdef61132a0c1259464b19e4590e33666aae38574") else {return XCTFail()}
2429+
let contract = web3.contract(jsonString, at: addr, abiVersion: 2)
2430+
XCTAssert(contract != nil)
2431+
let allMethods = contract!.contract.allMethods
2432+
let userDeviceCount = try contract!.method("userDeviceCount", parameters: [addr as AnyObject], options: nil)?.callPromise().wait()
2433+
print(userDeviceCount)
2434+
let totalUsers = try contract!.method("totalUsers", parameters: [], options: nil)?.callPromise().wait()
2435+
print(totalUsers)
2436+
let user = try contract!.method("users", parameters: [0 as AnyObject], options: nil)?.callPromise().wait()
2437+
print(user)
2438+
print(allMethods)
2439+
} catch {
2440+
print(error)
2441+
}
2442+
}
24242443
func getKeystoreData() -> Data? {
24252444
let bundle = Bundle(for: type(of: self))
24262445
guard let path = bundle.path(forResource: "key", ofType: "json") else {return nil}

0 commit comments

Comments
 (0)