Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: update to match other sdks #22

Merged
merged 11 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
with:
xcode-version: latest-stable
- name: package
run: xcodebuild test -scheme SingleFactorAuth -destination "platform=iOS Simulator,OS=16.2,name=iPhone 14" COMPILER_INDEX_STORE_ENABLE=NO
run: xcodebuild test -scheme SingleFactorAuth -destination "platform=iOS Simulator,OS=17.5,name=iPhone 14" COMPILER_INDEX_STORE_ENABLE=NO
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/Web3Auth/session-manager-swift.git",
"state" : {
"revision" : "20cc7bff065d7fe53164d17e7714a3f17d4cea2a",
"version" : "4.0.2"
"revision" : "67d5f7db655d02778861057fb280ecf47c923b09",
"version" : "5.0.0"
}
},
{
Expand All @@ -68,8 +68,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/torus-utils-swift.git",
"state" : {
"revision" : "303dc2cf41db7c10f769855edad0e717ced2d554",
"version" : "9.0.0"
"revision" : "eda55b8537a600e657d19d4c452c0a36f217883c",
"version" : "9.0.1"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/torusresearch/fetch-node-details-swift.git", from: "6.0.3"),
.package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "9.0.0"),
.package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "9.0.1"),
.package(url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"),
.package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "4.0.2"),
.package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "5.0.0"),
],
targets: [
.target(
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,34 @@ You can install the SingleFactorAuth Swift using Swift Package Manager.
...
dependencies: [
...
.package(url: "https://github.com/Web3Auth/single-factor-auth-swift/", from: "4.0.0")
.package(url: "https://github.com/Web3Auth/single-factor-auth-swift/", from: "8.0.0")
],
...
```


## Getting Started
Initialize the `SingleFactAuth` class by passing `SingleFactorAuthArgs`
Initialize the `SingleFactAuth` class by passing `SFAParams`

```swift
let singleFactorAuthArgs = SingleFactorAuthArgs(
web3AuthClientId: "<Your Client Id>",
network: Web3AuthNetwork.SAPPHIRE_MAINNET
)
let singleFactoreAuth = SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs)
let singleFactoreAuth = SingleFactorAuth(params: SFAParams)
```

Use the `getKey` function to login the user and get the privateKey and public address for the given user.

```swift
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(
verifier: TEST_VERIFIER,
verifierId: TOURUS_TEST_EMAIL,
verifierId: TORUS_TEST_EMAIL,
idToken: idToken
)

let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)
```

We also have included Session Management in this SDK so call initialize function to get TorusKey value without relogging in the user if a user has an active session it will return the TorusKey struct otherwise it will return nil.
Expand Down
6 changes: 3 additions & 3 deletions SingleFactorAuth.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "SingleFactorAuth"
spec.version = "7.0.0"
spec.version = "8.0.0"
spec.ios.deployment_target = "14.0"
spec.summary = "Enable one key flow for Web3Auth"
spec.homepage = "https://github.com/Web3Auth/single-factor-auth-swift"
Expand All @@ -12,6 +12,6 @@ Pod::Spec.new do |spec|
spec.source_files = "Sources/SingleFactorAuth/*.{swift,json}","Sources/SingleFactorAuth/**/*.{swift,json}"
spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.3'
spec.dependency 'curvelib.swift', '~> 1.0.1'
spec.dependency 'Torus-utils', '~> 9.0.0'
spec.dependency 'TorusSessionManager', '~> 4.0.2'
spec.dependency 'Torus-utils', '~> 9.0.1'
spec.dependency 'TorusSessionManager', '~> 5.0.0'
end
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import BigInt
import Foundation

public class TorusSFAKey: Codable {
public class SFAKey: Codable {
let privateKey: String
let publicAddress: String

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@ import FetchNodeDetails

public typealias Web3AuthNetwork = TorusNetwork

public class SingleFactorAuthArgs {
public class SFAParams{
private var network: TorusNetwork
private var networkUrl: String
private var web3AuthClientId: String
private var sessionTime: Int

public init(web3AuthClientId: String, network: Web3AuthNetwork, networkUrl: String = "") {
public init(web3AuthClientId: String, network: Web3AuthNetwork, sessionTime: Int = 86400, networkUrl: String = "") {
self.network = network
self.networkUrl = networkUrl
self.web3AuthClientId = web3AuthClientId
self.sessionTime = sessionTime
}

public func getWeb3AuthClientId() -> String {
Expand All @@ -28,4 +30,8 @@ public class SingleFactorAuthArgs {
public func setNetwork(network: Web3AuthNetwork) {
self.network = network
}

public func getSessionTime() -> Int {
return sessionTime
}
}
27 changes: 17 additions & 10 deletions Sources/SingleFactorAuth/SingleFactorAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,26 @@ public class SingleFactorAuth {
let torusUtils: TorusUtils
private var sessionManager: SessionManager

public init(singleFactorAuthArgs: SingleFactorAuthArgs) throws {
sessionManager = .init()
nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork())
let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true)
public init(params: SFAParams) throws {
sessionManager = SessionManager(sessionTime: params.getSessionTime(), allowedOrigin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift")
nodeDetailManager = NodeDetailManager(network: params.getNetwork())
let torusOptions = TorusOptions(clientId: params.getWeb3AuthClientId(), network: params.getNetwork(), enableOneKey: true)
try torusUtils = TorusUtils(params: torusOptions)
}

public func initialize() async throws -> TorusSFAKey {
let data = try await sessionManager.authorizeSession()
public func initialize() async throws -> SFAKey {
let data = try await sessionManager.authorizeSession(origin: Bundle.main.bundleIdentifier ?? "single-factor-auth-swift")
guard let privKey = data["privateKey"] as? String,
let publicAddress = data["publicAddress"] as? String else { throw SessionManagerError.decodingError }
return .init(privateKey: privKey, publicAddress: publicAddress)
}

public func isSessionIdExists() -> Bool {
if (sessionManager.getSessionID() != nil) && !(sessionManager.getSessionID()!.isEmpty) {
return true
}
return false
}

public func getTorusKey(loginParams: LoginParams) async throws -> TorusKey {
var retrieveSharesResponse: TorusKey
Expand Down Expand Up @@ -76,14 +83,14 @@ public class SingleFactorAuth {
return retrieveSharesResponse
}

public func getKey(loginParams: LoginParams) async throws -> TorusSFAKey {
public func connect(loginParams: LoginParams) async throws -> SFAKey {
let torusKey = try await getTorusKey(loginParams: loginParams)

let publicAddress = torusKey.finalKeyData.evmAddress
let privateKey = torusKey.finalKeyData.privKey

let torusSfaKey = TorusSFAKey(privateKey: privateKey, publicAddress: publicAddress)
_ = try await sessionManager.createSession(data: torusSfaKey)
return torusSfaKey
let sfaKey = SFAKey(privateKey: privateKey, publicAddress: publicAddress)
_ = try await sessionManager.createSession(data: sfaKey)
return sfaKey
}
}
33 changes: 16 additions & 17 deletions Tests/SingleFactorAuthTests/AquaTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,45 @@ import BigInt
import JWTKit
import XCTest
import FetchNodeDetails

@testable import SingleFactorAuth
import SingleFactorAuth

final class AquaTest: XCTestCase {
var singleFactoreAuth: SingleFactorAuth!
var singleFactorAuthArgs: SingleFactorAuthArgs!
var singleFactorAuthArgs: SFAParams!

let TOURUS_TEST_EMAIL = "[email protected]"
let TORUS_TEST_EMAIL = "[email protected]"
let TEST_VERIFIER = "torus-test-health"
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

override func setUp() {
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA))
singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs)
singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.AQUA))
singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs)
}

func testGetTorusKey() async throws {
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
func testConnect() async throws {
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)

let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8"
XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey())
XCTAssertEqual("0x8b32926cD9224fec3B296aA7250B049029434807", torusKey.getPublicAddress())
}

func testInitialise() async throws {
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)
let savedKey = try await singleFactoreAuth.initialize()
let requiredPrivateKey = "d8204e9f8c270647294c54acd8d49ee208789f981a7503158e122527d38626d8"
XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey())
XCTAssertEqual(torusKey.publicAddress, savedKey.getPublicAddress())
XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress())
}

func testAggregrateGetTorusKey() async throws {
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)])
let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)])
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)

let requiredPrivateKey = "6f8b884f19975fb0d138ed21b22a6a7e1b79e37f611d0a29f1442b34efc6bacd"
XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey())
Expand Down
33 changes: 16 additions & 17 deletions Tests/SingleFactorAuthTests/CyanTest.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,45 @@
import BigInt
import JWTKit
import XCTest

@testable import SingleFactorAuth
import SingleFactorAuth

final class CyanTest: XCTestCase {
var singleFactoreAuth: SingleFactorAuth!
var singleFactorAuthArgs: SingleFactorAuthArgs!
var singleFactorAuthArgs: SFAParams!

let TOURUS_TEST_EMAIL = "[email protected]"
let TORUS_TEST_EMAIL = "[email protected]"
let TEST_VERIFIER = "torus-test-health"
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

override func setUp() {
singleFactorAuthArgs = SingleFactorAuthArgs(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN))
singleFactoreAuth = try! SingleFactorAuth(singleFactorAuthArgs: singleFactorAuthArgs)
singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .legacy(.CYAN))
singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs)
}

func testGetTorusKey() async throws {
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
func testConnect() async throws {
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)

let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546"
XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey())
XCTAssertEqual("0x6b902fBCEb0E0374e5eB9eDFe68cD4B888c32150", torusKey.getPublicAddress())
}

func testInitialise() async throws {
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)
let requiredPrivateKey = "223d982054fa1ad27d1497560521e4cce5b8c6438c38533c7bad27ff21ce0546"
let savedKey = try await singleFactoreAuth.initialize()
XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey())
XCTAssertEqual(torusKey.publicAddress, savedKey.getPublicAddress())
XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress())
}

func testAggregrateGetTorusKey() async throws {
let idToken = try generateIdToken(email: TOURUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TOURUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)])
let torusKey = try await singleFactoreAuth.getKey(loginParams: loginParams)
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)])
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)

let requiredPrivateKey = "66af498ea82c95d52fdb8c8dedd44cf2f758424a0eecab7ac3dd8721527ea2d4"
XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey())
Expand Down
49 changes: 49 additions & 0 deletions Tests/SingleFactorAuthTests/SapphireDevnetTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import BigInt
import JWTKit
import XCTest
import FetchNodeDetails
import SingleFactorAuth

final class SapphireDevnetTests: XCTestCase {
var singleFactoreAuth: SingleFactorAuth!
var singleFactorAuthArgs: SFAParams!

let TORUS_TEST_EMAIL = "[email protected]"
let TEST_VERIFIER = "torus-test-health"
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

override func setUp() {
singleFactorAuthArgs = SFAParams(web3AuthClientId: "CLIENT ID", network: .sapphire(.SAPPHIRE_DEVNET))
singleFactoreAuth = try! SingleFactorAuth(params: singleFactorAuthArgs)
}

func testConnect() async throws {
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)

let requiredPrivateKey = "230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203"
XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey())
XCTAssertEqual("0x462A8BF111A55C9354425F875F89B22678c0Bc44", torusKey.getPublicAddress())
}

func testInitialise() async throws {
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken)
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)
let savedKey = try await singleFactoreAuth.initialize()
let requiredPrivateKey = "230dad9f42039569e891e6b066ff5258b14e9764ef5176d74aeb594d1a744203"
XCTAssertEqual(requiredPrivateKey, savedKey.getPrivateKey())
XCTAssertEqual(torusKey.getPublicAddress(), savedKey.getPublicAddress())
}

func testAggregrateGetTorusKey() async throws {
let idToken = try generateIdToken(email: TORUS_TEST_EMAIL)
let loginParams = LoginParams(verifier: TEST_AGGREGRATE_VERIFIER, verifierId: TORUS_TEST_EMAIL, idToken: idToken, subVerifierInfoArray: [TorusSubVerifierInfo(verifier: TEST_VERIFIER, idToken: idToken)])
let torusKey = try await singleFactoreAuth.connect(loginParams: loginParams)

let requiredPrivateKey = "edef171853fdf23ed3cfc702d32cf46f181b475a449d2f7b636924cabecd81d4"
XCTAssertEqual(requiredPrivateKey, torusKey.getPrivateKey())
XCTAssertEqual("0xfC58EB0475F1E3fa05877eE2e1350f6A619E2d78", torusKey.getPublicAddress())
}
}
Loading
Loading