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

fix: delete factor logic #7

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
27 changes: 25 additions & 2 deletions Shared/TssView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,24 @@ struct TssView: View {
// get factor key from keychain if input is empty

showSpinner = true

let allPub = try await TssModule.get_all_factor_pub(threshold_key: threshold_key, tss_tag: selected_tag)
if allPub.count <= 1 {
alertContent = "Cannot delete last factor"
showAlert = true
showSpinner = false
return
}

var deleteFactorKey: String?
var deleteFactor: String?
do {
let allFactorPub = try await TssModule.get_all_factor_pub(threshold_key: threshold_key, tss_tag: selected_tag)
print(allFactorPub)
// filterout device factor
let filterFactorPub = allFactorPub.filter({ $0 != deviceFactorPub })
print(filterFactorPub)
let filterFactorPub = allFactorPub.filter({ $0 != deviceFactorPub && $0 != selectedFactorPub })

if filterFactorPub.count == 0 { throw "No suitable factor to be delete "}

deleteFactor = filterFactorPub[0]

Expand All @@ -279,11 +289,13 @@ struct TssView: View {
} catch {
alertContent = "There is no extra factor key to be deleted"
showAlert = true
showSpinner = false
return
}
guard let deleteFactorKey = deleteFactorKey else {
alertContent = "There is no extra factor key to be deleted"
showAlert = true
showSpinner = false
return
}

Expand Down Expand Up @@ -315,6 +327,15 @@ struct TssView: View {
}
Button(action: {
Task {
let allPub = try await TssModule.get_all_factor_pub(threshold_key: threshold_key, tss_tag: selected_tag)

if allPub.count <= 1 {
alertContent = "Cannot delete last factor"
showAlert = true
showSpinner = false
return
}

var deleteFactorKey: String = ""
do {
showSpinner = true
Expand All @@ -326,7 +347,9 @@ struct TssView: View {
alertContent = "factor was deleted"
showAlert = true
showSpinner = false
return
}

do {
let sigs: [String] = try signatures.map { String(decoding: try JSONSerialization.data(withJSONObject: $0), as: UTF8.self) }
try await TssModule.delete_factor_pub(threshold_key: threshold_key, tss_tag: selected_tag, factor_key: deleteFactorKey, auth_signatures: sigs, delete_factor_pub: deviceFactorPub, nodeDetails: nodeDetails!, torusUtils: torusUtils!)
Expand Down