From 810dc3b0bfc38600b4d9ad97cf6dad2c121add47 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Tue, 14 May 2024 20:35:43 +0200 Subject: [PATCH] checkpoint: testnet tests --- Sources/TorusUtils/Helpers/NodeUtils.swift | 77 ++++---- Sources/TorusUtils/TorusUtils.swift | 14 +- Sources/TorusUtils/VerifierParams.swift | 6 +- Tests/TorusUtilsTests/AquaTest.swift | 8 +- Tests/TorusUtilsTests/Celeste.swift | 8 +- Tests/TorusUtilsTests/CyanTest.swift | 9 +- Tests/TorusUtilsTests/MainnetTest.swift | 8 +- .../TorusUtilsTests/SapphireDevnetTest.swift | 9 +- .../SapphireMainnetTests.swift | 11 +- Tests/TorusUtilsTests/TestnetTest.swift | 173 +++++++++++------- Tests/TorusUtilsTests/oneKeyTest.swift | 11 +- 11 files changed, 165 insertions(+), 169 deletions(-) diff --git a/Sources/TorusUtils/Helpers/NodeUtils.swift b/Sources/TorusUtils/Helpers/NodeUtils.swift index eddc0513..d0962a3d 100644 --- a/Sources/TorusUtils/Helpers/NodeUtils.swift +++ b/Sources/TorusUtils/Helpers/NodeUtils.swift @@ -219,38 +219,34 @@ public class NodeUtils { } group.addTask { + let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int + if isImport { do { let importShare = importedShares![i] - let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int - let subVerifiers: [String]? = extraParams["sub_verifier_ids"] as? [String] - let verifyParams: [ShareRequestParams.ShareRequestVerifyParams?]? = extraParams["verify_params"] as? [ShareRequestParams.ShareRequestVerifyParams] - - let params = ShareRequestParams( - item: [ShareRequestParams.ShareRequestItem( - verifieridentifier: verifier, - verifier_id: verifierParams.verifier_id, - extended_verifier_id: verifierParams.extended_verifier_id, - idtoken: idToken, - nodeSignatures: nodeSigs, - pub_key_x: importShare.pubKeyX, - pub_key_y: importShare.pubKeyY, - encrypted_share: importShare.encryptedShare, - encrypted_share_metadata: importShare.encryptedShareMetadata, - node_index: String(importShare.nodeIndex), - key_type: importShare.keyType, - nonce_data: importShare.nonceData, - nonce_signature: importShare.nonceSignature, - //extra_params: extraData - sub_verifier_ids: subVerifiers, - session_token_exp_second: sessionExpiry, - verify_params: verifyParams - )], - client_time: String(Int(floor(Date().timeIntervalSince1970 / 1000)) - ) + let shareRequestItem = ShareRequestParams.ShareRequestItem( + verifieridentifier: verifier, + verifier_id: verifierParams.verifier_id, + extended_verifier_id: verifierParams.extended_verifier_id, + idtoken: idToken, + nodeSignatures: nodeSigs, + pub_key_x: importShare.pubKeyX, + pub_key_y: importShare.pubKeyY, + encrypted_share: importShare.encryptedShare, + encrypted_share_metadata: importShare.encryptedShareMetadata, + node_index: String(importShare.nodeIndex), + key_type: importShare.keyType, + nonce_data: importShare.nonceData, + nonce_signature: importShare.nonceSignature, + // extra_params: extraData + sub_verifier_ids: verifierParams.sub_verifier_ids, + session_token_exp_second: sessionExpiry, + verify_params: verifierParams.verify_params ) + let params = ShareRequestParams(encrypted: "yes", item: [shareRequestItem], client_time: String(Int(floor(Date().timeIntervalSince1970 / 1000)))) + let jsonRPCRequest = JRPCRequest( method: JRPC_METHODS.IMPORT_SHARE, params: params @@ -270,25 +266,20 @@ public class NodeUtils { } } else { do { - let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int - let subVerifiers: [String]? = extraParams["sub_verifier_ids"] as? [String] - let verifyParams: [ShareRequestParams.ShareRequestVerifyParams?]? = extraParams["verify_params"] as? [ShareRequestParams.ShareRequestVerifyParams] - - let params = ShareRequestParams( - item: [ShareRequestParams.ShareRequestItem( - verifieridentifier: verifier, - verifier_id: verifierParams.verifier_id, - extended_verifier_id: verifierParams.extended_verifier_id, - idtoken: idToken, - nodeSignatures: nodeSigs, - // extra_params: extraData - sub_verifier_ids: subVerifiers, - session_token_exp_second: sessionExpiry, - verify_params: verifyParams - )], - client_time: String(Int(floor(Date().timeIntervalSince1970 / 1000))) + let shareRequestItem = ShareRequestParams.ShareRequestItem( + verifieridentifier: verifier, + verifier_id: verifierParams.verifier_id, + 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 ) + let params = ShareRequestParams(encrypted: "yes", item: [shareRequestItem], client_time: String(Int(floor(Date().timeIntervalSince1970 / 1000)))) + let jsonRPCRequest = JRPCRequest( method: JRPC_METHODS.GET_SHARE_OR_KEY_ASSIGN, params: params diff --git a/Sources/TorusUtils/TorusUtils.swift b/Sources/TorusUtils/TorusUtils.swift index e0ab32a9..daa2dd55 100644 --- a/Sources/TorusUtils/TorusUtils.swift +++ b/Sources/TorusUtils/TorusUtils.swift @@ -96,12 +96,11 @@ public class TorusUtils { indexes: [BigUInt], verifier: String, verifierParams: VerifierParams, - idToken: String, - extraParams: [String: Codable] = [:] + idToken: String ) async throws -> TorusKey { // This has to be done here as retrieveOrImport share does not have a reference to self // Moving this in javascript library will allow it to be done directly in the function - var params = extraParams + var params: [String: Codable] = [:] params.updateValue(sessionTime, forKey: "session_token_exp_second") if isLegacyNetwork() { @@ -128,8 +127,7 @@ public class TorusUtils { verifier: String, verifierParams: VerifierParams, idToken: String, - newPrivateKey: String, - extraParams: [String: Codable] = [:] + newPrivateKey: String ) async throws -> TorusKey { let nodePubs = TorusNodePubModelToINodePub(nodes: nodePubKeys) @@ -295,8 +293,6 @@ public class TorusUtils { group.addTask { do { let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int - let subVerifiers: [String]? = extraParams["sub_verifier_ids"] as? [String] - let verifyParams: [ShareRequestParams.ShareRequestVerifyParams?]? = extraParams["verify_params"] as? [ShareRequestParams.ShareRequestVerifyParams] let shareRequestItem = ShareRequestParams.ShareRequestItem( verifieridentifier: verifier, @@ -305,9 +301,9 @@ public class TorusUtils { idtoken: idToken, nodeSignatures: sigs, // extra_params: extraData - sub_verifier_ids: subVerifiers, + sub_verifier_ids: verifierParams.sub_verifier_ids, session_token_exp_second: sessionExpiry, - verify_params: verifyParams + verify_params: verifierParams.verify_params ) let params = ShareRequestParams(encrypted: "yes", item: [shareRequestItem], client_time: String(Int(floor(Date().timeIntervalSince1970 / 1000)))) diff --git a/Sources/TorusUtils/VerifierParams.swift b/Sources/TorusUtils/VerifierParams.swift index 30064d52..1c2057bc 100644 --- a/Sources/TorusUtils/VerifierParams.swift +++ b/Sources/TorusUtils/VerifierParams.swift @@ -4,9 +4,13 @@ public struct VerifierParams { // [key: string]: unknown; ?? Needs to be strongly typed in javascript public let verifier_id: String public let extended_verifier_id: String? + public let sub_verifier_ids: [String]? + public let verify_params: [ShareRequestParams.ShareRequestVerifyParams]? - public init(verifier_id: String, extended_verifier_id: String? = nil) { + public init(verifier_id: String, extended_verifier_id: String? = nil, sub_verifier_ids: [String]? = nil, verify_params: [ShareRequestParams.ShareRequestVerifyParams]? = nil) { self.verifier_id = verifier_id self.extended_verifier_id = extended_verifier_id + self.sub_verifier_ids = sub_verifier_ids + self.verify_params = verify_params } } diff --git a/Tests/TorusUtilsTests/AquaTest.swift b/Tests/TorusUtilsTests/AquaTest.swift index 427a3362..8494f8d4 100644 --- a/Tests/TorusUtilsTests/AquaTest.swift +++ b/Tests/TorusUtilsTests/AquaTest.swift @@ -148,13 +148,9 @@ class AquaTest: XCTestCase { let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) - let verifierParams = VerifierParams(verifier_id: verifierID) - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] + let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)]) - let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertEqual(data.finalKeyData.evmAddress, "0x5b58d8a16fDA79172cd42Dc3068d5CEf26a5C81D") XCTAssertEqual(data.finalKeyData.X, "37a4ac8cbef68e88bcec5909d9b6fffb539187365bb723f3d7bffe56ae80e31d") XCTAssertEqual(data.finalKeyData.Y, "f963f2d08ed4dd0da9b8a8d74c6fdaeef7bdcde31f84fcce19fa2173d40b2c10") diff --git a/Tests/TorusUtilsTests/Celeste.swift b/Tests/TorusUtilsTests/Celeste.swift index 7ad71c53..cdb62ab4 100644 --- a/Tests/TorusUtilsTests/Celeste.swift +++ b/Tests/TorusUtilsTests/Celeste.swift @@ -148,12 +148,8 @@ class CelesteTest: XCTestCase { let hashedIDToken = try KeyUtils.keccak256Data(jwt) let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) - let verifierParams = VerifierParams(verifier_id: verifierID) - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: verifierID, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] - let data = try await torus.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: verifierID, idtoken: jwt)]) + let data = try await torus.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertEqual(data.finalKeyData.evmAddress, "0x535Eb1AefFAc6f699A2a1A5846482d7b5b2BD564") XCTAssertLessThan(data.metadata.serverTimeOffset, 20) diff --git a/Tests/TorusUtilsTests/CyanTest.swift b/Tests/TorusUtilsTests/CyanTest.swift index 7bcba2ba..4e0b0c8b 100644 --- a/Tests/TorusUtilsTests/CyanTest.swift +++ b/Tests/TorusUtilsTests/CyanTest.swift @@ -144,14 +144,11 @@ class CyanTest: XCTestCase { let verifier: String = TORUS_TEST_AGGREGATE_VERIFIER let verifierID: String = TORUS_TEST_EMAIL let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL) - let verifierParams = VerifierParams(verifier_id: verifierID) + let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)]) let hashedIDToken = try KeyUtils.keccak256Data(jwt) - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) - let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + + let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertEqual(data.finalKeyData.evmAddress, "0x34117FDFEFBf1ad2DFA6d4c43804E6C710a6fB04") XCTAssertLessThan(data.metadata.serverTimeOffset, 20) diff --git a/Tests/TorusUtilsTests/MainnetTest.swift b/Tests/TorusUtilsTests/MainnetTest.swift index 549cbf7d..9dba3fd9 100644 --- a/Tests/TorusUtilsTests/MainnetTest.swift +++ b/Tests/TorusUtilsTests/MainnetTest.swift @@ -156,12 +156,8 @@ class MainnetTests: XCTestCase { let hashedIDToken = try KeyUtils.keccak256Data(jwt) let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) - let verifierParams = VerifierParams(verifier_id: verifierID) - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: verifierID, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] - let data = try await torus.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: verifierID, idtoken: jwt)]) + let data = try await torus.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertEqual(data.finalKeyData.evmAddress, "0x621a4d458cFd345dAE831D9E756F10cC40A50381") XCTAssertLessThan(data.metadata.serverTimeOffset, 20) diff --git a/Tests/TorusUtilsTests/SapphireDevnetTest.swift b/Tests/TorusUtilsTests/SapphireDevnetTest.swift index 5c42841d..02502efd 100644 --- a/Tests/TorusUtilsTests/SapphireDevnetTest.swift +++ b/Tests/TorusUtilsTests/SapphireDevnetTest.swift @@ -27,14 +27,9 @@ final class SapphireDevnetTest: XCTestCase { let email: String = generateRandomEmail(of: 6) let jwt = try! generateIdToken(email: email) let hashedIDToken = try KeyUtils.keccak256Data(jwt) - let verifierParams = VerifierParams(verifier_id: email) - + let verifierParams = VerifierParams(verifier_id: email, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: email, idtoken: jwt)]) let nodeDetails = try await fnd.getNodeDetails(verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierID: email) - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: email, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] - let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeSSSEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeSSSEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertLessThan(data.metadata.serverTimeOffset, 20) XCTAssertNotNil(data.finalKeyData.privKey) diff --git a/Tests/TorusUtilsTests/SapphireMainnetTests.swift b/Tests/TorusUtilsTests/SapphireMainnetTests.swift index ca89b723..656afbc5 100644 --- a/Tests/TorusUtilsTests/SapphireMainnetTests.swift +++ b/Tests/TorusUtilsTests/SapphireMainnetTests.swift @@ -261,21 +261,16 @@ class SapphireMainnetTests: XCTestCase { let email: String = generateRandomEmail(of: 6) let jwt = try! generateIdToken(email: email) let hashedIDToken = try KeyUtils.keccak256Data(jwt) - let verifierParams = VerifierParams(verifier_id: email) + let verifierParams = VerifierParams(verifier_id: email, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: email, idtoken: jwt)]) let nodeDetails = try await fnd.getNodeDetails(verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierID: email) - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: email, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] - let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeSSSEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + + let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeSSSEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: TORUS_TEST_AGGREGATE_VERIFIER, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertLessThan(data.metadata.serverTimeOffset, 20) XCTAssertNotNil(data.finalKeyData.privKey) XCTAssertNotNil(data.oAuthKeyData.evmAddress) XCTAssertEqual(data.metadata.typeOfUser, .v2) - XCTAssertEqual(data.metadata.nonce, BigUInt(0)) - XCTAssertEqual(data.metadata.upgraded, true) } func test_should_be_able_to_update_sessiontime_of_the_token_signature_data() async throws { diff --git a/Tests/TorusUtilsTests/TestnetTest.swift b/Tests/TorusUtilsTests/TestnetTest.swift index 97fb22b4..f1407e0b 100644 --- a/Tests/TorusUtilsTests/TestnetTest.swift +++ b/Tests/TorusUtilsTests/TestnetTest.swift @@ -16,18 +16,16 @@ class TestnetTest: XCTestCase { fnd = NodeDetailManager(network: .legacy(.TESTNET)) torus = TorusUtils(params: TorusOptions(clientId: "YOUR_CLIENT_ID", network: .legacy(.TESTNET))) } -/* - func getFNDAndTUData(verifer: String, veriferID: String, enableOneKey: Bool = false) async throws -> AllNodeDetailsModel { - let nodeDetails = try await fnd.getNodeDetails(verifier: verifer, verifierID: veriferID) - tu = TorusUtils(params: TorusOptions(clientId: "YOUR_CLIENT_ID", network: .legacy(.TESTNET), enableOneKey: enableOneKey)) - return nodeDetails - } - - func test_get_public_address() async throws { - let verifier: String = "google-lrc" - let verifierID: String = TORUS_TEST_EMAIL - let nodeDetails = try await getFNDAndTUData(verifer: verifier, veriferID: verifierID) - let val = try await tu.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + + func test_should_fetch_public_address() async throws { + let verifier = "google-lrc" + let verifierID = TORUS_TEST_EMAIL + let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) + let val = try await torus.getPublicAddress(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + + XCTAssertEqual(val.finalKeyData!.evmAddress, "0x9bcBAde70546c0796c00323CD1b97fa0a425A506") + XCTAssertLessThan(val.metadata!.serverTimeOffset, 20) + XCTAssertEqual(val.finalKeyData!.evmAddress, "0x9bcBAde70546c0796c00323CD1b97fa0a425A506") XCTAssertEqual(val.finalKeyData!.X, "894f633b3734ddbf08867816bc55da60803c1e7c2a38b148b7fb2a84160a1bb5") XCTAssertEqual(val.finalKeyData!.Y, "1cf2ea7ac63ee1a34da2330413692ba8538bf7cd6512327343d918e0102a1438") @@ -37,102 +35,139 @@ class TestnetTest: XCTestCase { XCTAssertNil(val.metadata?.pubNonce) XCTAssertEqual(val.metadata?.nonce, 0) XCTAssertEqual(val.metadata?.upgraded, false) - XCTAssertEqual(val.metadata?.typeOfUser, UserType(rawValue: "v1")) + XCTAssertEqual(val.metadata?.typeOfUser, .v1) + XCTAssertNotNil(val.nodesData) } - func test_getUserTypeAndAddress_testnet() async throws { - let verifier: String = "google-lrc" - let verifierID: String = TORUS_TEST_EMAIL - let nodeDetails = try await getFNDAndTUData(verifer: verifier, veriferID: verifierID) - let val = try await tu.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + func test_should_fetch_user_type_and_public_address() async throws { + var verifier: String = "google-lrc" + var verifierID: String = TORUS_TEST_EMAIL + let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) + var val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + + XCTAssertEqual(val.finalKeyData!.evmAddress, "0xf5804f608C233b9cdA5952E46EB86C9037fd6842") + XCTAssertLessThan(val.metadata!.serverTimeOffset, 20) + XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0x9bcBAde70546c0796c00323CD1b97fa0a425A506") XCTAssertEqual(val.oAuthKeyData!.X, "894f633b3734ddbf08867816bc55da60803c1e7c2a38b148b7fb2a84160a1bb5") XCTAssertEqual(val.oAuthKeyData!.Y, "1cf2ea7ac63ee1a34da2330413692ba8538bf7cd6512327343d918e0102a1438") - XCTAssertEqual(val.finalKeyData!.evmAddress, "0xf5804f608C233b9cdA5952E46EB86C9037fd6842") XCTAssertEqual(val.finalKeyData!.X, "ed737569a557b50722a8b5c0e4e5ca30cef1ede2f5674a0616b78246bb93dfd0") XCTAssertEqual(val.finalKeyData!.Y, "d9e8e6c54c12c4da38c2f0d1047fcf6089036127738f4ef72a83431339586ca9") - XCTAssertEqual(val.metadata?.pubNonce?.x, "f3f7caefd6540d923c9993113f34226371bd6714a5be6882dedc95a6a929a8") - XCTAssertEqual(val.metadata?.pubNonce?.y, "f28620603601ce54fa0d70fd691fb72ff52f5bf164bf1a91617922eaad8cc7a5") + XCTAssertEqual(val.metadata!.pubNonce!.x, "f3f7caefd6540d923c9993113f34226371bd6714a5be6882dedc95a6a929a8") + XCTAssertEqual(val.metadata!.pubNonce!.y, "f28620603601ce54fa0d70fd691fb72ff52f5bf164bf1a91617922eaad8cc7a5") + XCTAssertEqual(val.metadata?.nonce, 0) + XCTAssertEqual(val.metadata?.upgraded, false) + XCTAssertEqual(val.metadata?.typeOfUser, .v2) + + verifier = "tkey-google-lrc" + verifierID = "somev2user@gmail.com" + val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + + XCTAssertEqual(val.finalKeyData!.evmAddress, "0xE91200d82029603d73d6E307DbCbd9A7D0129d8D") + XCTAssertLessThan(val.metadata!.serverTimeOffset, 20) + + XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0x376597141d8d219553378313d18590F373B09795") + XCTAssertEqual(val.oAuthKeyData!.X, "86cd2db15b7a9937fa8ab7d0bf8e7f4113b64d1f4b2397aad35d6d4749d2fb6c") + XCTAssertEqual(val.oAuthKeyData!.Y, "86ef47a3724144331c31a3a322d85b6fc1a5d113b41eaa0052053b6e3c74a3e2") + XCTAssertEqual(val.finalKeyData!.evmAddress, "0xE91200d82029603d73d6E307DbCbd9A7D0129d8D") + XCTAssertEqual(val.finalKeyData!.X, "c350e338dde24df986915992fea6e0aef3560c245ca144ee7fe1498784c4ef4e") + XCTAssertEqual(val.finalKeyData!.Y, "a605e52b65d3635f89654519dfa7e31f7b45f206ef4189866ad0c2240d40f97f") + XCTAssertEqual(val.metadata?.pubNonce?.x, "ad121b67fa550da814bbbd54ec7070705d058c941e04c03e07967b07b2f90345") + XCTAssertEqual(val.metadata?.pubNonce?.y, "bfe2395b177a72ebb836aaf24cedff2f14cd9ed49047990f5cdb99e4981b5753") XCTAssertEqual(val.metadata?.nonce, 0) XCTAssertEqual(val.metadata?.upgraded, false) XCTAssertEqual(val.metadata?.typeOfUser, .v2) + XCTAssertNotNil(val.nodesData) + + verifier = "tkey-google-lrc" + verifierID = "caspertorus@gmail.com" + val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + + XCTAssertEqual(val.finalKeyData!.evmAddress, "0x1016DA7c47A04C76036637Ea02AcF1d29c64a456") + XCTAssertLessThan(val.metadata!.serverTimeOffset, 20) + + XCTAssertEqual(val.oAuthKeyData!.evmAddress, "0xd45383fbF04BccFa0450d7d8ee453ca86b7C6544") + XCTAssertEqual(val.oAuthKeyData!.X, "d25cc473fbb448d20b5551f3c9aa121e1924b3d197353347187c47ad13ecd5d8") + XCTAssertEqual(val.oAuthKeyData!.Y, "3394000f43160a925e6c3017dde1354ecb2b61739571c6584f58edd6b923b0f5") + XCTAssertEqual(val.finalKeyData!.evmAddress, "0x1016DA7c47A04C76036637Ea02AcF1d29c64a456") + XCTAssertEqual(val.finalKeyData!.X, "d3e222f6b23f0436b7c86e9cc4164eb5ea8448e4c0e7539c8b4f7fd00e8ec5c7") + XCTAssertEqual(val.finalKeyData!.Y, "1c47f5faccec6cf57c36919f6f0941fe3d8d65033cf2cc78f209304386044222") + XCTAssertEqual(val.metadata?.pubNonce?.x, "4f86b0e69992d1551f1b16ceb0909453dbe17b9422b030ee6c5471c2e16b65d0") + XCTAssertEqual(val.metadata?.pubNonce?.y, "640384f3d39debb04c4e9fe5a5ec6a1b494b0ad66d00ac9be6f166f21d116ca4") + XCTAssertEqual(val.metadata?.nonce, 0) + XCTAssertEqual(val.metadata?.upgraded, true) + XCTAssertEqual(val.metadata?.typeOfUser, .v2) + XCTAssertNotNil(val.nodesData) } - /* TODO: Investigate further - func test_key_assign_testnet() async throws { - let fakeEmail = generateRandomEmail(of: 6) - let verifier: String = "google-lrc" - let verifierID: String = fakeEmail - let nodeDetails = try await getFNDAndTUData(verifer: verifier, veriferID: verifierID) - let data = try await tu.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) - XCTAssertNotNil(data.finalKeyData) - XCTAssertNotEqual(data.finalKeyData?.evmAddress, "") - XCTAssertEqual(data.metadata?.typeOfUser, .v1) - } - */ + func test_should_be_able_to_key_assign() async throws { + let fakeEmail = generateRandomEmail(of: 6) + let verifier: String = "google-lrc" + let verifierID: String = fakeEmail + let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) + let data = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID) + XCTAssertNotEqual(data.finalKeyData?.evmAddress, "") + XCTAssertNotEqual(data.oAuthKeyData?.evmAddress, "") + XCTAssertEqual(data.metadata?.typeOfUser, .v1) + XCTAssertEqual(data.metadata?.upgraded, false) + } - func test_login_testnet() async throws { + func test_should_be_able_to_login() async throws { let verifier: String = TORUS_TEST_VERIFIER let verifierID: String = TORUS_TEST_EMAIL - let verifierParams = VerifierParams(verifier_id: verifierID) let jwt = try! generateIdToken(email: verifierID) - let extraParams = ["verifieridentifier": verifier, "verifier_id": verifierID] as [String: Codable] - let nodeDetails = try await getFNDAndTUData(verifer: verifier, veriferID: verifierID) - let data = try await tu.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: jwt, extraParams: extraParams) + let verifierParams = VerifierParams(verifier_id: verifierID) + let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) + let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: jwt) + XCTAssertEqual(data.finalKeyData.evmAddress, "0xF8d2d3cFC30949C1cb1499C9aAC8F9300535a8d6") - XCTAssertEqual(data.finalKeyData.X, "6de2e34d488dd6a6b596524075b032a5d5eb945bcc33923ab5b88fd4fd04b5fd") - XCTAssertEqual(data.finalKeyData.Y, "d5fb7b51b846e05362461357ec6e8ca075ea62507e2d5d7253b72b0b960927e9") - XCTAssertEqual(data.finalKeyData.privKey, "9b0fb017db14a0a25ed51f78a258713c8ae88b5e58a43acb70b22f9e2ee138e3") + XCTAssertLessThan(data.metadata.serverTimeOffset, 20) + XCTAssertEqual(data.oAuthKeyData.evmAddress, "0xF8d2d3cFC30949C1cb1499C9aAC8F9300535a8d6") XCTAssertEqual(data.oAuthKeyData.X, "6de2e34d488dd6a6b596524075b032a5d5eb945bcc33923ab5b88fd4fd04b5fd") XCTAssertEqual(data.oAuthKeyData.Y, "d5fb7b51b846e05362461357ec6e8ca075ea62507e2d5d7253b72b0b960927e9") XCTAssertEqual(data.oAuthKeyData.privKey, "9b0fb017db14a0a25ed51f78a258713c8ae88b5e58a43acb70b22f9e2ee138e3") + XCTAssertEqual(data.finalKeyData.evmAddress, "0xF8d2d3cFC30949C1cb1499C9aAC8F9300535a8d6") + XCTAssertEqual(data.finalKeyData.X, "6de2e34d488dd6a6b596524075b032a5d5eb945bcc33923ab5b88fd4fd04b5fd") + XCTAssertEqual(data.finalKeyData.Y, "d5fb7b51b846e05362461357ec6e8ca075ea62507e2d5d7253b72b0b960927e9") + XCTAssertEqual(data.finalKeyData.privKey, "9b0fb017db14a0a25ed51f78a258713c8ae88b5e58a43acb70b22f9e2ee138e3") + XCTAssertNotNil(data.sessionData) XCTAssertNil(data.metadata.pubNonce) XCTAssertEqual(data.metadata.nonce, BigUInt(0)) XCTAssertEqual(data.metadata.typeOfUser, .v1) XCTAssertNil(data.metadata.upgraded) + XCTAssertNotNil(data.nodesData) } - func test_aggregate_login_testnet() async throws { + func test_should_be_able_to_aggregate_login() async throws { let verifier: String = TORUS_TEST_AGGREGATE_VERIFIER let verifierID: String = TORUS_TEST_EMAIL - let verifierParams = VerifierParams(verifier_id: verifierID) let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL) - let hashedIDToken = try KeyUtils.keccak256Data(jwt.data(using: .utf8)!).toHexString() - let extraParams = ["verifier_id": TORUS_TEST_EMAIL, "sub_verifier_ids": [TORUS_TEST_VERIFIER], "verify_params": [["verifier_id": TORUS_TEST_EMAIL, "idtoken": jwt]]] as [String: Codable] - let nodeDetails = try await getFNDAndTUData(verifer: verifier, veriferID: verifierID) - let data = try await tu.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + let hashedIDToken = try KeyUtils.keccak256Data(jwt) + let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) + + let verifierParams = VerifierParams(verifier_id: verifierID, + sub_verifier_ids: [TORUS_TEST_VERIFIER], + verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: verifierID, idtoken: jwt)]) + let data = try await torus.retrieveShares(endpoints: nodeDetails.torusNodeEndpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken) + XCTAssertEqual(data.finalKeyData.evmAddress, "0x938a40E155d118BD31E439A9d92D67bd55317965") - XCTAssertEqual(data.finalKeyData.X, "1c50e34ef5b7afcf5b0c6501a6ae00ec3a09a321dd885c5073dd122e2a251b95") - XCTAssertEqual(data.finalKeyData.Y, "2cc74beb28f2c4a7c4034f80836d51b2781b36fefbeafb4eb1cd055bdf73b1e6") - XCTAssertEqual(data.finalKeyData.privKey, "3cbfa57d702327ec1af505adc88ad577804a1a7780bc013ed9e714c547fb5cb1") + XCTAssertLessThan(data.metadata.serverTimeOffset, 20) + XCTAssertEqual(data.oAuthKeyData.evmAddress, "0x938a40E155d118BD31E439A9d92D67bd55317965") XCTAssertEqual(data.oAuthKeyData.X, "1c50e34ef5b7afcf5b0c6501a6ae00ec3a09a321dd885c5073dd122e2a251b95") XCTAssertEqual(data.oAuthKeyData.Y, "2cc74beb28f2c4a7c4034f80836d51b2781b36fefbeafb4eb1cd055bdf73b1e6") XCTAssertEqual(data.oAuthKeyData.privKey, "3cbfa57d702327ec1af505adc88ad577804a1a7780bc013ed9e714c547fb5cb1") - XCTAssertEqual(data.sessionData.sessionTokenData.count, 0) - XCTAssertEqual(data.sessionData.sessionAuthKey, "") + XCTAssertEqual(data.finalKeyData.evmAddress, "0x938a40E155d118BD31E439A9d92D67bd55317965") + XCTAssertEqual(data.finalKeyData.X, "1c50e34ef5b7afcf5b0c6501a6ae00ec3a09a321dd885c5073dd122e2a251b95") + XCTAssertEqual(data.finalKeyData.Y, "2cc74beb28f2c4a7c4034f80836d51b2781b36fefbeafb4eb1cd055bdf73b1e6") + XCTAssertEqual(data.finalKeyData.privKey, "3cbfa57d702327ec1af505adc88ad577804a1a7780bc013ed9e714c547fb5cb1") + XCTAssertNotNil(data.sessionData) XCTAssertNil(data.metadata.pubNonce) XCTAssertEqual(data.metadata.nonce, BigUInt(0)) XCTAssertEqual(data.metadata.typeOfUser, .v1) XCTAssertNil(data.metadata.upgraded) + XCTAssertNotNil(data.nodesData) } } - -extension TestnetTest { - // TODO: Uncouple dependency on nodeIndex == "1" - func test_retrieveShares_some_nodes_down() async throws { - let verifier: String = TORUS_TEST_VERIFIER - let verifierID: String = TORUS_TEST_EMAIL - let verifierParams = VerifierParams(verifier_id: verifierID) - let jwt = try! generateIdToken(email: verifierID) - let extraParams = ["verifieridentifier": verifier, "verifier_id": verifierID] as [String: Codable] - let nodeDetails = try await getFNDAndTUData(verifer: verifier, veriferID: verifierID) - var endpoints = nodeDetails.getTorusNodeEndpoints() - endpoints[endpoints.count - 1] = "https://ndjnfjbfrj/random" - let data = try await tu.retrieveShares(endpoints: endpoints, indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: jwt, extraParams: extraParams) - XCTAssertEqual(data.finalKeyData.privKey, "9b0fb017db14a0a25ed51f78a258713c8ae88b5e58a43acb70b22f9e2ee138e3") - } - */ -} diff --git a/Tests/TorusUtilsTests/oneKeyTest.swift b/Tests/TorusUtilsTests/oneKeyTest.swift index f0699489..18ed46a5 100644 --- a/Tests/TorusUtilsTests/oneKeyTest.swift +++ b/Tests/TorusUtilsTests/oneKeyTest.swift @@ -73,15 +73,10 @@ class OneKeyTest: XCTestCase { let verifier: String = TORUS_TEST_AGGREGATE_VERIFIER let verifierID: String = TORUS_TEST_EMAIL let jwt = try! generateIdToken(email: TORUS_TEST_EMAIL) - let verifierParams = VerifierParams(verifier_id: verifierID) - let hashedIDToken = try KeyUtils.keccak256Data(jwt.data(using: .utf8)!).toHexString() - let extraParams = [ - "verify_params": [ShareRequestParams.ShareRequestVerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)], - "sub_verifier_ids": [TORUS_TEST_VERIFIER], - ] as [String: Codable] - + let verifierParams = VerifierParams(verifier_id: verifierID, sub_verifier_ids: [TORUS_TEST_VERIFIER], verify_params: [ShareRequestParams.ShareRequestVerifyParams(verifier_id: TORUS_TEST_EMAIL, idtoken: jwt)]) + let hashedIDToken = try KeyUtils.keccak256Data(jwt) let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID) - let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken, extraParams: extraParams) + let data = try await torus.retrieveShares(endpoints: nodeDetails.getTorusNodeEndpoints(), indexes: nodeDetails.getTorusIndexes(), verifier: verifier, verifierParams: verifierParams, idToken: hashedIDToken) XCTAssertLessThan(data.metadata.serverTimeOffset, 20)