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/tss sq rust #18

Draft
wants to merge 19 commits into
base: main
Choose a base branch
from
Draft

Feat/tss sq rust #18

wants to merge 19 commits into from

Conversation

ieow
Copy link
Contributor

@ieow ieow commented Oct 18, 2023

No description provided.

Comment on lines +11 to +43
func selectedServerToPointer (selectedServers: [UInt32]?) throws -> UnsafeMutablePointer<Int8>? {
guard let selectedServers = selectedServers else {
return nil
}
var serversPointer: UnsafeMutablePointer<Int8>?

let selected_servers_json = try JSONSerialization.data(withJSONObject: selectedServers as Any)
let selected_servers_str = String(data: selected_servers_json, encoding: .utf8)!
print(selected_servers_str)
serversPointer = UnsafeMutablePointer<Int8>(mutating: (selected_servers_str as NSString).utf8String)

guard let serversPointer = serversPointer else {
throw RuntimeError("convert error")
}
return serversPointer
}

func authSignaturesToPointer ( authSignatures : [String]?) throws -> UnsafeMutablePointer<Int8>? {
guard let authSignatures = authSignatures else {
return nil
}
let auth_signatures_json = try JSONSerialization.data(withJSONObject: authSignatures)
guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else {
throw RuntimeError("auth signatures error")
}
print(authSignatures)
print(auth_signatures_str.count)

let authSignaturesPointer = UnsafeMutablePointer<Int8>(mutating: (auth_signatures_str as NSString).utf8String)


return authSignaturesPointer
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

These pointers are probably not valid once the function returns, the memory locations they're assigned are local to this function.

Suggested change
func selectedServerToPointer (selectedServers: [UInt32]?) throws -> UnsafeMutablePointer<Int8>? {
guard let selectedServers = selectedServers else {
return nil
}
var serversPointer: UnsafeMutablePointer<Int8>?
let selected_servers_json = try JSONSerialization.data(withJSONObject: selectedServers as Any)
let selected_servers_str = String(data: selected_servers_json, encoding: .utf8)!
print(selected_servers_str)
serversPointer = UnsafeMutablePointer<Int8>(mutating: (selected_servers_str as NSString).utf8String)
guard let serversPointer = serversPointer else {
throw RuntimeError("convert error")
}
return serversPointer
}
func authSignaturesToPointer ( authSignatures : [String]?) throws -> UnsafeMutablePointer<Int8>? {
guard let authSignatures = authSignatures else {
return nil
}
let auth_signatures_json = try JSONSerialization.data(withJSONObject: authSignatures)
guard let auth_signatures_str = String(data: auth_signatures_json, encoding: .utf8) else {
throw RuntimeError("auth signatures error")
}
print(authSignatures)
print(auth_signatures_str.count)
let authSignaturesPointer = UnsafeMutablePointer<Int8>(mutating: (auth_signatures_str as NSString).utf8String)
return authSignaturesPointer
}

Comment on lines +566 to +588
//
// // TODO : Fix the deserialization of the Factor cloud metadata
// // ts implemetation break this
// let deviceShareJson : [String: Any]
// if resultJson["deviceShare"] != nil {
// guard var deviceShare = resultJson["deviceShare"] as? [String: Any] else {
// throw "invalid factor json without deviceShare or share indexkey"
// }
// deviceShareJson = deviceShare
// } else {
// guard let deviceShare = resultJson["share"] as? [String:Any] else {
// throw "invalid factor json without deviceShare or share indexkey"
// }
// deviceShareJson = deviceShare
// }
//
// guard let shareJson = deviceShareJson["share"] as? [String: Any] else {
// throw "Invalid factor key"
// }
// guard let shareIndex = shareJson["shareIndex"] as? String else {
// throw "Invalid factor key"
// }
// return shareIndex
Copy link
Collaborator

Choose a reason for hiding this comment

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

Remove commented code

Suggested change
//
// // TODO : Fix the deserialization of the Factor cloud metadata
// // ts implemetation break this
// let deviceShareJson : [String: Any]
// if resultJson["deviceShare"] != nil {
// guard var deviceShare = resultJson["deviceShare"] as? [String: Any] else {
// throw "invalid factor json without deviceShare or share indexkey"
// }
// deviceShareJson = deviceShare
// } else {
// guard let deviceShare = resultJson["share"] as? [String:Any] else {
// throw "invalid factor json without deviceShare or share indexkey"
// }
// deviceShareJson = deviceShare
// }
//
// guard let shareJson = deviceShareJson["share"] as? [String: Any] else {
// throw "Invalid factor key"
// }
// guard let shareIndex = shareJson["shareIndex"] as? String else {
// throw "Invalid factor key"
// }
// return shareIndex


public func toString() throws -> String {
let data = try JSONEncoder().encode(self)
// let data = try JSONSerialization.data(withJSONObject: self)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// let data = try JSONSerialization.data(withJSONObject: self)

Comment on lines +591 to +630
// private func patch_input_factor_key(factorKey: String, completion: @escaping (Result<Void, Error>) -> Void) {
// tkeyQueue.async {
// do {
// var errorCode: Int32 = -1
// let cFactorKey = UnsafeMutablePointer<Int8>(mutating: (factorKey as NSString).utf8String)
// let curvePointer = UnsafeMutablePointer<Int8>(mutating: (self.curveN as NSString).utf8String)
//
// withUnsafeMutablePointer(to: &errorCode, { error in
// threshold_key_patch_input_factor_key(self.pointer, cFactorKey, curvePointer, error)
// })
// guard errorCode == 0 else {
// throw RuntimeError("Error in ThresholdKey input_factor_key \(errorCode)")
// }
// completion(.success(()))
// } catch {
// completion(.failure(error))
// }
// }
// }

/// Patch and Inserts a `ShareStore` into `ThresholdKey` using `FactorKey`, useful for insertion before reconstruction to ensure the number of shares meet the minimum threshold.
///
/// - Parameters:
/// - factorKey : The `factorKey` to be inserted
///
/// - Throws: `RuntimeError`, indicates invalid parameters or invalid `ThresholdKey`.
// public func patch_input_factor_key(factorKey: String) async throws {
// return try await withCheckedThrowingContinuation {
// continuation in
// self.patch_input_factor_key(factorKey: factorKey) {
// result in
// switch result {
// case let .success(result):
// continuation.resume(returning: result)
// case let .failure(error):
// continuation.resume(throwing: error)
// }
// }
// }
// }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// private func patch_input_factor_key(factorKey: String, completion: @escaping (Result<Void, Error>) -> Void) {
// tkeyQueue.async {
// do {
// var errorCode: Int32 = -1
// let cFactorKey = UnsafeMutablePointer<Int8>(mutating: (factorKey as NSString).utf8String)
// let curvePointer = UnsafeMutablePointer<Int8>(mutating: (self.curveN as NSString).utf8String)
//
// withUnsafeMutablePointer(to: &errorCode, { error in
// threshold_key_patch_input_factor_key(self.pointer, cFactorKey, curvePointer, error)
// })
// guard errorCode == 0 else {
// throw RuntimeError("Error in ThresholdKey input_factor_key \(errorCode)")
// }
// completion(.success(()))
// } catch {
// completion(.failure(error))
// }
// }
// }
/// Patch and Inserts a `ShareStore` into `ThresholdKey` using `FactorKey`, useful for insertion before reconstruction to ensure the number of shares meet the minimum threshold.
///
/// - Parameters:
/// - factorKey : The `factorKey` to be inserted
///
/// - Throws: `RuntimeError`, indicates invalid parameters or invalid `ThresholdKey`.
// public func patch_input_factor_key(factorKey: String) async throws {
// return try await withCheckedThrowingContinuation {
// continuation in
// self.patch_input_factor_key(factorKey: factorKey) {
// result in
// switch result {
// case let .success(result):
// continuation.resume(returning: result)
// case let .failure(error):
// continuation.resume(throwing: error)
// }
// }
// }
// }

guard errorCode == 0 else {
throw RuntimeError("Error in ThresholdKey get_domain_store_item")
}
let string = String(cString: result!)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is unused.

Suggested change
let string = String(cString: result!)




let auth_signatures_json = try JSONSerialization.data(withJSONObject: threshold.authSignatures)
Copy link
Collaborator

@metalurgical metalurgical Oct 18, 2023

Choose a reason for hiding this comment

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

Should do a guard on threshold.authSignatures to resolve the warning here. Same for the places.

guard errorCode == 0 else {
throw RuntimeError("Error in ThresholdKey recover_factor \(errorCode)")
}
let hashOut = String(cString: result!)
Copy link
Collaborator

@metalurgical metalurgical Oct 18, 2023

Choose a reason for hiding this comment

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

This is unused. Why not use result instead of having to call compute_hash?

Suggested change
let hashOut = String(cString: result!)

threshold.setTorusUtils(torusUtils: torusUtils)
// threshold.init
print( try threshold.get_key_details().pub_key.getPublicKey(format: .EllipticCompress))
let factorKey = "36c1728c47c84dfe855949fa76daf82f8bda801af9374f30aa4c91b7fd7a8e3b"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is unused.

Suggested change
let factorKey = "36c1728c47c84dfe855949fa76daf82f8bda801af9374f30aa4c91b7fd7a8e3b"

print( try threshold.get_key_details().pub_key.getPublicKey(format: .EllipticCompress))
let factorKey = "36c1728c47c84dfe855949fa76daf82f8bda801af9374f30aa4c91b7fd7a8e3b"
let answer = "jsanswer"
let question = "js question"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is unused.

Suggested change
let question = "js question"

print(factor)

try await threshold.input_factor_key(factorKey: factor)
try await threshold.reconstruct()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
try await threshold.reconstruct()
let _ = try await threshold.reconstruct()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants