Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/move signatures to threshold <Breaking> #15

Draft
wants to merge 2 commits into
base: alpha
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions Sources/ThresholdKey/Modules/TssModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,9 @@ public final class TssModule {
/// - torusUtils: torusUtils used to retrieve dkg tss pub key
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docs need to be updated as well.

///
/// - 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) {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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, Error>) -> 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, Error>) -> Void) {
threshold_key.tkeyQueue.async {
do {
var errorCode: Int32 = -1
let curvePointer = UnsafeMutablePointer<Int8>(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")
Expand Down Expand Up @@ -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):
Expand All @@ -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, Error>) -> 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, Error>) -> Void) {
threshold_key.tkeyQueue.async {
do {
var errorCode: Int32 = -1
let curvePointer = UnsafeMutablePointer<Int8>(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")
Expand All @@ -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 {
Expand All @@ -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):
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)")

Expand Down
33 changes: 33 additions & 0 deletions Sources/ThresholdKey/ThresholdKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading