Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
metalurgical committed May 15, 2024
1 parent 8069060 commit 1e3a938
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 62 deletions.
40 changes: 20 additions & 20 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class NodeUtils {
verifierId: String,
extendedVerifierId: String? = nil) async throws -> KeyLookupResult {
let threshold = Int(trunc(Double((endpoints.count / 2) + 1)))

let params = GetOrSetKeyParams(distributed_metadata: true, verifier: verifier, verifier_id: verifierId, extended_verifier_id: extendedVerifierId, one_key_flow: true, fetch_node_index: true, client_time: String(Int(floor(Date().timeIntervalSince1970 / 1000))))
let jsonRPCRequest = JRPCRequest(
method: JRPC_METHODS.GET_OR_SET_KEY,
Expand Down Expand Up @@ -115,8 +115,8 @@ public class NodeUtils {
apiKey: String = "torus-default",
extraParams: [String: Codable] = [:]
) async throws -> TorusKey {
let minThresholdRequired = Int(trunc(Double((endpoints.count / 2) + 1)))
let threshold = Int(trunc(Double((endpoints.count / 2) + 1)))

var allowHostRequest = try MetadataUtils.makeUrlRequest(url: allowHost, httpMethod: .get)
allowHostRequest.addValue(apiKey, forHTTPHeaderField: "x-api-key")
allowHostRequest.addValue(verifier, forHTTPHeaderField: "origin")
Expand All @@ -131,7 +131,7 @@ public class NodeUtils {
let errorData = try JSONDecoder().decode(AllowRejected.self, from: allowHostResult.0)
throw TorusUtilError.gatingError("code: \(errorData.code), error: \(errorData.error)")
}

let sessionAuthKey = SecretKey()
let sessionAuthKeySerialized = try sessionAuthKey.serialize().addLeading0sForLength64()
let pubKey = try sessionAuthKey.toPublic().serialize(compressed: false)
Expand Down Expand Up @@ -197,19 +197,18 @@ public class NodeUtils {
let shareRequestResults: [JRPCResponse<ShareRequestResult>?] = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: [JRPCResponse<ShareRequestResult>?].self) {
group -> [JRPCResponse<ShareRequestResult>?] in
let isImport = isImportShareReq // Local copy for parellel code

for i in 0 ..< endpoints.count {
if !nodeSigs.indices.contains(i) {
continue
}

group.addTask {
let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int

if isImport {
do {
let importShare = importedShares![i]

let shareRequestItem = ShareRequestParams.ShareRequestItem(
verifieridentifier: verifier,
verifier_id: verifierParams.verifier_id,
Expand All @@ -231,7 +230,7 @@ public class NodeUtils {
)

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 Down Expand Up @@ -284,19 +283,20 @@ public class NodeUtils {
}
}
}

var collected = [JRPCResponse<ShareRequestResult>?]()
for try await value in group {
collected.append(value)
}

return collected
}

let shareResponses: [ShareRequestResult] = shareRequestResults.filter({ $0?.result != nil }).map({ $0!.result! })

let pubkeys = shareResponses.filter({$0.keys.count > 0}).map { $0.keys[0].publicKey }

let thresholdPublicKey = try thresholdSame(arr: pubkeys, threshold: minThresholdRequired)
let pubkeys = shareResponses.filter({ $0.keys.count > 0 }).map { $0.keys[0].publicKey }

let thresholdPublicKey = try thresholdSame(arr: pubkeys, threshold: threshold)

if thresholdPublicKey == nil {
throw TorusUtilError.retrieveOrImportShareError
Expand All @@ -317,7 +317,7 @@ public class NodeUtils {
throw TorusUtilError.runtime("invalid metadata result from nodes, nonce metadata is empty")
}

let thresholdReqCount = (importedShares != nil && importedShares!.count > 0) ? endpoints.count : minThresholdRequired
let thresholdReqCount = (importedShares != nil && importedShares!.count > 0) ? endpoints.count : threshold

// Invert comparision to return error early
if !(shareResponses.count >= thresholdReqCount && thresholdPublicKey != nil && (thresholdNonceData != nil || verifierParams.extended_verifier_id != nil || TorusUtils.isLegacyNetworkRouteMap(network: network))) {
Expand Down Expand Up @@ -376,16 +376,16 @@ public class NodeUtils {
shares.append(nil)
}
}

let validSigs = sessionTokenSigs.filter({ $0 != nil }).map({ $0! })

if verifierParams.extended_verifier_id == nil && validSigs.count < minThresholdRequired {
if verifierParams.extended_verifier_id == nil && validSigs.count < threshold {
throw TorusUtilError.retrieveOrImportShareError
}

let validTokens = sessionTokens.filter({ $0 != nil }).map({ $0! })

if verifierParams.extended_verifier_id == nil && validTokens.count < minThresholdRequired {
if verifierParams.extended_verifier_id == nil && validTokens.count < threshold {
throw TorusUtilError.runtime("Insufficient number of signatures from nodes")
}

Expand All @@ -404,7 +404,7 @@ public class NodeUtils {
}
}
let elements = Array(0 ..< decryptedShares.count)
let allCombis = kCombinations(elements: elements.slice, k: minThresholdRequired)
let allCombis = kCombinations(elements: elements.slice, k: threshold)

var privateKey: String?

Expand All @@ -431,7 +431,7 @@ public class NodeUtils {
throw TorusUtilError.privateKeyDeriveFailed
}

let thresholdIsNewKey = try thresholdSame(arr: isNewKeys, threshold: minThresholdRequired)
let thresholdIsNewKey = try thresholdSame(arr: isNewKeys, threshold: threshold)

let serverOffsetTimes = serverTimeOffsets.map({ Int($0) ?? 0 })

Expand Down
6 changes: 3 additions & 3 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class TorusUtils {
newPrivateKey: String
) async throws -> TorusKey {
let nodePubs = TorusNodePubModelToINodePub(nodes: nodePubKeys)

let threshold = Int(trunc(Double((endpoints.count / 2) + 1)))

let degree = threshold - 1
Expand Down Expand Up @@ -143,7 +143,7 @@ public class TorusUtils {
var jsonShares: [Data] = []

for i in 0 ..< nodeIndexes.count {
let share: Share? = shares[nodeIndexes[i].serialize().addLeading0sForLength64().hexString]
let share: Share? = shares[nodeIndexes[i].serialize().hexString.addLeading0sForLength64()]
if share == nil {
throw TorusUtilError.runtime("Missing node pub key for node index")
}
Expand All @@ -163,7 +163,7 @@ public class TorusUtils {
pubKeyY: pubKeyY,
encryptedShare: encParamsMetadata.ciphertext!,
encryptedShareMetadata: encParamsMetadata,
nodeIndex: Int(shares[nodeIndexes[i].magnitude.serialize().addLeading0sForLength64().hexString]!.shareIndex.magnitude.serialize().addLeading0sForLength64().hexString)!,
nodeIndex: Int(shares[nodeIndexes[i].magnitude.serialize().hexString.addLeading0sForLength64()]!.shareIndex.magnitude.serialize().hexString.addLeading0sForLength64())!,
keyType: "secp256k1",
nonceData: nonceData.hexString,
nonceSignature: nonceParams.signature)
Expand Down
10 changes: 5 additions & 5 deletions Tests/TorusUtilsTests/AquaTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class AquaTest: XCTestCase {
let verifier: String = "tkey-google-aqua"
let verifierID: String = TORUS_TEST_EMAIL
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xDfA967285AC699A70DA340F60d00DB19A272639d")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)
Expand All @@ -43,7 +43,7 @@ class AquaTest: XCTestCase {
var verifier: String = "tkey-google-aqua"
var verifierID: String = TORUS_TEST_EMAIL
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let result1 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
let result1 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)
XCTAssertLessThan(result1.metadata!.serverTimeOffset, 20)

XCTAssertEqual(result1.oAuthKeyData!.evmAddress, "0xDfA967285AC699A70DA340F60d00DB19A272639d")
Expand All @@ -62,7 +62,7 @@ class AquaTest: XCTestCase {
// 1/1 user
verifier = "tkey-google-aqua"
verifierID = "[email protected]"
let result2 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
let result2 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertLessThan(result2.metadata!.serverTimeOffset, 20)

Expand All @@ -82,7 +82,7 @@ class AquaTest: XCTestCase {

// 2/n user
verifierID = "[email protected]"
let result3 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
let result3 = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertLessThan(result2.metadata!.serverTimeOffset, 20)

Expand All @@ -105,7 +105,7 @@ class AquaTest: XCTestCase {
let verifier: String = "tkey-google-aqua"
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)
let data = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)
//note: duplicated checks on next two lines in javascript
XCTAssertNotNil(data.finalKeyData?.evmAddress)
XCTAssertNotNil(data.oAuthKeyData?.evmAddress)
Expand Down
10 changes: 5 additions & 5 deletions Tests/TorusUtilsTests/Celeste.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CelesteTest: XCTestCase {
let verifier = "tkey-google-celeste"
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)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.torusNodeEndpoints, verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)
Expand All @@ -43,7 +43,7 @@ class CelesteTest: XCTestCase {
var verifier: String = "tkey-google-celeste"
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)
var val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xC3115b9d6FaB99739b23DA9dfcBA47A4Ec4Cd113")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)
Expand All @@ -60,7 +60,7 @@ class CelesteTest: XCTestCase {

verifier = "tkey-google-celeste"
verifierID = "[email protected]"
val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0x8d69CE354DA39413f205FdC8680dE1F3FBBb36e2")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)
Expand All @@ -82,7 +82,7 @@ class CelesteTest: XCTestCase {

verifier = "tkey-google-celeste"
verifierID = "[email protected]"
val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
val = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0x8108c29976C458e76f797AD55A3715Ce80a3fe78")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)
Expand All @@ -106,7 +106,7 @@ class CelesteTest: XCTestCase {
let verifier: String = "tkey-google-celeste"
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)
let data = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)
XCTAssertNotEqual(data.finalKeyData?.evmAddress, "")
XCTAssertNotEqual(data.oAuthKeyData?.evmAddress, "")
XCTAssertEqual(data.metadata?.typeOfUser, .v1)
Expand Down
10 changes: 5 additions & 5 deletions Tests/TorusUtilsTests/CyanTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CyanTest: XCTestCase {
let verifier: String = "tkey-google-cyan"
let verifierID: String = TORUS_TEST_EMAIL
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
let val = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(val.finalKeyData!.evmAddress, "0xA3767911A84bE6907f26C572bc89426dDdDB2825")
XCTAssertLessThan(val.metadata!.serverTimeOffset, 20)
Expand All @@ -43,7 +43,7 @@ class CyanTest: XCTestCase {
var verifier: String = "tkey-google-cyan"
var verifierID: String = TORUS_TEST_EMAIL
let nodeDetails = try await fnd.getNodeDetails(verifier: verifier, verifierID: verifierID)
var data = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
var data = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(data.finalKeyData!.evmAddress, "0x3507F0d192a44E436B8a6C32a37d57D022861b1a")
XCTAssertLessThan(data.metadata!.serverTimeOffset, 20)
Expand All @@ -63,7 +63,7 @@ class CyanTest: XCTestCase {

verifier = "tkey-google-cyan"
verifierID = "[email protected]"
data = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
data = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(data.finalKeyData!.evmAddress, "0x8EA83Ace86EB414747F2b23f03C38A34E0217814")
XCTAssertLessThan(data.metadata!.serverTimeOffset, 20)
Expand All @@ -83,7 +83,7 @@ class CyanTest: XCTestCase {

verifier = "tkey-google-cyan"
verifierID = "[email protected]"
data = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), torusNodePubs: nodeDetails.getTorusNodePub(), verifier: verifier, verifierId: verifierID)
data = try await torus.getUserTypeAndAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)

XCTAssertEqual(data.finalKeyData!.evmAddress, "0xCC1f953f6972a9e3d685d260399D6B85E2117561")
XCTAssertLessThan(data.metadata!.serverTimeOffset, 20)
Expand All @@ -107,7 +107,7 @@ class CyanTest: XCTestCase {
let verifier: String = "tkey-google-cyan"
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)
let data = try await torus.getPublicAddress(endpoints: nodeDetails.getTorusNodeEndpoints(), verifier: verifier, verifierId: verifierID)
XCTAssertNotEqual(data.finalKeyData?.evmAddress, "")
XCTAssertNotEqual(data.oAuthKeyData?.evmAddress, "")
XCTAssertEqual(data.metadata?.typeOfUser, .v1)
Expand Down
Loading

0 comments on commit 1e3a938

Please sign in to comment.