From 477c9e33788e3132f8b7e184b53d829c326b2ec5 Mon Sep 17 00:00:00 2001 From: ieow Date: Wed, 12 Jul 2023 13:01:05 +0800 Subject: [PATCH 01/19] fix: enable sapphire network, --- Package.resolved | 25 ++++--- Package.swift | 2 +- Sources/CustomAuth/CustomAuth.swift | 68 +++++++++++++------ Sources/CustomAuth/CustomAuthFactory.swift | 65 +++++++++--------- .../EthereumNetworkFND+extension.swift | 41 +++++++---- Tests/CustomAuthTests/CustomAuthTests.swift | 14 ++-- Tests/CustomAuthTests/IntegrationTests.swift | 40 ++++++++++- Tests/CustomAuthTests/MockCASDKFactory.swift | 12 ++-- Tests/CustomAuthTests/MockTorusUtils.swift | 19 +++--- .../StubURLProtocolTests.swift | 11 +-- 10 files changed, 197 insertions(+), 100 deletions(-) diff --git a/Package.resolved b/Package.resolved index be177b8..70e1ee2 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,6 +1,15 @@ { "object": { "pins": [ + { + "package": "AnyCodable", + "repositoryURL": "https://github.com/Flight-School/AnyCodable", + "state": { + "branch": null, + "revision": "862808b2070cd908cb04f9aafe7de83d35f81b05", + "version": "0.6.7" + } + }, { "package": "BigInt", "repositoryURL": "https://github.com/attaswift/BigInt", @@ -15,17 +24,17 @@ "repositoryURL": "https://github.com/krzyzanowskim/CryptoSwift.git", "state": { "branch": null, - "revision": "eee9ad754926c40a0f7e73f152357d37b119b7fa", - "version": "1.7.1" + "revision": "32f641cf24fc7abc1c591a2025e9f2f572648b0f", + "version": "1.7.2" } }, { "package": "FetchNodeDetails", "repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git", "state": { - "branch": null, - "revision": "94a0ee598abfcc2104ceeda80eecf4b55242ba62", - "version": "4.0.1" + "branch": "sapphire", + "revision": "8382c7486c77b9899829665ec2c24e6386373605", + "version": null } }, { @@ -77,9 +86,9 @@ "package": "TorusUtils", "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { - "branch": null, - "revision": "93823201f248ce15dab59c027790da4b73375aed", - "version": "5.0.0" + "branch": "feat/sapphire-test", + "revision": "a837209afb020936d929b8372fa18f059e9d2e18", + "version": null } }, { diff --git a/Package.swift b/Package.swift index ae11919..6c77707 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", from: "5.0.0"), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("feat/sapphire-test")), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.0.1") ], diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 3cf2d00..49bb2c9 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -10,15 +10,17 @@ import Foundation import OSLog import TorusUtils import UIKit +import CommonSources // Global variable var tsSdkLogType = OSLogType.default /// Provides integration of an iOS app with Torus CustomAuth. open class CustomAuth { - let factory: CASDKFactoryProtocol +// let factory: CASDKFactoryProtocol + var nodeDetailManager: NodeDetailManager var torusUtils: AbstractTorusUtils - let fetchNodeDetails: FetchNodeDetails +// let fetchNodeDetails: AllNodeDetailsModel var urlSession: URLSession var enableOneKey: Bool /// You can pass your own custom url rather than using our default infura url, @@ -29,6 +31,7 @@ open class CustomAuth { public let subVerifierDetails: [SubVerifierDetails] public var authorizeURLHandler: URLOpenerTypes? var observer: NSObjectProtocol? // useful for Notifications + var network : TorusNetwork /// Initiate an CustomAuth instance. /// - Parameters: @@ -38,20 +41,21 @@ open class CustomAuth { /// - factory: Providng mocking by implementing TDSDKFactoryProtocol. /// - network: Etherum network to be used. /// - loglevel: Indicates the log level of this instance. All logs lower than this level will be ignored. - public init(aggregateVerifierType: verifierTypes, aggregateVerifier: String, subVerifierDetails: [SubVerifierDetails], factory: CASDKFactoryProtocol = CASDKFactory(), network: EthereumNetworkFND = .MAINNET, loglevel: OSLogType = .debug, urlSession: URLSession = URLSession.shared, enableOneKey: Bool = false, networkUrl: String? = nil) { + public init(aggregateVerifierType: verifierTypes, aggregateVerifier: String, subVerifierDetails: [SubVerifierDetails], network: TorusNetwork = .legacy(.MAINNET), loglevel: OSLogType = .debug, urlSession: URLSession = URLSession.shared, enableOneKey: Bool = false, networkUrl: String? = nil) { tsSdkLogType = loglevel self.networkUrl = networkUrl self.enableOneKey = enableOneKey // factory method - self.factory = factory + self.nodeDetailManager = NodeDetailManager(network: network) self.urlSession = urlSession - torusUtils = factory.createTorusUtils(loglevel: loglevel, urlSession: urlSession, enableOneKey: enableOneKey, network: network) - fetchNodeDetails = factory.createFetchNodeDetails(network: network, urlSession: urlSession, networkUrl: networkUrl) + + self.torusUtils = TorusUtils(loglevel: loglevel, urlSession: urlSession, enableOneKey: enableOneKey, serverTimeOffset: 1000, network: network) // verifier details self.aggregateVerifier = aggregateVerifier self.aggregateVerifierType = aggregateVerifierType self.subVerifierDetails = subVerifierDetails + self.network = network } /// Initiate an CustomAuth instance. @@ -60,19 +64,19 @@ open class CustomAuth { /// - aggregateVerifier: Name of the verifier to be used.. /// - subVerifierDetails: Details of each subverifiers to be used. public convenience init(aggregateVerifierType: verifierTypes, aggregateVerifier: String, subVerifierDetails: [SubVerifierDetails], enableOneKey: Bool = false, networkUrl: String? = nil) { - let factory = CASDKFactory() - self.init(aggregateVerifierType: aggregateVerifierType, aggregateVerifier: aggregateVerifier, subVerifierDetails: subVerifierDetails, factory: factory, network: .MAINNET, loglevel: .debug, enableOneKey: enableOneKey, networkUrl: networkUrl) +// let factory = CASDKFactory() + self.init(aggregateVerifierType: aggregateVerifierType, aggregateVerifier: aggregateVerifier, subVerifierDetails: subVerifierDetails, network: .legacy(.MAINNET), loglevel: .debug, enableOneKey: enableOneKey, networkUrl: networkUrl) } public convenience init(aggregateVerifierType: verifierTypes, aggregateVerifier: String, subVerifierDetails: [SubVerifierDetails], loglevel: OSLogType = .debug, enableOneKey: Bool = false, networkUrl: String? = nil) { - let factory = CASDKFactory() - self.init(aggregateVerifierType: aggregateVerifierType, aggregateVerifier: aggregateVerifier, subVerifierDetails: subVerifierDetails, factory: factory, network: .MAINNET, loglevel: loglevel, enableOneKey: enableOneKey, networkUrl: networkUrl) +// let factory = CASDKFactory() + self.init(aggregateVerifierType: aggregateVerifierType, aggregateVerifier: aggregateVerifier, subVerifierDetails: subVerifierDetails, network: .legacy(.MAINNET), loglevel: loglevel, enableOneKey: enableOneKey, networkUrl: networkUrl) } /// Retrieve information of Torus nodes from a predefined Etherum contract. /// - Returns: An array of URLs to the nodes. open func getNodeDetailsFromContract(verifier: String, verfierID: String) async throws -> AllNodeDetailsModel { - let nodeDetails = try await fetchNodeDetails.getNodeDetails(verifier: verifier, verifierID: verfierID) + let nodeDetails = try await nodeDetailManager.getNodeDetails(verifier: verifier, verifierID: verfierID) return nodeDetails } @@ -184,14 +188,25 @@ open class CustomAuth { /// - userData: Custom data that will be returned with `privateKey` and `publicAddress`. /// - Returns: A promise that resolve with a Dictionary that contain at least `privateKey` and `publicAddress` field.. open func getTorusKey(verifier: String, verifierId: String, idToken: String, userData: [String: Any] = [:]) async throws -> [String: Any] { - let extraParams = ["verifier_id": verifierId] as [String: Any] + let extraParams = ["verifier_id": verifierId] as [String: Codable] let buffer: Data = try! NSKeyedArchiver.archivedData(withRootObject: extraParams, requiringSecureCoding: false) + let verifierParams = VerifierParams(verifier_id: verifierId) do { - let nodeDetails = try await getNodeDetailsFromContract(verifier: verifier, verfierID: verifierId) - let responseFromRetrieveShares = try await torusUtils.retrieveShares(torusNodePubs: nodeDetails.getTorusNodePub(), endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: idToken, extraParams: buffer) + let nodeDetails = try await nodeDetailManager.getNodeDetails(verifier: verifier, verifierID: verifierId) + let responseFromRetrieveShares : RetrieveSharesResponse; + switch network { + case .legacy(_): + responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) + case .sapphire(_): + responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeSSSEndpoints, torusNodePubs: nil, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) + } + + +// (torusNodePubs: nodeDetails.getTorusNodePub(), endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: idToken, extraParams: buffer) var data = userData - data["privateKey"] = responseFromRetrieveShares.privateKey - data["publicAddress"] = responseFromRetrieveShares.publicAddress + print(responseFromRetrieveShares) + data["privateKey"] = responseFromRetrieveShares.privKey + data["publicAddress"] = responseFromRetrieveShares.ethAddress return data } catch { os_log("handleSingleLogin: err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) @@ -206,15 +221,26 @@ open class CustomAuth { /// - subVerifierDetails: An array of verifiers to be used for the aggregate login flow, with their respective token and verifier name. /// - Returns: A promise that resolve with a Dictionary that contain at least `privateKey` and `publicAddress` field.. open func getAggregateTorusKey(verifier: String, verifierId: String, idToken: String, subVerifierDetails: SubVerifierDetails, userData: [String: Any] = [:]) async throws -> [String: Any] { - let extraParams = ["verifieridentifier": verifier, "verifier_id": verifierId, "sub_verifier_ids": [subVerifierDetails.verifier], "verify_params": [["verifier_id": verifierId, "idtoken": idToken]]] as [String: Any] + let extraParams = ["verifieridentifier": verifier, "verifier_id": verifierId, "sub_verifier_ids": [subVerifierDetails.verifier], "verify_params": [["verifier_id": verifierId, "idtoken": idToken]]] as [String: Codable] let buffer: Data = try! NSKeyedArchiver.archivedData(withRootObject: extraParams, requiringSecureCoding: false) let hashedOnce = idToken.sha3(.keccak256) + + let verifierParams = VerifierParams(verifier_id: verifierId) + do { - let nodeDetails = try await getNodeDetailsFromContract(verifier: verifier, verfierID: verifierId) - let responseFromRetrieveShares = try await self.torusUtils.retrieveShares(torusNodePubs: nodeDetails.getTorusNodePub(), endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: hashedOnce, extraParams: buffer) + let nodeDetails = try await getNodeDetailsFromContract(verifier: verifier, verfierID: verifierId) + let responseFromRetrieveShares :RetrieveSharesResponse; + switch network { + case .legacy(_): + responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) + case .sapphire(_): + responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeSSSEndpoints, torusNodePubs: nil, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) + } + +// (torusNodePubs: nodeDetails.torusNodePub, endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: hashedOnce, extraParams: buffer) var data = userData - data["privateKey"] = responseFromRetrieveShares.privateKey - data["publicAddress"] = responseFromRetrieveShares.publicAddress + data["privateKey"] = responseFromRetrieveShares.privKey + data["publicAddress"] = responseFromRetrieveShares.ethAddress return data } catch { os_log("handleSingleIdVerifier err: %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) diff --git a/Sources/CustomAuth/CustomAuthFactory.swift b/Sources/CustomAuth/CustomAuthFactory.swift index 09af25e..9eaf432 100644 --- a/Sources/CustomAuth/CustomAuthFactory.swift +++ b/Sources/CustomAuth/CustomAuthFactory.swift @@ -9,41 +9,42 @@ import FetchNodeDetails import Foundation import OSLog import TorusUtils +import CommonSources /// A protocol should be implmented by users of `CustomAuth`. It provides a way /// to stub or mock the CustomAuth for testing. public protocol CASDKFactoryProtocol { - func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: EthereumNetworkFND) -> AbstractTorusUtils - func createFetchNodeDetails(network: EthereumNetworkFND, urlSession: URLSession, networkUrl: String?) -> FetchNodeDetails + func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: TorusNetwork) -> AbstractTorusUtils +// func createFetchNodeDetails(network: TorusNetwork, urlSession: URLSession, networkUrl: String?) -> AllNodeDetailsModel } -public class CASDKFactory: CASDKFactoryProtocol { - public func createFetchNodeDetails(network: EthereumNetworkFND, urlSession: URLSession = URLSession.shared, networkUrl: String? = nil) -> FetchNodeDetails { - var proxyAddress: String = "" - switch network { - case .MAINNET: - proxyAddress = FetchNodeDetails.proxyAddressMainnet - case .TESTNET: - proxyAddress = FetchNodeDetails.proxyAddressTestnet - case .CYAN: - proxyAddress = FetchNodeDetails.proxyAddressCyan - case .AQUA: - proxyAddress = FetchNodeDetails.proxyAddressAqua - case .CELESTE: - proxyAddress = FetchNodeDetails.proxyAddressCeleste - default: - proxyAddress = FetchNodeDetails.proxyAddressMainnet - } - guard let networkUrl = networkUrl else { return FetchNodeDetails(proxyAddress: proxyAddress, network: network, urlSession: urlSession) } - return FetchNodeDetails(proxyAddress: proxyAddress, network: .CUSTOM(path: networkUrl), urlSession: urlSession) - } - - public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession = URLSession.shared, enableOneKey: Bool, network: EthereumNetworkFND) -> AbstractTorusUtils { - let allowHost = network.signerMap.appending("/api/allow") - let signerHost = network.signerMap.appending("/api/sign") - return TorusUtils(loglevel: loglevel, urlSession: urlSession, enableOneKey: enableOneKey, signerHost: signerHost, allowHost: allowHost, network: network) - } - - public init() { - } -} +//public class CASDKFactory: CASDKFactoryProtocol { +// public func createFetchNodeDetails(network: TorusNetwork, urlSession: URLSession = URLSession.shared, networkUrl: String? = nil) -> FetchNodeDetails { +// var proxyAddress: String = "" +// switch network { +// case .MAINNET: +// proxyAddress = FetchNodeDetails.proxyAddressMainnet +// case .TESTNET: +// proxyAddress = FetchNodeDetails.proxyAddressTestnet +// case .CYAN: +// proxyAddress = FetchNodeDetails.proxyAddressCyan +// case .AQUA: +// proxyAddress = FetchNodeDetails.proxyAddressAqua +// case .CELESTE: +// proxyAddress = FetchNodeDetails.proxyAddressCeleste +// default: +// proxyAddress = FetchNodeDetails.proxyAddressMainnet +// } +// guard let networkUrl = networkUrl else { return FetchNodeDetails(proxyAddress: proxyAddress, network: network, urlSession: urlSession) } +// return FetchNodeDetails(proxyAddress: proxyAddress, network: .CUSTOM(path: networkUrl), urlSession: urlSession) +// } +// +// public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession = URLSession.shared, enableOneKey: Bool, network: TorusNetwork) -> AbstractTorusUtils { +// let allowHost = network.signerMap.appending("/api/allow") +// let signerHost = network.signerMap.appending("/api/sign") +// return TorusUtils(loglevel: loglevel, urlSession: urlSession, enableOneKey: enableOneKey, signerHost: signerHost, allowHost: allowHost, network: network) +// } +// +// public init() { +// } +//} diff --git a/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift b/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift index 5520ff0..ecffad1 100644 --- a/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift +++ b/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift @@ -7,22 +7,37 @@ import FetchNodeDetails import Foundation +import CommonSources -extension EthereumNetworkFND { +extension TorusNetwork { public var signerMap: String { switch self { - case .MAINNET: - return "https://signer.tor.us" - case .TESTNET: - return "https://signer.tor.us" - case .CYAN: - return "https://signer-polygon.tor.us" - case .AQUA: - return "https://signer-polygon.tor.us" - case .CELESTE: - return "https://signer-polygon.tor.us" - case let .CUSTOM(path): - return "https://signer.tor.us" + case .legacy(let network) : + switch network { + case .MAINNET: + return "https://signer.tor.us" + case .TESTNET: + return "https://signer.tor.us" + case .CYAN: + return "https://signer-polygon.tor.us" + case .AQUA: + return "https://signer-polygon.tor.us" + case .CELESTE: + return "https://signer-polygon.tor.us" + case let .CUSTOM(path): + return "https://signer.tor.us" + } + case .sapphire(let network) : + switch network { + + case .SAPPHIRE_MAINNET: + return "https://signer.tor.us" + case .SAPPHIRE_TESTNET: + return "https://signer.tor.us" + case .SAPPHIRE_DEVNET: + return "https://signer.tor.us" + + } } } } diff --git a/Tests/CustomAuthTests/CustomAuthTests.swift b/Tests/CustomAuthTests/CustomAuthTests.swift index cc5af65..5590615 100644 --- a/Tests/CustomAuthTests/CustomAuthTests.swift +++ b/Tests/CustomAuthTests/CustomAuthTests.swift @@ -22,9 +22,11 @@ final class MockSDKTest: XCTestCase { let subVerifier = [SubVerifierDetails(loginProvider: .jwt, clientId: fakeData.generateVerifier(), verifier: expectedVerifier, redirectURL: fakeData.generateVerifier())] let factory = MockFactory() - let CustomAuth = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifier, factory: factory) - var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils - +// let CustomAuth = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifiery) + let CustomAuth = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: expectedVerifier, subVerifierDetails: []) +// var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils + var mockTorusUtils = MockTorusUtils() + CustomAuth.torusUtils = mockTorusUtils // Set Mock data mockTorusUtils.retrieveShares_output["privateKey"] = expectedPrivateKey mockTorusUtils.retrieveShares_output["publicAddress"] = expectedPublicAddress @@ -55,8 +57,10 @@ final class MockSDKTest: XCTestCase { let subVerifier = [SubVerifierDetails(loginProvider: .jwt, clientId: fakeData.generateVerifier(), verifier: expectedVerifier, redirectURL: fakeData.generateVerifier())] let factory = MockFactory() - let CustomAuth = CustomAuth(aggregateVerifierType: .singleIdVerifier, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifier, factory: factory) - var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils + let CustomAuth = CustomAuth(aggregateVerifierType: .singleIdVerifier, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifier) +// var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils + var mockTorusUtils = MockTorusUtils() + CustomAuth.torusUtils = mockTorusUtils // Set Mock data mockTorusUtils.retrieveShares_output["privateKey"] = expectedPrivateKey diff --git a/Tests/CustomAuthTests/IntegrationTests.swift b/Tests/CustomAuthTests/IntegrationTests.swift index dc8b707..716d507 100644 --- a/Tests/CustomAuthTests/IntegrationTests.swift +++ b/Tests/CustomAuthTests/IntegrationTests.swift @@ -23,7 +23,7 @@ final class IntegrationTests: XCTestCase { redirectURL: "com.googleusercontent.apps.238941746713-vfap8uumijal4ump28p9jd3lbe6onqt4:/oauthredirect", browserRedirectURL: "https://scripts.toruswallet.io/redirect.html") - IntegrationTests.sdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: "torus-test-ios-public", subVerifierDetails: [sub], network: .CYAN) + IntegrationTests.sdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: "torus-test-ios-public", subVerifierDetails: [sub], network: .legacy(.CYAN)) } func test_getTorusKey() async { @@ -37,6 +37,44 @@ final class IntegrationTests: XCTestCase { XCTAssertEqual(data?["publicAddress"] as! String, "0x8AA6C8ddCD868873120aA265Fc63E3a2180375BA") exp1.fulfill() } catch { + print(error) + XCTFail(error.localizedDescription) + } + wait(for: [exp1], timeout: 15) + } + + + + let TORUS_TEST_EMAIL = "saasas@tr.us"; + let TORUS_IMPORT_EMAIL = "importeduser2@tor.us"; + + let TORUS_EXTENDED_VERIFIER_EMAIL = "testextenderverifierid@example.com"; + + let TORUS_TEST_VERIFIER = "torus-test-health"; + + let TORUS_TEST_AGGREGATE_VERIFIER = "torus-test-health-aggregate"; + let HashEnabledVerifier = "torus-test-verifierid-hash"; + + func test_Sapphire_getTorusKey() async { + + let sub = SubVerifierDetails(loginType: .web, + loginProvider: .google, + clientId: "221898609709-obfn3p63741l5333093430j3qeiinaa8.apps.googleusercontent.com", + verifier: "google-lrc", + redirectURL: "com.googleusercontent.apps.238941746713-vfap8uumijal4ump28p9jd3lbe6onqt4:/oauthredirect", + browserRedirectURL: "https://scripts.toruswallet.io/redirect.html") + let sdk = CustomAuth(aggregateVerifierType: .singleLogin, aggregateVerifier: "torus-test-ios-public", subVerifierDetails: [sub], network: .sapphire(.SAPPHIRE_DEVNET)) + + + let exp1 = XCTestExpectation(description: "Should be able to get key") + let email = "hello@tor.us" + let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL) + do { + let data = try await sdk.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: jwt) + XCTAssertEqual(data["publicAddress"] as! String, "0x4924f91f5d6701ddd41042d94832bb17b76f316f".lowercased()) + exp1.fulfill() + } catch { + print(error) XCTFail(error.localizedDescription) } wait(for: [exp1], timeout: 15) diff --git a/Tests/CustomAuthTests/MockCASDKFactory.swift b/Tests/CustomAuthTests/MockCASDKFactory.swift index 547808f..76bea80 100644 --- a/Tests/CustomAuthTests/MockCASDKFactory.swift +++ b/Tests/CustomAuthTests/MockCASDKFactory.swift @@ -10,16 +10,16 @@ import FetchNodeDetails import Foundation import OSLog import TorusUtils - +import CommonSources public class MockFactory: CASDKFactoryProtocol { init() {} - public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: EthereumNetworkFND) -> AbstractTorusUtils { + public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: TorusNetwork) -> AbstractTorusUtils { MockTorusUtils() } - public func createFetchNodeDetails(network: EthereumNetworkFND, urlSession: URLSession, networkUrl: String? = nil) -> FetchNodeDetails { - let net = network == .MAINNET ? "0xf20336e16B5182637f09821c27BDe29b0AFcfe80" : "0x6258c9d6c12ed3edda59a1a6527e469517744aa7" - return FetchNodeDetails(proxyAddress: net, network: network) - } +// public func createFetchNodeDetails(network: TorusNetwork, urlSession: URLSession, networkUrl: String? = nil) -> AllNodeDetailsModel { +// let net = network == .MAINNET ? "0xf20336e16B5182637f09821c27BDe29b0AFcfe80" : "0x6258c9d6c12ed3edda59a1a6527e469517744aa7" +// return AllNodeDetailsModel(proxyAddress: net, network: network) +// } } diff --git a/Tests/CustomAuthTests/MockTorusUtils.swift b/Tests/CustomAuthTests/MockTorusUtils.swift index ebd4f0b..dc1a66d 100644 --- a/Tests/CustomAuthTests/MockTorusUtils.swift +++ b/Tests/CustomAuthTests/MockTorusUtils.swift @@ -1,6 +1,8 @@ import CustomAuth import FetchNodeDetails import Foundation +import CommonSources +import BigInt @testable import TorusUtils // Added so the that we can assign values later. @@ -10,27 +12,28 @@ public protocol MockAbstractTorusUtils { } class MockTorusUtils: AbstractTorusUtils, MockAbstractTorusUtils { - func retrieveShares(torusNodePubs: [TorusNodePubModel], endpoints: [String], verifier: String, verifierId: String, idToken: String, extraParams: Data) async throws -> RetrieveSharesResponseModel { + + func retrieveShares(endpoints: [String], torusNodePubs: [CommonSources.TorusNodePubModel]?, verifier: String, verifierParams: VerifierParams, idToken: String, extraParams: [String : Codable]) async throws -> RetrieveSharesResponse { retrieveShares_input = [ "endpoints": endpoints, "verifierIdentifier": verifier, - "verifierId": verifierId, + "verifierId": verifierParams.verifier_id, "idToken": idToken, "extraParams": extraParams ] - return .init(publicKey: retrieveShares_output["publicAddress"] ?? "", privateKey: retrieveShares_output["privateKey"] ?? "") + return .init(ethAddress: retrieveShares_output["publicAddress"] ?? "", privKey: retrieveShares_output["privateKey"] ?? "", sessionTokenData: [], X: "", Y: "", metadataNonce: BigInt(0), postboxPubKeyX: "", postboxPubKeyY: "", sessionAuthKey: "", nodeIndexes: []) } - func getPublicAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel], verifier: String, verifierId: String, isExtended: Bool) async throws -> GetPublicAddressModel { - return .init(address: "") + func getPublicAddress(endpoints: [String], torusNodePubs: [CommonSources.TorusNodePubModel]?, verifier: String, verifierId: String, extendedVerifierId: String?) async throws -> String { + return "" } - func getUserTypeAndAddress(endpoints: [String], torusNodePub: [TorusNodePubModel], verifier: String, verifierID: String, doesKeyAssign: Bool)async throws -> GetUserAndAddressModel { + func getUserTypeAndAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierID: String, doesKeyAssign: Bool)async throws -> GetUserAndAddress{ return .init(typeOfUser: .v1, address: "", x: "", y: "") } - func getOrSetNonce(x: String, y: String, privateKey: String?, getOnly: Bool) async throws -> GetOrSetNonceResultModel { - return GetOrSetNonceResultModel(typeOfUser: "v1") + func getOrSetNonce(x: String, y: String, privateKey: String?, getOnly: Bool) async throws -> GetOrSetNonceResult { + return GetOrSetNonceResult(typeOfUser: "v1") } var retrieveShares_input: [String: Any] = [:] diff --git a/Tests/CustomAuthTests/StubURLProtocolTests.swift b/Tests/CustomAuthTests/StubURLProtocolTests.swift index 4cf6b7f..0a6717f 100644 --- a/Tests/CustomAuthTests/StubURLProtocolTests.swift +++ b/Tests/CustomAuthTests/StubURLProtocolTests.swift @@ -11,6 +11,7 @@ import Foundation import OSLog import TorusUtils import XCTest +import CommonSources final class StubURLProtocolTests: XCTestCase { // func testStubURLProtocol() { @@ -50,12 +51,12 @@ public class StubMockTorusUtils: TorusUtils { } public class StubMockCASDKFactory: CASDKFactoryProtocol { - public func createFetchNodeDetails(network: EthereumNetworkFND, urlSession: URLSession, networkUrl: String? = nil) -> FetchNodeDetails { - let net = network == .MAINNET ? "0xf20336e16B5182637f09821c27BDe29b0AFcfe80" : "0x6258c9d6c12ed3edda59a1a6527e469517744aa7" - return FetchNodeDetails(proxyAddress: net, network: network, urlSession: urlSession) - } +// public func createFetchNodeDetails(network: TorusNetwork, urlSession: URLSession, networkUrl: String? = nil) -> FetchNodeDetails { +// let net = network == .MAINNET ? "0xf20336e16B5182637f09821c27BDe29b0AFcfe80" : "0x6258c9d6c12ed3edda59a1a6527e469517744aa7" +// return FetchNodeDetails(proxyAddress: net, network: network, urlSession: urlSession) +// } - public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: EthereumNetworkFND) -> AbstractTorusUtils { + public func createTorusUtils(loglevel: OSLogType, urlSession: URLSession, enableOneKey: Bool, network: TorusNetwork) -> AbstractTorusUtils { let allowHost = network.signerMap.appending("/api/allow") let signerHost = network.signerMap.appending("/api/sign") return StubMockTorusUtils(loglevel: loglevel, urlSession: urlSession, enableOneKey: enableOneKey, signerHost: signerHost, allowHost: allowHost) From a8a8c4b6f34b279e1ed13bfef63209b862755226 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 13 Jul 2023 14:48:20 +0800 Subject: [PATCH 02/19] fix: added sessionTokenData and AuthKey --- Sources/CustomAuth/CustomAuth.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 49bb2c9..f78a79c 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -207,6 +207,10 @@ open class CustomAuth { print(responseFromRetrieveShares) data["privateKey"] = responseFromRetrieveShares.privKey data["publicAddress"] = responseFromRetrieveShares.ethAddress + data["sessionTokenData"] = responseFromRetrieveShares.sessionTokenData + data["sessionAuthKey"] = responseFromRetrieveShares.sessionAuthKey + + return data } catch { os_log("handleSingleLogin: err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) From bb362100d6efd2915238d455430fbfb5a56a744c Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 13 Jul 2023 18:07:42 +0800 Subject: [PATCH 03/19] fix: add datatype for aggregateTorusKey --- Sources/CustomAuth/CustomAuth.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index f78a79c..97e103a 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -245,6 +245,8 @@ open class CustomAuth { var data = userData data["privateKey"] = responseFromRetrieveShares.privKey data["publicAddress"] = responseFromRetrieveShares.ethAddress + data["sessionTokenData"] = responseFromRetrieveShares.sessionTokenData + data["sessionAuthKey"] = responseFromRetrieveShares.sessionAuthKey return data } catch { os_log("handleSingleIdVerifier err: %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) From 1f047cc07bb430fedbbd2f16ffc7c50856fb0911 Mon Sep 17 00:00:00 2001 From: hqjang-pepper Date: Wed, 26 Jul 2023 01:25:56 +0900 Subject: [PATCH 04/19] update sapphire pkg & codes --- Package.resolved | 6 +- Package.swift | 4 +- Sources/CustomAuth/CustomAuth.swift | 118 +++++++++++++++++-- Tests/CustomAuthTests/CustomAuthTests.swift | 40 ++++--- Tests/CustomAuthTests/IntegrationTests.swift | 12 +- Tests/CustomAuthTests/MockTorusUtils.swift | 10 +- 6 files changed, 149 insertions(+), 41 deletions(-) diff --git a/Package.resolved b/Package.resolved index 70e1ee2..09d58a4 100644 --- a/Package.resolved +++ b/Package.resolved @@ -33,7 +33,7 @@ "repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git", "state": { "branch": "sapphire", - "revision": "8382c7486c77b9899829665ec2c24e6386373605", + "revision": "5dc0393cb895e577f8666c30911dc07879748d62", "version": null } }, @@ -86,8 +86,8 @@ "package": "TorusUtils", "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { - "branch": "feat/sapphire-test", - "revision": "a837209afb020936d929b8372fa18f059e9d2e18", + "branch": "sapphire-fix", + "revision": "76bf518b88dc7483490f20156014556fbfedfcbd", "version": null } }, diff --git a/Package.swift b/Package.swift index 6c77707..761944f 100644 --- a/Package.swift +++ b/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "CustomAuth", platforms: [ - .iOS(.v13) + .iOS(.v13), .macOS(.v10_15) ], products: [ .library( @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("feat/sapphire-test")), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("sapphire-fix")), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.0.1") ], diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 97e103a..c3614a5 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -193,7 +193,7 @@ open class CustomAuth { let verifierParams = VerifierParams(verifier_id: verifierId) do { let nodeDetails = try await nodeDetailManager.getNodeDetails(verifier: verifier, verifierID: verifierId) - let responseFromRetrieveShares : RetrieveSharesResponse; + let responseFromRetrieveShares : TorusKey; switch network { case .legacy(_): responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) @@ -205,11 +205,60 @@ open class CustomAuth { // (torusNodePubs: nodeDetails.getTorusNodePub(), endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: idToken, extraParams: buffer) var data = userData print(responseFromRetrieveShares) - data["privateKey"] = responseFromRetrieveShares.privKey - data["publicAddress"] = responseFromRetrieveShares.ethAddress - data["sessionTokenData"] = responseFromRetrieveShares.sessionTokenData - data["sessionAuthKey"] = responseFromRetrieveShares.sessionAuthKey + let finalKeyData = responseFromRetrieveShares.finalKeyData + let oAuthKeyData = responseFromRetrieveShares.oAuthKeyData + let sessionData = responseFromRetrieveShares.sessionData + let metadata = responseFromRetrieveShares.metadata + let nodesData = responseFromRetrieveShares.nodesData + + // Convert each property to type Any and add them to the dictionary + if let finalKeyData = finalKeyData { + data["finalKeyData"] = [ + "evmAddress": finalKeyData.evmAddress, + "X": finalKeyData.X, + "Y": finalKeyData.Y, + "privKey": finalKeyData.privKey as Any // privKey is an optional, so cast to Any + ] + } + + if let oAuthKeyData = oAuthKeyData { + data["oAuthKeyData"] = [ + "evmAddress": oAuthKeyData.evmAddress, + "X": oAuthKeyData.X, + "Y": oAuthKeyData.Y, + "privKey": oAuthKeyData.privKey + ] + } + + if let sessionData = sessionData { + data["sessionData"] = [ + "sessionTokenData": sessionData.sessionTokenData.map { sessionToken in + return [ + "token": sessionToken.token, + "signature": sessionToken.signature, + "node_pubx": sessionToken.node_pubx, + "node_puby": sessionToken.node_puby + ] + }, + "sessionAuthKey": sessionData.sessionAuthKey + ] + } + + if let metadata = metadata { + data["metadata"] = [ + "pubNonce": metadata.pubNonce as Any, // pubNonce is optional, so cast to Any + "nonce": metadata.nonce as Any, // nonce is optional, so cast to Any + "typeOfUser": metadata.typeOfUser.rawValue, // UserType needs to be converted to rawValue + "upgraded": metadata.upgraded as Any // upgraded is optional, so cast to Any + ] + } + + if let nodesData = nodesData { + data["nodesData"] = [ + "nodeIndexes": nodesData.nodeIndexes + ] + } return data } catch { @@ -233,7 +282,7 @@ open class CustomAuth { do { let nodeDetails = try await getNodeDetailsFromContract(verifier: verifier, verfierID: verifierId) - let responseFromRetrieveShares :RetrieveSharesResponse; + let responseFromRetrieveShares :TorusKey; switch network { case .legacy(_): responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) @@ -243,10 +292,59 @@ open class CustomAuth { // (torusNodePubs: nodeDetails.torusNodePub, endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: hashedOnce, extraParams: buffer) var data = userData - data["privateKey"] = responseFromRetrieveShares.privKey - data["publicAddress"] = responseFromRetrieveShares.ethAddress - data["sessionTokenData"] = responseFromRetrieveShares.sessionTokenData - data["sessionAuthKey"] = responseFromRetrieveShares.sessionAuthKey + let finalKeyData = responseFromRetrieveShares.finalKeyData + let oAuthKeyData = responseFromRetrieveShares.oAuthKeyData + let sessionData = responseFromRetrieveShares.sessionData + let metadata = responseFromRetrieveShares.metadata + let nodesData = responseFromRetrieveShares.nodesData + + // Convert each property to type Any and add them to the dictionary + if let finalKeyData = finalKeyData { + data["finalKeyData"] = [ + "evmAddress": finalKeyData.evmAddress, + "X": finalKeyData.X, + "Y": finalKeyData.Y, + "privKey": finalKeyData.privKey as Any // privKey is an optional, so cast to Any + ] + } + + if let oAuthKeyData = oAuthKeyData { + data["oAuthKeyData"] = [ + "evmAddress": oAuthKeyData.evmAddress, + "X": oAuthKeyData.X, + "Y": oAuthKeyData.Y, + "privKey": oAuthKeyData.privKey + ] + } + + if let sessionData = sessionData { + data["sessionData"] = [ + "sessionTokenData": sessionData.sessionTokenData.map { sessionToken in + return [ + "token": sessionToken.token, + "signature": sessionToken.signature, + "node_pubx": sessionToken.node_pubx, + "node_puby": sessionToken.node_puby + ] + }, + "sessionAuthKey": sessionData.sessionAuthKey + ] + } + + if let metadata = metadata { + data["metadata"] = [ + "pubNonce": metadata.pubNonce as Any, // pubNonce is optional, so cast to Any + "nonce": metadata.nonce as Any, // nonce is optional, so cast to Any + "typeOfUser": metadata.typeOfUser.rawValue, // UserType needs to be converted to rawValue + "upgraded": metadata.upgraded as Any // upgraded is optional, so cast to Any + ] + } + + if let nodesData = nodesData { + data["nodesData"] = [ + "nodeIndexes": nodesData.nodeIndexes + ] + } return data } catch { os_log("handleSingleIdVerifier err: %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) diff --git a/Tests/CustomAuthTests/CustomAuthTests.swift b/Tests/CustomAuthTests/CustomAuthTests.swift index 5590615..5cb4669 100644 --- a/Tests/CustomAuthTests/CustomAuthTests.swift +++ b/Tests/CustomAuthTests/CustomAuthTests.swift @@ -31,14 +31,16 @@ final class MockSDKTest: XCTestCase { mockTorusUtils.retrieveShares_output["privateKey"] = expectedPrivateKey mockTorusUtils.retrieveShares_output["publicAddress"] = expectedPublicAddress do { - let nodeDetails = try await CustomAuth.getNodeDetailsFromContract(verifier: expectedVerifier, verfierID: expectedVerfierId) - let data = try await CustomAuth.getTorusKey(verifier: expectedVerifier, verifierId: expectedVerfierId, idToken: fakeData.generateVerifier()) - let mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils - XCTAssertEqual(mockTorusUtils.retrieveShares_input["endpoints"] as? [String], nodeDetails.getTorusNodeEndpoints()) - XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierIdentifier"] as? String, expectedVerifier) - XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierId"] as? String, expectedVerfierId) - XCTAssertEqual(data["privateKey"] as? String, expectedPrivateKey) - XCTAssertEqual(data["publicAddress"] as? String, expectedPublicAddress) + let nodeDetails = try await CustomAuth.getNodeDetailsFromContract(verifier: expectedVerifier, verfierID: expectedVerfierId) + let data = try await CustomAuth.getTorusKey(verifier: expectedVerifier, verifierId: expectedVerfierId, idToken: fakeData.generateVerifier()) + let mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils + let FinalKeyData = data["finalKeyData"] as! [String: Any] + print(FinalKeyData) + XCTAssertEqual(mockTorusUtils.retrieveShares_input["endpoints"] as? [String], nodeDetails.getTorusNodeEndpoints()) + XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierIdentifier"] as? String, expectedVerifier) + XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierId"] as? String, expectedVerfierId) + XCTAssertEqual(FinalKeyData["privKey"] as! String, expectedPrivateKey) + XCTAssertEqual(FinalKeyData["evmAddress"] as! String, expectedPublicAddress) expectation.fulfill() } catch { XCTFail(error.localizedDescription) @@ -55,25 +57,27 @@ final class MockSDKTest: XCTestCase { let expectedVerfierId = fakeData.generateRandomEmail(of: 6) let subVerifier = [SubVerifierDetails(loginProvider: .jwt, clientId: fakeData.generateVerifier(), verifier: expectedVerifier, redirectURL: fakeData.generateVerifier())] - let factory = MockFactory() let CustomAuth = CustomAuth(aggregateVerifierType: .singleIdVerifier, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifier) // var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils - var mockTorusUtils = MockTorusUtils() + let mockTorusUtils = MockTorusUtils() CustomAuth.torusUtils = mockTorusUtils // Set Mock data mockTorusUtils.retrieveShares_output["privateKey"] = expectedPrivateKey mockTorusUtils.retrieveShares_output["publicAddress"] = expectedPublicAddress do { - let nodeDetails = try await CustomAuth.getNodeDetailsFromContract(verifier: expectedVerifier, verfierID: expectedVerfierId) - let data = try await CustomAuth.getAggregateTorusKey(verifier: expectedVerifier, verifierId: expectedVerfierId, idToken: fakeData.generateVerifier(), subVerifierDetails: subVerifier[0]) - let mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils - XCTAssertEqual(mockTorusUtils.retrieveShares_input["endpoints"] as? [String], nodeDetails.getTorusNodeEndpoints()) - XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierIdentifier"] as? String, expectedVerifier) - XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierId"] as? String, expectedVerfierId) - XCTAssertEqual(data["privateKey"] as? String, expectedPrivateKey) - XCTAssertEqual(data["publicAddress"] as? String, expectedPublicAddress) + + let nodeDetails = try await CustomAuth.getNodeDetailsFromContract(verifier: expectedVerifier, verfierID: expectedVerfierId) + let data = try await CustomAuth.getAggregateTorusKey(verifier: expectedVerifier, verifierId: expectedVerfierId, idToken: fakeData.generateVerifier(), subVerifierDetails: subVerifier[0]) + print("Data", data) + let FinalKeyData = data["finalKeyData"] as! [String: Any] + let mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils + XCTAssertEqual(mockTorusUtils.retrieveShares_input["endpoints"] as? [String], nodeDetails.getTorusNodeEndpoints()) + XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierIdentifier"] as? String, expectedVerifier) + XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierId"] as? String, expectedVerfierId) + XCTAssertEqual(FinalKeyData["privKey"] as! String, expectedPrivateKey) + XCTAssertEqual(FinalKeyData["evmAddress"] as! String, expectedPublicAddress) expectation.fulfill() } catch { XCTFail(error.localizedDescription) diff --git a/Tests/CustomAuthTests/IntegrationTests.swift b/Tests/CustomAuthTests/IntegrationTests.swift index 716d507..2362a84 100644 --- a/Tests/CustomAuthTests/IntegrationTests.swift +++ b/Tests/CustomAuthTests/IntegrationTests.swift @@ -33,8 +33,10 @@ final class IntegrationTests: XCTestCase { let email = "hello@tor.us" let jwt = try! generateIdToken(email: email) do { - let data = try await IntegrationTests.sdk?.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: email, idToken: jwt) - XCTAssertEqual(data?["publicAddress"] as! String, "0x8AA6C8ddCD868873120aA265Fc63E3a2180375BA") + let data = try await IntegrationTests.sdk?.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: email, idToken: jwt) + let finalKeyDataDict = data?["finalKeyData"] as? [String: Any] + let evmAddress = finalKeyDataDict!["evmAddress"] as? String + XCTAssertEqual(evmAddress, "0x8AA6C8ddCD868873120aA265Fc63E3a2180375BA") exp1.fulfill() } catch { print(error) @@ -71,8 +73,10 @@ final class IntegrationTests: XCTestCase { let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL) do { let data = try await sdk.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: jwt) - XCTAssertEqual(data["publicAddress"] as! String, "0x4924f91f5d6701ddd41042d94832bb17b76f316f".lowercased()) - exp1.fulfill() + let finalKeyDataDict = data["finalKeyData"] as? [String: Any] + let evmAddress = finalKeyDataDict!["evmAddress"] as? String + XCTAssertEqual(evmAddress, "0x4924F91F5d6701dDd41042D94832bB17B76F316F") + exp1.fulfill() } catch { print(error) XCTFail(error.localizedDescription) diff --git a/Tests/CustomAuthTests/MockTorusUtils.swift b/Tests/CustomAuthTests/MockTorusUtils.swift index dc1a66d..034d510 100644 --- a/Tests/CustomAuthTests/MockTorusUtils.swift +++ b/Tests/CustomAuthTests/MockTorusUtils.swift @@ -13,7 +13,7 @@ public protocol MockAbstractTorusUtils { class MockTorusUtils: AbstractTorusUtils, MockAbstractTorusUtils { - func retrieveShares(endpoints: [String], torusNodePubs: [CommonSources.TorusNodePubModel]?, verifier: String, verifierParams: VerifierParams, idToken: String, extraParams: [String : Codable]) async throws -> RetrieveSharesResponse { + func retrieveShares(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierParams: VerifierParams, idToken: String, extraParams: [String:Codable]) async throws -> TorusKey { retrieveShares_input = [ "endpoints": endpoints, "verifierIdentifier": verifier, @@ -21,11 +21,13 @@ class MockTorusUtils: AbstractTorusUtils, MockAbstractTorusUtils { "idToken": idToken, "extraParams": extraParams ] - return .init(ethAddress: retrieveShares_output["publicAddress"] ?? "", privKey: retrieveShares_output["privateKey"] ?? "", sessionTokenData: [], X: "", Y: "", metadataNonce: BigInt(0), postboxPubKeyX: "", postboxPubKeyY: "", sessionAuthKey: "", nodeIndexes: []) + let finalKeyData : TorusKey.FinalKeyData = .init(evmAddress: retrieveShares_output["publicAddress"] ?? "", X: "", Y: "", privKey: retrieveShares_output["privateKey"] ?? "") + return TorusKey(finalKeyData: finalKeyData, oAuthKeyData: nil, sessionData: nil, metadata: nil, nodesData: nil) } - func getPublicAddress(endpoints: [String], torusNodePubs: [CommonSources.TorusNodePubModel]?, verifier: String, verifierId: String, extendedVerifierId: String?) async throws -> String { - return "" + func getPublicAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierId: String, extendedVerifierId: String?) async throws -> TorusPublicKey { +// GetPublicAddressResult(address: "") + return TorusPublicKey(finalKeyData: nil, oAuthKeyData: nil, metadata: nil, nodesData: nil) } func getUserTypeAndAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierID: String, doesKeyAssign: Bool)async throws -> GetUserAndAddress{ From 6cd51092dbe6f4e2bc7f944de9751cf3843a5983 Mon Sep 17 00:00:00 2001 From: himanshu Date: Wed, 26 Jul 2023 18:37:54 +0800 Subject: [PATCH 05/19] handle error notification --- .../CustomAuthDemo.xcodeproj/project.pbxproj | 10 ++++++ Sources/CustomAuth/CustomAuth.swift | 32 +++++++++++++++---- .../Extension/CASDK+extension.swift | 30 +++++++++++++++++ .../CustomAuth/Helpers/ASWebAuthSession.swift | 2 ++ 4 files changed, 67 insertions(+), 7 deletions(-) diff --git a/CustomAuthDemo/CustomAuthDemo.xcodeproj/project.pbxproj b/CustomAuthDemo/CustomAuthDemo.xcodeproj/project.pbxproj index 841f368..f6d5904 100644 --- a/CustomAuthDemo/CustomAuthDemo.xcodeproj/project.pbxproj +++ b/CustomAuthDemo/CustomAuthDemo.xcodeproj/project.pbxproj @@ -52,6 +52,7 @@ 511CEAEF2452D4EF00A7ACE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 51721D842474E4B700C22BAD /* torus-direct-swift-sdk */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "torus-direct-swift-sdk"; path = ..; sourceTree = ""; }; 517C5F5D24693F9D006D5A43 /* CustomAuthDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CustomAuthDemo.entitlements; sourceTree = ""; }; + A3C24F222A70FAF9002F4FC9 /* customauth-swift-sdk */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "customauth-swift-sdk"; path = ..; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -91,6 +92,7 @@ 511CEABF2452D4EC00A7ACE9 = { isa = PBXGroup; children = ( + A3C24F212A70FAF9002F4FC9 /* Packages */, 511CEACA2452D4EC00A7ACE9 /* CustomAuthDemo */, 511CEAE12452D4EF00A7ACE9 /* CustomAuthDemoTests */, 511CEAEC2452D4EF00A7ACE9 /* CustomAuthDemoUITests */, @@ -151,6 +153,14 @@ path = CustomAuthDemoUITests; sourceTree = ""; }; + A3C24F212A70FAF9002F4FC9 /* Packages */ = { + isa = PBXGroup; + children = ( + A3C24F222A70FAF9002F4FC9 /* customauth-swift-sdk */, + ); + name = Packages; + sourceTree = ""; + }; A84257F51AACA6652F347CFD /* Pods */ = { isa = PBXGroup; children = ( diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 3cf2d00..e3b486b 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -105,12 +105,21 @@ open class CustomAuth { if let subVerifier = subVerifierDetails.first { let loginURL = subVerifier.getLoginURL() await openURL(url: loginURL, view: controller, modalPresentationStyle: modalPresentationStyle) - let url = await withUnsafeContinuation({ continuation in - observeCallback { url in + + let url = try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation) in + observeCallbackWithError { url, err in + guard + err == nil, + let url = url + else { + continuation.resume(throwing: err!) + return + } + continuation.resume(returning: url) + return + } } - }) - let responseParameters = self.parseURL(url: url) os_log("ResponseParams after redirect: %@", log: getTorusLogger(log: CASDKLogger.core, type: .info), type: .info, responseParameters) do { @@ -140,11 +149,20 @@ open class CustomAuth { openURL(url: loginURL, view: controller, modalPresentationStyle: modalPresentationStyle) }) - let url = await withUnsafeContinuation({ continuation in - observeCallback { url in + let url = try await withUnsafeThrowingContinuation { (continuation: UnsafeContinuation) in + observeCallbackWithError { url, err in + guard + err == nil, + let url = url + else { + continuation.resume(throwing: err!) + return + } + continuation.resume(returning: url) + return + } } - }) let responseParameters = self.parseURL(url: url) os_log("ResponseParams after redirect: %@", log: getTorusLogger(log: CASDKLogger.core, type: .info), type: .info, responseParameters) do { diff --git a/Sources/CustomAuth/Extension/CASDK+extension.swift b/Sources/CustomAuth/Extension/CASDK+extension.swift index 172c2fd..461f750 100644 --- a/Sources/CustomAuth/Extension/CASDK+extension.swift +++ b/Sources/CustomAuth/Extension/CASDK+extension.swift @@ -50,6 +50,30 @@ extension CustomAuth { if let urlFromUserInfo = notification.userInfo?["URL"] as? URL { os_log("executing callback block", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error) block(urlFromUserInfo) + + } else { + assertionFailure() + } + } + } + + + public func observeCallbackWithError(_ block: @escaping (_ url: URL?, _ err: String?) -> Void) { + self.observer = CustomAuth.notificationCenter.addObserver( + forName: CustomAuth.didHandleCallbackURL, + object: nil, + queue: OperationQueue.main) { [weak self] notification in + self?.removeCallbackNotificationObserver() + os_log("notification.userInfo: %s", log: getTorusLogger(log: CASDKLogger.core, type: + .info), type: .info, notification.userInfo.debugDescription) + if let errorFromUserInfo = notification.userInfo?["ERROR"] as? String { + os_log("executing error callback block", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error) + block(nil, errorFromUserInfo) + } + else if let urlFromUserInfo = notification.userInfo?["URL"] as? URL { + os_log("executing callback block", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .info) + block(urlFromUserInfo, nil) + } else { assertionFailure() } @@ -90,6 +114,12 @@ extension CustomAuth { let notification = Notification(name: CustomAuth.didHandleCallbackURL, object: nil, userInfo: ["URL": url]) notificationCenter.post(notification) } + + open class func handleError(err: String) { + // CustomAuth.logger.info("Posting notification after Universal link/deep link flow") + let notification = Notification(name: CustomAuth.didHandleCallbackURL, object: nil, userInfo: ["ERROR": err]) + notificationCenter.post(notification) + } public func parseURL(url: URL) -> [String: String] { var responseParameters = [String: String]() diff --git a/Sources/CustomAuth/Helpers/ASWebAuthSession.swift b/Sources/CustomAuth/Helpers/ASWebAuthSession.swift index c0bcb2c..3b66d6d 100644 --- a/Sources/CustomAuth/Helpers/ASWebAuthSession.swift +++ b/Sources/CustomAuth/Helpers/ASWebAuthSession.swift @@ -25,6 +25,8 @@ open class ASWebAuthSession: NSObject, TorusURLHandlerTypes { let callbackURL = callbackURL else { print(authError?.localizedDescription as? String ?? "") + let errStr = authError?.localizedDescription as? String ?? "Something went wrong!!" + CustomAuth.handleError(err: errStr) return } CustomAuth.handle(url: callbackURL) From 0bb3753ccf94bc6483976f232d10fd4fae591d2b Mon Sep 17 00:00:00 2001 From: himanshu Date: Wed, 26 Jul 2023 18:38:27 +0800 Subject: [PATCH 06/19] cleanup --- Sources/CustomAuth/Extension/CASDK+extension.swift | 1 - 1 file changed, 1 deletion(-) diff --git a/Sources/CustomAuth/Extension/CASDK+extension.swift b/Sources/CustomAuth/Extension/CASDK+extension.swift index 461f750..0240adf 100644 --- a/Sources/CustomAuth/Extension/CASDK+extension.swift +++ b/Sources/CustomAuth/Extension/CASDK+extension.swift @@ -50,7 +50,6 @@ extension CustomAuth { if let urlFromUserInfo = notification.userInfo?["URL"] as? URL { os_log("executing callback block", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error) block(urlFromUserInfo) - } else { assertionFailure() } From 2e3cb162cf01b4d7b20afd510cd75e513a0a7c1c Mon Sep 17 00:00:00 2001 From: himanshu Date: Wed, 26 Jul 2023 19:00:11 +0800 Subject: [PATCH 07/19] add more logs --- Sources/CustomAuth/CustomAuth.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index e3b486b..fe038af 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -112,6 +112,7 @@ open class CustomAuth { err == nil, let url = url else { + os_log("observeCallbackWithError %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, err!) continuation.resume(throwing: err!) return } @@ -155,6 +156,7 @@ open class CustomAuth { err == nil, let url = url else { + os_log("observeCallbackWithError %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, err!) continuation.resume(throwing: err!) return } From de7193814ff2d52290b9b712ae3bc0e2d64a48c9 Mon Sep 17 00:00:00 2001 From: himanshu Date: Wed, 26 Jul 2023 19:46:28 +0800 Subject: [PATCH 08/19] remove logs --- Sources/CustomAuth/CustomAuth.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index fe038af..e3b486b 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -112,7 +112,6 @@ open class CustomAuth { err == nil, let url = url else { - os_log("observeCallbackWithError %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, err!) continuation.resume(throwing: err!) return } @@ -156,7 +155,6 @@ open class CustomAuth { err == nil, let url = url else { - os_log("observeCallbackWithError %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, err!) continuation.resume(throwing: err!) return } From 1f107eb9d9447c357dd916efe6f4b11a37d4d1a1 Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 28 Jul 2023 14:20:18 +0800 Subject: [PATCH 09/19] fix: update with latest torusUtils fixes --- Package.resolved | 4 +-- Package.swift | 2 +- Sources/CustomAuth/CustomAuth.swift | 30 ++++++---------------- Tests/CustomAuthTests/MockTorusUtils.swift | 7 +++-- 4 files changed, 14 insertions(+), 29 deletions(-) diff --git a/Package.resolved b/Package.resolved index 09d58a4..ed5a416 100644 --- a/Package.resolved +++ b/Package.resolved @@ -86,8 +86,8 @@ "package": "TorusUtils", "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { - "branch": "sapphire-fix", - "revision": "76bf518b88dc7483490f20156014556fbfedfcbd", + "branch": "fix/bug-fixes-27-jul", + "revision": "0ffcc932e1628847c69d5e083f56620af0e40a0f", "version": null } }, diff --git a/Package.swift b/Package.swift index 761944f..a88c84d 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("sapphire-fix")), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("fix/bug-fixes-27-jul")), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.0.1") ], diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index c3614a5..feff5cf 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -196,9 +196,9 @@ open class CustomAuth { let responseFromRetrieveShares : TorusKey; switch network { case .legacy(_): - responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) + responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, indexes: nodeDetails.torusIndexes, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) case .sapphire(_): - responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeSSSEndpoints, torusNodePubs: nil, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) + responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeSSSEndpoints, torusNodePubs: nodeDetails.torusNodePub, indexes: nodeDetails.torusIndexes, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) } @@ -233,16 +233,9 @@ open class CustomAuth { if let sessionData = sessionData { data["sessionData"] = [ - "sessionTokenData": sessionData.sessionTokenData.map { sessionToken in - return [ - "token": sessionToken.token, - "signature": sessionToken.signature, - "node_pubx": sessionToken.node_pubx, - "node_puby": sessionToken.node_puby - ] - }, + "sessionTokenData": sessionData.sessionTokenData, "sessionAuthKey": sessionData.sessionAuthKey - ] + ] as [String : Any] } if let metadata = metadata { @@ -285,9 +278,9 @@ open class CustomAuth { let responseFromRetrieveShares :TorusKey; switch network { case .legacy(_): - responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) + responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, indexes: nodeDetails.torusIndexes, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) case .sapphire(_): - responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeSSSEndpoints, torusNodePubs: nil, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) + responseFromRetrieveShares = try await self.torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeSSSEndpoints, torusNodePubs: nodeDetails.torusNodePub, indexes: nodeDetails.torusIndexes, verifier: verifier, verifierParams: verifierParams, idToken: hashedOnce, extraParams: extraParams) } // (torusNodePubs: nodeDetails.torusNodePub, endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierId, idToken: hashedOnce, extraParams: buffer) @@ -319,16 +312,9 @@ open class CustomAuth { if let sessionData = sessionData { data["sessionData"] = [ - "sessionTokenData": sessionData.sessionTokenData.map { sessionToken in - return [ - "token": sessionToken.token, - "signature": sessionToken.signature, - "node_pubx": sessionToken.node_pubx, - "node_puby": sessionToken.node_puby - ] - }, + "sessionTokenData": sessionData.sessionTokenData, "sessionAuthKey": sessionData.sessionAuthKey - ] + ] as [String : Any] } if let metadata = metadata { diff --git a/Tests/CustomAuthTests/MockTorusUtils.swift b/Tests/CustomAuthTests/MockTorusUtils.swift index 034d510..52b846b 100644 --- a/Tests/CustomAuthTests/MockTorusUtils.swift +++ b/Tests/CustomAuthTests/MockTorusUtils.swift @@ -13,7 +13,7 @@ public protocol MockAbstractTorusUtils { class MockTorusUtils: AbstractTorusUtils, MockAbstractTorusUtils { - func retrieveShares(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierParams: VerifierParams, idToken: String, extraParams: [String:Codable]) async throws -> TorusKey { + func retrieveShares(endpoints: [String], torusNodePubs: [CommonSources.TorusNodePubModel], indexes: [BigUInt], verifier: String, verifierParams: VerifierParams, idToken: String, extraParams: [String : Codable]) async throws -> TorusKey { retrieveShares_input = [ "endpoints": endpoints, "verifierIdentifier": verifier, @@ -25,9 +25,8 @@ class MockTorusUtils: AbstractTorusUtils, MockAbstractTorusUtils { return TorusKey(finalKeyData: finalKeyData, oAuthKeyData: nil, sessionData: nil, metadata: nil, nodesData: nil) } - func getPublicAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierId: String, extendedVerifierId: String?) async throws -> TorusPublicKey { -// GetPublicAddressResult(address: "") - return TorusPublicKey(finalKeyData: nil, oAuthKeyData: nil, metadata: nil, nodesData: nil) + func getPublicAddress(endpoints: [String], torusNodePubs: [CommonSources.TorusNodePubModel], verifier: String, verifierId: String, extendedVerifierId: String?) async throws -> TorusPublicKey { + return .init(finalKeyData: nil, oAuthKeyData: nil, metadata: nil, nodesData: nil) } func getUserTypeAndAddress(endpoints: [String], torusNodePubs: [TorusNodePubModel]?, verifier: String, verifierID: String, doesKeyAssign: Bool)async throws -> GetUserAndAddress{ From 6ab7c332553e3f8b630774e255b4078b5cb639dc Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 28 Jul 2023 17:37:31 +0800 Subject: [PATCH 10/19] fix: update torusUtils branch add data return for verifier and verifierId --- Package.resolved | 4 ++-- Package.swift | 2 +- Sources/CustomAuth/CustomAuth.swift | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index ed5a416..73ed941 100644 --- a/Package.resolved +++ b/Package.resolved @@ -86,8 +86,8 @@ "package": "TorusUtils", "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { - "branch": "fix/bug-fixes-27-jul", - "revision": "0ffcc932e1628847c69d5e083f56620af0e40a0f", + "branch": "master", + "revision": "7b146db06a4f19d13c4147b37519ce7ea48819d9", "version": null } }, diff --git a/Package.swift b/Package.swift index a88c84d..73a4c1b 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("fix/bug-fixes-27-jul")), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("master")), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.0.1") ], diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index feff5cf..387a36f 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -252,6 +252,8 @@ open class CustomAuth { "nodeIndexes": nodesData.nodeIndexes ] } + data["verifier"] = verifier + data["verifierId"] = verifierId return data } catch { @@ -331,6 +333,9 @@ open class CustomAuth { "nodeIndexes": nodesData.nodeIndexes ] } + + data["verifier"] = verifier + data["verifierId"] = verifierId return data } catch { os_log("handleSingleIdVerifier err: %@", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) From 03123f475091864cb98f00d1dadb2919eabbabbe Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 4 Aug 2023 17:41:59 +0800 Subject: [PATCH 11/19] fix: remove sapphire testnet --- Package.resolved | 10 +++++----- .../Extension/EthereumNetworkFND+extension.swift | 2 -- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/Package.resolved b/Package.resolved index 73ed941..32cd9b1 100644 --- a/Package.resolved +++ b/Package.resolved @@ -33,7 +33,7 @@ "repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git", "state": { "branch": "sapphire", - "revision": "5dc0393cb895e577f8666c30911dc07879748d62", + "revision": "9ec971573426247b0806e46b88a82fa8f96cf34a", "version": null } }, @@ -51,8 +51,8 @@ "repositoryURL": "https://github.com/vapor/jwt-kit.git", "state": { "branch": null, - "revision": "ef12bdfa557cd9d9f0c53396e0a9908f773bcd7b", - "version": "4.9.0" + "revision": "9e929d925434b91857661bcd455d1bd53f00bf22", + "version": "4.13.0" } }, { @@ -60,8 +60,8 @@ "repositoryURL": "https://github.com/auth0/JWTDecode.swift.git", "state": { "branch": null, - "revision": "7918a343e674c7707e0be120bb4e21d679be014c", - "version": "3.0.1" + "revision": "58af7278797871e460d79496621b3e5366b865b2", + "version": "3.1.0" } }, { diff --git a/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift b/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift index ecffad1..e409e1b 100644 --- a/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift +++ b/Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift @@ -32,8 +32,6 @@ extension TorusNetwork { case .SAPPHIRE_MAINNET: return "https://signer.tor.us" - case .SAPPHIRE_TESTNET: - return "https://signer.tor.us" case .SAPPHIRE_DEVNET: return "https://signer.tor.us" From 0e7898d7e415917f60615d0b9cf905a6f3137d76 Mon Sep 17 00:00:00 2001 From: himanshu Date: Mon, 14 Aug 2023 17:23:50 +0800 Subject: [PATCH 12/19] torus utils version locked --- Package.resolved | 96 ++++++++++++++++++++++++++++++++++++++++++------ Package.swift | 2 +- 2 files changed, 85 insertions(+), 13 deletions(-) diff --git a/Package.resolved b/Package.resolved index 32cd9b1..5c6bc3e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -32,14 +32,14 @@ "package": "FetchNodeDetails", "repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git", "state": { - "branch": "sapphire", - "revision": "9ec971573426247b0806e46b88a82fa8f96cf34a", - "version": null + "branch": null, + "revision": "c7b4e5a65eb737c029acb2d1aff84854a618384d", + "version": "5.0.0-alpha" } }, { "package": "GenericJSON", - "repositoryURL": "https://github.com/zoul/generic-json-swift", + "repositoryURL": "https://github.com/iwill/generic-json-swift", "state": { "branch": null, "revision": "0a06575f4038b504e78ac330913d920f1630f510", @@ -66,11 +66,29 @@ }, { "package": "secp256k1", - "repositoryURL": "https://github.com/Boilertalk/secp256k1.swift", + "repositoryURL": "https://github.com/GigaBitcoin/secp256k1.swift.git", + "state": { + "branch": null, + "revision": "1a14e189def5eaa92f839afdd2faad8e43b61a6e", + "version": "0.12.2" + } + }, + { + "package": "swift-atomics", + "repositoryURL": "https://github.com/apple/swift-atomics.git", + "state": { + "branch": null, + "revision": "6c89474e62719ddcc1e9614989fff2f68208fe10", + "version": "1.1.0" + } + }, + { + "package": "swift-collections", + "repositoryURL": "https://github.com/apple/swift-collections.git", "state": { "branch": null, - "revision": "cd187c632fb812fd93711a9f7e644adb7e5f97f0", - "version": "0.1.7" + "revision": "937e904258d22af6e447a0b72c0bc67583ef64a2", + "version": "1.0.4" } }, { @@ -82,13 +100,58 @@ "version": "2.5.0" } }, + { + "package": "swift-log", + "repositoryURL": "https://github.com/apple/swift-log.git", + "state": { + "branch": null, + "revision": "532d8b529501fb73a2455b179e0bbb6d49b652ed", + "version": "1.5.3" + } + }, + { + "package": "swift-nio", + "repositoryURL": "https://github.com/apple/swift-nio.git", + "state": { + "branch": null, + "revision": "cf281631ff10ec6111f2761052aa81896a83a007", + "version": "2.58.0" + } + }, + { + "package": "swift-nio-extras", + "repositoryURL": "https://github.com/apple/swift-nio-extras.git", + "state": { + "branch": null, + "revision": "0e0d0aab665ff1a0659ce75ac003081f2b1c8997", + "version": "1.19.0" + } + }, + { + "package": "swift-nio-ssl", + "repositoryURL": "https://github.com/apple/swift-nio-ssl.git", + "state": { + "branch": null, + "revision": "e866a626e105042a6a72a870c88b4c531ba05f83", + "version": "2.24.0" + } + }, + { + "package": "swift-nio-transport-services", + "repositoryURL": "https://github.com/apple/swift-nio-transport-services.git", + "state": { + "branch": null, + "revision": "e7403c35ca6bb539a7ca353b91cc2d8ec0362d58", + "version": "1.19.0" + } + }, { "package": "TorusUtils", "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { - "branch": "master", - "revision": "7b146db06a4f19d13c4147b37519ce7ea48819d9", - "version": null + "branch": null, + "revision": "35720bcd6f7cebba8bfbe4f5885f69b01f0a75a0", + "version": "6.0.0-alpha" } }, { @@ -96,8 +159,17 @@ "repositoryURL": "https://github.com/argentlabs/web3.swift", "state": { "branch": null, - "revision": "0474eb5e883bc4800b3909833207a1d35e72b808", - "version": "0.9.3" + "revision": "8ca33e700ed8de6137a0e1471017aa3b3c8de0db", + "version": "1.6.0" + } + }, + { + "package": "websocket-kit", + "repositoryURL": "https://github.com/vapor/websocket-kit.git", + "state": { + "branch": null, + "revision": "53fe0639a98903858d0196b699720decb42aee7b", + "version": "2.14.0" } } ] diff --git a/Package.swift b/Package.swift index 73a4c1b..c42fa89 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("master")), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .exact("6.0.0-alpha")), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.0.1") ], From 8c3357fbb16f300be0f0abe957c83644f7d007c7 Mon Sep 17 00:00:00 2001 From: himanshu Date: Mon, 14 Aug 2023 17:50:38 +0800 Subject: [PATCH 13/19] point torus utils to alpha branch --- Package.resolved | 10 +++++----- Package.swift | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Package.resolved b/Package.resolved index 5c6bc3e..20be38e 100644 --- a/Package.resolved +++ b/Package.resolved @@ -32,9 +32,9 @@ "package": "FetchNodeDetails", "repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git", "state": { - "branch": null, + "branch": "alpha", "revision": "c7b4e5a65eb737c029acb2d1aff84854a618384d", - "version": "5.0.0-alpha" + "version": null } }, { @@ -149,9 +149,9 @@ "package": "TorusUtils", "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { - "branch": null, - "revision": "35720bcd6f7cebba8bfbe4f5885f69b01f0a75a0", - "version": "6.0.0-alpha" + "branch": "alpha", + "revision": "82956b84fc60dbb3983f1663a315cfe798e29f06", + "version": null } }, { diff --git a/Package.swift b/Package.swift index c42fa89..ef4b58f 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .exact("6.0.0-alpha")), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", .branch("alpha")), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.0.1") ], From d9160ed18867be94c1551b32abc424db00a96db5 Mon Sep 17 00:00:00 2001 From: ieow Date: Mon, 28 Aug 2023 17:43:21 +0800 Subject: [PATCH 14/19] fix: merging issue --- Sources/CustomAuth/CustomAuth.swift | 12 ++++++------ Tests/CustomAuthTests/CustomAuthTests.swift | 12 ++++++------ Tests/CustomAuthTests/IntegrationTests.swift | 8 ++++---- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index edea586..480a409 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -84,7 +84,7 @@ open class CustomAuth { /// - browserType: Indicates the way to open the browser for login flow. Use `.external` for opening system safari, or `.asWebAuthSession` for opening an in-app ASwebAuthenticationSession. /// - modalPresentationStyle: Indicates the UIModalPresentationStyle for the popup. /// - Returns: A promise that resolve with a Dictionary that contain at least `privateKey` and `publicAddress` field.. - open func triggerLogin(controller: UIViewController? = nil, browserType: URLOpenerTypes = .asWebAuthSession, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> [String: Any] { + open func triggerLogin(controller: UIViewController? = nil, browserType: URLOpenerTypes = .asWebAuthSession, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKey { os_log("triggerLogin called with %@ %@", log: getTorusLogger(log: CASDKLogger.core, type: .info), type: .info, browserType.rawValue, modalPresentationStyle.rawValue) // Set browser authorizeURLHandler = browserType @@ -103,7 +103,7 @@ open class CustomAuth { } } - open func handleSingleLogins(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> [String: Any] { + open func handleSingleLogins(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKey { if let subVerifier = subVerifierDetails.first { let loginURL = subVerifier.getLoginURL() await openURL(url: loginURL, view: controller, modalPresentationStyle: modalPresentationStyle) @@ -144,7 +144,7 @@ open class CustomAuth { throw CASDKError.unknownError } - open func handleSingleIdVerifier(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> [String: Any] { + open func handleSingleIdVerifier(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKey { if let subVerifier = subVerifierDetails.first { let loginURL = subVerifier.getLoginURL() await MainActor.run(body: { @@ -186,12 +186,12 @@ open class CustomAuth { } - func handleAndAggregateVerifier(controller: UIViewController?) async throws -> [String: Any] { + func handleAndAggregateVerifier(controller: UIViewController?) async throws -> TorusKey { // TODO: implement verifier throw CASDKError.methodUnavailable } - func handleOrAggregateVerifier(controller: UIViewController?) async throws -> [String: Any] { + func handleOrAggregateVerifier(controller: UIViewController?) async throws -> TorusKey { // TODO: implement verifier throw CASDKError.methodUnavailable } @@ -210,7 +210,7 @@ open class CustomAuth { do { let nodeDetails = try await nodeDetailManager.getNodeDetails(verifier: verifier, verifierID: verifierId) // retrieveShares internall checks if network is legacy and calls getPublicAddress if required. - let responseFromRetrieveShares : TorusKey = responseFromRetrieveShares = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, indexes: nodeDetails.torusIndexes, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) + let responseFromRetrieveShares : TorusKey = try await torusUtils.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, indexes: nodeDetails.torusIndexes, verifier: verifier, verifierParams: verifierParams, idToken: idToken, extraParams: extraParams) return responseFromRetrieveShares } catch { diff --git a/Tests/CustomAuthTests/CustomAuthTests.swift b/Tests/CustomAuthTests/CustomAuthTests.swift index 5cb4669..0545035 100644 --- a/Tests/CustomAuthTests/CustomAuthTests.swift +++ b/Tests/CustomAuthTests/CustomAuthTests.swift @@ -34,13 +34,13 @@ final class MockSDKTest: XCTestCase { let nodeDetails = try await CustomAuth.getNodeDetailsFromContract(verifier: expectedVerifier, verfierID: expectedVerfierId) let data = try await CustomAuth.getTorusKey(verifier: expectedVerifier, verifierId: expectedVerfierId, idToken: fakeData.generateVerifier()) let mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils - let FinalKeyData = data["finalKeyData"] as! [String: Any] + let FinalKeyData = data.finalKeyData! print(FinalKeyData) XCTAssertEqual(mockTorusUtils.retrieveShares_input["endpoints"] as? [String], nodeDetails.getTorusNodeEndpoints()) XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierIdentifier"] as? String, expectedVerifier) XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierId"] as? String, expectedVerfierId) - XCTAssertEqual(FinalKeyData["privKey"] as! String, expectedPrivateKey) - XCTAssertEqual(FinalKeyData["evmAddress"] as! String, expectedPublicAddress) + XCTAssertEqual(FinalKeyData.privKey, expectedPrivateKey) + XCTAssertEqual(FinalKeyData.evmAddress, expectedPublicAddress) expectation.fulfill() } catch { XCTFail(error.localizedDescription) @@ -71,13 +71,13 @@ final class MockSDKTest: XCTestCase { let nodeDetails = try await CustomAuth.getNodeDetailsFromContract(verifier: expectedVerifier, verfierID: expectedVerfierId) let data = try await CustomAuth.getAggregateTorusKey(verifier: expectedVerifier, verifierId: expectedVerfierId, idToken: fakeData.generateVerifier(), subVerifierDetails: subVerifier[0]) print("Data", data) - let FinalKeyData = data["finalKeyData"] as! [String: Any] + let FinalKeyData = data.finalKeyData! let mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils XCTAssertEqual(mockTorusUtils.retrieveShares_input["endpoints"] as? [String], nodeDetails.getTorusNodeEndpoints()) XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierIdentifier"] as? String, expectedVerifier) XCTAssertEqual(mockTorusUtils.retrieveShares_input["verifierId"] as? String, expectedVerfierId) - XCTAssertEqual(FinalKeyData["privKey"] as! String, expectedPrivateKey) - XCTAssertEqual(FinalKeyData["evmAddress"] as! String, expectedPublicAddress) + XCTAssertEqual(FinalKeyData.privKey, expectedPrivateKey) + XCTAssertEqual(FinalKeyData.evmAddress, expectedPublicAddress) expectation.fulfill() } catch { XCTFail(error.localizedDescription) diff --git a/Tests/CustomAuthTests/IntegrationTests.swift b/Tests/CustomAuthTests/IntegrationTests.swift index 2362a84..cda9504 100644 --- a/Tests/CustomAuthTests/IntegrationTests.swift +++ b/Tests/CustomAuthTests/IntegrationTests.swift @@ -34,8 +34,8 @@ final class IntegrationTests: XCTestCase { let jwt = try! generateIdToken(email: email) do { let data = try await IntegrationTests.sdk?.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: email, idToken: jwt) - let finalKeyDataDict = data?["finalKeyData"] as? [String: Any] - let evmAddress = finalKeyDataDict!["evmAddress"] as? String + let finalKeyDataDict = data!.finalKeyData! + let evmAddress = finalKeyDataDict.evmAddress XCTAssertEqual(evmAddress, "0x8AA6C8ddCD868873120aA265Fc63E3a2180375BA") exp1.fulfill() } catch { @@ -73,8 +73,8 @@ final class IntegrationTests: XCTestCase { let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL) do { let data = try await sdk.getTorusKey(verifier: TORUS_TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: jwt) - let finalKeyDataDict = data["finalKeyData"] as? [String: Any] - let evmAddress = finalKeyDataDict!["evmAddress"] as? String + let finalKeyDataDict = data.finalKeyData! + let evmAddress = finalKeyDataDict.evmAddress XCTAssertEqual(evmAddress, "0x4924F91F5d6701dDd41042D94832bB17B76F316F") exp1.fulfill() } catch { From c8ab6dfd2f0c66505bcc1a08bbb78dd9ed5b1df4 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 31 Aug 2023 12:29:58 +0800 Subject: [PATCH 15/19] fix: add back verifier verifierId --- Sources/CustomAuth/CustomAuth.swift | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 480a409..3f21997 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -15,6 +15,12 @@ import CommonSources // Global variable var tsSdkLogType = OSLogType.default +public struct TorusKeyData { + public let torusKey : TorusKey + public let verifier : String + public let verifierId : String +} + /// Provides integration of an iOS app with Torus CustomAuth. open class CustomAuth { var nodeDetailManager: NodeDetailManager @@ -84,7 +90,7 @@ open class CustomAuth { /// - browserType: Indicates the way to open the browser for login flow. Use `.external` for opening system safari, or `.asWebAuthSession` for opening an in-app ASwebAuthenticationSession. /// - modalPresentationStyle: Indicates the UIModalPresentationStyle for the popup. /// - Returns: A promise that resolve with a Dictionary that contain at least `privateKey` and `publicAddress` field.. - open func triggerLogin(controller: UIViewController? = nil, browserType: URLOpenerTypes = .asWebAuthSession, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKey { + open func triggerLogin(controller: UIViewController? = nil, browserType: URLOpenerTypes = .asWebAuthSession, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKeyData { os_log("triggerLogin called with %@ %@", log: getTorusLogger(log: CASDKLogger.core, type: .info), type: .info, browserType.rawValue, modalPresentationStyle.rawValue) // Set browser authorizeURLHandler = browserType @@ -103,7 +109,7 @@ open class CustomAuth { } } - open func handleSingleLogins(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKey { + open func handleSingleLogins(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKeyData { if let subVerifier = subVerifierDetails.first { let loginURL = subVerifier.getLoginURL() await openURL(url: loginURL, view: controller, modalPresentationStyle: modalPresentationStyle) @@ -133,7 +139,7 @@ open class CustomAuth { data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") let torusKey = try await getTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, userData: data) - return torusKey + return TorusKeyData(torusKey: torusKey, verifier: aggregateVerifier, verifierId: verifierId) } catch { os_log("handleSingleLogin: err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) throw error @@ -144,7 +150,7 @@ open class CustomAuth { throw CASDKError.unknownError } - open func handleSingleIdVerifier(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKey { + open func handleSingleIdVerifier(controller: UIViewController?, modalPresentationStyle: UIModalPresentationStyle = .fullScreen) async throws -> TorusKeyData { if let subVerifier = subVerifierDetails.first { let loginURL = subVerifier.getLoginURL() await MainActor.run(body: { @@ -175,7 +181,7 @@ open class CustomAuth { data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") let aggTorusKey = try await getAggregateTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, subVerifierDetails: subVerifier, userData: newData) - return aggTorusKey + return TorusKeyData(torusKey: aggTorusKey, verifier: self.aggregateVerifier, verifierId: verifierId) } catch { os_log("handleSingleIdVerifier err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) throw error @@ -186,12 +192,12 @@ open class CustomAuth { } - func handleAndAggregateVerifier(controller: UIViewController?) async throws -> TorusKey { + func handleAndAggregateVerifier(controller: UIViewController?) async throws -> TorusKeyData { // TODO: implement verifier throw CASDKError.methodUnavailable } - func handleOrAggregateVerifier(controller: UIViewController?) async throws -> TorusKey { + func handleOrAggregateVerifier(controller: UIViewController?) async throws -> TorusKeyData { // TODO: implement verifier throw CASDKError.methodUnavailable } From 92dfe1fdbdb39391bb5807c392f0c127a92cac31 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 31 Aug 2023 12:32:09 +0800 Subject: [PATCH 16/19] fix: formatting --- Sources/CustomAuth/CustomAuth.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 3f21997..f71dd53 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -139,7 +139,7 @@ open class CustomAuth { data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") let torusKey = try await getTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, userData: data) - return TorusKeyData(torusKey: torusKey, verifier: aggregateVerifier, verifierId: verifierId) + return TorusKeyData(torusKey: torusKey, verifier: self.aggregateVerifier, verifierId: verifierId) } catch { os_log("handleSingleLogin: err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) throw error From ff5114daeac60a8473048c10db05938eaa7badf9 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 31 Aug 2023 13:11:07 +0800 Subject: [PATCH 17/19] fix: add userInfo --- Sources/CustomAuth/CustomAuth.swift | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index f71dd53..7921939 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -17,8 +17,7 @@ var tsSdkLogType = OSLogType.default public struct TorusKeyData { public let torusKey : TorusKey - public let verifier : String - public let verifierId : String + public var userInfo : [String: Any] } /// Provides integration of an iOS app with Torus CustomAuth. @@ -139,7 +138,9 @@ open class CustomAuth { data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") let torusKey = try await getTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, userData: data) - return TorusKeyData(torusKey: torusKey, verifier: self.aggregateVerifier, verifierId: verifierId) + var result = TorusKeyData(torusKey: torusKey, userInfo: newData) + result.userInfo["verifier"] = self.aggregateVerifier + return result } catch { os_log("handleSingleLogin: err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) throw error @@ -181,7 +182,9 @@ open class CustomAuth { data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") let aggTorusKey = try await getAggregateTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, subVerifierDetails: subVerifier, userData: newData) - return TorusKeyData(torusKey: aggTorusKey, verifier: self.aggregateVerifier, verifierId: verifierId) + var result = TorusKeyData(torusKey: aggTorusKey, userInfo: newData) + result.userInfo["verifier"] = self.aggregateVerifier + return result } catch { os_log("handleSingleIdVerifier err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) throw error From 1b2de26121afe912bd8dfd55b78542b5059d4af2 Mon Sep 17 00:00:00 2001 From: himanshu Date: Thu, 31 Aug 2023 14:28:37 +0800 Subject: [PATCH 18/19] merge user info with params --- Sources/CustomAuth/CustomAuth.swift | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Sources/CustomAuth/CustomAuth.swift b/Sources/CustomAuth/CustomAuth.swift index 7921939..696a8a9 100644 --- a/Sources/CustomAuth/CustomAuth.swift +++ b/Sources/CustomAuth/CustomAuth.swift @@ -137,9 +137,12 @@ open class CustomAuth { let idToken = data["tokenForKeys"] as! String data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") + + let torusKey = try await getTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, userData: data) - var result = TorusKeyData(torusKey: torusKey, userInfo: newData) - result.userInfo["verifier"] = self.aggregateVerifier + var mergedUserInfo = newData.merging(responseParameters) { (_, new) in new } + mergedUserInfo["verifier"] = self.aggregateVerifier + let result = TorusKeyData(torusKey: torusKey, userInfo: mergedUserInfo) return result } catch { os_log("handleSingleLogin: err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) @@ -182,8 +185,9 @@ open class CustomAuth { data.removeValue(forKey: "tokenForKeys") data.removeValue(forKey: "verifierId") let aggTorusKey = try await getAggregateTorusKey(verifier: self.aggregateVerifier, verifierId: verifierId, idToken: idToken, subVerifierDetails: subVerifier, userData: newData) - var result = TorusKeyData(torusKey: aggTorusKey, userInfo: newData) - result.userInfo["verifier"] = self.aggregateVerifier + var mergedUserInfo = newData.merging(responseParameters) { (_, new) in new } + mergedUserInfo["verifier"] = self.aggregateVerifier + let result = TorusKeyData(torusKey: aggTorusKey, userInfo: mergedUserInfo) return result } catch { os_log("handleSingleIdVerifier err: %s", log: getTorusLogger(log: CASDKLogger.core, type: .error), type: .error, error.localizedDescription) From 8f76791c13dc886cc82ccf5cf04c7bdec8d0162e Mon Sep 17 00:00:00 2001 From: ieow Date: Fri, 1 Sep 2023 16:06:36 +0800 Subject: [PATCH 19/19] feat: update to torusUtils 6.0.1 --- Package.resolved | 4 ++-- Package.swift | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Package.resolved b/Package.resolved index 28c287d..b858466 100644 --- a/Package.resolved +++ b/Package.resolved @@ -78,8 +78,8 @@ "repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git", "state": { "branch": null, - "revision": "c3b8fee6cdaf3ed9d14d4f85a32eb6b843d0d50e", - "version": "6.0.0" + "revision": "61b36686c2def5f2f40d8cb0d5bc3af34559c1d5", + "version": "6.0.1" } } ] diff --git a/Package.swift b/Package.swift index 32ab18c..621481c 100644 --- a/Package.swift +++ b/Package.swift @@ -14,7 +14,7 @@ let package = Package( targets: ["CustomAuth"]) ], dependencies: [ - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", from: "6.0.0"), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", from: "6.0.1"), .package(name: "jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.13.0"), .package(name: "JWTDecode", url: "https://github.com/auth0/JWTDecode.swift.git", from: "3.1.0") ],