Skip to content

Commit

Permalink
infura v3 complete update
Browse files Browse the repository at this point in the history
  • Loading branch information
BaldyAsh committed Jun 24, 2019
1 parent 5c0e7e8 commit 363955f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 43 deletions.
15 changes: 15 additions & 0 deletions Sources/web3swift/Web3/Web3+Constants.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// Web3+Constants.swift
// web3swift
//
// Created by Anton on 24/06/2019.
// Copyright © 2019 Bankex Foundation. All rights reserved.

This comment has been minimized.

Copy link
@skywinder

skywinder Jun 26, 2019

Collaborator

🤭

//

import Foundation

struct Constants {
static let infuraHttpScheme = ".infura.io/v3/"
static let infuraWsScheme = ".infura.io/ws/v3/"
static let infuraToken = "4406c3acf862426c83991f1752c46dd8"
}
8 changes: 3 additions & 5 deletions Sources/web3swift/Web3/Web3+InfuraProviders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ public enum BlockNumber {
/// Custom Web3 HTTP provider of Infura nodes.
public final class InfuraProvider: Web3HttpProvider {
public init?(_ net:Networks, accessToken token: String? = nil, keystoreManager manager: KeystoreManager? = nil) {
var requestURLstring = "https://" + net.name + ".infura.io/"
if token != nil {
requestURLstring = requestURLstring + token!
}
var requestURLstring = "https://" + net.name + Constants.infuraHttpScheme
requestURLstring += token != nil ? token! : Constants.infuraToken
let providerURL = URL(string: requestURLstring)
super.init(providerURL!, network: net, keystoreManager: manager)
}
Expand All @@ -55,7 +53,7 @@ public final class InfuraWebsocketProvider: WebsocketProvider {
|| network == Networks.Ropsten
|| network == Networks.Mainnet else {return nil}
let networkName = network.name
let urlString = "wss://\(networkName).infura.io/ws/v3/"
let urlString = "wss://" + networkName + Constants.infuraWsScheme
guard URL(string: urlString) != nil else {return nil}
super.init(urlString,
delegate: delegate,
Expand Down
56 changes: 23 additions & 33 deletions Sources/web3swift/Web3/Web3+WebsocketProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public class WebsocketProvider: Web3Provider, IWebsocketProvider, WebSocketDeleg
if !(endpointString.hasPrefix("wss://") || endpointString.hasPrefix("ws://")) {
return nil
}
if net == nil {
if endpointString.hasPrefix("wss://") && endpointString.hasSuffix(".infura.io/ws") {
if endpointString.hasPrefix("wss://") && endpointString.hasSuffix(Constants.infuraWsScheme) {
if net == nil {
let networkString = endpointString.replacingOccurrences(of: "wss://", with: "")
.replacingOccurrences(of: ".infura.io/ws", with: "")
.replacingOccurrences(of: Constants.infuraWsScheme, with: "")
switch networkString {
case "mainnet":
network = Networks.Mainnet
Expand All @@ -143,17 +143,17 @@ public class WebsocketProvider: Web3Provider, IWebsocketProvider, WebSocketDeleg
default:
break
}
} else {
network = net
}
if network != nil {
endpointString += projectId ?? Constants.infuraToken
}
} else {
network = net
}
if network != nil {
endpointString += projectId ?? "4406c3acf862426c83991f1752c46dd8"
}
url = URL(string: endpointString)!
delegate = wsdelegate
attachedKeystoreManager = manager
socket = WebSocket(url: endpoint)
socket = WebSocket(url: url)
socket.delegate = self
}

Expand All @@ -162,22 +162,16 @@ public class WebsocketProvider: Web3Provider, IWebsocketProvider, WebSocketDeleg
projectId: String? = nil,
keystoreManager manager: KeystoreManager? = nil,
network net: Networks? = nil) {
guard URL(string: endpoint) != nil else {return nil}
var finalEndpoint = endpoint
websocketConnected = false
var endpointString = endpoint
if !(endpointString.hasPrefix("wss://") || endpointString.hasPrefix("ws://")) {
if !(endpoint.hasPrefix("wss://") || endpoint.hasPrefix("ws://")) {
return nil
}
if net == nil {
if endpointString.hasPrefix("wss://")
&& (endpointString.hasSuffix(".infura.io/ws/v3/")
|| endpointString.hasSuffix(".infura.io/ws/v3")
|| endpointString.hasSuffix(".infura.io/ws/")
|| endpointString.hasSuffix(".infura.io/ws")) {
let networkString = endpointString.replacingOccurrences(of: "wss://", with: "")
.replacingOccurrences(of: ".infura.io/ws/v3/", with: "")
.replacingOccurrences(of: ".infura.io/ws/v3", with: "")
.replacingOccurrences(of: ".infura.io/ws/", with: "")
.replacingOccurrences(of: ".infura.io/ws", with: "")
if endpoint.hasPrefix("wss://") && endpoint.hasSuffix(Constants.infuraWsScheme) {
if net == nil {
let networkString = endpoint.replacingOccurrences(of: "wss://", with: "")
.replacingOccurrences(of: Constants.infuraWsScheme, with: "")
switch networkString {
case "mainnet":
network = Networks.Mainnet
Expand All @@ -190,21 +184,17 @@ public class WebsocketProvider: Web3Provider, IWebsocketProvider, WebSocketDeleg
default:
break
}
} else {
network = net
}
if network != nil {
finalEndpoint += projectId ?? Constants.infuraToken
}
} else {
network = net
}
if network != nil {
if endpointString.hasSuffix(".infura.io/ws/v3") { endpointString += "/"}
else if endpointString.hasSuffix(".infura.io/ws/") { endpointString += "v3/"}
else if endpointString.hasSuffix(".infura.io/ws") { endpointString += "/v3/"}
endpointString += projectId ?? "4406c3acf862426c83991f1752c46dd8"
}
guard let endpointUrl = URL(string: endpointString) else {return nil}
url = endpointUrl
url = URL(string: finalEndpoint)!
delegate = wsdelegate
attachedKeystoreManager = manager
socket = WebSocket(url: endpointUrl)
socket = WebSocket(url: url)
socket.delegate = self
}

Expand Down
16 changes: 11 additions & 5 deletions web3swift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
3AA816372276E48400F5DB52 /* web3swift_Eventloop_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA816222276E48400F5DB52 /* web3swift_Eventloop_Tests.swift */; };
3AA816382276E48400F5DB52 /* web3swift_Websockets_Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA816232276E48400F5DB52 /* web3swift_Websockets_Tests.swift */; };
3AA8163A2276E4AE00F5DB52 /* SECP256k1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA816392276E4AE00F5DB52 /* SECP256k1.swift */; };
3AEF4ABF22C0B6BE00AC7929 /* Web3+Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AEF4ABE22C0B6BE00AC7929 /* Web3+Constants.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -383,6 +384,7 @@
3AA8163F2276E5A800F5DB52 /* LICENSE.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = LICENSE.md; sourceTree = "<group>"; };
3AA816402276E5A800F5DB52 /* CHANGELOG.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = CHANGELOG.md; sourceTree = "<group>"; };
3AA816412276E5A900F5DB52 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
3AEF4ABE22C0B6BE00AC7929 /* Web3+Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Web3+Constants.swift"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -697,6 +699,7 @@
3AA815542276E44100F5DB52 /* Web3+Utils.swift */,
3AA815552276E44100F5DB52 /* Web3+MutatingTransaction.swift */,
3AA815562276E44100F5DB52 /* Web3+JSONRPC.swift */,
3AEF4ABE22C0B6BE00AC7929 /* Web3+Constants.swift */,
3AA815572276E44100F5DB52 /* Web3+Instance.swift */,
3AA815582276E44100F5DB52 /* Web3+Contract.swift */,
3AA815592276E44100F5DB52 /* Web3+EventParser.swift */,
Expand Down Expand Up @@ -1177,6 +1180,7 @@
3AA816062276E44100F5DB52 /* Web3+ST20.swift in Sources */,
3AA815FA2276E44100F5DB52 /* Promise+Web3+Eth+GetAccounts.swift in Sources */,
3AA815E72276E44100F5DB52 /* Array+Extension.swift in Sources */,
3AEF4ABF22C0B6BE00AC7929 /* Web3+Constants.swift in Sources */,
3AA815DD2276E44100F5DB52 /* Web3+Options.swift in Sources */,
3AA815A92276E44100F5DB52 /* ENSReverseRegistrar.swift in Sources */,
3AA815EF2276E44100F5DB52 /* Promise+HttpProvider.swift in Sources */,
Expand Down Expand Up @@ -1449,7 +1453,7 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 8DB897QKJA;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -1496,7 +1500,7 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 8DB897QKJA;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -1545,7 +1549,7 @@
CLANG_ENABLE_OBJC_WEAK = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 8DB897QKJA;
"FRAMEWORK_SEARCH_PATHS[sdk=iphoneos*]" = "$(SRCROOT)/Carthage/Build/iOS";
"FRAMEWORK_SEARCH_PATHS[sdk=iphonesimulator*]" = "$(SRCROOT)/Carthage/Build/iOS";
"FRAMEWORK_SEARCH_PATHS[sdk=macosx*]" = "$(SRCROOT)/Carthage/Build/Mac";
Expand Down Expand Up @@ -1577,7 +1581,7 @@
CLANG_ENABLE_OBJC_WEAK = YES;
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = 8DB897QKJA;
"FRAMEWORK_SEARCH_PATHS[sdk=iphoneos*]" = "$(SRCROOT)/Carthage/Build/iOS";
"FRAMEWORK_SEARCH_PATHS[sdk=iphonesimulator*]" = "$(SRCROOT)/Carthage/Build/iOS";
"FRAMEWORK_SEARCH_PATHS[sdk=macosx*]" = "$(SRCROOT)/Carthage/Build/Mac";
Expand Down Expand Up @@ -1607,10 +1611,11 @@
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_WEAK = YES;
CODE_SIGN_IDENTITY = "";
CODE_SIGN_IDENTITY = "Mac Developer";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = 8DB897QKJA;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down Expand Up @@ -1647,6 +1652,7 @@
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = 8DB897QKJA;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down

0 comments on commit 363955f

Please sign in to comment.