diff --git a/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift b/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift index df008da..8dff5be 100644 --- a/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift +++ b/Sources/ThresholdKey/Modules/TssSecurityQuestionModule.swift @@ -15,8 +15,20 @@ public struct TssSecurityQuestionData : Codable{ // Security question has low entrophy, hence it is not recommended way to secure the factor key or share public final class TssSecurityQuestionModule { // set question - public static func setSecurityQuestion( threshold : ThresholdKey, factorKey :String, question: String, answer: String, description: String, tag: String) throws { + public static func set_security_question( threshold : ThresholdKey, factorKey :String, question: String, answer: String, description: String, tag: String) throws { // + 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"} + let factorBigInt = BigInt( sign: .plus, magnitude: BigUInt(Data(hex: factorKey))) guard let hash = answer.data(using: .utf8)?.sha3(.keccak256) else { throw "invalid answer format" @@ -25,11 +37,11 @@ public final class TssSecurityQuestionModule { let nonceBigInt = factorBigInt - hashBigInt let nonce = nonceBigInt.serialize().toHexString() + print(nonce) - let domainKey = TssSecurityQuestion + ":" + tag // set to metadata using nonce, question, description, tag let data = TssSecurityQuestionData(nonce: nonce, question: question, description: description) - print(data) + let jsonData = try JSONEncoder().encode(data) guard let jsonStr = String(data: jsonData, encoding: .utf8) else { throw "Invalid security question data" @@ -38,13 +50,34 @@ public final class TssSecurityQuestionModule { } + public static func delete_security_question( threshold : ThresholdKey, tag: String) throws { + // + 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 "Security Question is not set"} + + let data : [String:String] = [:] + + let jsonData = try JSONEncoder().encode(data) + guard let jsonStr = String(data: jsonData, encoding: .utf8) else { + throw "Invalid security question data" + } + try threshold.set_general_store_domain(key: domainKey, data: jsonStr ) + } + // get question - public static func getSecurityQuestion( threshold: ThresholdKey, tag: String ) throws -> String { + public static func get_question( threshold: ThresholdKey, tag: String ) throws -> String { // get data format from json let domainKey = TssSecurityQuestion + ":" + tag let jsonStr = try threshold.get_general_store_domain(key: domainKey) - print(jsonStr) guard let data = jsonStr.data(using: .utf8) else { throw "invalid security question data" } @@ -54,7 +87,7 @@ public final class TssSecurityQuestionModule { } // getFactorKey - public static func getFactorKey ( threshold: ThresholdKey, answer: String , tag: String ) throws -> String { + public static func get_factor_key ( threshold: ThresholdKey, answer: String , tag: String ) throws -> String { // get data format from json let domainKey = TssSecurityQuestion + ":" + tag @@ -78,4 +111,13 @@ public final class TssSecurityQuestionModule { return factorkeyBigInt.serialize().toHexString() } + + + public static func input_share ( threshold :ThresholdKey, answer: String, tag: String) async throws -> String { + let factorKey = try TssSecurityQuestionModule.get_factor_key(threshold: threshold, answer: answer, tag: tag) + + try await threshold.input_factor_key(factorKey: factorKey) + return factorKey + } + } diff --git a/Sources/ThresholdKey/ThresholdKey.swift b/Sources/ThresholdKey/ThresholdKey.swift index b380268..5243765 100644 --- a/Sources/ThresholdKey/ThresholdKey.swift +++ b/Sources/ThresholdKey/ThresholdKey.swift @@ -755,7 +755,7 @@ public class ThresholdKey { threshold_key_set_general_store_domain(pointer, keyPointer, dataPointer, error) }) guard errorCode == 0 else { - throw RuntimeError("Error in ThresholdKey get_tkey_store_item") + throw RuntimeError("Error in ThresholdKey set_domain_store_item : error Code : \(errorCode)") } } @@ -777,7 +777,7 @@ public class ThresholdKey { threshold_key_get_general_store_domain(pointer, keyPointer, error) }) guard errorCode == 0 else { - throw RuntimeError("Error in ThresholdKey get_tkey_store_item") + throw RuntimeError("Error in ThresholdKey get_domain_store_item") } let string = String(cString: result!) string_free(result) diff --git a/Sources/libtkey/libtkey.xcframework/Info.plist b/Sources/libtkey/libtkey.xcframework/Info.plist index 51ab6cd..dd0cadd 100644 --- a/Sources/libtkey/libtkey.xcframework/Info.plist +++ b/Sources/libtkey/libtkey.xcframework/Info.plist @@ -6,30 +6,30 @@ LibraryIdentifier - ios-arm64_x86_64-simulator + ios-arm64 LibraryPath libtkey.a SupportedArchitectures arm64 - x86_64 SupportedPlatform ios - SupportedPlatformVariant - simulator LibraryIdentifier - ios-arm64 + ios-arm64_x86_64-simulator LibraryPath libtkey.a SupportedArchitectures arm64 + x86_64 SupportedPlatform ios + SupportedPlatformVariant + simulator CFBundlePackageType diff --git a/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a b/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a index af0e1c5..36c080d 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 64d5ff3..e08f253 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 195103d..5a1392b 100644 --- a/Tests/tkeypkgTests/modules/tkey_pkgTssSecurityQuestionModuleTests.swift +++ b/Tests/tkeypkgTests/modules/tkey_pkgTssSecurityQuestionModuleTests.swift @@ -25,22 +25,38 @@ final class tkey_pkgTssSecurityQuestionModuleTests: XCTestCase { threshold_key = nil } - func test() async { + func test() async throws { let key_reconstruction_details = try! await threshold_key.reconstruct() let question = "favorite marvel character" let answer = "iron man" let answer_2 = "captain america" let factor_key = try? PrivateKey.generate() - try? TssSecurityQuestionModule.setSecurityQuestion(threshold: threshold_key, factorKey: factor_key!.hex, question: question, answer: answer, description: "please enter password", tag: "special") + var allIndex = try! threshold_key.get_shares_indexes() + allIndex.removeAll(where: {$0 == "1"}) + try! TssModule.backup_share_with_factor_key(threshold_key: threshold_key, shareIndex: allIndex[0], factorKey: factor_key!.hex) - let questionReturn = try? TssSecurityQuestionModule.getSecurityQuestion(threshold: threshold_key, tag: "special") - print(questionReturn) + try TssSecurityQuestionModule.set_security_question(threshold: threshold_key, factorKey: factor_key!.hex, question: question, answer: answer, description: "please enter password", tag: "special") + + + do { + try TssSecurityQuestionModule.set_security_question(threshold: threshold_key, factorKey: factor_key!.hex, question: question, answer: answer_2, description: "please enter password", tag: "special") + XCTFail("Should not able to set quesetion twice") + } catch {} + + let questionReturn = try? TssSecurityQuestionModule.get_question(threshold: threshold_key, tag: "special") XCTAssertEqual(questionReturn, question) - let factor = try? TssSecurityQuestionModule.getFactorKey(threshold: threshold_key, answer: answer, tag: "special") + let factor = try TssSecurityQuestionModule.get_factor_key(threshold: threshold_key, answer: answer, tag: "special") + + let factor1 = try await TssSecurityQuestionModule.input_share(threshold: threshold_key, answer: answer, tag: "special") + do { + let factor = try await TssSecurityQuestionModule.input_share(threshold: threshold_key, answer: answer_2, tag: "special") + XCTFail("Should be able to get factor using incorrect answer") + } catch {} + - XCTAssertEqual(String(factor!.suffix(64)), factor_key!.hex) + XCTAssertEqual(String(factor.suffix(64)), factor_key!.hex) } }