Skip to content

Commit

Permalink
Merge pull request #103 from torusresearch/import_check
Browse files Browse the repository at this point in the history
add check for import share
  • Loading branch information
metalurgical authored Sep 13, 2024
2 parents 303dc2c + 4bc19c7 commit 23083bf
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
9 changes: 9 additions & 0 deletions Sources/TorusUtils/Extensions/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ extension String {
return self
}
}

public func addLeading0sForLength128() -> String {
if count < 128 {
let toAdd = String(repeating: "0", count: 128 - count)
return toAdd + self
} else {
return self
}
}

public func hexEncodedToString() -> String {
var finalString = ""
Expand Down
5 changes: 4 additions & 1 deletion Sources/TorusUtils/Helpers/KeyUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public class KeyUtils {
publicKeyUnprefixed = publicKeyUnprefixed.strip04Prefix()
}

if !(publicKeyUnprefixed.count == 128) {

if (publicKeyUnprefixed.count <= 128) {
publicKeyUnprefixed = publicKeyUnprefixed.addLeading0sForLength128()
} else {
throw TorusUtilError.invalidPubKeySize
}

Expand Down
24 changes: 19 additions & 5 deletions Sources/TorusUtils/Helpers/NodeUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ internal class NodeUtils {
idToken: String,
importedShares: [ImportedShare]?,
apiKey: String = "torus-default",
newPrivateKey: String?,
extraParams: TorusUtilsExtraParams
) async throws -> TorusKey {
let threshold = Int(trunc(Double((endpoints.count / 2) + 1)))
Expand Down Expand Up @@ -402,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 @@ -505,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 @@ -519,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 Expand Up @@ -569,6 +574,15 @@ internal class NodeUtils {
finalPrivKey = privateKeyWithNonce.magnitude.serialize().hexString.addLeading0sForLength64()
}

// This is a sanity check to make doubly sure we are returning the correct private key after importing a share
if isImportShareReq {
if newPrivateKey == nil {
throw TorusUtilError.importShareFailed
} else if (!(finalPrivKey == newPrivateKey!.addLeading0sForLength64())) {
throw TorusUtilError.importShareFailed
}
}

var isUpgraded: Bool?
if typeOfUser == .v2 {
isUpgraded = metadataNonce == BigInt(0)
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
}
}
4 changes: 2 additions & 2 deletions Sources/TorusUtils/TorusUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class TorusUtils {
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: extraParams)
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, newPrivateKey: nil, extraParams: extraParams)
}

/// Retrieves user information, defaulting the user type to .v2
Expand Down Expand Up @@ -185,7 +185,7 @@ public class TorusUtils {
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)
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, newPrivateKey: newPrivateKey, extraParams: extraParams)
}

/// Retrieves user information
Expand Down

0 comments on commit 23083bf

Please sign in to comment.