From 9797680abf2b35d3b25f431a56e7874d5a42ca00 Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Fri, 19 Jul 2024 16:34:02 +0200 Subject: [PATCH] review comment: behave like some(k,t) --- .../TorusUtils/Helpers/MetadataUtils.swift | 6 +- Sources/TorusUtils/Helpers/NodeUtils.swift | 117 ++++++++---------- 2 files changed, 56 insertions(+), 67 deletions(-) diff --git a/Sources/TorusUtils/Helpers/MetadataUtils.swift b/Sources/TorusUtils/Helpers/MetadataUtils.swift index fe934f29..f188277a 100644 --- a/Sources/TorusUtils/Helpers/MetadataUtils.swift +++ b/Sources/TorusUtils/Helpers/MetadataUtils.swift @@ -21,7 +21,11 @@ internal class MetadataUtils { public static func decrypt(privateKey: String, opts: ECIES) throws -> Data { let secret = try SecretKey(hex: privateKey) - let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: opts.ephemPublicKey), iv: opts.iv, mac: opts.mac) + var publicKey = opts.ephemPublicKey + if opts.ephemPublicKey.count == 128 { // missing 04 prefix + publicKey = "04" + publicKey + } + let msg = try EncryptedMessage(cipherText: opts.ciphertext, ephemeralPublicKey: PublicKey(hex: publicKey), iv: opts.iv, mac: opts.mac) let result = try Encryption.decrypt(sk: secret, encrypted: msg) return result } diff --git a/Sources/TorusUtils/Helpers/NodeUtils.swift b/Sources/TorusUtils/Helpers/NodeUtils.swift index b6c12b4e..ddec03b2 100644 --- a/Sources/TorusUtils/Helpers/NodeUtils.swift +++ b/Sources/TorusUtils/Helpers/NodeUtils.swift @@ -29,10 +29,7 @@ internal class NodeUtils { var nonceResult: GetOrSetNonceResult? var nodeIndexes: [Int] = [] - let minRequired = Int(trunc(Double(endpoints.count * 3 / 4) + 1)) - - let lookupResults: [JRPCResponse?] = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: [JRPCResponse?].self) { group -> [JRPCResponse?] in - var received: Int = 0 + let (keyResult, lookupResults, errorResult): (KeyLookupResult.KeyResult?, [JRPCResponse?], ErrorMessage?) = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: (KeyLookupResult.KeyResult?, [JRPCResponse?], ErrorMessage?).self) { group -> (KeyLookupResult.KeyResult?, [JRPCResponse?], ErrorMessage?) in for endpoint in endpoints { group.addTask { do { @@ -47,25 +44,26 @@ internal class NodeUtils { } } var collected = [JRPCResponse?]() + var errorResult: ErrorMessage? + var lookupPubKeys = [JRPCResponse?]() + var keyResult: KeyLookupResult.KeyResult? for try await value in group { collected.append(value) - if value != nil && value?.error == nil { - received += 1 - if received >= minRequired { - group.cancelAll() - } - } - } - return collected - } - let lookupPubKeys = lookupResults.filter({ $0 != nil && $0?.error == nil }) + lookupPubKeys = collected.filter({ $0 != nil && $0?.error == nil }) + + errorResult = try thresholdSame(arr: collected.filter({ $0?.error != nil }).map { $0?.error }, threshold: threshold) as? ErrorMessage - let errorResult = try thresholdSame(arr: lookupResults.filter({ $0?.error != nil }).map { $0?.error }, threshold: threshold) + let normalizedKeyResults = lookupPubKeys.map({ normalizeKeysResult(result: ($0!.result)!) }) - let normalizedKeyResults = lookupPubKeys.map({ normalizeKeysResult(result: ($0!.result)!) }) + keyResult = try thresholdSame(arr: normalizedKeyResults, threshold: threshold) - let keyResult = try thresholdSame(arr: normalizedKeyResults, threshold: threshold) + if keyResult != nil { + group.cancelAll() + } + } + return (keyResult, lookupPubKeys, errorResult) + } if keyResult != nil && nonceResult == nil && extendedVerifierId == nil && !TorusUtils.isLegacyNetworkRouteMap(network: network) { for i in 0 ..< lookupResults.count { @@ -84,6 +82,9 @@ internal class NodeUtils { if nonceResult == nil { let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(metadataHost: legacyMetadataHost, network: network, X: keyResult!.keys[0].pub_key_X, Y: keyResult!.keys[0].pub_key_Y) nonceResult = metadataNonce + if nonceResult!.nonce != nil { + nonceResult!.nonce = nil + } } } @@ -113,7 +114,7 @@ internal class NodeUtils { nodeIndexes: nodeIndexes, serverTimeOffset: serverTimeOffset, nonceResult: nonceResult, - errorResult: errorResult == nil ? nil : errorResult as? ErrorMessage) + errorResult: errorResult) } public static func retrieveOrImportShare( @@ -175,9 +176,8 @@ internal class NodeUtils { encoder.outputFormatting = .sortedKeys let rpcdata = try encoder.encode(jsonRPCRequest) - let minRequired = Int(trunc(Double(endpoints.count * 3 / 4) + 1)) - - let commitmentRequestResults: [JRPCResponse?] = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: [JRPCResponse?].self) { group -> [JRPCResponse?] in + let nodeSigs: [CommitmentRequestResult] = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: [CommitmentRequestResult].self) { group -> [CommitmentRequestResult] in + let minRequiredCommitmments = Int(trunc(Double(endpoints.count * 3 / 4) + 1)) var received: Int = 0 for endpoint in endpoints { group.addTask { @@ -192,16 +192,18 @@ internal class NodeUtils { } } } - var collected = [JRPCResponse?]() + var collected = [CommitmentRequestResult]() for try await value in group { - collected.append(value) - if !isImportShareReq && value != nil && value?.error == nil { + if (value != nil && value?.error == nil) { + collected.append(value!.result!) received += 1 - if received >= minRequired { - group.cancelAll() + if !isImportShareReq { + if received >= minRequiredCommitmments { + group.cancelAll() + } } - } else if isImportShareReq { - if value == nil || value?.error != nil { + } else { + if isImportShareReq { // cannot continue, all must pass for import group.cancelAll() } @@ -210,21 +212,18 @@ internal class NodeUtils { return collected } - let completedCommitmentRequests = commitmentRequestResults.filter({ $0 != nil && $0?.error == nil }) - - if importedShareCount > 0 && !(commitmentRequestResults.count == endpoints.count) { + if importedShareCount > 0 && !(nodeSigs.count == endpoints.count) { throw TorusUtilError.commitmentRequestFailed } - let nodeSigs = completedCommitmentRequests.filter({ $0?.error == nil }).map({ $0!.result }) - var thresholdNonceData: GetOrSetNonceResult? let sessionExpiry: Int? = extraParams["session_token_exp_second"] as? Int var shareImportSuccess = false - var shareRequestResults: [ShareRequestResult?] = [] + var shareResponses = [ShareRequestResult]() + var thresholdPublicKey: KeyAssignment.PublicKey? if isImportShareReq { var importedItems: [ShareRequestParams.ShareRequestItem] = [] @@ -279,21 +278,15 @@ internal class NodeUtils { throw TorusUtilError.importShareFailed } - shareRequestResults = decoded.result! + shareResponses = decoded.result! - } else { - let shareResults: [JRPCResponse?] = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: [JRPCResponse?].self) { - group -> [JRPCResponse?] in - var received = 0 + let pubkeys = shareResponses.filter({ $0.keys.count > 0 }).map { $0.keys[0].publicKey } + thresholdPublicKey = try thresholdSame(arr: pubkeys, threshold: threshold) + } else { + (shareResponses, thresholdPublicKey) = try await withThrowingTaskGroup(of: JRPCResponse?.self, returning: ([ShareRequestResult], KeyAssignment.PublicKey?).self) { + group -> ([ShareRequestResult], KeyAssignment.PublicKey?) in for i in 0 ..< endpoints.count { - if !nodeSigs.indices.contains(i) { - if isImportShareReq { - throw TorusUtilError.importShareFailed - } - continue - } - group.addTask { do { let shareRequestItem = ShareRequestParams.ShareRequestItem( @@ -330,29 +323,26 @@ internal class NodeUtils { } var collected = [JRPCResponse?]() + var shareResponses = [ShareRequestResult]() + var thresholdPublicKey: KeyAssignment.PublicKey? for try await value in group { collected.append(value) - if !isImportShareReq { - if value != nil && value?.error == nil { - received += 1 - if received >= minRequired { - group.cancelAll() - } - } + + shareResponses = collected.filter({ $0 != nil && $0!.result != nil }).map({ $0!.result! }) + + let pubkeys = shareResponses.filter({ $0.keys.count > 0 }).map { $0.keys[0].publicKey } + + thresholdPublicKey = try thresholdSame(arr: pubkeys, threshold: threshold) + + if thresholdPublicKey != nil { + group.cancelAll() } } - return collected + return (shareResponses, thresholdPublicKey) } - shareRequestResults = shareResults.map({ $0?.result }) } - let shareResponses: [ShareRequestResult] = shareRequestResults.filter({ $0 != nil }).map({ $0! }) - - 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 } @@ -379,11 +369,6 @@ internal class NodeUtils { if thresholdNonceData == nil && verifierParams.extended_verifier_id == nil && !TorusUtils.isLegacyNetworkRouteMap(network: network) { let metadataNonce = try await MetadataUtils.getOrSetSapphireMetadataNonce(metadataHost: legacyMetadataHost, network: network, X: thresholdPublicKey!.X, Y: thresholdPublicKey!.Y, serverTimeOffset: serverTimeOffsetResponse, getOnly: false) thresholdNonceData = metadataNonce - if thresholdNonceData != nil { - if thresholdNonceData!.nonce != nil { - thresholdNonceData!.nonce = nil - } - } } let thresholdReqCount = (importedShares != nil && importedShares!.count > 0) ? endpoints.count : threshold