mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
fix: Handle node public key mismatch and show warning (#1720)
* Handle node public key mismatch and show warning - Add a mismatchKey flag to Node and MessageTopBar to indicate a public key mismatch. - Set the public key to a default error value (all zeros) when a node's public key changes. - Display a warning in the MessageTopBar when a key mismatch is detected in PKC. - Only clear all nodes when a different mynode number is present. * feat: Add key mismatch detection to NodeInfoDao This commit introduces a new feature to the `NodeInfoDao` that detects and handles public key mismatches for existing nodes. - A new function `upsertCheckKeyMatch` is added to `NodeInfoDao` that checks for public key changes when upserting a node. If a mismatch is detected, the public key is set to `ERROR_BYTE_STRING`, and a warning is logged. - The function `upsertCheckKeyMatch` is used instead of `upsert` in `NodeRepository` and in `putAll` inside of `NodeInfoDao`. - A new test `testPkcMismatch` is added to `NodeInfoDaoTest` to verify the key mismatch detection. - Changed `testNodes` to have unique public keys. - Added `mismatchKey` state to the node model. * detekt spacing * Refactor: Correctly handle different node installations in NodeRepository The logic for detecting different node installations in `NodeRepository.kt` was inverted, this commit fixes the logic to use `!=` instead of `==` to detect if the node number has changed.
This commit is contained in:
parent
53c240198c
commit
a28dc377ae
6 changed files with 47 additions and 12 deletions
|
|
@ -103,13 +103,16 @@ class NodeRepository @Inject constructor(
|
|||
).mapLatest { list -> list.map { it.toModel() } }.flowOn(dispatchers.io).conflate()
|
||||
|
||||
suspend fun upsert(node: NodeEntity) = withContext(dispatchers.io) {
|
||||
nodeInfoDao.upsert(node)
|
||||
nodeInfoDao.upsertCheckKeyMatch(node)
|
||||
}
|
||||
|
||||
suspend fun installNodeDB(mi: MyNodeEntity, nodes: List<NodeEntity>) = withContext(dispatchers.io) {
|
||||
val isDifferentNode = myNodeInfo.value?.myNodeNum != mi.myNodeNum
|
||||
nodeInfoDao.clearMyNodeInfo()
|
||||
nodeInfoDao.setMyNodeInfo(mi) // set MyNodeEntity first
|
||||
nodeInfoDao.clearNodeInfo()
|
||||
if (isDifferentNode) {
|
||||
nodeInfoDao.clearNodeInfo()
|
||||
}
|
||||
nodeInfoDao.putAll(nodes)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
package com.geeksville.mesh.database.dao
|
||||
|
||||
import android.util.Log
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.MapColumn
|
||||
|
|
@ -26,10 +27,11 @@ import androidx.room.Transaction
|
|||
import androidx.room.Upsert
|
||||
import com.geeksville.mesh.database.entity.MetadataEntity
|
||||
import com.geeksville.mesh.database.entity.MyNodeEntity
|
||||
import com.geeksville.mesh.database.entity.NodeWithRelations
|
||||
import com.geeksville.mesh.database.entity.NodeEntity
|
||||
import com.geeksville.mesh.database.entity.NodeWithRelations
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
private const val TAG = "NodeInfoDao"
|
||||
@Suppress("TooManyFunctions")
|
||||
@Dao
|
||||
interface NodeInfoDao {
|
||||
|
|
@ -108,8 +110,20 @@ interface NodeInfoDao {
|
|||
@Upsert
|
||||
fun upsert(node: NodeEntity)
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun putAll(nodes: List<NodeEntity>)
|
||||
fun upsertCheckKeyMatch(node: NodeEntity) {
|
||||
val existingNode = getNodeByNum(node.num)
|
||||
if (existingNode != null && existingNode.user.publicKey != node.user.publicKey) {
|
||||
Log.w(TAG, "Node ${node.num} has changed its public key")
|
||||
val user =
|
||||
node.user.toBuilder().setPublicKey(NodeEntity.ERROR_BYTE_STRING).build()
|
||||
node.user = user
|
||||
}
|
||||
upsert(node)
|
||||
}
|
||||
@Transaction
|
||||
fun putAll(nodes: List<NodeEntity>) {
|
||||
nodes.forEach { upsertCheckKeyMatch(it) }
|
||||
}
|
||||
|
||||
@Query("DELETE FROM nodes")
|
||||
fun clearNodeInfo()
|
||||
|
|
@ -122,4 +136,7 @@ interface NodeInfoDao {
|
|||
|
||||
@Query("DELETE FROM metadata WHERE num=:num")
|
||||
fun deleteMetadata(num: Int)
|
||||
|
||||
@Query("SELECT * FROM nodes WHERE num = :num")
|
||||
fun getNodeByNum(num: Int): NodeEntity?
|
||||
}
|
||||
|
|
|
|||
|
|
@ -151,7 +151,7 @@ data class NodeEntity(
|
|||
|
||||
val isUnknownUser get() = user.hwModel == MeshProtos.HardwareModel.UNSET
|
||||
val hasPKC get() = !user.publicKey.isEmpty
|
||||
val errorByteString: ByteString get() = ByteString.copyFrom(ByteArray(32) { 0 })
|
||||
val errorByteString: ByteString get() = ERROR_BYTE_STRING
|
||||
|
||||
fun setPosition(p: MeshProtos.Position, defaultTime: Int = currentTime()) {
|
||||
position = p.copy { time = if (p.time != 0) p.time else defaultTime }
|
||||
|
|
@ -174,6 +174,7 @@ data class NodeEntity(
|
|||
fun degD(i: Int) = i * 1e-7
|
||||
fun degI(d: Double) = (d * 1e7).toInt()
|
||||
|
||||
val ERROR_BYTE_STRING: ByteString = ByteString.copyFrom(ByteArray(32) { 0 })
|
||||
fun currentTime() = (System.currentTimeMillis() / 1000).toInt()
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue