Skip to content

Commit

Permalink
- Remove "Result" everywhere
Browse files Browse the repository at this point in the history
- Make Podfile more explicit
  • Loading branch information
shamatar committed Oct 30, 2018
1 parent 7a2178d commit 7f83ea2
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 88 deletions.
5 changes: 2 additions & 3 deletions web3swift.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ s.swift_version = '4.1'
s.module_name = 'Web3swift'
s.ios.deployment_target = "9.0"
s.osx.deployment_target = "10.11"
s.source_files = "web3swift/**/*.{h,swift}",
s.exclude_files = "web3swift/ObjectiveCbridge/Classes/*.{swift}", "web3swift/Utils/Classes/ENS.swift", "web3swift/Utils/Classes/ENSResolver.swift"
s.public_header_files = "web3swift/**/*.{h}"
s.source_files = "web3swift/{PrecompiledContracts,Promises,Web3,Contract,KeystoreManager,Transaction,Convenience}/Classes/*.{h,swift}", "web3swift/Utils/Classes/{EIP67Code, EIP681}.swift", "web3swift/HookedFunctions/Classes/Web3+Wallet.swift", "web3swift/web3swift.h"
s.public_header_files = "web3swift/web3swift.h"
s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'YES' }

s.frameworks = 'CoreImage'
Expand Down
27 changes: 14 additions & 13 deletions web3swift/HookedFunctions/Classes/Web3+BrowserFunctions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,22 @@ extension web3.BrowserFunctions {
return self.prepareTxForApproval(transaction, options: options)
}

