From 0b5d7654e5dde5f54117756cadf4755aee51190f Mon Sep 17 00:00:00 2001 From: ieow Date: Thu, 24 Aug 2023 15:31:12 +0800 Subject: [PATCH 1/3] feat: isolate tss feature from tkey --- Sources/ThresholdKey/ThresholdKey.swift | 53 +++++++++---------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/Sources/ThresholdKey/ThresholdKey.swift b/Sources/ThresholdKey/ThresholdKey.swift index aa82dc0..e300caf 100644 --- a/Sources/ThresholdKey/ThresholdKey.swift +++ b/Sources/ThresholdKey/ThresholdKey.swift @@ -84,26 +84,21 @@ 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_share: 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? + // use default value for now var tssDeviceSharePointer: UnsafeMutablePointer? var tssFactorPubPointer: OpaquePointer? - var device_index: Int32 = device_tss_index ?? 2 + var device_index: Int32 = 2 + let useTss = false + 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 storePtr: OpaquePointer? if input != nil { storePtr = input!.pointer @@ -114,7 +109,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, sharePointer, storePtr, neverInitializeNewKey, includeLocalMetadataTransitions, curvePointer, useTss, tssDeviceSharePointer, tssDeviceIndexPointer, tssFactorPubPointer, error) }) }) guard errorCode == 0 else { throw RuntimeError("Error in ThresholdKey Initialize") } @@ -133,18 +128,14 @@ public class ThresholdKey { /// - 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_share: 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_share: import_share, 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): @@ -227,18 +218,16 @@ 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 { + let useTss = false var options: OpaquePointer? - if tss_options != nil { - options = tss_options!.pointer - } 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, options, error) }) guard errorCode == 0 else { throw RuntimeError("Error in ThresholdKey generate_new_share") @@ -255,15 +244,13 @@ 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` /// /// - 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 +262,18 @@ 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) + + let useTss = false var options: OpaquePointer? - if tss_options != nil { - options = tss_options!.pointer - } + 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, options, error) }) guard errorCode == 0 else { throw RuntimeError("Error in Threshold while Deleting share") @@ -301,10 +288,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) { From 5546cddab9f9b2de6051c16235af38ffa3a07f5d Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Thu, 7 Sep 2023 08:59:08 +0200 Subject: [PATCH 2/3] fix: parameter name for import_metadata_key --- Sources/ThresholdKey/ThresholdKey.swift | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/Sources/ThresholdKey/ThresholdKey.swift b/Sources/ThresholdKey/ThresholdKey.swift index e300caf..7cdaaeb 100644 --- a/Sources/ThresholdKey/ThresholdKey.swift +++ b/Sources/ThresholdKey/ThresholdKey.swift @@ -84,19 +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?, 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? - // use default value for now - var tssDeviceSharePointer: UnsafeMutablePointer? - var tssFactorPubPointer: OpaquePointer? + var keyPointer: UnsafeMutablePointer? var device_index: Int32 = 2 let useTss = false - if import_share != nil { - sharePointer = UnsafeMutablePointer(mutating: NSString(string: import_share!).utf8String) + if import_metdata_key != nil { + keyPointer = UnsafeMutablePointer(mutating: NSString(string: import_metdata_key!).utf8String) } var storePtr: OpaquePointer? @@ -109,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, useTss, tssDeviceSharePointer, tssDeviceIndexPointer, tssFactorPubPointer, error) }) }) + threshold_key_initialize(self.pointer, keyPointer, storePtr, neverInitializeNewKey, includeLocalMetadataTransitions, curvePointer, useTss, nil, tssDeviceIndexPointer, nil, error) }) }) guard errorCode == 0 else { throw RuntimeError("Error in ThresholdKey Initialize") } @@ -124,7 +121,7 @@ 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. @@ -132,10 +129,10 @@ public class ThresholdKey { /// - 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 ) 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 ) { + 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): @@ -222,12 +219,11 @@ public class ThresholdKey { tkeyQueue.async { do { let useTss = false - var options: OpaquePointer? 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, useTss, options, error) + threshold_key_generate_share(self.pointer, curvePointer, useTss, nil, error) }) guard errorCode == 0 else { throw RuntimeError("Error in ThresholdKey generate_new_share") @@ -270,10 +266,9 @@ public class ThresholdKey { let shareIndexPointer = UnsafeMutablePointer(mutating: (share_index as NSString).utf8String) let useTss = false - var options: OpaquePointer? withUnsafeMutablePointer(to: &errorCode, { error in - threshold_key_delete_share(self.pointer, shareIndexPointer, curvePointer, useTss, 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") From 680ae74f6c481c56a3a511c061d82baa4e20b1df Mon Sep 17 00:00:00 2001 From: metalurgical <97008724+metalurgical@users.noreply.github.com> Date: Thu, 7 Sep 2023 09:06:26 +0200 Subject: [PATCH 3/3] update: ci branch --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index b3bf43e..867e601 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -2,6 +2,7 @@ on: push: branches: - main + - alpha pull_request: types: - opened