Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Sources/web3swift/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ open class BrowserViewController: UIViewController {
return webView
}()

private var bundle: Bundle {
Bundle(for: BrowserViewController.self)
}

lazy var config: WKWebViewConfiguration = {
let config = WKWebViewConfiguration()

var js = ""

if let filepath = Bundle.main.path(forResource: "browser.min", ofType: "js") {

let bundleURL = bundle.bundleURL.appendingPathComponent("Browser.bundle")
if let scripteBundle = Bundle(url: bundleURL),
let filepath = scripteBundle.path(forResource: "browser.min", ofType: "js") {
do {
js += try String(contentsOfFile: filepath)
NSLog("Loaded browser.js")
Expand Down
33 changes: 33 additions & 0 deletions Sources/web3swift/Promises/Promise+HttpProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ import PromiseKit

extension Web3HttpProvider {

public static func postRaw<T: Encodable>(_ request: JSONRPCEncodablerequest<T>, providerURL: URL, queue: DispatchQueue = .main, session: URLSession) -> Promise<Data> {
let rp = Promise<Data>.pending()
var task: URLSessionTask? = nil
queue.async {
do {
let encoder = JSONEncoder()
let requestData = try encoder.encode(request)
var urlRequest = URLRequest(url: providerURL, cachePolicy: URLRequest.CachePolicy.reloadIgnoringCacheData)
urlRequest.httpMethod = "POST"
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
urlRequest.httpBody = requestData
task = session.dataTask(with: urlRequest){ (data, response, error) in
guard error == nil else {
rp.resolver.reject(error!)
return
}
guard data != nil else {
rp.resolver.reject(Web3Error.nodeError(desc: "Node response is empty"))
return
}
rp.resolver.fulfill(data!)
}
task?.resume()
} catch {
rp.resolver.reject(error)
}
}
return rp.promise.ensure(on: queue) {
task = nil
}
}

static func post(_ request: JSONRPCrequest, providerURL: URL, queue: DispatchQueue = .main, session: URLSession) -> Promise<JSONRPCresponse> {
let rp = Promise<Data>.pending()
var task: URLSessionTask? = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,51 @@ extension web3.Eth {
return sendRawTransactionPromise(deserializedTX)
}

public func sendRawTransactionPromise(_ transaction: EthereumTransaction) -> Promise<TransactionSendingResult>{
public func sendRawTransactionPromise(_ transaction: EthereumTransaction ,_ transactionEncodeData: Data? = nil) -> Promise<TransactionSendingResult>{
// print(transaction)
let queue = web3.requestDispatcher.queue
do {
guard let request = EthereumTransaction.createRawTransaction(transaction: transaction) else {
throw Web3Error.processingError(desc: "Transaction is invalid")
}
let rp = web3.dispatch(request)
return rp.map(on: queue ) { response in
guard let value: String = response.getValue() else {
if response.error != nil {
throw Web3Error.nodeError(desc: response.error!.message)

if transactionEncodeData != nil {
guard let request = EthereumTransaction.createRawTransaction(transaction: transaction , transactionEncodeData: transactionEncodeData) else {
throw Web3Error.processingError(desc: "Transaction is invalid")
}
let rp = web3.dispatch(request)
return rp.map(on: queue ) { response in
guard let value: String = response.getValue() else {
if response.error != nil {
throw Web3Error.nodeError(desc: response.error!.message)
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
}
let result = TransactionSendingResult(transaction: transaction, hash: value)
for hook in self.web3.postSubmissionHooks {
hook.queue.async {
hook.function(result)
}
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
return result
}
let result = TransactionSendingResult(transaction: transaction, hash: value)
for hook in self.web3.postSubmissionHooks {
hook.queue.async {
hook.function(result)
} else {
guard let request = EthereumTransaction.createRawTransaction(transaction: transaction) else {
throw Web3Error.processingError(desc: "Transaction is invalid")
}
let rp = web3.dispatch(request)
return rp.map(on: queue ) { response in
guard let value: String = response.getValue() else {
if response.error != nil {
throw Web3Error.nodeError(desc: response.error!.message)
}
throw Web3Error.nodeError(desc: "Invalid value from Ethereum node")
}
let result = TransactionSendingResult(transaction: transaction, hash: value)
for hook in self.web3.postSubmissionHooks {
hook.queue.async {
hook.function(result)
}
}
return result
}
return result
}
} catch {
let returnPromise = Promise<TransactionSendingResult>.pending()
Expand Down
38 changes: 24 additions & 14 deletions Sources/web3swift/Transaction/EthereumTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public struct EthereumTransaction: CustomStringConvertible {
self.to = to
}


public init (nonce: BigUInt, gasPrice: BigUInt, gasLimit: BigUInt, to: EthereumAddress, value: BigUInt, data: Data, v: BigUInt, r: BigUInt, s: BigUInt) {
self.nonce = nonce
self.gasPrice = gasPrice
Expand Down Expand Up @@ -177,8 +176,8 @@ public struct EthereumTransaction: CustomStringConvertible {
return RLP.encode(fields)
}
} else {
let fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value!, self.data, self.v, self.r, self.s] as [AnyObject]
return RLP.encode(fields)
let fields = [self.nonce, self.gasPrice, self.gasLimit, self.to.addressData, self.value!, self.data, self.v, self.r, self.s] as [AnyObject]
return RLP.encode(fields)
}
}

Expand Down Expand Up @@ -276,17 +275,28 @@ public struct EthereumTransaction: CustomStringConvertible {
return request
}

static func createRawTransaction(transaction: EthereumTransaction) -> JSONRPCrequest? {
guard transaction.sender != nil else {return nil}
guard let encodedData = transaction.encode() else {return nil}
let hex = encodedData.toHexString().addHexPrefix().lowercased()
var request = JSONRPCrequest()
request.method = JSONRPCmethod.sendRawTransaction
let params = [hex] as Array<Encodable>
let pars = JSONRPCparams(params: params)
request.params = pars
if !request.isValid {return nil}
return request
static func createRawTransaction(transaction: EthereumTransaction, transactionEncodeData: Data? = nil) -> JSONRPCrequest? {
if transactionEncodeData != nil {
let hex = transactionEncodeData!.toHexString().addHexPrefix().lowercased()
var request = JSONRPCrequest()
request.method = JSONRPCmethod.sendRawTransaction
let params = [hex] as Array<Encodable>
let pars = JSONRPCparams(params: params)
request.params = pars
if !request.isValid {return nil}
return request
} else {
guard transaction.sender != nil else {return nil}
guard let encodedData = transaction.encode() else {return nil}
let hex = encodedData.toHexString().addHexPrefix().lowercased()
var request = JSONRPCrequest()
request.method = JSONRPCmethod.sendRawTransaction
let params = [hex] as Array<Encodable>
let pars = JSONRPCparams(params: params)
request.params = pars
if !request.isValid {return nil}
return request
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions Sources/web3swift/Utils/ENS/ENS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public class ENS {
guard let resolver = try? self.registry.getResolver(forDomain: node) else {
throw Web3Error.processingError(desc: "Failed to get resolver for domain")
}
guard let isNameSupports = try? resolver.supportsInterface(interfaceID: Resolver.InterfaceName.name.hash()) else {
throw Web3Error.processingError(desc: "Resolver don't support interface with this ID")
}
guard isNameSupports else {
throw Web3Error.processingError(desc: "Name isn't supported")
}
// guard let isNameSupports = try? resolver.supportsInterface(interfaceID: Resolver.InterfaceName.name.hash()) else {
// throw Web3Error.processingError(desc: "Resolver don't support interface with this ID")
// }
// guard isNameSupports else {
// throw Web3Error.processingError(desc: "Name isn't supported")
// }
guard let name = try? resolver.getCanonicalName(forNode: node) else {
throw Web3Error.processingError(desc: "Can't get name")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/web3swift/Web3/Web3+Eth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ extension web3.Eth {
/// This function is synchronous!
///
/// Returns the Result object that indicates either success of failure.
public func sendRawTransaction(_ transaction: EthereumTransaction) throws -> TransactionSendingResult {
let result = try self.sendRawTransactionPromise(transaction).wait()
public func sendRawTransaction(_ transaction: EthereumTransaction, _ transactionEncodeData: Data? = nil) throws -> TransactionSendingResult {
let result = try self.sendRawTransactionPromise(transaction,transactionEncodeData).wait()
return result
}

Expand Down
12 changes: 12 additions & 0 deletions Sources/web3swift/Web3/Web3+JSONRPC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import Foundation
import BigInt
//import EthereumAddress

public struct JSONRPCEncodablerequest<T: Encodable>: Encodable {
public var jsonrpc: String = "2.0"
public var method: JSONRPCmethod?
public var params: T?
public var id: UInt64 = Counter.increment()

public init(method: JSONRPCmethod, params: T?) {
self.method = method
self.params = params
}
}

/// Global counter object to enumerate JSON RPC requests.
public struct Counter {
public static var counter = UInt64(1)
Expand Down