Skip to content

Commit

Permalink
Merge branch 'alpha' into feat/tssSecurityQuestion
Browse files Browse the repository at this point in the history
  • Loading branch information
ieow committed Sep 7, 2023
2 parents 515c59f + 6c0fd93 commit 8640242
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 100 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ on:
push:
branches:
- main
- alpha
pull_request:
types:
- opened
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
157 changes: 135 additions & 22 deletions Sources/ThresholdKey/Modules/TssModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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<Int8>? = UnsafeMutablePointer<Int8>(mutating: NSString(string: tss_tag).utf8String)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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, Error>) -> Void) {
threshold_key.tkeyQueue.async {
do {
Expand Down Expand Up @@ -386,25 +471,48 @@ 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)

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.
Expand All @@ -417,19 +525,25 @@ public final class TssModule {
let cShareIndex = UnsafeMutablePointer<Int8>(mutating: (shareIndex as NSString).utf8String)
let cFactorKey = UnsafeMutablePointer<Int8>(mutating: (factorKey as NSString).utf8String)
let curvePointer = UnsafeMutablePointer<Int8>(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 {
Expand All @@ -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)
}
}
24 changes: 0 additions & 24 deletions Sources/ThresholdKey/StorageLayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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: "-.* ")
Expand Down
Loading

0 comments on commit 8640242

Please sign in to comment.