Skip to content

Commit

Permalink
Merge pull request #50 from torusresearch/encapsulate_torusnetwork
Browse files Browse the repository at this point in the history
refactor: implement web3authnetwork
  • Loading branch information
himanshuchawla009 authored Oct 8, 2024
2 parents 4bd96c3 + aeabe62 commit e190dea
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 66 deletions.
16 changes: 8 additions & 8 deletions Sources/CommonSources/Models/AllNodeDetailsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ public struct AllNodeDetailsModel:Equatable, Decodable {
return lhs.currentEpoch == rhs.currentEpoch && lhs.torusNodeEndpoints == rhs.torusNodeEndpoints && lhs.torusNodePub == rhs.torusNodePub && lhs.currentEpoch == rhs.currentEpoch && lhs.torusIndexes == rhs.torusIndexes && lhs.updated == rhs.updated
}

public var currentEpoch : String
public var torusNodeEndpoints : Array<String>
public var torusNodeSSSEndpoints : Array<String>
public var torusNodeRSSEndpoints : Array<String>
public var torusNodeTSSEndpoints : Array<String>
public var torusIndexes : Array<BigUInt>
public var torusNodePub : Array<TorusNodePubModel>
public var updated = false
private var currentEpoch : String
private var torusNodeEndpoints : Array<String>
private var torusNodeSSSEndpoints : Array<String>
private var torusNodeRSSEndpoints : Array<String>
private var torusNodeTSSEndpoints : Array<String>
private var torusIndexes : Array<BigUInt>
private var torusNodePub : Array<TorusNodePubModel>
private var updated = false

public enum CodingKeys: String, CodingKey {
case currentEpoch = "currentEpoch"
Expand Down
12 changes: 1 addition & 11 deletions Sources/CommonSources/TorusNetwork.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

public enum TorusNetwork : Equatable, Hashable {
internal enum TorusNetwork : Equatable, Hashable {
case legacy(LegacyNetwork)
case sapphire(SapphireNetwork)

Expand Down Expand Up @@ -68,7 +68,6 @@ public enum LegacyNetwork: Equatable, Hashable {
case CYAN
case AQUA
case CELESTE
case CUSTOM(path: String)

public var path: String {
switch self {
Expand All @@ -78,8 +77,6 @@ public enum LegacyNetwork: Equatable, Hashable {
return "goerli"
case .CYAN, .AQUA, .CELESTE:
return "polygon-mainnet"
case let .CUSTOM(path):
return path
}
}

Expand All @@ -95,8 +92,6 @@ public enum LegacyNetwork: Equatable, Hashable {
return "aqua"
case .CELESTE:
return "celeste"
case .CUSTOM(_):
return "custom"
}
}

Expand All @@ -112,8 +107,6 @@ public enum LegacyNetwork: Equatable, Hashable {
return LegacyNetworkMigrationInfo(migrationCompleted: true, networkIdentifier: self.name, networkMigratedTo: SapphireNetwork.SAPPHIRE_MAINNET)
case .CELESTE:
return LegacyNetworkMigrationInfo(migrationCompleted: true, networkIdentifier: self.name, networkMigratedTo: SapphireNetwork.SAPPHIRE_MAINNET)
case .CUSTOM(_):
return LegacyNetworkMigrationInfo(migrationCompleted: false, networkIdentifier: self.name, networkMigratedTo: SapphireNetwork.SAPPHIRE_MAINNET)
}
}

Expand All @@ -124,7 +117,6 @@ public enum LegacyNetwork: Equatable, Hashable {
case .CYAN: return "polygon-mainnet"
case .AQUA: return "polygon-mainnet"
case .CELESTE: return "polygon-mainnet"
case .CUSTOM(let path) : return path
}
}

Expand All @@ -135,7 +127,6 @@ public enum LegacyNetwork: Equatable, Hashable {
case .CYAN: return "https://signer-polygon.web3auth.io"
case .AQUA: return "https://signer-polygon.web3auth.io"
case .CELESTE: return "https://signer-polygon.web3auth.io"
case .CUSTOM(let path) : return path
}
}

Expand All @@ -146,7 +137,6 @@ public enum LegacyNetwork: Equatable, Hashable {
case .CYAN: return "https://metadata.web3auth.io"
case .AQUA: return "https://metadata.web3auth.io"
case .CELESTE: return "https://metadata.web3auth.io"
case .CUSTOM(let path): return path
}
}
}
Expand Down
73 changes: 73 additions & 0 deletions Sources/CommonSources/Web3AuthNetwork.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
public enum Web3AuthNetwork : Equatable, Hashable {
case SAPPHIRE_DEVNET
case SAPPHIRE_MAINNET
case MAINNET
case TESTNET
case CYAN
case AQUA
case CELESTE

public var path: String {
return self.torusNetwork.path
}

public var name: String {
return self.torusNetwork.name
}

public var signerMap: String {
return self.torusNetwork.signerMap
}

public var isLegacy: Bool {
if case .legacy(_) = torusNetwork {
return true
}
return false
}

public var isSapphire: Bool {
if case .sapphire(_) = torusNetwork {
return true
}
return false
}

public var metadataMap: String {
get throws {
if case .legacy(let legacyNetwork) = torusNetwork {
return legacyNetwork.metadataMap
}

if case .sapphire(let sapphireNetwork) = torusNetwork {
if sapphireNetwork == .SAPPHIRE_MAINNET {
return "https://node-1.node.web3auth.io/metadata"
} else {
return "https://node-1.dev-node.web3auth.io/metadata"
}
}

throw FetchNodeError.InvalidNetwork(self.name)
}
}


internal var torusNetwork: TorusNetwork {
switch self {
case .SAPPHIRE_DEVNET:
return .sapphire(.SAPPHIRE_DEVNET)
case .SAPPHIRE_MAINNET:
return .sapphire(.SAPPHIRE_MAINNET)
case .MAINNET:
return .legacy(.MAINNET)
case .TESTNET:
return .legacy(.TESTNET)
case .CYAN:
return .legacy(.CYAN)
case .AQUA:
return .legacy(.AQUA)
case .CELESTE:
return .legacy(.CELESTE)
}
}
}
6 changes: 0 additions & 6 deletions Sources/FetchNodeDetails/Extensions/String.swift

