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

update: use latest torus-utils #21

Merged
merged 2 commits into from
Aug 6, 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
15 changes: 3 additions & 12 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
{
"pins" : [
{
"identity" : "anycodable",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Flight-School/AnyCodable",
"state" : {
"revision" : "862808b2070cd908cb04f9aafe7de83d35f81b05",
"version" : "0.6.7"
}
},
{
"identity" : "bigint",
"kind" : "remoteSourceControl",
Expand Down Expand Up @@ -75,10 +66,10 @@
{
"identity" : "torus-utils-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/torusresearch/torus-utils-swift",
"location" : "https://github.com/torusresearch/torus-utils-swift.git",
"state" : {
"revision" : "608c28404c506983bfec7bbd957632fc0544db8c",
"version" : "8.1.2"
"revision" : "303dc2cf41db7c10f769855edad0e717ced2d554",
"version" : "9.0.0"
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ let package = Package(
targets: ["SingleFactorAuth"])
],
dependencies: [
.package(url: "https://github.com/torusresearch/fetch-node-details-swift.git", from: "6.0.1"),
.package(url: "https://github.com/torusresearch/torus-utils-swift.git", from: "8.1.0"),
.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/vapor/jwt-kit.git", from: "4.0.0"),
.package(url: "https://github.com/Web3Auth/session-manager-swift.git", from: "4.0.2"),
],
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 = "6.0.0"
spec.version = "7.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 @@ -10,8 +10,8 @@ Pod::Spec.new do |spec|
spec.module_name = "SingleFactorAuth"
spec.source = { :git => "https://github.com/web3Auth/single-factor-auth-swift", :tag => spec.version }
spec.source_files = "Sources/SingleFactorAuth/*.{swift,json}","Sources/SingleFactorAuth/**/*.{swift,json}"
spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.1'
spec.dependency 'Torus-fetchNodeDetails', '~> 6.0.3'
spec.dependency 'curvelib.swift', '~> 1.0.1'
spec.dependency 'Torus-utils', '~> 8.1.0'
spec.dependency 'Torus-utils', '~> 9.0.0'
spec.dependency 'TorusSessionManager', '~> 4.0.2'
end
11 changes: 11 additions & 0 deletions Sources/SingleFactorAuth/Data+Extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

internal extension Data {
var bytes: Array<UInt8> {
Array(self)
}

func toHexString() -> String {
bytes.toHexString()
}
}
48 changes: 17 additions & 31 deletions Sources/SingleFactorAuth/SingleFactorAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import FetchNodeDetails
import Foundation
import SessionManager
import TorusUtils
import curveSecp256k1
#if canImport(curveSecp256k1)
import curveSecp256k1
#endif

public class SingleFactorAuth {
let nodeDetailManager: NodeDetailManager
let torusUtils: TorusUtils
private var sessionManager: SessionManager

public init(singleFactorAuthArgs: SingleFactorAuthArgs) {
public init(singleFactorAuthArgs: SingleFactorAuthArgs) throws {
sessionManager = .init()
nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork().network)
torusUtils = TorusUtils(
enableOneKey: true,
network: singleFactorAuthArgs.getNetwork().network,
clientId: singleFactorAuthArgs.getWeb3AuthClientId()
)
nodeDetailManager = NodeDetailManager(network: singleFactorAuthArgs.getNetwork())
let torusOptions = TorusOptions(clientId: singleFactorAuthArgs.getWeb3AuthClientId(), network: singleFactorAuthArgs.getNetwork(), enableOneKey: true)
try torusUtils = TorusUtils(params: torusOptions)
}

public func initialize() async throws -> TorusSFAKey {
Expand All @@ -33,7 +32,7 @@ public class SingleFactorAuth {

let details = try await nodeDetailManager.getNodeDetails(verifier: loginParams.verifier, verifierID: loginParams.verifierId)

let userDetails = try await torusUtils.getUserTypeAndAddress(endpoints: details.getTorusNodeEndpoints(), torusNodePubs: details.getTorusNodePub(), verifier: loginParams.verifier, verifierId: loginParams.verifierId)
let userDetails = try await torusUtils.getUserTypeAndAddress(endpoints: details.getTorusNodeEndpoints(), verifier: loginParams.verifier, verifierId: loginParams.verifierId)

if userDetails.metadata?.upgraded == true {
throw "User already has enabled MFA"
Expand All @@ -42,45 +41,32 @@ public class SingleFactorAuth {
if let subVerifierInfoArray = loginParams.subVerifierInfoArray, !subVerifierInfoArray.isEmpty {
var aggregateIdTokenSeeds = [String]()
var subVerifierIds = [String]()
var verifyParams = [[String: String]]()
var verifyParams = [VerifyParams]()
for value in subVerifierInfoArray {
aggregateIdTokenSeeds.append(value.idToken)

var verifyParam: [String: String] = [:]
verifyParam["verifier_id"] = loginParams.verifierId
verifyParam["idtoken"] = value.idToken
let verifyParam = VerifyParams(verifier_id: loginParams.verifierId, idtoken: value.idToken)

verifyParams.append(verifyParam)
subVerifierIds.append(value.verifier)
}
aggregateIdTokenSeeds.sort()

let extraParams = [
"verifier_id": loginParams.verifierId,
"sub_verifier_ids": subVerifierIds,
"verify_params": verifyParams,
] as [String: Codable]
let verifierParams = VerifierParams(verifier_id: loginParams.verifierId, sub_verifier_ids: subVerifierIds, verify_params: verifyParams)

let verifierParams = VerifierParams(verifier_id: loginParams.verifierId)

let aggregateIdToken = try curveSecp256k1.keccak256(data: Data(aggregateIdTokenSeeds.joined(separator: "\u{001d}").utf8)).toHexString()

retrieveSharesResponse = try await torusUtils.retrieveShares(
endpoints: details.getTorusNodeEndpoints(),
torusNodePubs: details.getTorusNodePub(),
indexes: details.getTorusIndexes(),
verifier: loginParams.verifier,
verifierParams: verifierParams,
idToken: aggregateIdToken,
extraParams: extraParams
idToken: aggregateIdToken
)
} else {
let verifierParams = VerifierParams(verifier_id: loginParams.verifierId)

retrieveSharesResponse = try await torusUtils.retrieveShares(
endpoints: details.getTorusNodeEndpoints(),
torusNodePubs: details.getTorusNodePub(),
indexes: details.getTorusIndexes(),
verifier: loginParams.verifier,
verifierParams: verifierParams,
idToken: loginParams.idToken
Expand All @@ -89,12 +75,12 @@ public class SingleFactorAuth {

return retrieveSharesResponse
}

public func getKey(loginParams: LoginParams) async throws -> TorusSFAKey {
let torusKey = try await self.getTorusKey(loginParams: loginParams)
let publicAddress = torusKey.finalKeyData?.evmAddress ?? ""
let privateKey = torusKey.finalKeyData?.privKey ?? ""
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)
Expand Down
58 changes: 5 additions & 53 deletions Sources/SingleFactorAuth/SingleFactorAuthArgs.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import FetchNodeDetails
import TorusUtils

public class SingleFactorAuthArgs {
public static let SIGNER_MAP: [Web3AuthNetwork: String] = [
Web3AuthNetwork.SAPPHIRE_MAINNET: "https://signer.tor.us",
Web3AuthNetwork.SAPPHIRE_DEVNET: "https://signer.tor.us",
Web3AuthNetwork.MAINNET: "https://signer.tor.us",
Web3AuthNetwork.TESTNET: "https://signer.tor.us",
Web3AuthNetwork.CYAN: "https://signer-polygon.tor.us",
Web3AuthNetwork.AQUA: "https://signer-polygon.tor.us",
]
public typealias Web3AuthNetwork = TorusNetwork

private var network: Web3AuthNetwork
public class SingleFactorAuthArgs {
private var network: TorusNetwork
private var networkUrl: String
private var web3AuthClientId: String

Expand All @@ -20,7 +12,7 @@ public class SingleFactorAuthArgs {
self.networkUrl = networkUrl
self.web3AuthClientId = web3AuthClientId
}

public func getWeb3AuthClientId() -> String {
return web3AuthClientId
}
Expand All @@ -30,50 +22,10 @@ public class SingleFactorAuthArgs {
}

public func getSignerUrl() -> String? {
return SingleFactorAuthArgs.SIGNER_MAP[network]
return network.signerMap
}

public func setNetwork(network: Web3AuthNetwork) {
self.network = network
}
}

public enum Web3AuthNetwork : Equatable, Hashable {
case SAPPHIRE_DEVNET
case SAPPHIRE_MAINNET
case MAINNET
case TESTNET
case CYAN
case AQUA
case CELESTE
case CUSTOM(path: String)

public var path: String {
return self.network.path
}

public var name: String {
return self.network.name
}

public var network: TorusNetwork {
switch self {
case .SAPPHIRE_DEVNET:
return .sapphire(.SAPPHIRE_DEVNET)
case .SAPPHIRE_MAINNET:
return .sapphire(.SAPPHIRE_MAINNET)
case .MAINNET:
return .legacy(.MAINNET)
case .TESTNET:
return .legacy(.TESTNET)
case .CYAN:
return .legacy(.CYAN)
case .AQUA:
return .legacy(.AQUA)
case .CELESTE:
return .legacy(.CELESTE)
case .CUSTOM(path: let path):
return .legacy(.CUSTOM(path: path))
}
}
}
5 changes: 3 additions & 2 deletions Tests/SingleFactorAuthTests/AquaTest.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import BigInt
import JWTKit
import XCTest
import FetchNodeDetails

@testable import SingleFactorAuth

Expand All @@ -13,8 +14,8 @@ final class AquaTest: XCTestCase {
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

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

func testGetTorusKey() async throws {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SingleFactorAuthTests/CyanTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ final class CyanTest: XCTestCase {
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

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

func testGetTorusKey() async throws {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SingleFactorAuthTests/SingleFactorAuthTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ final class SingleFactorAuthTests: XCTestCase {
let TEST_AGGREGRATE_VERIFIER = "torus-test-health-aggregate"

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

func testGetTorusKey() async throws {
Expand Down
Loading