Skip to content

Commit

Permalink
Merge pull request #106 from torusresearch/check_is_new_key
Browse files Browse the repository at this point in the history
fix: Additional check for isNewKey
  • Loading branch information
grvgoel81 authored Sep 13, 2024
2 parents d9e51c5 + e1a6bf7 commit 4bc19c7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
14 changes: 9 additions & 5 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,10 @@ internal class NodeUtils {
var sessionTokens: [String?] = []
var nodeIndexes: [Int?] = []
var sessionTokenDatas: [SessionToken?] = []
var isNewKeys: [String] = []
var isNewKeys: [IsNewKeyResponse] = []

for item in shareResponses {
isNewKeys.append(item.isNewKey)
isNewKeys.append(IsNewKeyResponse(isNewKey: item.isNewKey == "true", publicKeyX: item.keys.first?.publicKey.X ?? ""))

if !item.sessionTokenSigs.isEmpty {
if !item.sessionTokenSigMetadata.isEmpty {
Expand Down Expand Up @@ -506,7 +506,12 @@ internal class NodeUtils {
throw TorusUtilError.privateKeyDeriveFailed
}

let thresholdIsNewKey: String? = try thresholdSame(arr: isNewKeys, threshold: threshold)
var isNewKey = false;
for item in isNewKeys {
if (item.isNewKey && item.publicKeyX.lowercased() == thresholdPublicKey!.X.lowercased()) {
isNewKey = true
}
}

let oAuthKey = privateKey!
let oAuthPublicKey = try SecretKey(hex: oAuthKey).toPublic().serialize(compressed: false)
Expand All @@ -520,8 +525,7 @@ internal class NodeUtils {
finalPubKey = oAuthPublicKey
} else if TorusUtils.isLegacyNetworkRouteMap(network: network) {
if enableOneKey {
let isNewKey = !(thresholdIsNewKey == "true")
let nonce = try await MetadataUtils.getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffsetResponse, X: thresholdPublicKey!.X, Y: thresholdPublicKey!.Y, privateKey: oAuthKey, getOnly: isNewKey)
let nonce = try await MetadataUtils.getOrSetNonce(legacyMetadataHost: legacyMetadataHost, serverTimeOffset: serverTimeOffsetResponse, X: thresholdPublicKey!.X, Y: thresholdPublicKey!.Y, privateKey: oAuthKey, getOnly: !isNewKey)
metadataNonce = BigInt(nonce.nonce?.addLeading0sForLength64() ?? "0", radix: 16) ?? BigInt(0)
typeOfUser = UserType(rawValue: nonce.typeOfUser?.lowercased() ?? "v1")!
if typeOfUser == .v2 {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

internal struct IsNewKeyResponse: Codable {
public var isNewKey: Bool;
public var publicKeyX: String;

public init(isNewKey: Bool, publicKeyX: String) {
self.isNewKey = isNewKey
self.publicKeyX = publicKeyX
}
}

0 comments on commit 4bc19c7

Please sign in to comment.