From eacaa85f065cf23273500fad9319608b186fb7c8 Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 14 Sep 2023 16:57:21 +0800 Subject: [PATCH] feat: move signatures, torusUtils, nodeDetails to threshold_key class --- Sources/ThresholdKey/Modules/TssModule.swift | 43 +++++++++++--------- Sources/ThresholdKey/ThresholdKey.swift | 33 +++++++++++++++ Tests/tkeypkgTests/IntegrationTests.swift | 39 ++++++++++++++---- 3 files changed, 86 insertions(+), 29 deletions(-) diff --git a/Sources/ThresholdKey/Modules/TssModule.swift b/Sources/ThresholdKey/Modules/TssModule.swift index f0977e5..8d0470b 100644 --- a/Sources/ThresholdKey/Modules/TssModule.swift +++ b/Sources/ThresholdKey/Modules/TssModule.swift @@ -252,9 +252,9 @@ public final class TssModule { /// - torusUtils: torusUtils used to retrieve dkg tss pub key /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. - public static func create_tagged_tss_share(threshold_key: ThresholdKey, tss_tag: String, deviceTssShare: String?, factorPub: String, deviceTssIndex: Int32, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils) async throws { + public static func create_tagged_tss_share(threshold_key: ThresholdKey, tss_tag: String, deviceTssShare: String?, factorPub: String, deviceTssIndex: Int32) async throws { try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) - try await TssModule.update_tss_pub_key(threshold_key: threshold_key, tss_tag: tss_tag, nodeDetails: nodeDetails, torusUtils: torusUtils) + try await TssModule.update_tss_pub_key(threshold_key: threshold_key, tss_tag: tss_tag) return try await withCheckedThrowingContinuation { continuation in create_tagged_tss_share(threshold_key: threshold_key, deviceTssShare: deviceTssShare, factorPub: factorPub, deviceTssIndex: deviceTssIndex) { @@ -289,12 +289,12 @@ public final class TssModule { /// - prefetch: Fetch the next nonce's pub key /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. - public static func update_tss_pub_key(threshold_key: ThresholdKey, tss_tag: String, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils, prefetch: Bool = false) async throws { + public static func update_tss_pub_key(threshold_key: ThresholdKey, tss_tag: String, prefetch: Bool = false) async throws { try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) let nonce = String(try get_tss_nonce(threshold_key: threshold_key, tss_tag: tss_tag, prefetch: prefetch)) - let public_address = try await get_dkg_pub_key(threshold_key: threshold_key, tssTag: tss_tag, nonce: nonce, nodeDetails: nodeDetails, torusUtils: torusUtils) + let public_address = try await get_dkg_pub_key(threshold_key: threshold_key, tssTag: tss_tag, nonce: nonce) let pk_encoded = try JSONEncoder().encode(public_address) guard let public_key = String(data: pk_encoded, encoding: .utf8) else { throw RuntimeError("update_tss_pub_key - Conversion Error - ResultString") @@ -364,12 +364,12 @@ public final class TssModule { } } - private static func generate_tss_share(threshold_key: ThresholdKey, input_tss_share: String, tss_input_index: Int32, auth_signatures: [String], new_factor_pub: String, new_tss_index: Int32, selected_servers: [Int32]? = nil, completion: @escaping (Result) -> Void) { + private static func generate_tss_share(threshold_key: ThresholdKey, input_tss_share: String, tss_input_index: Int32, new_factor_pub: String, new_tss_index: Int32, selected_servers: [Int32]? = nil, completion: @escaping (Result) -> Void) { threshold_key.tkeyQueue.async { do { var errorCode: Int32 = -1 let curvePointer = UnsafeMutablePointer(mutating: (threshold_key.curveN as NSString).utf8String) - + let auth_signatures = try threshold_key.getAuthSignatures() let auth_signatures_json = try JSONSerialization.data(withJSONObject: auth_signatures) guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else { throw RuntimeError("auth signatures error") @@ -399,14 +399,14 @@ public final class TssModule { } } - public static func generate_tss_share(threshold_key: ThresholdKey, tss_tag: String, input_tss_share: String, tss_input_index: Int32, auth_signatures: [String], new_factor_pub: String, new_tss_index: Int32, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils, selected_servers: [Int32]? = nil) async throws { + public static func generate_tss_share(threshold_key: ThresholdKey, tss_tag: String, input_tss_share: String, tss_input_index: Int32, new_factor_pub: String, new_tss_index: Int32, selected_servers: [Int32]? = nil) async throws { try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) - try await update_tss_pub_key(threshold_key: threshold_key, tss_tag: tss_tag, nodeDetails: nodeDetails, torusUtils: torusUtils, prefetch: true) + try await update_tss_pub_key(threshold_key: threshold_key, tss_tag: tss_tag, prefetch: true) return try await withCheckedThrowingContinuation { continuation in - generate_tss_share(threshold_key: threshold_key, input_tss_share: input_tss_share, tss_input_index: tss_input_index, auth_signatures: auth_signatures, new_factor_pub: new_factor_pub, new_tss_index: new_tss_index) { + generate_tss_share(threshold_key: threshold_key, input_tss_share: input_tss_share, tss_input_index: tss_input_index, new_factor_pub: new_factor_pub, new_tss_index: new_tss_index) { result in switch result { case let .success(result): @@ -418,12 +418,13 @@ public final class TssModule { } } - private static func delete_tss_share(threshold_key: ThresholdKey, input_tss_share: String, tss_input_index: Int32, auth_signatures: [String], delete_factor_pub: String, selected_servers: [Int32]? = nil, completion: @escaping (Result) -> Void) { + private static func delete_tss_share(threshold_key: ThresholdKey, input_tss_share: String, tss_input_index: Int32, delete_factor_pub: String, selected_servers: [Int32]? = nil, completion: @escaping (Result) -> Void) { threshold_key.tkeyQueue.async { do { var errorCode: Int32 = -1 let curvePointer = UnsafeMutablePointer(mutating: (threshold_key.curveN as NSString).utf8String) - + + let auth_signatures = try threshold_key.getAuthSignatures() let auth_signatures_json = try JSONSerialization.data(withJSONObject: auth_signatures) guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else { throw RuntimeError("auth signatures error") @@ -444,7 +445,7 @@ public final class TssModule { threshold_key_delete_tss_share(threshold_key.pointer, inputSharePointer, tss_input_index, factorPubPointer, serversPointer, authSignaturesPointer, curvePointer, error) }) guard errorCode == 0 else { - throw RuntimeError("Error in ThresholdKey delete tss share") + throw RuntimeError("Error in ThresholdKey delete tss share : error code \(String(errorCode))") } completion(.success(())) } catch { @@ -453,13 +454,13 @@ public final class TssModule { } } - public static func delete_tss_share(threshold_key: ThresholdKey, tss_tag: String, input_tss_share: String, tss_input_index: Int32, auth_signatures: [String], delete_factor_pub: String, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils, selected_servers: [Int32]? = nil) async throws { - try await update_tss_pub_key(threshold_key: threshold_key, tss_tag: tss_tag, nodeDetails: nodeDetails, torusUtils: torusUtils, prefetch: true) + public static func delete_tss_share(threshold_key: ThresholdKey, tss_tag: String, input_tss_share: String, tss_input_index: Int32, delete_factor_pub: String, selected_servers: [Int32]? = nil) async throws { + try await update_tss_pub_key(threshold_key: threshold_key, tss_tag: tss_tag, prefetch: true) try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) return try await withCheckedThrowingContinuation { continuation in - delete_tss_share(threshold_key: threshold_key, input_tss_share: input_tss_share, tss_input_index: tss_input_index, auth_signatures: auth_signatures, delete_factor_pub: delete_factor_pub) { + delete_tss_share(threshold_key: threshold_key, input_tss_share: input_tss_share, tss_input_index: tss_input_index, delete_factor_pub: delete_factor_pub) { result in switch result { case let .success(result): @@ -484,12 +485,12 @@ public final class TssModule { /// - torusUtils: torus utils /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. - public static func add_factor_pub(threshold_key: ThresholdKey, tss_tag: String, factor_key: String, auth_signatures: [String], new_factor_pub: String, new_tss_index: Int32, selected_servers: [Int32]? = nil, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils) async throws { + public static func add_factor_pub(threshold_key: ThresholdKey, tss_tag: String, factor_key: String, new_factor_pub: String, new_tss_index: Int32, selected_servers: [Int32]? = nil) async throws { if factor_key.count > 66 { throw RuntimeError("Invalid factor Key") } try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) let (tss_index, tss_share) = try await get_tss_share(threshold_key: threshold_key, tss_tag: tss_tag, factorKey: factor_key) - try await TssModule.generate_tss_share(threshold_key: threshold_key, tss_tag: tss_tag, input_tss_share: tss_share, tss_input_index: Int32(tss_index)!, auth_signatures: auth_signatures, new_factor_pub: new_factor_pub, new_tss_index: new_tss_index, nodeDetails: nodeDetails, torusUtils: torusUtils, selected_servers: selected_servers) + try await TssModule.generate_tss_share(threshold_key: threshold_key, tss_tag: tss_tag, input_tss_share: tss_share, tss_input_index: Int32(tss_index)!, new_factor_pub: new_factor_pub, new_tss_index: new_tss_index, selected_servers: selected_servers) } /// Delete factor pub from tss metadata @@ -504,12 +505,12 @@ public final class TssModule { /// - selected_servers: node indexes of the server that will be communicated to /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. - public static func delete_factor_pub(threshold_key: ThresholdKey, tss_tag: String, factor_key: String, auth_signatures: [String], delete_factor_pub: String, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils, selected_servers: [Int32]? = nil) async throws { + public static func delete_factor_pub(threshold_key: ThresholdKey, tss_tag: String, factor_key: String, delete_factor_pub: String, selected_servers: [Int32]? = nil) async throws { if factor_key.count > 66 { throw RuntimeError("Invalid factor Key") } try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) let (tss_index, tss_share) = try await get_tss_share(threshold_key: threshold_key, tss_tag: tss_tag, factorKey: factor_key) - try await TssModule.delete_tss_share(threshold_key: threshold_key, tss_tag: tss_tag, input_tss_share: tss_share, tss_input_index: Int32(tss_index)!, auth_signatures: auth_signatures, delete_factor_pub: delete_factor_pub, nodeDetails: nodeDetails, torusUtils: torusUtils, selected_servers: selected_servers) + try await TssModule.delete_tss_share(threshold_key: threshold_key, tss_tag: tss_tag, input_tss_share: tss_share, tss_input_index: Int32(tss_index)!, delete_factor_pub: delete_factor_pub, selected_servers: selected_servers) } /// Backup device share with factor key @@ -568,9 +569,11 @@ public final class TssModule { /// - Returns: `TSSPubKeyResult` /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. - public static func get_dkg_pub_key(threshold_key: ThresholdKey, tssTag: String, nonce: String, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils) async throws -> TSSPubKeyResult { + public static func get_dkg_pub_key(threshold_key: ThresholdKey, tssTag: String, nonce: String) async throws -> TSSPubKeyResult { let extendedVerifierId = try threshold_key.get_extended_verifier_id() let split = extendedVerifierId.components(separatedBy: "\u{001c}") + let torusUtils = try threshold_key.getTorusUtils() + let nodeDetails = try threshold_key.getnodeDetails() let result = try await torusUtils.getPublicAddress(endpoints: nodeDetails.torusNodeEndpoints, torusNodePubs: nodeDetails.torusNodePub, verifier: split[0], verifierId: split[1], extendedVerifierId: "\(split[1])\u{0015}\(tssTag)\u{0016}\(nonce)") diff --git a/Sources/ThresholdKey/ThresholdKey.swift b/Sources/ThresholdKey/ThresholdKey.swift index aaf3721..7d8c9a9 100644 --- a/Sources/ThresholdKey/ThresholdKey.swift +++ b/Sources/ThresholdKey/ThresholdKey.swift @@ -10,7 +10,40 @@ public class ThresholdKey { private(set) var use_tss: Bool = false internal let curveN = "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141" internal let tkeyQueue = DispatchQueue(label: "thresholdkey.queue") + internal var authSignatures: [String]? + internal var nodeDetails: AllNodeDetailsModel? + internal var torusUtils: TorusUtils? + + public func getAuthSignatures () throws -> [String] { + guard let result = self.authSignatures else { + throw "authSignatures is undefined" + } + return result + } + public func getnodeDetails () throws -> AllNodeDetailsModel { + guard let result = self.nodeDetails else { + throw "authSignatures is undefined" + } + return result + } + public func getTorusUtils () throws -> TorusUtils { + guard let result = self.torusUtils else { + throw "authSignatures is undefined" + } + return result + } + + public func setAuthSignatures ( authSignatures: [String]) { + self.authSignatures = authSignatures + } + public func setnodeDetails (nodeDetails : AllNodeDetailsModel) { + self.nodeDetails = nodeDetails + } + public func setTorusUtils (torusUtils : TorusUtils) { + self.torusUtils = torusUtils + } + /// Instantiate a `ThresholdKey` object, /// /// - Parameters: diff --git a/Tests/tkeypkgTests/IntegrationTests.swift b/Tests/tkeypkgTests/IntegrationTests.swift index c7e8ca6..d13f896 100644 --- a/Tests/tkeypkgTests/IntegrationTests.swift +++ b/Tests/tkeypkgTests/IntegrationTests.swift @@ -37,6 +37,11 @@ final class integrationTests: XCTestCase { manual_sync: false, rss_comm: rss_comm ) + + // setting variables needed for tss operations + threshold.setAuthSignatures(authSignatures: signatures) + threshold.setnodeDetails(nodeDetails: nodeDetail) + threshold.setTorusUtils(torusUtils: torusUtils) _ = try! await threshold.initialize() _ = try! await threshold.reconstruct() @@ -48,7 +53,7 @@ final class integrationTests: XCTestCase { let factorPub = try factorKey.toPublic() try TssModule.backup_share_with_factor_key(threshold_key: threshold, shareIndex: shareIndex.hex, factorKey: factorKey.hex) - try await TssModule.create_tagged_tss_share(threshold_key: threshold, tss_tag: tssTag, deviceTssShare: nil, factorPub: factorPub, deviceTssIndex: 2, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.create_tagged_tss_share(threshold_key: threshold, tss_tag: tssTag, deviceTssShare: nil, factorPub: factorPub, deviceTssIndex: 2 ) let (tss_index, tss_share) = try await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tssTag, factorKey: factorKey.hex) @@ -57,7 +62,7 @@ final class integrationTests: XCTestCase { let newFactorKey = try PrivateKey.generate() let newFactorPub = try newFactorKey.toPublic() // 2/2 -> 2/3 tss - try await TssModule.generate_tss_share(threshold_key: threshold, tss_tag: tssTag, input_tss_share: tss_share, tss_input_index: Int32(tss_index)!, auth_signatures: signatures, new_factor_pub: newFactorPub, new_tss_index: 3, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.generate_tss_share(threshold_key: threshold, tss_tag: tssTag, input_tss_share: tss_share, tss_input_index: Int32(tss_index)!, new_factor_pub: newFactorPub, new_tss_index: 3) let (tss_index3, tss_share3) = try await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tssTag, factorKey: newFactorKey.hex) let (_, tss_share_updated) = try await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tssTag, factorKey: factorKey.hex) @@ -73,6 +78,12 @@ final class integrationTests: XCTestCase { enable_logging: true, manual_sync: false ) + + // setting variables needed for tss operations + threshold2.setAuthSignatures(authSignatures: signatures) + threshold2.setnodeDetails(nodeDetails: nodeDetail) + threshold2.setTorusUtils(torusUtils: torusUtils) + _ = try! await threshold2.initialize() try await threshold2.input_factor_key(factorKey: factorKey.hex) @@ -91,7 +102,7 @@ final class integrationTests: XCTestCase { XCTAssertEqual(tss_index3, tss_index2_3) // 2/3 -> 2/2 tss - try await TssModule.delete_tss_share(threshold_key: threshold, tss_tag: tssTag, input_tss_share: tss_share3, tss_input_index: Int32(tss_index3)!, auth_signatures: signatures, delete_factor_pub: newFactorPub, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.delete_tss_share(threshold_key: threshold, tss_tag: tssTag, input_tss_share: tss_share3, tss_input_index: Int32(tss_index3)!, delete_factor_pub: newFactorPub) // XCTAssertThrowsError( try await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tssTag, factorKey: newFactorKey.hex) ) let (tss_index_updated2, tss_share_updated2) = try await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tssTag, factorKey: factorKey.hex) @@ -101,10 +112,10 @@ final class integrationTests: XCTestCase { XCTAssertNotEqual(tss_share_updated, tss_share_updated2) // 2/2 -> 2/3 tss - try await TssModule.add_factor_pub(threshold_key: threshold, tss_tag: tssTag, factor_key: factorKey.hex, auth_signatures: signatures, new_factor_pub: newFactorPub, new_tss_index: 3, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.add_factor_pub(threshold_key: threshold, tss_tag: tssTag, factor_key: factorKey.hex, new_factor_pub: newFactorPub, new_tss_index: 3) // 2/3 -> 2/2 tss - try await TssModule.delete_factor_pub(threshold_key: threshold, tss_tag: tssTag, factor_key: factorKey.hex, auth_signatures: signatures, delete_factor_pub: newFactorPub, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.delete_factor_pub(threshold_key: threshold, tss_tag: tssTag, factor_key: factorKey.hex, delete_factor_pub: newFactorPub ) } func test_TssModule_multi_tag() async throws { @@ -142,6 +153,11 @@ final class integrationTests: XCTestCase { manual_sync: true, rss_comm: rss_comm ) + + // setting variables needed for tss operations + threshold.setAuthSignatures(authSignatures: signatures) + threshold.setnodeDetails(nodeDetails: nodeDetail) + threshold.setTorusUtils(torusUtils: torusUtils) _ = try! await threshold.initialize() _ = try! await threshold.reconstruct() @@ -149,7 +165,7 @@ final class integrationTests: XCTestCase { let share = try threshold.output_share(shareIndex: shareIndex.hex) print(share) - let testTags = ["tag1", "tag2", "tag3", "tag4", "tag5"] + let testTags = ["tag1", "tag2", "tag3", "tag4"] var tssMods: [(ThresholdKey, String)] = [] @@ -171,7 +187,7 @@ final class integrationTests: XCTestCase { factorPubs.append(factorPub) try await TssModule.set_tss_tag(threshold_key: threshold, tss_tag: tag) - try await TssModule.create_tagged_tss_share(threshold_key: threshold, tss_tag: tag, deviceTssShare: nil, factorPub: factorPub, deviceTssIndex: 2, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.create_tagged_tss_share(threshold_key: threshold, tss_tag: tag, deviceTssShare: nil, factorPub: factorPub, deviceTssIndex: 2) let (tssIndex, tssShare) = try! await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tag, factorKey: factorKey.hex) tssIndexes.append(tssIndex) @@ -188,7 +204,7 @@ final class integrationTests: XCTestCase { newFactorKeys.append(newFactorKey) newFactorPubs.append(newFactorPub) - try await TssModule.add_factor_pub(threshold_key: threshold, tss_tag: tag, factor_key: factorKeys[index].hex, auth_signatures: signatures, new_factor_pub: newFactorPub, new_tss_index: 3, nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.add_factor_pub(threshold_key: threshold, tss_tag: tag, factor_key: factorKeys[index].hex, new_factor_pub: newFactorPub, new_tss_index: 3) try await threshold.sync_local_metadata_transistions() @@ -235,6 +251,11 @@ final class integrationTests: XCTestCase { enable_logging: true, manual_sync: false ) + // setting variables needed for tss operations + threshold2.setAuthSignatures(authSignatures: signatures) + threshold2.setnodeDetails(nodeDetails: nodeDetail) + threshold2.setTorusUtils(torusUtils: torusUtils) + _ = try! await threshold2.initialize() try! await threshold2.input_share(share: share, shareType: nil) _ = try! await threshold2.reconstruct() @@ -255,7 +276,7 @@ final class integrationTests: XCTestCase { newFactorKeys2.append(newFactorKey2) newFactorPubs2.append(newFactorPub2) - try await TssModule.delete_factor_pub(threshold_key: threshold, tss_tag: tag, factor_key: newFactorKeys[index].hex, auth_signatures: signatures, delete_factor_pub: newFactorPubs[index], nodeDetails: nodeDetail, torusUtils: torusUtils) + try await TssModule.delete_factor_pub(threshold_key: threshold, tss_tag: tag, factor_key: newFactorKeys[index].hex, delete_factor_pub: newFactorPubs[index]) } try await threshold.sync_local_metadata_transistions() print(try threshold.get_all_tss_tags())