This file was deleted.

11 changes: 5 additions & 6 deletions Sources/FetchNodeDetails/NodeDetailManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ open class NodeDetailManager {
private var torusNodeRSSEndpoints: [String] = []
private var torusNodeTSSEndpoints: [String] = []

private var network: TorusNetwork = .sapphire( SapphireNetwork.SAPPHIRE_MAINNET )
private var network: Web3AuthNetwork = .SAPPHIRE_MAINNET

private var urlSession: URLSession
private var updated = false
Expand All @@ -24,7 +24,7 @@ open class NodeDetailManager {
}


public init(network: TorusNetwork, fndEndpoint: String? = nil, logLevel: OSLogType = .default, urlSession: URLSession = URLSession.shared) {
public init(network: Web3AuthNetwork, fndEndpoint: String? = nil, logLevel: OSLogType = .default, urlSession: URLSession = URLSession.shared) {
fndLogType = logLevel // to be used across application
self.network = network
self.urlSession = urlSession
Expand All @@ -34,8 +34,7 @@ open class NodeDetailManager {
}

public func getNodeDetails(verifier: String, verifierID: String) async throws -> AllNodeDetailsModel {

switch network {
switch network.torusNetwork {
case .legacy(let legacyNetwork):
if updated && !MULTI_CLUSTER_NETWORKS.contains(legacyNetwork) {
return nodeDetails
Expand Down Expand Up @@ -74,11 +73,11 @@ open class NodeDetailManager {
}

public func getMetadataUrl() async throws -> String {
switch network {
switch network.torusNetwork {
case .legacy(let legacyNetwork):
return legacyNetwork.metadataMap
case .sapphire(_):
return try await self.getNodeDetails(verifier: "test-verifier", verifierID: "test-verifier-id").torusNodeEndpoints[0].replacingOccurrences(of: "/sss/jrpc", with: "/metadata")
return try await self.getNodeDetails(verifier: "test-verifier", verifierID: "test-verifier-id").getTorusNodeEndpoints()[0].replacingOccurrences(of: "/sss/jrpc", with: "/metadata")
}
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/FndBase/Utils/Utils.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
public func fetchLocalConfig(network: TorusNetwork) throws -> AllNodeDetailsModel {
public func fetchLocalConfig(network: Web3AuthNetwork) throws -> AllNodeDetailsModel {

switch network {
switch network.torusNetwork {
case .legacy(let network ) :
if (network.migration_map.migrationCompleted) {
return try SapphireConfig(network: network.migration_map.networkMigratedTo )
Expand Down
24 changes: 12 additions & 12 deletions Tests/FetchNodeDetailsTests/fetchNodeDetailsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class fetchNodeDetailsTests: XCTestCase {
var timeout: TimeInterval = 10
var verifierId = "hello@tor.us"
func test_getNode_Mainnet() async throws {
let fnd = NodeDetailManager(network: .legacy(.MAINNET))
let fnd = NodeDetailManager(network: .MAINNET)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: verifierId)
XCTAssertEqual(result, SampleOutputMainnet().val)
}
Expand All @@ -23,56 +23,56 @@ class fetchNodeDetailsTests: XCTestCase {
// }

func test_getNode_Testnet() async throws {
let fnd = NodeDetailManager(network: .legacy(.TESTNET))
let fnd = NodeDetailManager(network: .TESTNET)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: verifierId)
XCTAssertEqual(result, SampleOutputTestnet().val)
}

func test_getNode_SapphireDevnet() async throws {
let fnd = NodeDetailManager(network: .sapphire(.SAPPHIRE_DEVNET))
let fnd = NodeDetailManager(network: .SAPPHIRE_DEVNET)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: verifierId)
var config = try! SapphireConfig(network: .SAPPHIRE_DEVNET)
config.updated = true
config.setUpdated(updated: true)
XCTAssertEqual(result, config)
XCTAssertEqual(result, SampleOutputSAPPHIREDEVNET().val)
}

func test_getNode_SapphireMainnet() async throws {
let fnd = NodeDetailManager(network: .sapphire(.SAPPHIRE_MAINNET))
let fnd = NodeDetailManager(network: .SAPPHIRE_MAINNET)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: verifierId)
var config = try! SapphireConfig(network: .SAPPHIRE_MAINNET)
config.updated = true
config.setUpdated(updated: true)
XCTAssertEqual(result, config)
XCTAssertEqual(result, SampleOutputSAPPHIREMAINNET().val)
}

func test_getNode_Cyan() async throws {
let fnd = NodeDetailManager(network: .legacy(.CYAN))
let fnd = NodeDetailManager(network: .CYAN)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: verifierId)
XCTAssertEqual(result, SampleOutputCyan().val)
}

func test_getNode_Aqua() async throws {
let fnd = NodeDetailManager(network: .legacy(.AQUA))
let fnd = NodeDetailManager(network: .AQUA)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: verifierId)
XCTAssertEqual(result, SampleOutputAqua().val)
}

func test_getNode_Celeste() async throws {
let fnd = NodeDetailManager(network: .legacy(.CELESTE))
let fnd = NodeDetailManager(network: .CELESTE)
let result = try await fnd.getNodeDetails(verifier: "google", verifierID: "hello@tor.us")
XCTAssertEqual(result, SampleOutputCeleste().val)
}

func test_getMetadata_Mainnet() async throws {
let fnd = NodeDetailManager(network: .legacy(.MAINNET))
let fnd = NodeDetailManager(network: .MAINNET)
let result = try await fnd.getMetadataUrl()
XCTAssertEqual(result, "https://metadata.web3auth.io")
}

func test_getMetadata_SapphireMainnet() async throws {
let fnd = NodeDetailManager(network: .sapphire(.SAPPHIRE_MAINNET))
let fnd = NodeDetailManager(network: .SAPPHIRE_MAINNET)
let result = try await fnd.getMetadataUrl()
XCTAssertEqual(result, SampleOutputSAPPHIREMAINNET().val.torusNodeEndpoints[0].replacingOccurrences(of: "/sss/jrpc", with: "/metadata"))
XCTAssertEqual(result, SampleOutputSAPPHIREMAINNET().val.getTorusNodeEndpoints()[0].replacingOccurrences(of: "/sss/jrpc", with: "/metadata"))
}
}
2 changes: 1 addition & 1 deletion Torus-fetchNodeDetails.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "Torus-fetchNodeDetails"
spec.version = "6.0.3"
spec.version = "7.0.0"
spec.ios.deployment_target = '13.0'
spec.summary = "Fetches the node details from torus nodelist smart contract"
spec.homepage = "https://github.com/torusresearch/fetch-node-details-swift"
Expand Down
Binary file removed cocoapods/.DS_Store
Binary file not shown.
24 changes: 6 additions & 18 deletions cocoapods/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
PODS:
- BigInt (5.0.0)
- GenericJSON (2.0.2)
- secp256k1.swift (0.1.4)
- Torus-fetchNodeDetails (4.0.1):
- web3.swift (~> 0.9.3)
- web3.swift (0.9.3):
- BigInt (~> 5.0.0)
- GenericJSON (~> 2.0)
- secp256k1.swift (~> 0.1)
- BigInt (5.2.0)
- Torus-fetchNodeDetails (7.0.0):
- BigInt (~> 5.2.0)

DEPENDENCIES:
- Torus-fetchNodeDetails (from `../`)

SPEC REPOS:
trunk:
- BigInt
- GenericJSON
- secp256k1.swift
- web3.swift

EXTERNAL SOURCES:
Torus-fetchNodeDetails:
:path: "../"

SPEC CHECKSUMS:
BigInt: 74b4d88367b0e819d9f77393549226d36faeb0d8
GenericJSON: 79a840eeb77030962e8cf02a62d36bd413b67626
secp256k1.swift: a7e7a214f6db6ce5db32cc6b2b45e5c4dd633634
Torus-fetchNodeDetails: a92d02f1bfe91bd61c90f92d64df023babaa5bfa
web3.swift: c6c3bf1f853743f8a7a58d263e5f7e46545e22c7
BigInt: f668a80089607f521586bbe29513d708491ef2f7
Torus-fetchNodeDetails: d12f3511de52bdca33ccabf35f618c296edf94c5

PODFILE CHECKSUM: 33add72342a492adb43676625c404c97ced731da

COCOAPODS: 1.11.3
COCOAPODS: 1.15.2
4 changes: 2 additions & 2 deletions cocoapods/cptest/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ struct ContentView: View {

func execute() {
Task {
let fnd = FetchNodeDetails()
let fnd = NodeDetailManager(network: .SAPPHIRE_MAINNET)
do {
let val = try await fnd.getNodeDetails(verifier: "google", verifierID: "hello@tor.us")
print(val)
success = true
message = val.getNodeListAddress()
message = val.getTorusNodeEndpoints().joined(separator: "; ")
showAlert = true
} catch let err {
success = false
Expand Down

0 comments on commit e190dea

Please sign in to comment.