From d1b9ae05b2dbf67dd051b7e88284f466d25173e4 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:34:58 +0200 Subject: [PATCH] review comment: add TorusUtilsExtraParams class --- Sources/TorusUtils/Helpers/NodeUtils.swift | 68 ++++++++++++++++-- .../jsonRPC/Requests/ShareRequestParams.swift | 32 ++++++++- Sources/TorusUtils/TorusUtils.swift | 20 ++++-- .../TorusUtils/TorusUtilsExtraParams.swift | 69 +++++++++++++++++++ 4 files changed, 175 insertions(+), 14 deletions(-) create mode 100644 Sources/TorusUtils/TorusUtilsExtraParams.swift diff --git a/Sources/TorusUtils/Helpers/NodeUtils.swift b/Sources/TorusUtils/Helpers/NodeUtils.swift index 24fef187..6cd16f2e 100644 --- a/Sources/TorusUtils/Helpers/NodeUtils.swift +++ b/Sources/TorusUtils/Helpers/NodeUtils.swift @@ -130,7 +130,7 @@ internal class NodeUtils { idToken: String, importedShares: [ImportedShare]?, apiKey: String = "torus-default", - extraParams: [String: Codable] = [:] + extraParams: TorusUtilsExtraParams ) async throws -> TorusKey { let threshold = Int(trunc(Double((endpoints.count / 2) + 1))) @@ -218,7 +218,7 @@ internal class NodeUtils { var thresholdNonceData: GetOrSetNonceResult? - let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int + let sessionExpiry = extraParams.session_token_exp_second var shareImportSuccess = false @@ -246,11 +246,39 @@ internal class NodeUtils { key_type: importShare.key_type, nonce_data: importShare.nonce_data, nonce_signature: importShare.nonce_signature, - // extra_params: extraData sub_verifier_ids: verifierParams.sub_verifier_ids, session_token_exp_second: sessionExpiry, verify_params: verifierParams.verify_params, - sss_endpoint: endpoints[j] + sss_endpoint: endpoints[j], + + enable_verifier_id_hash: extraParams.enable_verifier_id_hash, + app_s: extraParams.app_s, + app_id: extraParams.app_id, + domain: extraParams.domain, + nonce: extraParams.nonce, + message: extraParams.message, + signature: extraParams.signature, + clientDataJson: extraParams.clientDataJson, + authenticatorData: extraParams.authenticatorData, + publicKey: extraParams.publicKey, + challenge: extraParams.challenge, + rpOrigin: extraParams.rpOrigin, + rpId: extraParams.rpId, + jwk_endpoint: extraParams.jwk_endpoint, + default_node_set: extraParams.default_node_set, + jwt_verifier_id_field: extraParams.jwt_verifier_id_field, + jwt_verifier_id_case_sensitive: extraParams.jwt_verifier_id_case_sensitive, + jwk_keys: extraParams.jwk_keys, + jwt_validation_fields: extraParams.jwt_validation_fields, + jwt_validation_values: extraParams.jwt_validation_values, + index: extraParams.index, + email: extraParams.email, + id: extraParams.id, + correct_id_token: extraParams.correct_id_token, + verify_param: extraParams.verify_param, + threshold: extraParams.threshold, + pub_k_x: extraParams.pub_k_x, + pub_k_y: extraParams.pub_k_y ) importedItems.append(shareRequestItem) @@ -295,10 +323,38 @@ internal class NodeUtils { extended_verifier_id: verifierParams.extended_verifier_id, idtoken: idToken, nodesignatures: nodeSigs, - // extra_params: extraData sub_verifier_ids: verifierParams.sub_verifier_ids, session_token_exp_second: sessionExpiry, - verify_params: verifierParams.verify_params + verify_params: verifierParams.verify_params, + + enable_verifier_id_hash: extraParams.enable_verifier_id_hash, + app_s: extraParams.app_s, + app_id: extraParams.app_id, + domain: extraParams.domain, + nonce: extraParams.nonce, + message: extraParams.message, + signature: extraParams.signature, + clientDataJson: extraParams.clientDataJson, + authenticatorData: extraParams.authenticatorData, + publicKey: extraParams.publicKey, + challenge: extraParams.challenge, + rpOrigin: extraParams.rpOrigin, + rpId: extraParams.rpId, + jwk_endpoint: extraParams.jwk_endpoint, + default_node_set: extraParams.default_node_set, + jwt_verifier_id_field: extraParams.jwt_verifier_id_field, + jwt_verifier_id_case_sensitive: extraParams.jwt_verifier_id_case_sensitive, + jwk_keys: extraParams.jwk_keys, + jwt_validation_fields: extraParams.jwt_validation_fields, + jwt_validation_values: extraParams.jwt_validation_values, + index: extraParams.index, + email: extraParams.email, + id: extraParams.id, + correct_id_token: extraParams.correct_id_token, + verify_param: extraParams.verify_param, + threshold: extraParams.threshold, + pub_k_x: extraParams.pub_k_x, + pub_k_y: extraParams.pub_k_y ) let params = ShareRequestParams(encrypted: "yes", item: [shareRequestItem], client_time: String(Int(trunc(Double((serverTimeOffset ?? 0) + Int(Date().timeIntervalSince1970)))))) diff --git a/Sources/TorusUtils/Helpers/jsonRPC/Requests/ShareRequestParams.swift b/Sources/TorusUtils/Helpers/jsonRPC/Requests/ShareRequestParams.swift index 2b7efbd4..637da76c 100644 --- a/Sources/TorusUtils/Helpers/jsonRPC/Requests/ShareRequestParams.swift +++ b/Sources/TorusUtils/Helpers/jsonRPC/Requests/ShareRequestParams.swift @@ -17,11 +17,41 @@ internal struct ShareRequestParams: Codable { public var key_type: TorusKeyType? public var nonce_data: String? public var nonce_signature: String? - // [key: string]; This should be strongly typed public var sub_verifier_ids: [String]? public var session_token_exp_second: Int? public var verify_params: [VerifyParams?]? public var sss_endpoint: String? + + // TODO: This is a bit of a mess from here due to legacy reasons and should be cleaned up in future. + // Note: Nil values by default are excluded from serialization + public var enable_verifier_id_hash: Bool? // most + public var app_s: String? // meta + public var app_id: String? // meta + public var domain: String? // farcaster + public var nonce: String? // farcaster + public var message: String? // farcaster + public var signature: String? // farcaster, passkey, webauthn + public var clientDataJson: String? // passkey, webauthn + public var authenticatorData: String? // passkey, webauthn + public var publicKey: String? // passkey, webauthn + public var challenge: String? // passkey, webauthn + public var rpOrigin: String? // passkey, webauthn + public var rpId: String? // passkey, webauthn + public var jwk_endpoint: String? // passkey, jwt + public var default_node_set: [String]? // passkey, jwt + public var jwt_verifier_id_field: String? // passkey, jwt + public var jwt_verifier_id_case_sensitive: Bool? // passkey, jwt + public var jwk_keys: String? // passkey, jwt + public var jwt_validation_fields: [String]? // passkey, jwt + public var jwt_validation_values: [String]? // passkey, jwt + public var index: Int? // demo + public var email: String? // demo + public var id: String? // test, jwt, passkey + public var correct_id_token: String? // test + public var verify_param: String? // OrAggregate + public var threshold: Int? // SingleID + public var pub_k_x: String? // Signature + public var pub_k_y: String? // Signature } public var encrypted: String = "yes" diff --git a/Sources/TorusUtils/TorusUtils.swift b/Sources/TorusUtils/TorusUtils.swift index e312672a..aa1af7cf 100644 --- a/Sources/TorusUtils/TorusUtils.swift +++ b/Sources/TorusUtils/TorusUtils.swift @@ -124,13 +124,14 @@ public class TorusUtils { endpoints: [String], verifier: String, verifierParams: VerifierParams, - idToken: String + idToken: String, + extraParams: TorusUtilsExtraParams = TorusUtilsExtraParams() ) async throws -> TorusKey { - // This has to be done here as retrieveOrImport share does not have a reference to self - var params: [String: Codable] = [:] - params.updateValue(sessionTime, forKey: "session_token_exp_second") + if extraParams.session_token_exp_second == nil { + extraParams.session_token_exp_second = sessionTime + } - return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: [], apiKey: apiKey, extraParams: params) + return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: [], apiKey: apiKey, extraParams: extraParams) } /// Retrieves user information, defaulting the user type to .v2 @@ -170,7 +171,8 @@ public class TorusUtils { verifier: String, verifierParams: VerifierParams, idToken: String, - newPrivateKey: String + newPrivateKey: String, + extraParams: TorusUtilsExtraParams = TorusUtilsExtraParams() ) async throws -> TorusKey { let nodePubs = TorusNodePubModelToINodePub(nodes: nodePubKeys) if endpoints.count != nodeIndexes.count { @@ -179,7 +181,11 @@ public class TorusUtils { let sharesData = try KeyUtils.generateShares(serverTimeOffset: serverTimeOffset ?? 0, nodeIndexes: nodeIndexes, nodePubKeys: nodePubs, privateKey: newPrivateKey) - return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? 0, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: sharesData) + if extraParams.session_token_exp_second == nil { + extraParams.session_token_exp_second = sessionTime + } + + return try await NodeUtils.retrieveOrImportShare(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffset ?? 0, enableOneKey: enableOneKey, allowHost: allowHost, network: network, clientId: clientId, endpoints: endpoints, verifier: verifier, verifierParams: verifierParams, idToken: idToken, importedShares: sharesData, extraParams: extraParams) } /// Retrieves user information diff --git a/Sources/TorusUtils/TorusUtilsExtraParams.swift b/Sources/TorusUtils/TorusUtilsExtraParams.swift new file mode 100644 index 00000000..6c9733b9 --- /dev/null +++ b/Sources/TorusUtils/TorusUtilsExtraParams.swift @@ -0,0 +1,69 @@ +import Foundation + +// TODO: This class is a bit of a mess for legacy reasons and should be cleaned up in future. + +public class TorusUtilsExtraParams: Codable { + var enable_verifier_id_hash: Bool? // most + var app_s: String? // meta + var app_id: String? // meta + var domain: String? // farcaster + var nonce: String? // farcaster + var message: String? // farcaster + var signature: String? // farcaster, passkey, webauthn + var clientDataJson: String? // passkey, webauthn + var authenticatorData: String? // passkey, webauthn + var publicKey: String? // passkey, webauthn + var challenge: String? // passkey, webauthn + var rpOrigin: String? // passkey, webauthn + var rpId: String? // passkey, webauthn + var jwk_endpoint: String? // passkey, jwt + var default_node_set: [String]? // passkey, jwt + var jwt_verifier_id_field: String? // passkey, jwt + var jwt_verifier_id_case_sensitive: Bool? // passkey, jwt + var jwk_keys: String? // passkey, jwt + var jwt_validation_fields: [String]? // passkey, jwt + var jwt_validation_values: [String]? // passkey, jwt + var index: Int? // demo + var email: String? // demo + var id: String? // test, jwt, passkey + var correct_id_token: String? // test + var verify_param: String? // OrAggregate + var session_token_exp_second: Int? + var threshold: Int? // SingleID + var pub_k_x: String? // Signature + var pub_k_y: String? // Signature + + public init() {} + + public init(enable_verifier_id_hash: Bool? = nil, app_s: String? = nil, app_id: String? = nil, domain: String? = nil, nonce: String? = nil, message: String? = nil, signature: String? = nil, clientDataJson: String? = nil, authenticatorData: String? = nil, publicKey: String? = nil, challenge: String? = nil, rpOrigin: String? = nil, rpId: String? = nil, jwk_endpoint: String? = nil, default_node_set: [String]? = nil, jwt_verifier_id_field: String? = nil, jwt_verifier_id_case_sensitive: Bool? = nil, jwk_keys: String? = nil, jwt_validation_fields: [String]? = nil, jwt_validation_values: [String]? = nil, index: Int? = nil, email: String? = nil, id: String? = nil, correct_id_token: String? = nil, verify_param: String? = nil, session_token_exp_second: Int? = nil, threshold: Int? = nil, pub_k_x: String? = nil, pub_k_y: String? = nil) { + self.enable_verifier_id_hash = enable_verifier_id_hash + self.app_s = app_s + self.app_id = app_id + self.domain = domain + self.nonce = nonce + self.message = message + self.signature = signature + self.clientDataJson = clientDataJson + self.authenticatorData = authenticatorData + self.publicKey = publicKey + self.challenge = challenge + self.rpOrigin = rpOrigin + self.rpId = rpId + self.jwk_endpoint = jwk_endpoint + self.default_node_set = default_node_set + self.jwt_verifier_id_field = jwt_verifier_id_field + self.jwt_verifier_id_case_sensitive = jwt_verifier_id_case_sensitive + self.jwk_keys = jwk_keys + self.jwt_validation_fields = jwt_validation_fields + self.jwt_validation_values = jwt_validation_values + self.index = index + self.email = email + self.id = id + self.correct_id_token = correct_id_token + self.verify_param = verify_param + self.session_token_exp_second = session_token_exp_second + self.threshold = threshold + self.pub_k_x = pub_k_x + self.pub_k_y = pub_k_y + } +}