Skip to content

Commit

Permalink
fix: typealias instead of reimplementation of network
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed Aug 1, 2024
1 parent 0ca6469 commit e9ae8be
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 64 deletions.
18 changes: 10 additions & 8 deletions Sources/SingleFactorAuth/SingleFactorAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import FetchNodeDetails
import Foundation
import SessionManager
import TorusUtils
import curveSecp256k1
#if canImport(curveSecp256k1)
import curveSecp256k1
#endif

public class SingleFactorAuth {
let nodeDetailManager: NodeDetailManager
Expand All @@ -13,9 +15,9 @@ public class SingleFactorAuth {

public init(singleFactorAuthArgs: SingleFactorAuthArgs) throws {
sessionManager = .init()
nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork().network)
let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork().network, enableOneKey: true)
try torusUtils = TorusUtils( params: torusOptions)
nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork())
let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true)
try torusUtils = TorusUtils(params: torusOptions)
}

public func initialize() async throws -> TorusSFAKey {
Expand Down Expand Up @@ -51,7 +53,7 @@ public class SingleFactorAuth {
aggregateIdTokenSeeds.sort()

let verifierParams = VerifierParams(verifier_id: loginParams.verifierId, sub_verifier_ids: subVerifierIds, verify_params: verifyParams)

let aggregateIdToken = try curveSecp256k1.keccak256(data: Data(aggregateIdTokenSeeds.joined(separator: "\u{001d}").utf8)).toHexString()

retrieveSharesResponse = try await torusUtils.retrieveShares(
Expand All @@ -73,10 +75,10 @@ public class SingleFactorAuth {

return retrieveSharesResponse
}

public func getKey(loginParams: LoginParams) async throws -> TorusSFAKey {
let torusKey = try await self.getTorusKey(loginParams: loginParams)
let torusKey = try await getTorusKey(loginParams: loginParams)

let publicAddress = torusKey.finalKeyData.evmAddress
let privateKey = torusKey.finalKeyData.privKey

Expand Down
58 changes: 5 additions & 53 deletions Sources/SingleFactorAuth/SingleFactorAuthArgs.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import FetchNodeDetails
import TorusUtils

public class SingleFactorAuthArgs {
public static let SIGNER_MAP: [Web3AuthNetwork: String] = [
Web3AuthNetwork.SAPPHIRE_MAINNET: "https://signer.tor.us",
Web3AuthNetwork.SAPPHIRE_DEVNET: "https://signer.tor.us",
Web3AuthNetwork.MAINNET: "https://signer.tor.us",
Web3AuthNetwork.TESTNET: "https://signer.tor.us",
Web3AuthNetwork.CYAN: "https://signer-polygon.tor.us",
Web3AuthNetwork.AQUA: "https://signer-polygon.tor.us",
]
public typealias Web3AuthNetwork = TorusNetwork

private var network: Web3AuthNetwork
public class SingleFactorAuthArgs {
private var network: TorusNetwork
private var networkUrl: String
private var web3AuthClientId: String

Expand All @@ -20,7 +12,7 @@ public class SingleFactorAuthArgs {
self.networkUrl = networkUrl
self.web3AuthClientId = web3AuthClientId
}

public func getWeb3AuthClientId() -> String {
return web3AuthClientId
}
Expand All @@ -30,50 +22,10 @@ public class SingleFactorAuthArgs {
}

public func getSignerUrl() -> String? {
return SingleFactorAuthArgs.SIGNER_MAP[network]
return network.signerMap
}

public func setNetwork(network: Web3AuthNetwork) {
self.network = network
}
}

public enum Web3AuthNetwork : Equatable, Hashable {
case SAPPHIRE_DEVNET
case SAPPHIRE_MAINNET
case MAINNET
case TESTNET
case CYAN
case AQUA
case CELESTE
case CUSTOM(path: String)

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

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

public var network: 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)
case .CUSTOM(path: let path):
return .legacy(.CUSTOM(path: path))
}
}
}
3 changes: 2 additions & 1 deletion Tests/SingleFactorAuthTests/AquaTest.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BigInt
import JWTKit
import XCTest
import FetchNodeDetails

@testable import SingleFactorAuth

Expand All @@ -13,7 +14,7 @@ final class AquaTest: XCTestCase {
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

override func setUp() {
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: Web3AuthNetwork.AQUA)
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA))
singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SingleFactorAuthTests/CyanTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class CyanTest: XCTestCase {
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

override func setUp() {
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: Web3AuthNetwork.CYAN)
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN))
singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs)
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class SingleFactorAuthTests: XCTestCase {
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

override func setUp() {
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: Web3AuthNetwork.TESTNET)
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET))
singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs)
}

Expand Down

0 comments on commit e9ae8be

Please sign in to comment.