diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e6901d4..6216984 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,4 +28,4 @@ jobs: with: xcode-version: latest-stable - name: package - run: xcodebuild test -scheme SingleFactorAuth -destination "platform=iOS Simulator,OS=16.2,name=iPhone 14" COMPILER_INDEX_STORE_ENABLE=NO + run: xcodebuild test -scheme SingleFactorAuth -destination "platform=iOS Simulator,OS=17.5,name=iPhone 14" COMPILER_INDEX_STORE_ENABLE=NO diff --git a/Package.resolved b/Package.resolved index b855b54..d1fa5fa 100644 --- a/Package.resolved +++ b/Package.resolved @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Web3Auth/session-manager-swift.git", "state" : { - "revision" : "20cc7bff065d7fe53164d17e7714a3f17d4cea2a", - "version" : "4.0.2" + "revision" : "67d5f7db655d02778861057fb280ecf47c923b09", + "version" : "5.0.0" } }, { @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/torusresearch/torus-utils-swift.git", "state" : { - "revision" : "303dc2cf41db7c10f769855edad0e717ced2d554", - "version" : "9.0.0" + "revision" : "eda55b8537a600e657d19d4c452c0a36f217883c", + "version" : "9.0.1" } } ], diff --git a/Package.swift b/Package.swift index 0d8a593..8a73111 100644 --- a/Package.swift +++ b/Package.swift @@ -16,9 +16,9 @@ let package = Package( ], dependencies: [ .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/torusresearch/torus-utils-swift.git", from: "9.0.1"), .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"), + .package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "5.0.0"), ], targets: [ .target( diff --git a/README.md b/README.md index e20c8e1..c0fc0d9 100644 --- a/README.md +++ b/README.md @@ -21,34 +21,34 @@ You can install the SingleFactorAuth Swift using Swift Package Manager. ... dependencies: [ ... - .package(url: "https://github.com/Web3Auth/single-factor-auth-swift/", from: "4.0.0") + .package(url: "https://github.com/Web3Auth/single-factor-auth-swift/", from: "8.0.0") ], ... ``` ## Getting Started -Initialize the `SingleFactAuth` class by passing `SingleFactorAuthArgs` +Initialize the `SingleFactAuth` class by passing `SFAParams` ```swift let singleFactorAuthArgs = SingleFactorAuthArgs( web3AuthClientId: "", network: Web3AuthNetwork.SAPPHIRE_MAINNET ) -let singleFactoreAuth = SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) +let singleFactoreAuth = SingleFactorAuth(params: SFAParams) ``` Use the `getKey` function to login the user and get the privateKey and public address for the given user. ```swift -let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) +let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) let loginParams = LoginParams( verifier: TEST_VERIFIER, - verifierId: TOURUS_TEST_EMAIL, + verifierId: TORUS_TEST_EMAIL, idToken: idToken ) -let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) +let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) ``` We also have included Session Management in this SDK so call initialize function to get TorusKey value without relogging in the user if a user has an active session it will return the TorusKey struct otherwise it will return nil. diff --git a/SingleFactorAuth.podspec b/SingleFactorAuth.podspec index 3473b9e..4ef3984 100644 --- a/SingleFactorAuth.podspec +++ b/SingleFactorAuth.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "SingleFactorAuth" - spec.version = "7.0.0" + spec.version = "8.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" @@ -12,6 +12,6 @@ Pod::Spec.new do |spec| spec.source_files = "Sources/SingleFactorAuth/*.{swift,json}","Sources/SingleFactorAuth/**/*.{swift,json}" spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.3' spec.dependency 'curvelib.swift', '~> 1.0.1' - spec.dependency 'Torus-utils', '~> 9.0.0' - spec.dependency 'TorusSessionManager', '~> 4.0.2' + spec.dependency 'Torus-utils', '~> 9.0.1' + spec.dependency 'TorusSessionManager', '~> 5.0.0' end diff --git a/Sources/SingleFactorAuth/TorusKey.swift b/Sources/SingleFactorAuth/SFAKey.swift similarity index 93% rename from Sources/SingleFactorAuth/TorusKey.swift rename to Sources/SingleFactorAuth/SFAKey.swift index 0fa5770..2beb0c7 100644 --- a/Sources/SingleFactorAuth/TorusKey.swift +++ b/Sources/SingleFactorAuth/SFAKey.swift @@ -1,7 +1,7 @@ import BigInt import Foundation -public class TorusSFAKey: Codable { +public class SFAKey: Codable { let privateKey: String let publicAddress: String diff --git a/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift b/Sources/SingleFactorAuth/SFAParams.swift similarity index 75% rename from Sources/SingleFactorAuth/SingleFactorAuthArgs.swift rename to Sources/SingleFactorAuth/SFAParams.swift index 9974c35..a198290 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift +++ b/Sources/SingleFactorAuth/SFAParams.swift @@ -2,15 +2,17 @@ import FetchNodeDetails public typealias Web3AuthNetwork = TorusNetwork -public class SingleFactorAuthArgs { +public class SFAParams{ private var network: TorusNetwork private var networkUrl: String private var web3AuthClientId: String + private var sessionTime: Int - public init(web3AuthClientId: String, network: Web3AuthNetwork, networkUrl: String = "") { + public init(web3AuthClientId: String, network: Web3AuthNetwork, sessionTime: Int = 86400, networkUrl: String = "") { self.network = network self.networkUrl = networkUrl self.web3AuthClientId = web3AuthClientId + self.sessionTime = sessionTime } public func getWeb3AuthClientId() -> String { @@ -28,4 +30,8 @@ public class SingleFactorAuthArgs { public func setNetwork(network: Web3AuthNetwork) { self.network = network } + + public func getSessionTime() -> Int { + return sessionTime + } } diff --git a/Sources/SingleFactorAuth/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index f4a8058..37ebe79 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -13,19 +13,26 @@ public class SingleFactorAuth { let torusUtils: TorusUtils private var sessionManager: SessionManager - public init(singleFactorAuthArgs: SingleFactorAuthArgs) throws { - sessionManager = .init() - nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork()) - let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true) + public init(params: SFAParams) throws { + sessionManager = SessionManager(sessionTime: params.getSessionTime(), allowedOrigin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") + nodeDetailManager = NodeDetailManager(network: params.getNetwork()) + let torusOptions = TorusOptions(clientId: params.getWeb3AuthClientId(), network: params.getNetwork(), enableOneKey: true) try torusUtils = TorusUtils(params: torusOptions) } - public func initialize() async throws -> TorusSFAKey { - let data = try await sessionManager.authorizeSession() + public func initialize() async throws -> SFAKey { + let data = try await sessionManager.authorizeSession(origin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") guard let privKey = data["privateKey"] as? String, let publicAddress = data["publicAddress"] as? String else { throw SessionManagerError.decodingError } return .init(privateKey: privKey, publicAddress: publicAddress) } + + public func isSessionIdExists() -> Bool { + if (sessionManager.getSessionID() != nil) && !(sessionManager.getSessionID()!.isEmpty) { + return true + } + return false + } public func getTorusKey(loginParams: LoginParams) async throws -> TorusKey { var retrieveSharesResponse: TorusKey @@ -76,14 +83,14 @@ public class SingleFactorAuth { return retrieveSharesResponse } - public func getKey(loginParams: LoginParams) async throws -> TorusSFAKey { + public func connect(loginParams: LoginParams) async throws -> SFAKey { 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) - return torusSfaKey + let sfaKey = SFAKey(privateKey: privateKey, publicAddress: publicAddress) + _ = try await sessionManager.createSession(data: sfaKey) + return sfaKey } } diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index d677c7c..2bc5f4d 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -2,26 +2,25 @@ import BigInt import JWTKit import XCTest import FetchNodeDetails - -@testable import SingleFactorAuth +import SingleFactorAuth final class AquaTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SingleFactorAuthArgs! + var singleFactorAuthArgs: SFAParams! - let TOURUS_TEST_EMAIL = "hello@tor.us" + let TORUS_TEST_EMAIL = "hello@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } - func testGetTorusKey() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + func testConnect() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) @@ -29,19 +28,19 @@ final class AquaTest: XCTestCase { } func testInitialise() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let savedKey = try await singleFactoreAuth.initialize() let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8" XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.publicAddress, savedKey.getPublicAddress()) + XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) } func testAggregrateGetTorusKey() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "6f8b884f19975fb0d138ed21b22a6a7e1b79e37f611d0a29f1442b34efc6bacd" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) diff --git a/Tests/SingleFactorAuthTests/CyanTest.swift b/Tests/SingleFactorAuthTests/CyanTest.swift index bb2e855..ecf1b81 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -1,26 +1,25 @@ import BigInt import JWTKit import XCTest - -@testable import SingleFactorAuth +import SingleFactorAuth final class CyanTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SingleFactorAuthArgs! + var singleFactorAuthArgs: SFAParams! - let TOURUS_TEST_EMAIL = "hello@tor.us" + let TORUS_TEST_EMAIL = "hello@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } - func testGetTorusKey() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + func testConnect() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) @@ -28,19 +27,19 @@ final class CyanTest: XCTestCase { } func testInitialise() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546" let savedKey = try await singleFactoreAuth.initialize() XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.publicAddress, savedKey.getPublicAddress()) + XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) } func testAggregrateGetTorusKey() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "66af498ea82c95d52fdb8c8dedd44cf2f758424a0eecab7ac3dd8721527ea2d4" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) diff --git a/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift b/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift new file mode 100644 index 0000000..32f13a5 --- /dev/null +++ b/Tests/SingleFactorAuthTests/SapphireDevnetTests.swift @@ -0,0 +1,49 @@ +import BigInt +import JWTKit +import XCTest +import FetchNodeDetails +import SingleFactorAuth + +final class SapphireDevnetTests: XCTestCase { + var singleFactoreAuth: SingleFactorAuth! + var singleFactorAuthArgs: SFAParams! + + let TORUS_TEST_EMAIL = "devnettestuser@tor.us" + let TEST_VERIFIER = "torus-test-health" + let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" + + override func setUp() { + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .sapphire(.SAPPHIRE_DEVNET)) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) + } + + func testConnect() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) + + let requiredPrivateKey = "230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203" + XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) + XCTAssertEqual("0x462A8BF111A55C9354425F875F89B22678c0Bc44", torusKey.getPublicAddress()) + } + + func testInitialise() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) + let savedKey = try await singleFactoreAuth.initialize() + let requiredPrivateKey = "230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203" + XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) + XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + } + + func testAggregrateGetTorusKey() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) + + let requiredPrivateKey = "edef171853fdf23ed3cfc702d32cf46f181b475a449d2f7b636924cabecd81d4" + XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) + XCTAssertEqual("0xfC58EB0475F1E3fa05877eE2e1350f6A619E2d78", torusKey.getPublicAddress()) + } +} diff --git a/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift b/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift new file mode 100644 index 0000000..16d7131 --- /dev/null +++ b/Tests/SingleFactorAuthTests/SapphireMainnetTests.swift @@ -0,0 +1,49 @@ +import BigInt +import JWTKit +import XCTest +import FetchNodeDetails +import SingleFactorAuth + +final class SapphireMainnetTests: XCTestCase { + var singleFactoreAuth: SingleFactorAuth! + var singleFactorAuthArgs: SFAParams! + + let TORUS_TEST_EMAIL = "devnettestuser@tor.us" + let TEST_VERIFIER = "torus-test-health" + let TEST_AGGREGRATE_VERIFIER = "torus-aggregate-sapphire-mainnet" + + override func setUp() { + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .sapphire(.SAPPHIRE_MAINNET)) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) + } + + func testConnect() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) + + let requiredPrivateKey = "2c4b346a91ecd11fe8a02d111d00bd921bf9b543f0a1e811face91b5f28947d6" + XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) + XCTAssertEqual("0x0934d844a0a6db37CF75aF0269436ae1b2Ae5D36", torusKey.getPublicAddress()) + } + + func testInitialise() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) + let savedKey = try await singleFactoreAuth.initialize() + let requiredPrivateKey = "2c4b346a91ecd11fe8a02d111d00bd921bf9b543f0a1e811face91b5f28947d6" + XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) + XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) + } + + func testAggregrateGetTorusKey() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) + + let requiredPrivateKey = "0c724bb285560dc41e585b91aa2ded94fdd703c2e7133dcc64b1361b0d1fd105" + XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) + XCTAssertEqual("0xA92E2C756B5b2abABc127907b02D4707dc085612", torusKey.getPublicAddress()) + } +} diff --git a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift index 2f6df02..f1d1c07 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -1,45 +1,44 @@ import BigInt import JWTKit import XCTest - -@testable import SingleFactorAuth +import SingleFactorAuth final class SingleFactorAuthTests: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SingleFactorAuthArgs! + var singleFactorAuthArgs: SFAParams! - let TOURUS_TEST_EMAIL = "hello@tor.us" + let TORUS_TEST_EMAIL = "hello@tor.us" let TEST_VERIFIER = "torus-test-health" let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate" override func setUp() { - singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } - func testGetTorusKey() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + func testConnect() async throws { + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) XCTAssertEqual("0x53010055542cCc0f2b6715a5c53838eC4aC96EF7", torusKey.getPublicAddress()) } func testInitialise() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let savedKey = try await singleFactoreAuth.initialize() let requiredPrivateKey = "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4" XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) - XCTAssertEqual(torusKey.publicAddress, savedKey.getPublicAddress()) + XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress()) } func testAggregrateGetTorusKey() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) - let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams) + let idToken = try generateIdToken(email: TORUS_TEST_EMAIL) + let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)]) + let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "ad47959db4cb2e63e641bac285df1b944f54d1a1cecdaeea40042b60d53c35d2" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) diff --git a/Tests/SingleFactorAuthTests/Utils.swift b/Tests/SingleFactorAuthTests/Utils.swift index 8a9e2a6..27c5fb4 100644 --- a/Tests/SingleFactorAuthTests/Utils.swift +++ b/Tests/SingleFactorAuthTests/Utils.swift @@ -12,6 +12,7 @@ struct TestPayload: JWTPayload, Equatable { case iat case email case audience = "aud" + case name } var subject: SubjectClaim @@ -22,6 +23,7 @@ struct TestPayload: JWTPayload, Equatable { var issuer: IssuerClaim var iat: IssuedAtClaim var email: String + var name: String // call its verify method. func verify(using signer: JWTSigner) throws { @@ -29,6 +31,7 @@ struct TestPayload: JWTPayload, Equatable { } } + func generateRandomEmail(of length: Int) -> String { let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" var s = "" @@ -53,12 +56,21 @@ func generateIdToken(email: String) throws -> String { // Parses the JWT and verifies its signature. let today = Date() - let modifiedDate = Calendar.current.date(byAdding: .hour, value: 1, to: today)! + let modifiedDate = Calendar.current.date(byAdding: .minute, value: 2, to: today)! let emailComponent = email.components(separatedBy: "@")[0] let subject = "email|" + emailComponent - let payload = TestPayload(subject: SubjectClaim(stringLiteral: subject), expiration: ExpirationClaim(value: modifiedDate), audience: "torus-key-test", isAdmin: false, emailVerified: true, issuer: "torus-key-test", iat: IssuedAtClaim(value: Date()), email: email) + let payload = TestPayload( + subject: SubjectClaim(stringLiteral: subject), + expiration: ExpirationClaim(value: modifiedDate), // eat + audience: "torus-key-test", + isAdmin: false, + emailVerified: true, + issuer: "torus-key-test", + iat: IssuedAtClaim(value: today), + email: email, + name: email) let jwt = try signers.sign(payload) return jwt } catch {