diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4f880bb..867e601 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,8 +1,8 @@ on: push: branches: - - master - - v2 + - main + - alpha pull_request: types: - opened diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml deleted file mode 100644 index 267f63b..0000000 --- a/.github/workflows/pr.yaml +++ /dev/null @@ -1,19 +0,0 @@ -name: Enforce PR Source Branch - -on: - pull_request: - types: - - opened - - synchronize - -jobs: - enforce-branch: - runs-on: ubuntu-latest - - steps: - - name: Check PR source branch - run: | - if ["${{ github.event.pull_request.base.ref }}" == "main" && "${{ github.event.pull_request.head.ref }}" != "alpha" ]; then - echo "Only pull requests from the alpha branch are allowed to main." - exit 1 - fi \ No newline at end of file diff --git a/Package.swift b/Package.swift index c8783a5..2262457 100644 --- a/Package.swift +++ b/Package.swift @@ -17,7 +17,7 @@ let package = Package( dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift" , branch: "alpha"), + .package(name: "TorusUtils", url: "https://github.com/torusresearch/torus-utils-swift" , from: "6.0.1"), // dev dependencies only .package(name:"CryptoSwift", url: "https://github.com/krzyzanowskim/CryptoSwift.git",from: "1.5.1"), .package(name:"jwt-kit", url: "https://github.com/vapor/jwt-kit.git", from: "4.0.0"), diff --git a/Sources/ThresholdKey/Modules/TssModule.swift b/Sources/ThresholdKey/Modules/TssModule.swift index 06ab4dd..f0977e5 100644 --- a/Sources/ThresholdKey/Modules/TssModule.swift +++ b/Sources/ThresholdKey/Modules/TssModule.swift @@ -8,7 +8,7 @@ import CommonSources import FetchNodeDetails import TorusUtils -public struct GetTSSPubKeyResult: Codable { +public struct TSSPubKeyResult: Codable { public struct Point: Codable { public var x: String public var y: String @@ -40,6 +40,12 @@ public final class TssModule { } } + /// Set the active tss tag + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: The tss tag to be set. + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. static func set_tss_tag(threshold_key: ThresholdKey, tss_tag: String) async throws { return try await withCheckedThrowingContinuation { continuation in @@ -55,6 +61,13 @@ public final class TssModule { } } + /// Returns the current active tss tag + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// + /// - Returns: `String` + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. public static func get_tss_tag(threshold_key: ThresholdKey) throws -> String { var errorCode: Int32 = -1 let result = withUnsafeMutablePointer(to: &errorCode, { error in @@ -67,6 +80,13 @@ public final class TssModule { return string } + /// Get all tss tags in the metadata + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// + /// - Returns: Array of String + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. public static func get_all_tss_tags(threshold_key: ThresholdKey) throws -> [String] { var errorCode: Int32 = -1 @@ -87,6 +107,14 @@ public final class TssModule { return result_vec } + /// Returns all factor public keys for tagged tss key + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: The tss tag. + /// + /// - Returns: Array of String + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. public static func get_all_factor_pub(threshold_key: ThresholdKey, tss_tag: String) async throws -> [String] { try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) @@ -109,6 +137,14 @@ public final class TssModule { return result_vec } + /// Returns the tagged final tss public key + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: The tss tag + /// + /// - Returns: `String` + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. public static func get_tss_pub_key(threshold_key: ThresholdKey, tss_tag: String) async throws -> String { try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) @@ -124,6 +160,15 @@ public final class TssModule { return string } + /// Returns the tagged tss nonce + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: The tss tag. + /// - prefetch: Fetch the next nonce's pub key + /// + /// - 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 { var errorCode: Int32 = -1 let tss_tag_pointer: UnsafeMutablePointer? = UnsafeMutablePointer(mutating: NSString(string: tss_tag).utf8String) @@ -140,6 +185,16 @@ public final class TssModule { return nonce } + /// Returns latest tss index and tss share registed to the factor key + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: The tss tag. + /// - factorKey: Public key of the new factor that was previously added + /// - threshold: The threshold + /// + /// - Returns: `(String, String)` + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. public static func get_tss_share(threshold_key: ThresholdKey, tss_tag: String, factorKey: String, threshold: Int32 = 0) async throws -> (String, String) { if factorKey.count > 66 { throw RuntimeError("Invalid factor Key") } try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) @@ -186,6 +241,17 @@ public final class TssModule { } } + /// Initialize new tagged tss key and create the tss share + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: tss key's tag. + /// - deviceTssShare : specific value that will be tss share of the final tss key, optional. *note: after tss refresh (add factor pub or delete factor pub) current tss share will be invalidated* + /// - factorPub: public key of the new factor that added (registered) + /// - deviceTssIndex: Tss Index of tss share should be associated + /// - nodeDetails: nodeDetails that sdk should comunicate to + /// - 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 { 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) @@ -214,6 +280,15 @@ public final class TssModule { } } + /// Get and update latest dkg tss_pub_key with latest nonce + /// - Parameters: + /// - threshold_key: The threshold key to act on + /// - tss_tag: The tss tag. + /// - nodeDetails: nodeDetails that sdk should comunicate to + /// - torusUtils: torusUtils used to retrieve dkg tss pub key + /// - 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 { try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) @@ -261,7 +336,17 @@ public final class TssModule { } } - public static func copy_factor_pub(threshold_key: ThresholdKey, tss_tag: String, factorKey: String, newFactorPub: String, tss_index: Int32, threshold: Int32 = 0) async throws { + /// Copy tss share from existing factor to new factor key (register) + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: The tss tah. + /// - factor_key: Valid factor key that needed to execute this operation. + /// - auth_signatures: Signature data that need to be validated by signing server . + /// - new_factor_pub: Public key of the new factor that added (registered) + /// - tss_index: tss_index that should match with existing factor key's tss_index + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. + public static func copy_factor_pub(threshold_key: ThresholdKey, tss_tag: String, factorKey: String, newFactorPub: String, tss_index: Int32) async throws { if factorKey.count > 66 { throw RuntimeError("Invalid factor Key") } try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) @@ -332,7 +417,7 @@ 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) { threshold_key.tkeyQueue.async { do { @@ -386,15 +471,39 @@ public final class TssModule { } } + /// Generate tss_index's share and register to new factor key + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: tss key's tag. + /// - factor_key: valid factor key that needed to execute this operation. + /// - auth_signatures: signature data that need to be validated by signing server . + /// - new_factor_pub: public key of the new factor that added (registered) + /// - new_tss_index: tss_index of the new tss share (to be registered) + /// - selected_servers: node indexes of the server that will be communicated to + /// - nodeDetails: node details + /// - 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 { 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) - } + /// Delete factor pub from tss metadata + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - tss_tag: tss key's tag. + /// - factor_key: valid factor key that needed to execute this operation. + /// - auth_signatures: signature data that need to be validated by signing server . + /// - delete_factor_pub: public key of the factor that need to be deleted. + /// - nodeDetails: node details + /// - torusUtils: torus utils + /// - 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 { if factor_key.count > 66 { throw RuntimeError("Invalid factor Key") } try await TssModule.set_tss_tag(threshold_key: threshold_key, tss_tag: tss_tag) @@ -402,9 +511,8 @@ public final class TssModule { 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) } - - - /// backup device share with factor key + + /// Backup device share with factor key /// - Parameters: /// - threshold_key: The threshold key to act on. /// - shareIndex: Index of the Share to be backed up. @@ -417,19 +525,25 @@ public final class TssModule { let cShareIndex = UnsafeMutablePointer(mutating: (shareIndex as NSString).utf8String) let cFactorKey = UnsafeMutablePointer(mutating: (factorKey as NSString).utf8String) let curvePointer = UnsafeMutablePointer(mutating: (threshold_key.curveN as NSString).utf8String) - - withUnsafeMutablePointer(to: &errorCode, { error in threshold_key_backup_share_with_factor_key( threshold_key.pointer, cShareIndex, cFactorKey, curvePointer, error)}) - guard errorCode == 0 else { - throw RuntimeError("Error in ThresholdKey backup_share_with_factor_key") - } + + withUnsafeMutablePointer(to: &errorCode, { error in threshold_key_backup_share_with_factor_key(threshold_key.pointer, cShareIndex, cFactorKey, curvePointer, error) }) + guard errorCode == 0 else { + throw RuntimeError("Error in ThresholdKey backup_share_with_factor_key") + } } - public static func find_device_share_index ( threshold_key: ThresholdKey, factor_key: String ) async throws -> String { + /// Find the metadata share Index registred with factor_key + /// - Parameters: + /// - threshold_key: The threshold key to act on. + /// - factor_key: factor key to be used. + /// + /// - Throws: `RuntimeError`, indicates invalid parameters was used or invalid threshold key. + public static func find_device_share_index(threshold_key: ThresholdKey, factor_key: String) async throws -> String { let result = try await threshold_key.storage_layer_get_metadata(private_key: factor_key) guard let resultData = result.data(using: .utf8) else { throw "Invalid factor key" } - guard let resultJson = try JSONSerialization.jsonObject(with: resultData ) as? [String: Any] else { + guard let resultJson = try JSONSerialization.jsonObject(with: resultData) as? [String: Any] else { throw "Invalid factor key" } guard let deviceShareJson = resultJson["deviceShare"] as? [String: Any] else { @@ -443,28 +557,27 @@ public final class TssModule { } return shareIndex } - - /// get dkg public key + + /// Function to get dkg public key /// - Parameters: /// - threshold_key: The threshold key to act on. /// - tssTag: tssTag used. - /// - nonce: nonce + /// - nonce: nonce /// - nodeDetails: node details /// - torusUtils: torus utils - /// - Returns: `GetTSSPubKeyResult` + /// - 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 -> GetTSSPubKeyResult { + public static func get_dkg_pub_key(threshold_key: ThresholdKey, tssTag: String, nonce: String, nodeDetails: AllNodeDetailsModel, torusUtils: TorusUtils) async throws -> TSSPubKeyResult { let extendedVerifierId = try threshold_key.get_extended_verifier_id() let split = extendedVerifierId.components(separatedBy: "\u{001c}") 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)") - print("result in service provider", result) guard let x = result.finalKeyData?.X, let y = result.finalKeyData?.Y, let nodeIndexes = result.nodesData?.nodeIndexes else { throw RuntimeError("conversion error") } - let pubKey = GetTSSPubKeyResult.Point(x: x, y: y) - return GetTSSPubKeyResult(publicKey: pubKey, nodeIndexes: nodeIndexes) + let pubKey = TSSPubKeyResult.Point(x: x, y: y) + return TSSPubKeyResult(publicKey: pubKey, nodeIndexes: nodeIndexes) } } diff --git a/Sources/ThresholdKey/StorageLayer.swift b/Sources/ThresholdKey/StorageLayer.swift index 5dc0193..92ce5ee 100644 --- a/Sources/ThresholdKey/StorageLayer.swift +++ b/Sources/ThresholdKey/StorageLayer.swift @@ -3,15 +3,6 @@ import Foundation import lib #endif -/* -extension NSMutableData { - func appendString(_ string: String) { - if let data = string.data(using: .utf8) { - append(data) - } - } -} -*/ public final class StorageLayer { private(set) var pointer: OpaquePointer? @@ -21,21 +12,6 @@ public final class StorageLayer { // on entry for the callback private var obj_ref: UnsafeMutableRawPointer? - /* for multipart form data - static func createMultipartBody(data: Data, boundary: String, file: String) -> Data { - let body = NSMutableData() - let lineBreak = "\r\n" - let boundaryPrefix = "--\(boundary)\r\n" - body.appendString(boundaryPrefix) - body.appendString("Content-Disposition: form-data; name=\"\(file)\"\r\n") - body.appendString("Content-Type: \("application/json;charset=utf-8")\r\n\r\n") - body.append(data) - body.appendString("\r\n") - body.appendString("--\(boundary)--\(lineBreak)") - return body as Data - } - */ - private static func percentEscapeString( string: String ) -> String { var characterSet = CharacterSet.alphanumerics characterSet.insert(charactersIn: "-.* ") diff --git a/Sources/ThresholdKey/ThresholdKey.swift b/Sources/ThresholdKey/ThresholdKey.swift index aa82dc0..aaf3721 100644 --- a/Sources/ThresholdKey/ThresholdKey.swift +++ b/Sources/ThresholdKey/ThresholdKey.swift @@ -84,24 +84,16 @@ public class ThresholdKey { return Metadata(pointer: result!) } - private func initialize(import_share: String?, input: ShareStore?, never_initialize_new_key: Bool?, include_local_metadata_transitions: Bool?, use_tss: Bool = false, device_tss_share: String?, device_tss_index: Int32?, tss_factor_pub: KeyPoint?, completion: @escaping (Result) -> Void) { + private func initialize(import_metdata_key: String?, input: ShareStore?, never_initialize_new_key: Bool?, include_local_metadata_transitions: Bool?, completion: @escaping (Result) -> Void) { tkeyQueue.async { do { var errorCode: Int32 = -1 - var sharePointer: UnsafeMutablePointer? - var tssDeviceSharePointer: UnsafeMutablePointer? - var tssFactorPubPointer: OpaquePointer? - var device_index: Int32 = device_tss_index ?? 2 - if import_share != nil { - sharePointer = UnsafeMutablePointer(mutating: NSString(string: import_share!).utf8String) - } - - if device_tss_share != nil { - tssDeviceSharePointer = UnsafeMutablePointer(mutating: NSString(string: device_tss_share!).utf8String) - } - - if tss_factor_pub != nil { - tssFactorPubPointer = tss_factor_pub!.pointer + var keyPointer: UnsafeMutablePointer? + var device_index: Int32 = 2 + let useTss = false + + if import_metdata_key != nil { + keyPointer = UnsafeMutablePointer(mutating: NSString(string: import_metdata_key!).utf8String) } var storePtr: OpaquePointer? @@ -114,7 +106,7 @@ public class ThresholdKey { let curvePointer = UnsafeMutablePointer(mutating: NSString(string: self.curveN).utf8String) let ptr = withUnsafeMutablePointer(to: &device_index, { tssDeviceIndexPointer in withUnsafeMutablePointer(to: &errorCode, { error in - threshold_key_initialize(self.pointer, sharePointer, storePtr, neverInitializeNewKey, includeLocalMetadataTransitions, curvePointer, use_tss, tssDeviceSharePointer, tssDeviceIndexPointer, tssFactorPubPointer, error) }) }) + threshold_key_initialize(self.pointer, keyPointer, storePtr, neverInitializeNewKey, includeLocalMetadataTransitions, false, curvePointer, useTss, nil, tssDeviceIndexPointer, nil, error) }) }) guard errorCode == 0 else { throw RuntimeError("Error in ThresholdKey Initialize") } @@ -129,22 +121,18 @@ public class ThresholdKey { /// Initializes a `ThresholdKey` object. /// /// - Parameters: - /// - import_share: Share to be imported, optional. + /// - import_metdata_key: Metadata key to be imported, optional. /// - input: `ShareStore` to be used, optional. /// - never_initialize_new_key: Do not initialize a new tKey if an existing one is found. /// - include_local_matadata_transitions: Proritize existing metadata transitions over cloud fetched transitions. - /// - use_tss: Whether TSS is used or not. - /// - device_tss_share: Device share for TSS, optional - /// - device_tss_index: Device index for TSS, optional - /// - tss_factor_pub: Factor Key for TSS, optional /// /// - Returns: `KeyDetails` /// /// - Throws: `RuntimeError`, indicates invalid parameters. - public func initialize(import_share: String? = nil, input: ShareStore? = nil, never_initialize_new_key: Bool? = nil, include_local_metadata_transitions: Bool? = nil, use_tss: Bool = false, device_tss_share: String? = nil, device_tss_index: Int32? = nil, tss_factor_pub: KeyPoint? = nil) async throws -> KeyDetails { + public func initialize(import_metdata_key: String? = nil, input: ShareStore? = nil, never_initialize_new_key: Bool? = nil, include_local_metadata_transitions: Bool? = nil ) async throws -> KeyDetails { return try await withCheckedThrowingContinuation { continuation in - self.initialize(import_share: import_share, input: input, never_initialize_new_key: never_initialize_new_key, include_local_metadata_transitions: include_local_metadata_transitions, use_tss: use_tss, device_tss_share: device_tss_share, device_tss_index: device_tss_index, tss_factor_pub: tss_factor_pub) { + self.initialize(import_metdata_key: import_metdata_key, input: input, never_initialize_new_key: never_initialize_new_key, include_local_metadata_transitions: include_local_metadata_transitions ) { result in switch result { case let .success(result): @@ -174,7 +162,9 @@ public class ThresholdKey { } } - /// Reconstructs the private key, this assumes that the number of shares inserted into the `ThrehsoldKey` are equal or greater than the threshold. + /// Reconstructs the private key, this assumes that the number of shares inserted into the `ThresholdKey` are equal or greater than the threshold. + /// + /// - Returns: `KeyReconstructionDetails` /// /// - Throws: `RuntimeError`. public func reconstruct() async throws -> KeyReconstructionDetails { @@ -227,18 +217,15 @@ public class ThresholdKey { return ShareStoreArray(pointer: result!) } - private func generate_new_share(use_tss: Bool = false, tss_options: TssOptions? = nil, completion: @escaping (Result) -> Void) { + private func generate_new_share(completion: @escaping (Result) -> Void) { tkeyQueue.async { do { - var options: OpaquePointer? - if tss_options != nil { - options = tss_options!.pointer - } + let useTss = false var errorCode: Int32 = -1 let curvePointer = UnsafeMutablePointer(mutating: (self.curveN as NSString).utf8String) let ptr = withUnsafeMutablePointer(to: &errorCode, { error in - threshold_key_generate_share(self.pointer, curvePointer, use_tss, options, error) + threshold_key_generate_share(self.pointer, curvePointer, useTss, nil, error) }) guard errorCode == 0 else { throw RuntimeError("Error in ThresholdKey generate_new_share") @@ -254,16 +241,12 @@ public class ThresholdKey { /// Generates a new share. /// - /// - Parameters: - /// - use_tss: Whether TSS should be used or not.. - /// - tss_options: TSS options that should be used for TSS. - /// - /// - Returns: `GenerateShareStoreArray` + /// - Returns: `GenerateShareStoreResult` /// /// - Throws: `RuntimeError`, indicates invalid `ThresholdKey`. - public func generate_new_share(use_tss: Bool = false, tss_options: TssOptions? = nil) async throws -> GenerateShareStoreResult { + public func generate_new_share() async throws -> GenerateShareStoreResult { return try await withCheckedThrowingContinuation { - continuation in self.generate_new_share(use_tss: use_tss, tss_options: tss_options) { + continuation in self.generate_new_share() { result in switch result { case let .success(result): @@ -275,18 +258,17 @@ public class ThresholdKey { } } - private func delete_share(share_index: String, use_tss: Bool = false, tss_options: TssOptions? = nil, completion: @escaping (Result) -> Void) { + private func delete_share(share_index: String, completion: @escaping (Result) -> Void) { tkeyQueue.async { do { var errorCode: Int32 = -1 let curvePointer = UnsafeMutablePointer(mutating: (self.curveN as NSString).utf8String) let shareIndexPointer = UnsafeMutablePointer(mutating: (share_index as NSString).utf8String) - var options: OpaquePointer? - if tss_options != nil { - options = tss_options!.pointer - } + + let useTss = false + withUnsafeMutablePointer(to: &errorCode, { error in - threshold_key_delete_share(self.pointer, shareIndexPointer, curvePointer, use_tss, options, error) + threshold_key_delete_share(self.pointer, shareIndexPointer, curvePointer, useTss, nil, error) }) guard errorCode == 0 else { throw RuntimeError("Error in Threshold while Deleting share") @@ -301,10 +283,8 @@ public class ThresholdKey { /// Deletes a share at the specified index. Caution is advised to not try delete a share that would prevent the total number of shares being below the threshold. /// - Parameters: /// - share_index: Share index to be deleted. - /// - use_tss: Whether TSS should be used or not.. - /// - tss_options: TSS options that should be used for TSS. /// - Throws: `RuntimeError`, indicates invalid share index or invalid `ThresholdKey`. - public func delete_share(share_index: String, use_tss: Bool = false, tss_options: TssOptions? = nil) async throws { + public func delete_share(share_index: String) async throws { return try await withCheckedThrowingContinuation { continuation in self.delete_share(share_index: share_index) { @@ -576,7 +556,7 @@ public class ThresholdKey { /// Retrieves all share indexes for a `ThresholdKey`. /// - /// - Returns: Array of `String` + /// - Returns: Array of String /// /// - Throws: `RuntimeError`, indicates invalid `ThresholdKey`. public func get_shares_indexes() throws -> [String] { @@ -1067,6 +1047,14 @@ public class ThresholdKey { } } + /// Function to assign a public key to the service provider, used only for TSS + /// + /// - Parameters: + /// - tag: The tss tag + /// - nonce: The tss nonce + /// - public_key: The pulic key to be assigned + /// + /// - Throws: `RuntimeError`, indicates invalid parameters or invalid `ThresholdKey`. public func service_provider_assign_public_key(tag: String, nonce: String, public_key: String) throws { var errorCode: Int32 = -1 let tagPointer = UnsafeMutablePointer(mutating: NSString(string: tag).utf8String) @@ -1078,6 +1066,12 @@ public class ThresholdKey { } } + + /// Function to get all tss tags + /// + /// - Returns: Array of String + /// + /// - Throws: `RuntimeError`, indicates invalid parameters or invalid `ThresholdKey`. public func get_all_tss_tags() throws -> [String] { var errorCode: Int32 = -1 @@ -1097,7 +1091,12 @@ public class ThresholdKey { return result_vec } - + + /// Function to get extended verifier id + /// + /// - Returns: String + /// + /// - Throws: `RuntimeError`, indicates invalid parameters or invalid `ThresholdKey`. public func get_extended_verifier_id() throws -> String { var errorCode: Int32 = -1 diff --git a/Sources/libtkey/include/tkey.h b/Sources/libtkey/include/tkey.h index 187c7c9..809cda3 100644 --- a/Sources/libtkey/include/tkey.h +++ b/Sources/libtkey/include/tkey.h @@ -66,7 +66,7 @@ struct ServiceProvider* service_provider(bool enable_logging, char* postbox_key, char* curve_n, bool use_tss, char* verifier_name, char* verifier_id, struct NodeDetails* tss_node_details, struct NodeDetails* rss_node_details,struct NodeDetails* sss_node_details, int* error_code); void service_provider_free(struct ServiceProvider* prt); struct FFIThresholdKey* threshold_key(struct Metadata* metadata, struct ShareStorePolyIDShareIndexMap* shares, struct FFIStorageLayer* storage_layer, struct ServiceProvider* service_provider, struct LocalMetadataTransitions* local_metadata_transitions, struct Metadata* last_fetch_cloud_metadata, bool enable_logging, bool manual_sync, struct FFIRssComm* rss_comm, int* error_code); - struct KeyDetails* threshold_key_initialize(struct FFIThresholdKey* threshold_key, char* import_share, struct ShareStore* input, bool never_initialize_new_key, bool include_local_metadata_transitions, char* curve_n, bool use_tss, char* device_share, int* device_tss_index, struct KeyPoint* factor_pub, int* error_code); + struct KeyDetails* threshold_key_initialize(struct FFIThresholdKey* threshold_key, char* import_share, struct ShareStore* input, bool never_initialize_new_key, bool include_local_metadata_transitions, bool delete_1_of_1, char* curve_n, bool use_tss, char* device_share, int* device_tss_index, struct KeyPoint* factor_pub, int* error_code); struct KeyDetails* threshold_key_get_key_details(struct FFIThresholdKey* threshold_key, int* error_code); struct KeyReconstructionDetails* threshold_key_reconstruct(struct FFIThresholdKey* threshold_key, char* curve_n, int* error_code); void threshold_key_free(struct FFIThresholdKey* ptr); diff --git a/Sources/libtkey/libtkey.xcframework/ios-arm64-simulator/libtkey.a b/Sources/libtkey/libtkey.xcframework/ios-arm64-simulator/libtkey.a deleted file mode 100644 index ac6f65a..0000000 Binary files a/Sources/libtkey/libtkey.xcframework/ios-arm64-simulator/libtkey.a and /dev/null differ diff --git a/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a b/Sources/libtkey/libtkey.xcframework/ios-arm64/libtkey.a index a6936f7..4d89a2b 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 f352319..ab241f2 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/IntegrationTests.swift b/Tests/tkeypkgTests/IntegrationTests.swift index cef7a81..c7e8ca6 100644 --- a/Tests/tkeypkgTests/IntegrationTests.swift +++ b/Tests/tkeypkgTests/IntegrationTests.swift @@ -41,7 +41,7 @@ final class integrationTests: XCTestCase { _ = try! await threshold.initialize() _ = try! await threshold.reconstruct() let shareIndex = try! await threshold.generate_new_share() - let share = try threshold.output_share(shareIndex: shareIndex.hex) + let _ = try threshold.output_share(shareIndex: shareIndex.hex) let tssTag = "testing" let factorKey = try PrivateKey.generate()