From 629116bd4235776203e398106b6c944d4912fb25 Mon Sep 17 00:00:00 2001 From: Alex Vlasov Date: Thu, 13 Sep 2018 19:38:13 +0300 Subject: [PATCH] complete EIP681 Fix the most stupid Ethereum address parsing --- web3swift.xcodeproj/project.pbxproj | 2 ++ .../Classes/EthereumAddress.swift | 5 ++++- web3swift/Utils/Classes/EIP681.swift | 22 +++++++++++++++++++ .../web3swift_local_node_Tests.swift | 5 +++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/web3swift.xcodeproj/project.pbxproj b/web3swift.xcodeproj/project.pbxproj index c8f03ed12..07049485b 100755 --- a/web3swift.xcodeproj/project.pbxproj +++ b/web3swift.xcodeproj/project.pbxproj @@ -150,6 +150,7 @@ 81C0FD052044A8AE00D82FAF /* Web3+Protocols.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C0FCF120440EB500D82FAF /* Web3+Protocols.swift */; }; 81C0FD062044A8D100D82FAF /* TransactionSigner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C0FCF520440F9900D82FAF /* TransactionSigner.swift */; }; 81C146F71FF274B200AA943E /* Web3+Structures.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C146F61FF274B200AA943E /* Web3+Structures.swift */; }; + 81C456FE214A54D50091FF45 /* key.json in Resources */ = {isa = PBXBuildFile; fileRef = 8159C50F2135929700197B91 /* key.json */; }; 81C5DA0E207254D000424CD6 /* ABIv2Elements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C5DA0D207254D000424CD6 /* ABIv2Elements.swift */; }; 81C5DA0F207254D000424CD6 /* ABIv2Elements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C5DA0D207254D000424CD6 /* ABIv2Elements.swift */; }; 81C5DA11207254F600424CD6 /* ABIv2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 81C5DA10207254F600424CD6 /* ABIv2.swift */; }; @@ -1009,6 +1010,7 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 81C456FE214A54D50091FF45 /* key.json in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/web3swift/KeystoreManager/Classes/EthereumAddress.swift b/web3swift/KeystoreManager/Classes/EthereumAddress.swift index 92edc8bf0..c2d260f3a 100755 --- a/web3swift/KeystoreManager/Classes/EthereumAddress.swift +++ b/web3swift/KeystoreManager/Classes/EthereumAddress.swift @@ -100,8 +100,11 @@ public struct EthereumAddress: Equatable { self.type = .normal return } + } else { + self._address = data.toHexString().addHexPrefix() + self.type = .normal + return } - return nil case .contractDeployment: self._address = "0x" self.type = .contractDeployment diff --git a/web3swift/Utils/Classes/EIP681.swift b/web3swift/Utils/Classes/EIP681.swift index 3bde214f8..c88982aa8 100644 --- a/web3swift/Utils/Classes/EIP681.swift +++ b/web3swift/Utils/Classes/EIP681.swift @@ -144,6 +144,26 @@ extension Web3 { } else if let val = BigUInt(value.stripHexPrefix(), radix: 16) { nativeValue = val as AnyObject } + case .int(bits: _): + if let val = BigInt(value, radix: 10) { + nativeValue = val as AnyObject + } else if let val = BigInt(value.stripHexPrefix(), radix: 16) { + nativeValue = val as AnyObject + } + case .string: + nativeValue = value as AnyObject + case .dynamicBytes: + if let val = Data.fromHex(value) { + nativeValue = val as AnyObject + } else if let val = value.data(using: .utf8) { + nativeValue = val as AnyObject + } + case .bytes(length: _): + if let val = Data.fromHex(value) { + nativeValue = val as AnyObject + } else if let val = value.data(using: .utf8) { + nativeValue = val as AnyObject + } default: continue } @@ -151,6 +171,8 @@ extension Web3 { inputs.append(ABIv2.Element.InOut(name: String(inputNumber), type: inputType)) code.parameters.append(EIP681Code.EIP681Parameter(type: inputType, value: nativeValue!)) inputNumber = inputNumber + 1 + } else { + return nil } } else { switch comp.name { diff --git a/web3swiftTests/web3swift_local_node_Tests.swift b/web3swiftTests/web3swift_local_node_Tests.swift index cc1b5a668..77fee87f7 100755 --- a/web3swiftTests/web3swift_local_node_Tests.swift +++ b/web3swiftTests/web3swift_local_node_Tests.swift @@ -71,6 +71,11 @@ class web3swift_local_node_Tests: XCTestCase { } } + func testGetNodeAccounts() { + let web3 = Web3.new(URL.init(string: "http://127.0.0.1:8545")!)! + guard case .success(let allAddresses) = web3.eth.getAccounts() else {return XCTFail()} + print(allAddresses) + }