diff --git a/Sources/ThresholdKey/Common/helper.swift b/Sources/ThresholdKey/Common/helper.swift new file mode 100644 index 0000000..9288599 --- /dev/null +++ b/Sources/ThresholdKey/Common/helper.swift @@ -0,0 +1,44 @@ +// +// File.swift +// +// +// Created by CW Lee on 26/09/2023. +// + +import Foundation + + +func selectedServerToPointer (selectedServers: [UInt32]?) throws -> UnsafeMutablePointer? { + guard let selectedServers = selectedServers else { + return nil + } + var serversPointer: UnsafeMutablePointer? + + let selected_servers_json = try JSONSerialization.data(withJSONObject: selectedServers as Any) + let selected_servers_str = String(data: selected_servers_json, encoding: .utf8)! + print(selected_servers_str) + serversPointer = UnsafeMutablePointer(mutating: (selected_servers_str as NSString).utf8String) + + guard let serversPointer = serversPointer else { + throw RuntimeError("convert error") + } + return serversPointer +} + +func authSignaturesToPointer ( authSignatures : [String]?) throws -> UnsafeMutablePointer? { + guard let authSignatures = authSignatures else { + return nil + } + let auth_signatures_json = try JSONSerialization.data(withJSONObject: authSignatures) + guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else { + throw RuntimeError("auth signatures error") + } + print(authSignatures) + print(auth_signatures_str.count) + + let authSignaturesPointer = UnsafeMutablePointer(mutating: (auth_signatures_str as NSString).utf8String) + + + return authSignaturesPointer +} + diff --git a/Sources/ThresholdKey/Modules/TssModule.swift b/Sources/ThresholdKey/Modules/TssModule.swift index d08d823..91cce50 100644 --- a/Sources/ThresholdKey/Modules/TssModule.swift +++ b/Sources/ThresholdKey/Modules/TssModule.swift @@ -169,7 +169,7 @@ public final class TssModule { /// - Returns: `Int32` /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. - public static func get_tss_nonce(threshold_key: ThresholdKey, tss_tag: String, prefetch: Bool = false) throws -> Int32 { + public static func get_tss_nonce(threshold_key: ThresholdKey, tss_tag: String, prefetch: Bool = false, incNonce: Int32 = 1) throws -> Int32 { var errorCode: Int32 = -1 let tss_tag_pointer: UnsafeMutablePointer? = UnsafeMutablePointer(mutating: NSString(string: tss_tag).utf8String) var nonce = withUnsafeMutablePointer(to: &errorCode, { error in @@ -179,7 +179,7 @@ public final class TssModule { } if prefetch { - nonce += 1 + nonce += incNonce } return nonce @@ -208,7 +208,7 @@ public final class TssModule { threshold_key_get_tss_share(threshold_key.pointer, factorKeyPointer, threshold, curvePointer, error) }) guard errorCode == 0 else { - throw RuntimeError("Error in ThresholdKey get_tss_share") + throw RuntimeError("Error in ThresholdKey get_tss_share \(errorCode)") } let string = String(cString: result!) string_free(result) @@ -289,10 +289,10 @@ 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, prefetch: Bool = false) async throws { + public static func update_tss_pub_key(threshold_key: ThresholdKey, tss_tag: String, prefetch: Bool = false, incNonce: Int32 = 1) 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 nonce = String(try get_tss_nonce(threshold_key: threshold_key, tss_tag: tss_tag, prefetch: prefetch, incNonce: incNonce)) 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) @@ -472,7 +472,7 @@ public final class TssModule { } } - public static func register_factor (threshold_key: ThresholdKey, tss_tag: String, factor_key: String, new_factor_pub: String, new_tss_index: Int32, selected_servers: [Int32]? = nil ) async throws { + public static func create_factor (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) diff --git a/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift b/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift index a44388f..9c896f0 100644 --- a/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift +++ b/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift @@ -67,36 +67,44 @@ public final class TssSecurityQuestionModule { /// - Returns: `` /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key of failed to set security question - public static func set_security_question( threshold : ThresholdKey, question: String, answer: String, factorKey :String, tag: String ) async throws -> String { - - let domainKey = TssSecurityQuestion + ":" + tag - - var isSet = false - do { - let question = try TssSecurityQuestionModule.get_question(threshold: threshold, tag: tag) - if question.count > 0 { - isSet = true - } - } catch {} - - if isSet {throw "Trying to set Security Question again"} + public static func set_security_question( threshold : ThresholdKey, question: String, answer: String, factorKey :String, selectedServer:String, tag: String ) async throws -> String { + try await TssModule.set_tss_tag(threshold_key: threshold, tss_tag: tag) + try await TssModule.update_tss_pub_key(threshold_key: threshold, tss_tag: tag, prefetch: true) + var errorCode: Int32 = -1 let hash = try compute_hash(threshold: threshold, answer: answer, tag: tag) - - let hashKey = PrivateKey(hex: hash) - let hashPub = try hashKey.toPublic(); + let factorKeyPtr = UnsafeMutablePointer(mutating: (factorKey as NSString).utf8String) + let questionPtr = UnsafeMutablePointer(mutating: (question as NSString).utf8String) + let hashPtr = UnsafeMutablePointer(mutating: (hash as NSString).utf8String) + let curvePointer = UnsafeMutablePointer(mutating: (threshold.curveN as NSString).utf8String) - let shareIndex: Int32 = 3 - let data = TssSecurityQuestionData( shareIndex: String(shareIndex), factorPublicKey: hashPub, question: question ) - try threshold.set_general_store_domain(key: domainKey, data: data.toJsonString() ) - // register - try await TssModule.register_factor(threshold_key: threshold, tss_tag: tag, factor_key: factorKey, new_factor_pub: hashPub, new_tss_index: shareIndex) - let deviceShareIndex = try await TssModule.find_device_share_index(threshold_key: threshold, factor_key: factorKey) + let auth_signatures_json = try JSONSerialization.data(withJSONObject: threshold.authSignatures) + guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else { + throw RuntimeError("auth signatures error") + } + let authSignaturesPointer = UnsafeMutablePointer(mutating: (auth_signatures_str as NSString).utf8String) - try TssModule.backup_share_with_factor_key(threshold_key: threshold, shareIndex: deviceShareIndex, factorKey: hash) + let selectedServers:[UInt32] = [1,2,3] + let selected_servers_json = try JSONSerialization.data(withJSONObject: selectedServers as Any) + guard let selected_servers_str = String(data: selected_servers_json, encoding: .utf8) else { + throw RuntimeError("selectedServers error") + } + let serversPointer = UnsafeMutablePointer(mutating: (selected_servers_str as NSString).utf8String) + +// let tssIndex: UInt32 = 2 + + withUnsafeMutablePointer(to: &errorCode, { error in + tss_security_question_set_security_question(threshold.pointer, factorKeyPtr, questionPtr, hashPtr, 2, serversPointer, authSignaturesPointer, + curvePointer, error) + }) + guard errorCode == 0 else { + throw RuntimeError("Error in ThresholdKey set_security_question \(errorCode)") + } + let shareIndex = try await TssModule.find_device_share_index(threshold_key: threshold, factor_key: factorKey); + try TssModule.backup_share_with_factor_key(threshold_key: threshold, shareIndex: shareIndex, factorKey: hash) return hash } @@ -113,41 +121,37 @@ public final class TssSecurityQuestionModule { /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key or fail to change security question public static func change_security_question( threshold : ThresholdKey, newQuestion: String, newAnswer: String, answer: String, tag: String) async throws -> (String, String){ - let domainKey = TssSecurityQuestion + ":" + tag - - let storeStr = try threshold.get_general_store_domain(key: domainKey) - var store = try TssSecurityQuestionData.fromJsonString(jsonStr: storeStr) + + try await TssModule.set_tss_tag(threshold_key: threshold, tss_tag: tag) + try await TssModule.update_tss_pub_key(threshold_key: threshold, tss_tag: tag, prefetch: true, incNonce: 2) + var errorCode: Int32 = -1 - // hash answer and new answer let hash = try compute_hash(threshold: threshold, answer: answer, tag: tag) let newHash = try compute_hash(threshold: threshold, answer: newAnswer, tag: tag) + let newQuestionPtr = UnsafeMutablePointer(mutating: (newQuestion as NSString).utf8String) + let hashPtr = UnsafeMutablePointer(mutating: (hash as NSString).utf8String) + let newHashPtr = UnsafeMutablePointer(mutating: (newHash as NSString).utf8String) + let curvePointer = UnsafeMutablePointer(mutating: (threshold.curveN as NSString).utf8String) - let hashKey = PrivateKey(hex: hash) - let hashPub = try hashKey.toPublic(); - - let newHashKey = PrivateKey(hex: newHash) - let newHashPub = try newHashKey.toPublic(); - - // create new factor using newHash - let (tssIndex, _) = try await TssModule.get_tss_share(threshold_key: threshold, tss_tag: tag, factorKey: hash) - try await TssModule.register_factor(threshold_key: threshold, tss_tag: tag, factor_key: hash, new_factor_pub: newHashPub, new_tss_index: Int32(tssIndex)!) - let deviceShareIndex = try await TssModule.find_device_share_index(threshold_key: threshold, factor_key: hash) - try TssModule.backup_share_with_factor_key(threshold_key: threshold, shareIndex: deviceShareIndex, factorKey: newHash) - - // delete old hash factor - try await TssModule.delete_factor_pub(threshold_key: threshold, tss_tag: tag, factor_key: hash, delete_factor_pub: hashPub) - // delete share metadata - - - store.question = newQuestion - store.factorPublicKey = newHashPub - - // set updated data to domain store - let jsonStr = try store.toJsonString() - try threshold.set_general_store_domain(key: domainKey, data: jsonStr ) - - try await threshold.sync_metadata(); + let auth_signatures_json = try JSONSerialization.data(withJSONObject: threshold.authSignatures) + guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else { + throw RuntimeError("auth signatures error") + } + let authSignaturesPointer = UnsafeMutablePointer(mutating: (auth_signatures_str as NSString).utf8String) + let selectedServers:[UInt32] = [1,2,3] + let selected_servers_json = try JSONSerialization.data(withJSONObject: selectedServers as Any) + guard let selected_servers_str = String(data: selected_servers_json, encoding: .utf8) else { + throw RuntimeError("selectedServers error") + } + let serversPointer = UnsafeMutablePointer(mutating: (selected_servers_str as NSString).utf8String) + + withUnsafeMutablePointer(to: &errorCode, { error in + tss_security_question_change_question(threshold.pointer, newHashPtr, newQuestionPtr, hashPtr, serversPointer, authSignaturesPointer, curvePointer, error) + }) + guard errorCode == 0 else { + throw RuntimeError("Error in ThresholdKey change_security_question \(errorCode)") + } return (hash, newHash) } @@ -159,26 +163,43 @@ public final class TssSecurityQuestionModule { /// - Returns: `String` public key of the factor /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key or fail to delete security question - public static func delete_security_question( threshold : ThresholdKey, tag: String, factorKey: String) async throws -> String { - // - let domainKey = TssSecurityQuestion + ":" + tag + public static func delete_security_question( threshold : ThresholdKey, tag: String, factorKey: String + , answer: String? = nil) async throws -> String { - let jsonStr = try threshold.get_general_store_domain(key: domainKey) - let jsonObj = try TssSecurityQuestionData.fromJsonString(jsonStr: jsonStr) + try await TssModule.set_tss_tag(threshold_key: threshold, tss_tag: tag) + try await TssModule.update_tss_pub_key(threshold_key: threshold, tss_tag: tag, prefetch: true) + + var errorCode: Int32 = -1 + let factorKeyPtr = UnsafeMutablePointer(mutating: (factorKey as NSString).utf8String) + var hashPtr : UnsafeMutablePointer?; + if let answer = answer { + let hash = try self.compute_hash(threshold: threshold, answer: answer, tag: tag) + hashPtr = UnsafeMutablePointer(mutating: (hash as NSString).utf8String) + } + let curvePointer = UnsafeMutablePointer(mutating: (threshold.curveN as NSString).utf8String) - if jsonObj.question.count == 0 { - throw "Security Question is not set" + let auth_signatures_json = try JSONSerialization.data(withJSONObject: threshold.authSignatures) + guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else { + throw RuntimeError("auth signatures error") } - let deleteFactorPub = jsonObj.factorPublicKey - - // replace with delete store domain - // sync_metadata is not required as delete_factor_pub will sync metadata - let jsonStr1 = "{}" - try threshold.set_general_store_domain(key: domainKey, data: jsonStr1 ) + let authSignaturesPointer = UnsafeMutablePointer(mutating: (auth_signatures_str as NSString).utf8String) - try await TssModule.delete_factor_pub(threshold_key: threshold, tss_tag: tag, factor_key: factorKey, delete_factor_pub: deleteFactorPub) + let selectedServers:[UInt32] = [1,2,3] + let selected_servers_json = try JSONSerialization.data(withJSONObject: selectedServers as Any) + guard let selected_servers_str = String(data: selected_servers_json, encoding: .utf8) else { + throw RuntimeError("selectedServers error") + } + let serversPointer = UnsafeMutablePointer(mutating: (selected_servers_str as NSString).utf8String) - return deleteFactorPub + let result = withUnsafeMutablePointer(to: &errorCode, { error in + tss_security_question_delete_security_question(threshold.pointer, factorKeyPtr, hashPtr, serversPointer, authSignaturesPointer, curvePointer, error) + }) + guard errorCode == 0 else { + throw RuntimeError("Error in ThresholdKey delete_security_question \(errorCode)") + } + let hash = String(cString: result!) + string_free(result) + return hash } @@ -190,14 +211,21 @@ public final class TssSecurityQuestionModule { /// - Returns: `String` question /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key or fail to get security question - public static func get_question( threshold: ThresholdKey, tag: String ) throws -> String { - // get data format from json - let domainKey = TssSecurityQuestion + ":" + tag + public static func get_question( threshold: ThresholdKey, tag: String ) async throws -> String { - let jsonStr = try threshold.get_general_store_domain(key: domainKey) - let jsonObj = try TssSecurityQuestionData.fromJsonString(jsonStr: jsonStr) + try await TssModule.set_tss_tag(threshold_key: threshold, tss_tag: tag) + var errorCode: Int32 = -1 + + let result = withUnsafeMutablePointer(to: &errorCode, { error in + tss_security_question_get_question(threshold.pointer, error) + }) + guard errorCode == 0 else { + throw RuntimeError("Error in ThresholdKey get_question \(errorCode)") + } + let question = String(cString: result!) + string_free(result) + return question - return jsonObj.question } @@ -210,18 +238,23 @@ public final class TssSecurityQuestionModule { /// - Returns: `String` factor key /// /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key or fail to delete security question - public static func recover_factor ( threshold: ThresholdKey, answer: String , tag: String ) throws -> String { - // get data format from json - let domainKey = TssSecurityQuestion + ":" + tag - let jsonStr = try threshold.get_general_store_domain(key: domainKey) - let store = try TssSecurityQuestionData.fromJsonString(jsonStr: jsonStr) - - // hash answer + public static func recover_factor ( threshold: ThresholdKey, answer: String , tag: String ) async throws -> String { + + try await TssModule.set_tss_tag(threshold_key: threshold, tss_tag: tag) + var errorCode: Int32 = -1 + let hash = try compute_hash(threshold: threshold, answer: answer, tag: tag) - let factorPub = try PrivateKey(hex: hash).toPublic(format: .EllipticCompress); - if (factorPub != store.factorPublicKey) { - throw "Invalid Answer" + + let answerPtr = UnsafeMutablePointer(mutating: (hash as NSString).utf8String) + + let result = withUnsafeMutablePointer(to: &errorCode, { error in + tss_security_question_recover_factor(threshold.pointer, answerPtr, error) + }) + guard errorCode == 0 else { + throw RuntimeError("Error in ThresholdKey recover_factor \(errorCode)") } + let hashOut = String(cString: result!) + string_free(result) return hash } diff --git a/Sources/ThresholdKey/ThresholdKey.swift b/Sources/ThresholdKey/ThresholdKey.swift index 8f90873..ce9e8c0 100644 --- a/Sources/ThresholdKey/ThresholdKey.swift +++ b/Sources/ThresholdKey/ThresholdKey.swift @@ -588,25 +588,25 @@ public class ThresholdKey { } - private func patch_input_factor_key(factorKey: String, completion: @escaping (Result) -> Void) { - tkeyQueue.async { - do { - var errorCode: Int32 = -1 - let cFactorKey = UnsafeMutablePointer(mutating: (factorKey as NSString).utf8String) - let curvePointer = UnsafeMutablePointer(mutating: (self.curveN as NSString).utf8String) - - withUnsafeMutablePointer(to: &errorCode, { error in - threshold_key_patch_input_factor_key(self.pointer, cFactorKey, curvePointer, error) - }) - guard errorCode == 0 else { - throw RuntimeError("Error in ThresholdKey input_factor_key \(errorCode)") - } - completion(.success(())) - } catch { - completion(.failure(error)) - } - } - } +// private func patch_input_factor_key(factorKey: String, completion: @escaping (Result) -> Void) { +// tkeyQueue.async { +// do { +// var errorCode: Int32 = -1 +// let cFactorKey = UnsafeMutablePointer(mutating: (factorKey as NSString).utf8String) +// let curvePointer = UnsafeMutablePointer(mutating: (self.curveN as NSString).utf8String) +// +// withUnsafeMutablePointer(to: &errorCode, { error in +// threshold_key_patch_input_factor_key(self.pointer, cFactorKey, curvePointer, error) +// }) +// guard errorCode == 0 else { +// throw RuntimeError("Error in ThresholdKey input_factor_key \(errorCode)") +// } +// completion(.success(())) +// } catch { +// completion(.failure(error)) +// } +// } +// } /// Patch and Inserts a `ShareStore` into `ThresholdKey` using `FactorKey`, useful for insertion before reconstruction to ensure the number of shares meet the minimum threshold. /// @@ -614,20 +614,20 @@ public class ThresholdKey { /// - factorKey : The `factorKey` to be inserted /// /// - Throws: `RuntimeError`, indicates invalid parameters or invalid `ThresholdKey`. - public func patch_input_factor_key(factorKey: String) async throws { - return try await withCheckedThrowingContinuation { - continuation in - self.patch_input_factor_key(factorKey: factorKey) { - result in - switch result { - case let .success(result): - continuation.resume(returning: result) - case let .failure(error): - continuation.resume(throwing: error) - } - } - } - } +// public func patch_input_factor_key(factorKey: String) async throws { +// return try await withCheckedThrowingContinuation { +// continuation in +// self.patch_input_factor_key(factorKey: factorKey) { +// result in +// switch result { +// case let .success(result): +// continuation.resume(returning: result) +// case let .failure(error): +// continuation.resume(throwing: error) +// } +// } +// } +// } /// Retrieves all share indexes for a `ThresholdKey`. /// diff --git a/Sources/libtkey/include/tkey.h b/Sources/libtkey/include/tkey.h index de37e66..985f67c 100644 --- a/Sources/libtkey/include/tkey.h +++ b/Sources/libtkey/include/tkey.h @@ -30,7 +30,6 @@ struct TssOptions; struct NodeDetails; struct FFIRssComm; - //Methods char* get_version(int* error_code); void string_free(char *ptr); @@ -82,6 +81,7 @@ void generate_share_store_result_free(struct GenerateShareStoreResult* ptr); void share_store_poly_id_index_map_free(struct ShareStorePolyIDShareIndexMap* ptr); struct GenerateShareStoreResult* threshold_key_generate_share(struct FFIThresholdKey* threshold_key, char* curve_n, bool use_tss, struct TssOptions* tss_options, int* error_code); + void threshold_key_import_tss_key(struct FFIThresholdKey* threshold_key, bool update_metadata, char* tss_tag, char* import_key, int new_tss_index, struct KeyPoint* factor_pub, char* selected_servers, char* auth_signatures, char* curve_n, int* error_code); void threshold_key_delete_share(struct FFIThresholdKey* threshold_key, char* share_index, char* curve_n, bool use_tss, struct TssOptions* tss_options, int* error_code); void threshold_key_delete_tkey(struct FFIThresholdKey* threshold_key, char* curve_n, int* error_code); char* threshold_key_output_share(struct FFIThresholdKey* threshold_key, char* share_index, char* share_type, char* curve_n, int* error_code); @@ -94,7 +94,6 @@ void threshold_key_input_share(struct FFIThresholdKey* threshold_key, char* share, char* share_type, char* curve_n, int* error_code); struct ShareStore* threshold_key_output_share_store(struct FFIThresholdKey* threshold_key, char* share_index, char* poly_id, char* curve_n, int* error_code); void threshold_key_input_share_store(struct FFIThresholdKey* threshold_key, struct ShareStore* share_store, int* error_code); - void threshold_key_patch_input_factor_key(struct FFIThresholdKey* threshold_key, char* factor_key, char* curve_n, int* error_code); void threshold_key_input_factor_key(struct FFIThresholdKey* threshold_key, char* factor_key, int* error_code); char* threshold_key_get_shares_indexes(struct FFIThresholdKey* threshold_key, int* error_code); char* threshold_key_encrypt(struct FFIThresholdKey* threshold_key, char* data, char* curve_n, int* error_code); @@ -207,10 +206,16 @@ // TssOptions struct TssOptions* tss_options(char* input_tss_share, int input_tss_index, struct KeyPoint* factor_pub, char* auth_signatures, char* selected_servers, int* new_tss_index, struct KeyPoint* new_factor_pub, int* error_code); void tss_options_free(struct TssOptions* ptr); - //NodeDetails + //Module: tss security-question + void tss_security_question_set_security_question(struct FFIThresholdKey* threshold_key, char* factor_key, char* questions, char* answer, unsigned int tss_index, char* selected_servers, char* auth_signatures, char* curve_n, int* error_code); + char* tss_security_question_recover_factor(struct FFIThresholdKey* threshold_key, char* answer, int* error_code); + void tss_security_question_change_question(struct FFIThresholdKey* threshold_key, char* new_answer, char* new_questions, char* answer, char* selected_servers, char* auth_signatures, char* curve_n, int* error_code); + char* tss_security_question_delete_security_question(struct FFIThresholdKey* threshold_key, char* factor_key, char* answer, char* selected_servers, char* auth_signatures, char* curve_n, int* error_code); + char* tss_security_question_get_question(struct FFIThresholdKey* threshold_key, int* error_code); + // NodeDetails struct NodeDetails* node_details(char* server_endpoints, char* server_public_keys, int server_threshold, int* error_code); void node_details_free(struct NodeDetails* ptr); - //RssComm + // RssComm struct FFIRSSComm* rss_comm(char* (*network_callback)(char*, char*, void*, int*), void* parent_instance_ref, int* error_code); void* rss_comm_free(struct FFIRssComm* ptr); #ifdef __cplusplus diff --git a/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a b/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a index 4e8fcc0..54b941d 100644 Binary files a/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a and b/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a differ diff --git a/Sources/libtkey/libtkey.xcframework/ios-arm64_x86_64-simulator/libtkey.a b/Sources/libtkey/libtkey.xcframework/ios-arm64_x86_64-simulator/libtkey.a index 0fe8218..2a2cc73 100644 Binary files a/Sources/libtkey/libtkey.xcframework/ios-arm64_x86_64-simulator/libtkey.a and b/Sources/libtkey/libtkey.xcframework/ios-arm64_x86_64-simulator/libtkey.a differ diff --git a/Tests/tkeypkgTests/modules/tkey_pkgTssSecurityQuestionModuleTests.swift b/Tests/tkeypkgTests/modules/tkey_pkgTssSecurityQuestionModuleTests.swift index f543625..3a812a4 100644 --- a/Tests/tkeypkgTests/modules/tkey_pkgTssSecurityQuestionModuleTests.swift +++ b/Tests/tkeypkgTests/modules/tkey_pkgTssSecurityQuestionModuleTests.swift @@ -67,26 +67,28 @@ final class tkey_pkgTssSecurityQuestionModuleTests: XCTestCase { let factor_key = try! PrivateKey.generate() let factor_pub = try factor_key.toPublic(format: .EllipticCompress) var allIndex = try! threshold_key.get_shares_indexes() + let tag = "special" allIndex.removeAll(where: {$0 == "1"}) try await TssModule.create_tagged_tss_share(threshold_key: threshold_key, tss_tag: "special", deviceTssShare: nil, factorPub: factor_pub, deviceTssIndex: 2) try! TssModule.backup_share_with_factor_key(threshold_key: threshold_key, shareIndex: allIndex[0], factorKey: factor_key.hex) - let sq_factor = try await TssSecurityQuestionModule.set_security_question(threshold: threshold_key, question: question, answer: answer,factorKey: factor_key.hex, tag: "special") + try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tag) + let sq_factor = await try TssSecurityQuestionModule.set_security_question(threshold: threshold_key, question: question, answer: answer,factorKey: factor_key.hex, selectedServer: "[1,2,3]", tag: "special") do { - let _ = try await TssSecurityQuestionModule.set_security_question(threshold: threshold_key, question: question, answer: answer_2, factorKey: factor_key.hex, tag: "special") + let _ = try await TssSecurityQuestionModule.set_security_question(threshold: threshold_key, question: question, answer: answer_2, factorKey: factor_key.hex, selectedServer: "[1,2,3]", tag: "special") XCTFail("Should not able to set quesetion twice") } catch {} - let questionReturn = try? TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") + let questionReturn = try? await TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") XCTAssertEqual(questionReturn, question) - let factor = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") + let factor = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") do { - let _ = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer_2, tag: "special") + let _ = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer_2, tag: "special") XCTFail("Should be able to get factor using incorrect answer") } catch {} try await threshold_key.input_factor_key(factorKey: factor) @@ -101,24 +103,24 @@ final class tkey_pkgTssSecurityQuestionModuleTests: XCTestCase { do { - let _ = try TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") + let _ = try await TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") XCTFail("Should not able get question after delete") }catch{} do { - let _ = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") + let _ = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") XCTFail("Should not able get question after delete") }catch{} // able to set new question and answer - let sq_factor2 = try await TssSecurityQuestionModule.set_security_question(threshold: threshold_key, question: question2, answer: answer_2, factorKey: factor_key.hex, tag: "special") + let sq_factor2 = try await TssSecurityQuestionModule.set_security_question(threshold: threshold_key, question: question2, answer: answer_2, factorKey: factor_key.hex, selectedServer: "[]", tag: "special") - let questionReturn2 = try TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") + let questionReturn2 = try await TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") XCTAssertEqual(questionReturn2, question2) - let factor2 = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer_2, tag: "special") + let factor2 = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer_2, tag: "special") do { - let _ = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") + let _ = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") XCTFail("Should be able to get factor using incorrect answer") } catch {} @@ -135,12 +137,12 @@ final class tkey_pkgTssSecurityQuestionModuleTests: XCTestCase { XCTAssertEqual(old_sq_factor, sq_factor2) do { - let _ = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer_2, tag: "special") + let _ = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer_2, tag: "special") XCTFail("Should be not able to get factor using incorrect answer") } catch {} - let questionChanged = try TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") - let factorChanged = try TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") + let questionChanged = try await TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") + let factorChanged = try await TssSecurityQuestionModule.recover_factor(threshold: threshold_key, answer: answer, tag: "special") XCTAssertEqual(String(factorChanged.suffix(64)), new_sq_factor) XCTAssertEqual(questionChanged, question) @@ -156,8 +158,8 @@ final class tkey_pkgTssSecurityQuestionModuleTests: XCTestCase { let _ = try await newThreshold.initialize(); - let newInstanceQuestion = try TssSecurityQuestionModule.get_question(threshold: newThreshold, tag: "special") - let newInstanceFactor = try TssSecurityQuestionModule.recover_factor(threshold: newThreshold, answer: answer, tag: "special") + let newInstanceQuestion = try await TssSecurityQuestionModule.get_question(threshold: newThreshold, tag: "special") + let newInstanceFactor = try await TssSecurityQuestionModule.recover_factor(threshold: newThreshold, answer: answer, tag: "special") try await newThreshold.input_factor_key(factorKey: newInstanceFactor) try await newThreshold.input_factor_key(factorKey: newInstanceFactor) @@ -210,9 +212,9 @@ final class tkey_pkgTssSecurityQuestionModuleTests: XCTestCase { let answer = "jsanswer" let question = "js question" - let questionResult = try TssSecurityQuestionModule.get_question(threshold: threshold, tag: "default") + let questionResult = try await TssSecurityQuestionModule.get_question(threshold: threshold, tag: "default") print(questionResult) - let factor = try TssSecurityQuestionModule.recover_factor(threshold: threshold, answer: answer, tag: "default") + let factor = try await TssSecurityQuestionModule.recover_factor(threshold: threshold, answer: answer, tag: "default") print(factor) try await threshold.input_factor_key(factorKey: factor)