diff --git a/Package.resolved b/Package.resolved index bee738a..b855b54 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,14 +1,5 @@ { "pins" : [ - { - "identity" : "anycodable", - "kind" : "remoteSourceControl", - "location" : "https://github.com/Flight-School/AnyCodable", - "state" : { - "revision" : "862808b2070cd908cb04f9aafe7de83d35f81b05", - "version" : "0.6.7" - } - }, { "identity" : "bigint", "kind" : "remoteSourceControl", @@ -75,10 +66,10 @@ { "identity" : "torus-utils-swift", "kind" : "remoteSourceControl", - "location" : "https://github.com/torusresearch/torus-utils-swift", + "location" : "https://github.com/torusresearch/torus-utils-swift.git", "state" : { - "revision" : "608c28404c506983bfec7bbd957632fc0544db8c", - "version" : "8.1.2" + "revision" : "303dc2cf41db7c10f769855edad0e717ced2d554", + "version" : "9.0.0" } } ], diff --git a/Package.swift b/Package.swift index 24e9291..0d8a593 100644 --- a/Package.swift +++ b/Package.swift @@ -15,8 +15,8 @@ let package = Package( targets: ["SingleFactorAuth"]) ], dependencies: [ - .package(url: "https://github.com/torusresearch/fetch-node-details-swift.git", from: "6.0.1"), - .package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "8.1.0"), + .package(url: "https://github.com/torusresearch/fetch-node-details-swift.git", from: "6.0.3"), + .package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "9.0.0"), .package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "4.0.2"), ], diff --git a/SingleFactorAuth.podspec b/SingleFactorAuth.podspec index 2c5d6f8..3473b9e 100644 --- a/SingleFactorAuth.podspec +++ b/SingleFactorAuth.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "SingleFactorAuth" - spec.version = "6.0.0" + spec.version = "7.0.0" spec.ios.deployment_target = "14.0" spec.summary = "Enable one key flow for Web3Auth" spec.homepage = "https://github.com/Web3Auth/single-factor-auth-swift" @@ -10,8 +10,8 @@ Pod::Spec.new do |spec| spec.module_name = "SingleFactorAuth" spec.source = { :git => "https://github.com/web3Auth/single-factor-auth-swift", :tag => spec.version } spec.source_files = "Sources/SingleFactorAuth/*.{swift,json}","Sources/SingleFactorAuth/**/*.{swift,json}" - spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.1' + spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.3' spec.dependency 'curvelib.swift', '~> 1.0.1' - spec.dependency 'Torus-utils', '~> 8.1.0' + spec.dependency 'Torus-utils', '~> 9.0.0' spec.dependency 'TorusSessionManager', '~> 4.0.2' end diff --git a/Sources/SingleFactorAuth/Data+Extension.swift b/Sources/SingleFactorAuth/Data+Extension.swift new file mode 100644 index 0000000..55a59bb --- /dev/null +++ b/Sources/SingleFactorAuth/Data+Extension.swift @@ -0,0 +1,11 @@ +import Foundation + +internal extension Data { + var bytes: Array { + Array(self) + } + + func toHexString() -> String { + bytes.toHexString() + } +} diff --git a/Sources/SingleFactorAuth/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index cf0c130..f4a8058 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -4,21 +4,20 @@ import FetchNodeDetails import Foundation import SessionManager import TorusUtils -import curveSecp256k1 +#if canImport(curveSecp256k1) + import curveSecp256k1 +#endif public class SingleFactorAuth { let nodeDetailManager: NodeDetailManager let torusUtils: TorusUtils private var sessionManager: SessionManager - public init(singleFactorAuthArgs: SingleFactorAuthArgs) { + public init(singleFactorAuthArgs: SingleFactorAuthArgs) throws { sessionManager = .init() - nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork().network) - torusUtils = TorusUtils( - enableOneKey: true, - network: singleFactorAuthArgs.getNetwork().network, - clientId: singleFactorAuthArgs.getWeb3AuthClientId() - ) + 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 { @@ -33,7 +32,7 @@ public class SingleFactorAuth { let details = try await nodeDetailManager.getNodeDetails(verifier: loginParams.verifier, verifierID: loginParams.verifierId) - let userDetails = try await torusUtils.getUserTypeAndAddress(endpoints: details.getTorusNodeEndpoints(), torusNodePubs: details.getTorusNodePub(), verifier: loginParams.verifier, verifierId: loginParams.verifierId) + let userDetails = try await torusUtils.getUserTypeAndAddress(endpoints: details.getTorusNodeEndpoints(), verifier: loginParams.verifier, verifierId: loginParams.verifierId) if userDetails.metadata?.upgraded == true { throw "User already has enabled MFA" @@ -42,45 +41,32 @@ public class SingleFactorAuth { if let subVerifierInfoArray = loginParams.subVerifierInfoArray, !subVerifierInfoArray.isEmpty { var aggregateIdTokenSeeds = [String]() var subVerifierIds = [String]() - var verifyParams = [[String: String]]() + var verifyParams = [VerifyParams]() for value in subVerifierInfoArray { aggregateIdTokenSeeds.append(value.idToken) - var verifyParam: [String: String] = [:] - verifyParam["verifier_id"] = loginParams.verifierId - verifyParam["idtoken"] = value.idToken + let verifyParam = VerifyParams(verifier_id: loginParams.verifierId, idtoken: value.idToken) verifyParams.append(verifyParam) subVerifierIds.append(value.verifier) } aggregateIdTokenSeeds.sort() - let extraParams = [ - "verifier_id": loginParams.verifierId, - "sub_verifier_ids": subVerifierIds, - "verify_params": verifyParams, - ] as [String: Codable] + let verifierParams = VerifierParams(verifier_id: loginParams.verifierId, sub_verifier_ids: subVerifierIds, verify_params: verifyParams) - let verifierParams = VerifierParams(verifier_id: loginParams.verifierId) - let aggregateIdToken = try curveSecp256k1.keccak256(data: Data(aggregateIdTokenSeeds.joined(separator: "\u{001d}").utf8)).toHexString() retrieveSharesResponse = try await torusUtils.retrieveShares( endpoints: details.getTorusNodeEndpoints(), - torusNodePubs: details.getTorusNodePub(), - indexes: details.getTorusIndexes(), verifier: loginParams.verifier, verifierParams: verifierParams, - idToken: aggregateIdToken, - extraParams: extraParams + idToken: aggregateIdToken ) } else { let verifierParams = VerifierParams(verifier_id: loginParams.verifierId) retrieveSharesResponse = try await torusUtils.retrieveShares( endpoints: details.getTorusNodeEndpoints(), - torusNodePubs: details.getTorusNodePub(), - indexes: details.getTorusIndexes(), verifier: loginParams.verifier, verifierParams: verifierParams, idToken: loginParams.idToken @@ -89,12 +75,12 @@ public class SingleFactorAuth { return retrieveSharesResponse } - + public func getKey(loginParams: LoginParams) async throws -> TorusSFAKey { - let torusKey = try await self.getTorusKey(loginParams: loginParams) - - let publicAddress = torusKey.finalKeyData?.evmAddress ?? "" - let privateKey = torusKey.finalKeyData?.privKey ?? "" + let torusKey = try await getTorusKey(loginParams: loginParams) + + let publicAddress = torusKey.finalKeyData.evmAddress + let privateKey = torusKey.finalKeyData.privKey let torusSfaKey = TorusSFAKey(privateKey: privateKey, publicAddress: publicAddress) _ = try await sessionManager.createSession(data: torusSfaKey) diff --git a/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift b/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift index 32ddd4c..9974c35 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift @@ -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 @@ -20,7 +12,7 @@ public class SingleFactorAuthArgs { self.networkUrl = networkUrl self.web3AuthClientId = web3AuthClientId } - + public func getWeb3AuthClientId() -> String { return web3AuthClientId } @@ -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)) - } - } -} diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index f5d5c8f..d677c7c 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -1,6 +1,7 @@ import BigInt import JWTKit import XCTest +import FetchNodeDetails @testable import SingleFactorAuth @@ -13,8 +14,8 @@ final class AquaTest: XCTestCase { let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: Web3AuthNetwork.AQUA) - singleFactoreAuth = SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) + singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) } func testGetTorusKey() async throws { diff --git a/Tests/SingleFactorAuthTests/CyanTest.swift b/Tests/SingleFactorAuthTests/CyanTest.swift index 910a8d1..bb2e855 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -13,8 +13,8 @@ final class CyanTest: XCTestCase { let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: Web3AuthNetwork.CYAN) - singleFactoreAuth = SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) + singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) } func testGetTorusKey() async throws { diff --git a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift index 9f10ab4..2f6df02 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -13,8 +13,8 @@ final class SingleFactorAuthTests: XCTestCase { let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: Web3AuthNetwork.TESTNET) - singleFactoreAuth = SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) + singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) } func testGetTorusKey() async throws {