Skip to content

Commit

Permalink
Merge pull request #59 from torusresearch/alpha
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
himanshuchawla009 authored Sep 1, 2023
2 parents 6ec2946 + 8f76791 commit 57bd8da
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 158 deletions.
10 changes: 10 additions & 0 deletions CustomAuthDemo/CustomAuthDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
511CEAEF2452D4EF00A7ACE9 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
51721D842474E4B700C22BAD /* torus-direct-swift-sdk */ = {isa = PBXFileReference; lastKnownFileType = folder; name = "torus-direct-swift-sdk"; path = ..; sourceTree = "<group>"; };
517C5F5D24693F9D006D5A43 /* CustomAuthDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CustomAuthDemo.entitlements; sourceTree = "<group>"; };
A3C24F222A70FAF9002F4FC9 /* customauth-swift-sdk */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "customauth-swift-sdk"; path = ..; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -91,6 +92,7 @@
511CEABF2452D4EC00A7ACE9 = {
isa = PBXGroup;
children = (
A3C24F212A70FAF9002F4FC9 /* Packages */,
511CEACA2452D4EC00A7ACE9 /* CustomAuthDemo */,
511CEAE12452D4EF00A7ACE9 /* CustomAuthDemoTests */,
511CEAEC2452D4EF00A7ACE9 /* CustomAuthDemoUITests */,
Expand Down Expand Up @@ -151,6 +153,14 @@
path = CustomAuthDemoUITests;
sourceTree = "<group>";
};
A3C24F212A70FAF9002F4FC9 /* Packages */ = {
isa = PBXGroup;
children = (
A3C24F222A70FAF9002F4FC9 /* customauth-swift-sdk */,
);
name = Packages;
sourceTree = "<group>";
};
A84257F51AACA6652F347CFD /* Pods */ = {
isa = PBXGroup;
children = (
Expand Down
41 changes: 16 additions & 25 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -24,17 +33,8 @@
"repositoryURL": "https://github.com/torusresearch/fetch-node-details-swift.git",
"state": {
"branch": null,
"revision": "94a0ee598abfcc2104ceeda80eecf4b55242ba62",
"version": "4.0.1"
}
},
{
"package": "GenericJSON",
"repositoryURL": "https://github.com/zoul/generic-json-swift",
"state": {
"branch": null,
"revision": "0a06575f4038b504e78ac330913d920f1630f510",
"version": "2.0.2"
"revision": "68459eb481c382bfbbe28345b8eb25b3897c7447",
"version": "5.0.0"
}
},
{
Expand All @@ -57,11 +57,11 @@
},
{
"package": "secp256k1",
"repositoryURL": "https://github.com/Boilertalk/secp256k1.swift",
"repositoryURL": "https://github.com/GigaBitcoin/secp256k1.swift.git",
"state": {
"branch": null,
"revision": "cd187c632fb812fd93711a9f7e644adb7e5f97f0",
"version": "0.1.7"
"revision": "1a14e189def5eaa92f839afdd2faad8e43b61a6e",
"version": "0.12.2"
}
},
{
Expand All @@ -78,17 +78,8 @@
"repositoryURL": "https://github.com/torusresearch/torus-utils-swift.git",
"state": {
"branch": null,
"revision": "5545ce7e4d00e1a5821434c4b673258235dd05d5",
"version": "5.1.1"
}
},
{
"package": "web3.swift",
"repositoryURL": "https://github.com/argentlabs/web3.swift",
"state": {
"branch": null,
"revision": "0474eb5e883bc4800b3909833207a1d35e72b808",
"version": "0.9.3"
"revision": "61b36686c2def5f2f40d8cb0d5bc3af34559c1d5",
"version": "6.0.1"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let package = Package(
targets: ["CustomAuth"])
],
dependencies: [
.package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift.git", from: "5.1.1"),
.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")
],
Expand Down
122 changes: 78 additions & 44 deletions Sources/CustomAuth/CustomAuth.swift

Large diffs are not rendered by default.

65 changes: 33 additions & 32 deletions Sources/CustomAuth/CustomAuthFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
// }
//}
29 changes: 29 additions & 0 deletions Sources/CustomAuth/Extension/CASDK+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,29 @@ extension CustomAuth {
}
}


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()
}
}
}

@MainActor public func openURL(url: String, view: UIViewController?, modalPresentationStyle: UIModalPresentationStyle) {
os_log("opening URL: %s", log: getTorusLogger(log: CASDKLogger.core, type: .info), type: .info, url)
switch authorizeURLHandler {
Expand Down Expand Up @@ -90,6 +113,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]()
Expand Down
39 changes: 26 additions & 13 deletions Sources/CustomAuth/Extension/EthereumNetworkFND+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,35 @@

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_DEVNET:
return "https://signer.tor.us"

}
}
}
}
2 changes: 2 additions & 0 deletions Sources/CustomAuth/Helpers/ASWebAuthSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 30 additions & 22 deletions Tests/CustomAuthTests/CustomAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,25 @@ 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
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!
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, expectedPrivateKey)
XCTAssertEqual(FinalKeyData.evmAddress, expectedPublicAddress)
expectation.fulfill()
} catch {
XCTFail(error.localizedDescription)
Expand All @@ -53,23 +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, factory: factory)
var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils
let CustomAuth = CustomAuth(aggregateVerifierType: .singleIdVerifier, aggregateVerifier: expectedVerifier, subVerifierDetails: subVerifier)
// var mockTorusUtils = CustomAuth.torusUtils as! MockAbstractTorusUtils
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!
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, expectedPrivateKey)
XCTAssertEqual(FinalKeyData.evmAddress, expectedPublicAddress)
expectation.fulfill()
} catch {
XCTFail(error.localizedDescription)
Expand Down
Loading

0 comments on commit 57bd8da

Please sign in to comment.