public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: Web3Options) -> (transaction: EthereumTransaction?, options: Web3Options?) {
var transaction = trans
var options = opts
guard let _ = options.from else {return (nil, nil)}
let gasPriceResult = self.web3.eth.getGasPrice()
if case .failure(_) = gasPriceResult {
public func prepareTxForApproval(_ trans: EthereumTransaction, options opts: Web3Options) throws -> (transaction: EthereumTransaction?, options: Web3Options?) {
do {
var transaction = trans
var options = opts
guard let _ = options.from else {return (nil, nil)}
let gasPrice = try self.web3.eth.getGasPrice()
transaction.gasPrice = gasPrice
options.gasPrice = gasPrice
guard let gasEstimate = self.estimateGas(transaction, options: options) else {return (nil, nil)}
transaction.gasLimit = gasEstimate
options.gasLimit = gasEstimate
print(transaction)
return (transaction, options)
} catch {
return (nil, nil)
}
transaction.gasPrice = gasPriceResult.value!
options.gasPrice = gasPriceResult.value!
guard let gasEstimate = self.estimateGas(transaction, options: options) else {return (nil, nil)}
transaction.gasLimit = gasEstimate
options.gasLimit = gasEstimate
print(transaction)
return (transaction, options)
}

public func signTransaction(_ transactionJSON: [String: Any], password: String = "web3swift") -> String? {
Expand Down
32 changes: 12 additions & 20 deletions web3swift/Web3/Classes/Web3+Personal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ extension web3.Personal {
- important: This call is synchronous
*/
public func signPersonalMessage(message: Data, from: EthereumAddress, password:String = "web3swift") -> Result<Data, Web3Error> {
do {
let result = try self.signPersonalMessagePromise(message: message, from: from, password: password).wait()
return Result(result)
} catch {
return Result.failure(error as! Web3Error)
}
public func signPersonalMessage(message: Data, from: EthereumAddress, password:String = "web3swift") throws -> Data {
let result = try self.signPersonalMessagePromise(message: message, from: from, password: password).wait()
return result
}

/**
Expand All @@ -47,13 +43,9 @@ extension web3.Personal {
- important: This call is synchronous. Does nothing if private keys are stored locally.
*/
public func unlockAccount(account: EthereumAddress, password:String = "web3swift", seconds: UInt64 = 300) -> Result<Bool, Web3Error> {
do {
let result = try self.unlockAccountPromise(account: account).wait()
return Result(result)
} catch {
return Result.failure(error as! Web3Error)
}
public func unlockAccount(account: EthereumAddress, password:String = "web3swift", seconds: UInt64 = 300) throws -> Bool {
let result = try self.unlockAccountPromise(account: account).wait()
return result
}

/**
Expand All @@ -67,11 +59,11 @@ extension web3.Personal {
- Result object
*/
public func ecrecover(personalMessage: Data, signature: Data) -> Result<EthereumAddress, Web3Error> {
public func ecrecover(personalMessage: Data, signature: Data) throws -> EthereumAddress {
guard let recovered = Web3.Utils.personalECRecover(personalMessage, signature: signature) else {
return Result.failure(Web3Error.dataError)
throw Web3Error.dataError
}
return Result(recovered)
return recovered
}

/**
Expand All @@ -85,10 +77,10 @@ extension web3.Personal {
- Result object
*/
public func ecrecover(hash: Data, signature: Data) -> Result<EthereumAddress, Web3Error> {
public func ecrecover(hash: Data, signature: Data) throws -> EthereumAddress {
guard let recovered = Web3.Utils.hashECRecover(hash: hash, signature: signature) else {
return Result.failure(Web3Error.dataError)
throw Web3Error.dataError
}
return Result(recovered)
return recovered
}
}
39 changes: 9 additions & 30 deletions web3swift/Web3/Classes/Web3+TxPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,18 @@ import Foundation
import BigInt

extension web3.TxPool {
public func getInspect() -> Result<[String:[String:[String:String]]], Web3Error> {
do {
let result = try self.getInspectPromise().wait()
return Result(result)
} catch {
if let err = error as? Web3Error {
return Result.failure(err)
}
return Result.failure(Web3Error.generalError(err: error))
}
public func getInspect() throws -> [String:[String:[String:String]]] {
let result = try self.getInspectPromise().wait()
return result
}

public func getStatus() -> Result<TxPoolStatus, Web3Error> {
do {
let result = try self.getStatusPromise().wait()
return Result(result)
} catch {
if let err = error as? Web3Error {
return Result.failure(err)
}
return Result.failure(Web3Error.generalError(err: error))
}
public func getStatus() throws -> TxPoolStatus {
let result = try self.getStatusPromise().wait()
return result
}

public func getContent() -> Result<TxPoolContent, Web3Error> {
do {
let result = try self.getContentPromise().wait()
return Result(result)
} catch {
if let err = error as? Web3Error {
return Result.failure(err)
}
return Result.failure(Web3Error.generalError(err: error))
}
public func getContent() throws -> TxPoolContent {
let result = try self.getContentPromise().wait()
return result
}
}
14 changes: 7 additions & 7 deletions web3swift/Web3/Classes/Web3.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,24 @@ public struct Web3 {
}

struct ResultUnwrapper {
static func getResponse(_ response: [String: Any]?) -> Result<Any, Web3Error> {
static func getResponse(_ response: [String: Any]?) throws -> Any {
guard response != nil, let res = response else {
return Result.failure(Web3Error.connectionError)
throw Web3Error.connectionError
}
if let error = res["error"] {
if let errString = error as? String {
return Result.failure(Web3Error.nodeError(desc: errString))
throw Web3Error.nodeError(desc: errString)
} else if let errDict = error as? [String:Any] {
if errDict["message"] != nil, let descr = errDict["message"]! as? String {
return Result.failure(Web3Error.nodeError(desc: descr))
throw Web3Error.nodeError(desc: descr)
}
}
return Result.failure(Web3Error.unknownError)
throw Web3Error.unknownError
}
guard let result = res["result"] else {
return Result.failure(Web3Error.dataError)
throw Web3Error.dataError
}
return Result(result)
return result
}
}

Expand Down
6 changes: 2 additions & 4 deletions web3swift/web3swift-Bridging-Header.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//
// web3swift-Bridging-Header.h
// web3swift
//
// Created by Alexander Vlasov on 08.08.2018.
// Copyright © 2018 Bankex Foundation. All rights reserved.
// Created by Alex Vlasov.
// Copyright © 2018 Alex Vlasov. All rights reserved.
//

#ifndef web3swift_Bridging_Header_h
Expand Down
6 changes: 2 additions & 4 deletions web3swift/web3swift.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//
// web3swift.h
// web3swift
//
// Created by Petr Korolev on 06/12/2017.
// Copyright © 2017 Bankex Foundation. All rights reserved.
// Created by Alex Vlasov.
// Copyright © 2018 Alex Vlasov. All rights reserved.
//

#if TARGET_OS_IPHONE
Expand Down
11 changes: 4 additions & 7 deletions web3swiftTests/web3swift_rinkeby_personalSignature_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@ import EthereumAddress
class web3swift_rinkeby_personalSignature_Tests: XCTestCase {


func testPersonalSignature() {
func testPersonalSignature() throws {
let web3 = Web3.InfuraRinkebyWeb3()
let tempKeystore = try! EthereumKeystoreV3(password: "")
let keystoreManager = KeystoreManager([tempKeystore!])
web3.addKeystoreManager(keystoreManager)
let message = "Hello World"
let expectedAddress = keystoreManager.addresses![0]
print(expectedAddress)
let signRes = web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "")
guard case .success(let signature) = signRes else {return XCTFail()}
let signature = try web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "")
let unmarshalledSignature = SECP256K1.unmarshalSignature(signatureData: signature)!
print("V = " + String(unmarshalledSignature.v))
print("R = " + Data(unmarshalledSignature.r).toHexString())
print("S = " + Data(unmarshalledSignature.s).toHexString())
print("Personal hash = " + Web3.Utils.hashPersonalMessage(message.data(using: .utf8)!)!.toHexString())
let recoveredSigner = web3.personal.ecrecover(personalMessage: message.data(using: .utf8)!, signature: signature)
guard case .success(let signer) = recoveredSigner else {return XCTFail()}
let signer = try web3.personal.ecrecover(personalMessage: message.data(using: .utf8)!, signature: signature)
XCTAssert(expectedAddress == signer, "Failed to sign personal message")
}

Expand All @@ -43,8 +41,7 @@ class web3swift_rinkeby_personalSignature_Tests: XCTestCase {
let message = "Hello World"
let expectedAddress = keystoreManager.addresses![0]
print(expectedAddress)
let signRes = web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "")
guard case .success(let signature) = signRes else {return XCTFail()}
let signature = try web3.personal.signPersonalMessage(message: message.data(using: .utf8)!, from: expectedAddress, password: "")
let unmarshalledSignature = SECP256K1.unmarshalSignature(signatureData: signature)!
print("V = " + String(unmarshalledSignature.v))
print("R = " + Data(unmarshalledSignature.r).toHexString())
Expand Down

0 comments on commit 7f83ea2

Please sign in to comment.