Skip to content

Commit

Permalink
checkpoint: testnet tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed May 14, 2024
1 parent d3ba2f7 commit 810dc3b
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 169 deletions.
77 changes: 34 additions & 43 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 5 additions & 9 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)

Expand Down Expand Up @@ -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,
Expand All @@ -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))))
Expand Down
6 changes: 5 additions & 1 deletion Sources/TorusUtils/VerifierParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
8 changes: 2 additions & 6 deletions Tests/TorusUtilsTests/AquaTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 2 additions & 6 deletions Tests/TorusUtilsTests/Celeste.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 3 additions & 6 deletions Tests/TorusUtilsTests/CyanTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions Tests/TorusUtilsTests/MainnetTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 2 additions & 7 deletions Tests/TorusUtilsTests/SapphireDevnetTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions Tests/TorusUtilsTests/SapphireMainnetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit 810dc3b

Please sign in to comment.