Tunnel creation: Compute public key from private key as the text is being edited
This commit is contained in:
parent
91ea0fe7e5
commit
423fc4f9e0
|
@ -27,6 +27,8 @@ class TunnelViewModel {
|
|||
case deletePeer = "Delete peer"
|
||||
}
|
||||
|
||||
static let keyLengthInBase64 = 44
|
||||
|
||||
class InterfaceData {
|
||||
var scratchpad: [InterfaceField: String] = [:]
|
||||
var fieldsWithError: Set<InterfaceField> = []
|
||||
|
@ -53,6 +55,16 @@ class TunnelViewModel {
|
|||
} else {
|
||||
scratchpad[field] = stringValue
|
||||
}
|
||||
if (field == .privateKey) {
|
||||
if (stringValue.count == TunnelViewModel.keyLengthInBase64),
|
||||
let privateKey = Data(base64Encoded: stringValue),
|
||||
privateKey.count == 32 {
|
||||
let publicKey = Curve25519.generatePublicKey(fromPrivateKey: privateKey)
|
||||
scratchpad[.publicKey] = publicKey.base64EncodedString()
|
||||
} else {
|
||||
scratchpad.removeValue(forKey: .publicKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -160,6 +160,17 @@ extension TunnelEditTableViewController {
|
|||
cell.onValueChanged = { [weak interfaceData] value in
|
||||
interfaceData?[field] = value
|
||||
}
|
||||
// Compute public key live
|
||||
if (field == .privateKey) {
|
||||
cell.onValueBeingEdited = { [weak self, weak interfaceData] value in
|
||||
if let interfaceData = interfaceData, let s = self {
|
||||
interfaceData[.privateKey] = value
|
||||
if let row = s.interfaceFieldsBySection[section].firstIndex(of: .publicKey) {
|
||||
s.tableView.reloadRows(at: [IndexPath(row: row, section: section)], with: .none)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return cell
|
||||
}
|
||||
} else if ((numberOfPeers > 0) && (section < (numberOfInterfaceSections + numberOfPeers * numberOfPeerSections))) {
|
||||
|
@ -289,6 +300,7 @@ class TunnelsEditTableViewKeyValueCell: UITableViewCell {
|
|||
}
|
||||
|
||||
var onValueChanged: ((String) -> Void)? = nil
|
||||
var onValueBeingEdited: ((String) -> Void)? = nil
|
||||
|
||||
let keyLabel: UILabel
|
||||
let valueTextField: UITextField
|
||||
|
@ -352,6 +364,13 @@ extension TunnelsEditTableViewKeyValueCell: UITextFieldDelegate {
|
|||
onValueChanged(textField.text ?? "")
|
||||
}
|
||||
}
|
||||
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
||||
if let onValueBeingEdited = onValueBeingEdited {
|
||||
let modifiedText = ((textField.text ?? "") as NSString).replacingCharacters(in: range, with: string)
|
||||
onValueBeingEdited(modifiedText)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
class TunnelsEditTableViewButtonCell: UITableViewCell {
|
||||
|
|
Loading…
Reference in New Issue