From c0e51520eaccb424a46790d0f6b9cbbb7511bdf0 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:15:27 +0200 Subject: [PATCH 01/11] refactor: update to match other sdks --- Package.resolved | 8 ++++---- Package.swift | 4 ++-- README.md | 6 +++--- SingleFactorAuth.podspec | 4 ++-- .../{SingleFactorAuthArgs.swift => SFAParams.swift} | 2 +- Sources/SingleFactorAuth/SingleFactorAuth.swift | 13 ++++++++++--- Tests/SingleFactorAuthTests/AquaTest.swift | 12 ++++++------ Tests/SingleFactorAuthTests/CyanTest.swift | 12 ++++++------ .../SingleFactorAuthTests.swift | 12 ++++++------ 9 files changed, 40 insertions(+), 33 deletions(-) rename Sources/SingleFactorAuth/{SingleFactorAuthArgs.swift => SFAParams.swift} (95%) diff --git a/Package.resolved b/Package.resolved index b855b54..e6623d4 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" : "cad9cf59c2aaf062d80b6ad88e6d3ef2680b94ef", + "version" : "4.1.0" } }, { @@ -68,8 +68,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/torusresearch/torus-utils-swift.git", "state" : { - "revision" : "303dc2cf41db7c10f769855edad0e717ced2d554", - "version" : "9.0.0" + "revision" : "23083bf8196d1f78c05b085ba8347f94734b1a16", + "version" : "9.0.1" } } ], diff --git a/Package.swift b/Package.swift index 0d8a593..98c29fb 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: "4.1.0"), ], targets: [ .target( diff --git a/README.md b/README.md index e20c8e1..0bd2df6 100644 --- a/README.md +++ b/README.md @@ -28,14 +28,14 @@ dependencies: [ ## 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. @@ -48,7 +48,7 @@ let loginParams = LoginParams( 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..13c418a 100644 --- a/SingleFactorAuth.podspec +++ b/SingleFactorAuth.podspec @@ -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', '~> 4.1.0' end diff --git a/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift b/Sources/SingleFactorAuth/SFAParams.swift similarity index 95% rename from Sources/SingleFactorAuth/SingleFactorAuthArgs.swift rename to Sources/SingleFactorAuth/SFAParams.swift index 9974c35..0e745cb 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuthArgs.swift +++ b/Sources/SingleFactorAuth/SFAParams.swift @@ -2,7 +2,7 @@ import FetchNodeDetails public typealias Web3AuthNetwork = TorusNetwork -public class SingleFactorAuthArgs { +public class SFAParams{ private var network: TorusNetwork private var networkUrl: String private var web3AuthClientId: String diff --git a/Sources/SingleFactorAuth/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index f4a8058..8e08a11 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -13,7 +13,7 @@ public class SingleFactorAuth { let torusUtils: TorusUtils private var sessionManager: SessionManager - public init(singleFactorAuthArgs: SingleFactorAuthArgs) throws { + public init(singleFactorAuthArgs: SFAParams) throws { sessionManager = .init() nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork()) let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true) @@ -21,11 +21,18 @@ public class SingleFactorAuth { } public func initialize() async throws -> TorusSFAKey { - let data = try await sessionManager.authorizeSession() + 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,7 +83,7 @@ public class SingleFactorAuth { return retrieveSharesResponse } - public func getKey(loginParams: LoginParams) async throws -> TorusSFAKey { + public func connect(loginParams: LoginParams) async throws -> TorusSFAKey { let torusKey = try await getTorusKey(loginParams: loginParams) let publicAddress = torusKey.finalKeyData.evmAddress diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index d677c7c..bf04c58 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -7,21 +7,21 @@ import FetchNodeDetails final class AquaTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SingleFactorAuthArgs! + var singleFactorAuthArgs: SFAParams! let TOURUS_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)) + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) } - func testGetTorusKey() async throws { + func testConnect() 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) @@ -31,7 +31,7 @@ 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let savedKey = try await singleFactoreAuth.initialize() let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8" XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) @@ -41,7 +41,7 @@ final class AquaTest: XCTestCase { 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 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..dd8c1c4 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -6,21 +6,21 @@ import XCTest final class CyanTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SingleFactorAuthArgs! + var singleFactorAuthArgs: SFAParams! let TOURUS_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)) + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) } - func testGetTorusKey() async throws { + func testConnect() 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) @@ -30,7 +30,7 @@ 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546" let savedKey = try await singleFactoreAuth.initialize() XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) @@ -40,7 +40,7 @@ final class CyanTest: XCTestCase { 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "66af498ea82c95d52fdb8c8dedd44cf2f758424a0eecab7ac3dd8721527ea2d4" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) diff --git a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift index 2f6df02..3a350e6 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -6,21 +6,21 @@ import XCTest final class SingleFactorAuthTests: XCTestCase { var singleFactoreAuth: SingleFactorAuth! - var singleFactorAuthArgs: SingleFactorAuthArgs! + var singleFactorAuthArgs: SFAParams! let TOURUS_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)) + singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) } - func testGetTorusKey() async throws { + func testConnect() 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) XCTAssertEqual("0x53010055542cCc0f2b6715a5c53838eC4aC96EF7", torusKey.getPublicAddress()) @@ -29,7 +29,7 @@ final class SingleFactorAuthTests: 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let savedKey = try await singleFactoreAuth.initialize() let requiredPrivateKey = "296045a5599afefda7afbdd1bf236358baff580a0fe2db62ae5c1bbe817fbae4" XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey()) @@ -39,7 +39,7 @@ final class SingleFactorAuthTests: XCTestCase { 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 torusKey = try await singleFactoreAuth.connect(loginParams: loginParams) let requiredPrivateKey = "ad47959db4cb2e63e641bac285df1b944f54d1a1cecdaeea40042b60d53c35d2" XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey()) From 68a370a534f896b157a521b738e1d07b8a8b126b Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:16:00 +0200 Subject: [PATCH 02/11] Update SingleFactorAuth.podspec --- SingleFactorAuth.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SingleFactorAuth.podspec b/SingleFactorAuth.podspec index 13c418a..9c51803 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" From c1184ac43586f885771bc40e174706e2e542c7aa Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:21:31 +0200 Subject: [PATCH 03/11] fix deps --- Package.resolved | 4 ++-- Package.swift | 2 +- SingleFactorAuth.podspec | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Package.resolved b/Package.resolved index e6623d4..8969d80 100644 --- a/Package.resolved +++ b/Package.resolved @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Web3Auth/session-manager-swift.git", "state" : { - "revision" : "cad9cf59c2aaf062d80b6ad88e6d3ef2680b94ef", - "version" : "4.1.0" + "revision" : "b0e429712884059ff096ebfb7ef689d8bc95712b", + "version" : "5.0.0" } }, { diff --git a/Package.swift b/Package.swift index 98c29fb..8a73111 100644 --- a/Package.swift +++ b/Package.swift @@ -18,7 +18,7 @@ let package = Package( .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.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.1.0"), + .package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "5.0.0"), ], targets: [ .target( diff --git a/SingleFactorAuth.podspec b/SingleFactorAuth.podspec index 9c51803..4ef3984 100644 --- a/SingleFactorAuth.podspec +++ b/SingleFactorAuth.podspec @@ -13,5 +13,5 @@ Pod::Spec.new do |spec| spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.3' spec.dependency 'curvelib.swift', '~> 1.0.1' spec.dependency 'Torus-utils', '~> 9.0.1' - spec.dependency 'TorusSessionManager', '~> 4.1.0' + spec.dependency 'TorusSessionManager', '~> 5.0.0' end From 7050a399b6e1ab33885f8955b64d44b931cf5f23 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 13 Sep 2024 10:33:52 +0200 Subject: [PATCH 04/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0bd2df6..ea347e4 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ 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") ], ... ``` From adac22a45195ecf767db17462e954bf72c8530f1 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 13 Sep 2024 11:12:48 +0200 Subject: [PATCH 05/11] update --- .../SingleFactorAuth/{TorusKey.swift => SFAKey.swift} | 2 +- Sources/SingleFactorAuth/SingleFactorAuth.swift | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) rename Sources/SingleFactorAuth/{TorusKey.swift => SFAKey.swift} (93%) 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/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index 8e08a11..bf6d15b 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -20,7 +20,7 @@ public class SingleFactorAuth { try torusUtils = TorusUtils(params: torusOptions) } - public func initialize() async throws -> TorusSFAKey { + 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 } @@ -83,14 +83,14 @@ public class SingleFactorAuth { return retrieveSharesResponse } - public func connect(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 } } From e5ab7cb1fcf8cdc41164b9bdf1a9fe418e8a5fa5 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 17 Sep 2024 05:45:20 +0200 Subject: [PATCH 06/11] Update Package.resolved --- Package.resolved | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.resolved b/Package.resolved index 8969d80..d1fa5fa 100644 --- a/Package.resolved +++ b/Package.resolved @@ -50,7 +50,7 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/Web3Auth/session-manager-swift.git", "state" : { - "revision" : "b0e429712884059ff096ebfb7ef689d8bc95712b", + "revision" : "67d5f7db655d02778861057fb280ecf47c923b09", "version" : "5.0.0" } }, @@ -68,7 +68,7 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/torusresearch/torus-utils-swift.git", "state" : { - "revision" : "23083bf8196d1f78c05b085ba8347f94734b1a16", + "revision" : "eda55b8537a600e657d19d4c452c0a36f217883c", "version" : "9.0.1" } } From 2b48fba8028c04b4cf6b9b5176b777c4b1c2bd33 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 17 Sep 2024 06:03:13 +0200 Subject: [PATCH 07/11] update --- .github/workflows/main.yml | 2 +- Sources/SingleFactorAuth/SingleFactorAuth.swift | 4 ++-- Tests/SingleFactorAuthTests/AquaTest.swift | 2 +- Tests/SingleFactorAuthTests/CyanTest.swift | 2 +- Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) 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/Sources/SingleFactorAuth/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index bf6d15b..ee1da24 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -13,8 +13,8 @@ public class SingleFactorAuth { let torusUtils: TorusUtils private var sessionManager: SessionManager - public init(singleFactorAuthArgs: SFAParams) throws { - sessionManager = .init() + public init(singleFactorAuthArgs: SFAParams, sessionTime: Int) throws { + sessionManager = SessionManager(sessionTime: sessionTime) nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork()) let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true) try torusUtils = TorusUtils(params: torusOptions) diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index bf04c58..d569929 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -15,7 +15,7 @@ final class AquaTest: XCTestCase { override func setUp() { singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs, sessionTime: 86400) } func testConnect() async throws { diff --git a/Tests/SingleFactorAuthTests/CyanTest.swift b/Tests/SingleFactorAuthTests/CyanTest.swift index dd8c1c4..d10a958 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -14,7 +14,7 @@ final class CyanTest: XCTestCase { override func setUp() { singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs, sessionTime: 86400) } func testConnect() async throws { diff --git a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift index 3a350e6..618281a 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -14,7 +14,7 @@ final class SingleFactorAuthTests: XCTestCase { override func setUp() { singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs) + singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs, sessionTime: 86400) } func testConnect() async throws { From 8aa271dbcd805ee01f4854cb92fcddf8d13d5938 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 17 Sep 2024 06:21:34 +0200 Subject: [PATCH 08/11] add default for allowedOrigin --- Sources/SingleFactorAuth/SingleFactorAuth.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/SingleFactorAuth/SingleFactorAuth.swift b/Sources/SingleFactorAuth/SingleFactorAuth.swift index ee1da24..73ae6d8 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -14,7 +14,7 @@ public class SingleFactorAuth { private var sessionManager: SessionManager public init(singleFactorAuthArgs: SFAParams, sessionTime: Int) throws { - sessionManager = SessionManager(sessionTime: sessionTime) + sessionManager = SessionManager(sessionTime: sessionTime, allowedOrigin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork()) let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true) try torusUtils = TorusUtils(params: torusOptions) From e3ec925483d3506b83a5fd88bea6aeda2ed625fe Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 17 Sep 2024 07:07:04 +0200 Subject: [PATCH 09/11] update --- Sources/SingleFactorAuth/SFAParams.swift | 8 +++++++- Sources/SingleFactorAuth/SingleFactorAuth.swift | 8 ++++---- Tests/SingleFactorAuthTests/AquaTest.swift | 2 +- Tests/SingleFactorAuthTests/CyanTest.swift | 2 +- Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift | 2 +- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Sources/SingleFactorAuth/SFAParams.swift b/Sources/SingleFactorAuth/SFAParams.swift index 0e745cb..a198290 100644 --- a/Sources/SingleFactorAuth/SFAParams.swift +++ b/Sources/SingleFactorAuth/SFAParams.swift @@ -6,11 +6,13 @@ 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 SFAParams{ 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 73ae6d8..37ebe79 100644 --- a/Sources/SingleFactorAuth/SingleFactorAuth.swift +++ b/Sources/SingleFactorAuth/SingleFactorAuth.swift @@ -13,10 +13,10 @@ public class SingleFactorAuth { let torusUtils: TorusUtils private var sessionManager: SessionManager - public init(singleFactorAuthArgs: SFAParams, sessionTime: Int) throws { - sessionManager = SessionManager(sessionTime: sessionTime, allowedOrigin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift") - 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) } diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index d569929..a08100b 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -15,7 +15,7 @@ final class AquaTest: XCTestCase { override func setUp() { singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs, sessionTime: 86400) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } func testConnect() async throws { diff --git a/Tests/SingleFactorAuthTests/CyanTest.swift b/Tests/SingleFactorAuthTests/CyanTest.swift index d10a958..56b1cf2 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -14,7 +14,7 @@ final class CyanTest: XCTestCase { override func setUp() { singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs, sessionTime: 86400) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } func testConnect() async throws { diff --git a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift index 618281a..4e08843 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -14,7 +14,7 @@ final class SingleFactorAuthTests: XCTestCase { override func setUp() { singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.TESTNET)) - singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs, sessionTime: 86400) + singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs) } func testConnect() async throws { From 9d1ff3f5a7af2008411dd3a33f81c66ba3324daf Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 17 Sep 2024 07:26:48 +0200 Subject: [PATCH 10/11] update tests --- README.md | 4 +- Tests/SingleFactorAuthTests/AquaTest.swift | 19 ++++--- Tests/SingleFactorAuthTests/CyanTest.swift | 19 ++++--- .../SapphireDevnetTests.swift | 49 +++++++++++++++++++ .../SapphireMainnetTests.swift | 49 +++++++++++++++++++ .../SingleFactorAuthTests.swift | 19 ++++--- 6 files changed, 127 insertions(+), 32 deletions(-) create mode 100644 Tests/SingleFactorAuthTests/SapphireDevnetTests.swift create mode 100644 Tests/SingleFactorAuthTests/SapphireMainnetTests.swift diff --git a/README.md b/README.md index ea347e4..c0fc0d9 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ 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 ) diff --git a/Tests/SingleFactorAuthTests/AquaTest.swift b/Tests/SingleFactorAuthTests/AquaTest.swift index a08100b..2bc5f4d 100644 --- a/Tests/SingleFactorAuthTests/AquaTest.swift +++ b/Tests/SingleFactorAuthTests/AquaTest.swift @@ -2,14 +2,13 @@ import BigInt import JWTKit import XCTest import FetchNodeDetails - -@testable import SingleFactorAuth +import SingleFactorAuth final class AquaTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! 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" @@ -19,8 +18,8 @@ final class AquaTest: XCTestCase { } func testConnect() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) + 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" @@ -29,18 +28,18 @@ 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 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 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" diff --git a/Tests/SingleFactorAuthTests/CyanTest.swift b/Tests/SingleFactorAuthTests/CyanTest.swift index 56b1cf2..ecf1b81 100644 --- a/Tests/SingleFactorAuthTests/CyanTest.swift +++ b/Tests/SingleFactorAuthTests/CyanTest.swift @@ -1,14 +1,13 @@ import BigInt import JWTKit import XCTest - -@testable import SingleFactorAuth +import SingleFactorAuth final class CyanTest: XCTestCase { var singleFactoreAuth: SingleFactorAuth! 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" @@ -18,8 +17,8 @@ final class CyanTest: XCTestCase { } func testConnect() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) + 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" @@ -28,18 +27,18 @@ 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 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 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" 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 4e08843..f1d1c07 100644 --- a/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift +++ b/Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift @@ -1,14 +1,13 @@ import BigInt import JWTKit import XCTest - -@testable import SingleFactorAuth +import SingleFactorAuth final class SingleFactorAuthTests: XCTestCase { var singleFactoreAuth: SingleFactorAuth! 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" @@ -18,8 +17,8 @@ final class SingleFactorAuthTests: XCTestCase { } func testConnect() async throws { - let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL) - let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken) + 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()) @@ -27,18 +26,18 @@ final class SingleFactorAuthTests: 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 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 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" From 5ec723bd201785cc07952c47ec9ac5ab4b2502c7 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 17 Sep 2024 07:33:26 +0200 Subject: [PATCH 11/11] fix: idToken generation --- Tests/SingleFactorAuthTests/Utils.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) 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 {