From b5a503c0cbb2658c121aa6161aeb88c34314d7fe Mon Sep 17 00:00:00 2001
From: James Rich <2199651+jamesarich@users.noreply.github.com>
Date: Fri, 20 Jun 2025 18:47:52 +0000
Subject: [PATCH] fix: clear public key when private key edited (#2190)
---
.../components/SecurityConfigItemList.kt | 14 +++++++++++++-
app/src/main/res/values/strings.xml | 2 +-
2 files changed, 14 insertions(+), 2 deletions(-)
diff --git a/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt b/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt
index c3982b3c9..8dd41ffd4 100644
--- a/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt
+++ b/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt
@@ -34,6 +34,7 @@ import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
+import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
@@ -59,6 +60,7 @@ import com.geeksville.mesh.ui.common.components.SwitchPreference
import com.geeksville.mesh.ui.node.NodeActionButton
import com.geeksville.mesh.ui.radioconfig.RadioConfigViewModel
import com.geeksville.mesh.util.encodeToString
+import com.geeksville.mesh.util.toByteString
import com.google.protobuf.ByteString
import java.security.SecureRandom
@@ -103,6 +105,15 @@ fun SecurityConfigItemList(
val focusManager = LocalFocusManager.current
var securityInput by rememberSaveable { mutableStateOf(securityConfig) }
+ var publicKey by rememberSaveable { mutableStateOf(securityInput.publicKey) }
+ LaunchedEffect(securityInput.privateKey) {
+ if (securityInput.privateKey != securityConfig.privateKey) {
+ publicKey = "".toByteString()
+ } else if (securityInput.privateKey == securityConfig.privateKey) {
+ publicKey = securityConfig.publicKey
+ }
+ }
+
val exportConfigLauncher = rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult()
) {
@@ -157,7 +168,7 @@ fun SecurityConfigItemList(
item {
EditBase64Preference(
title = stringResource(R.string.public_key),
- value = securityInput.publicKey,
+ value = publicKey,
enabled = enabled,
readOnly = true,
keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
@@ -314,6 +325,7 @@ fun PrivateKeyRegenerateDialog(
onClick = {
securityInput = securityInput.copy {
clearPrivateKey()
+ clearPublicKey()
// Generate a random "f" value
val f = ByteArray(32).apply {
SecureRandom().nextBytes(this)
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index c7d2d71cc..192be9e64 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -689,7 +689,7 @@
Client Notification
Compromised keys detected, select OK to regenerate.
Regenerate Private Key
- Are you sure you want to regenerate your Private Key?
+ Are you sure you want to regenerate your Private Key?\n\nNodes that may have previously exchanged keys with this node will need to Remove that node and re-exchange keys in order to resume secure communication.
Export Keys
Exports public and private keys to a file. Please store somewhere securely.