From b7a4fa4de0d44e6ad52b4e1e0b9cfa086d10301b Mon Sep 17 00:00:00 2001 From: philon- Date: Thu, 14 Aug 2025 11:54:50 +0200 Subject: [PATCH] Refactor PKI mismatch logic --- packages/web/src/core/subscriptions.ts | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/web/src/core/subscriptions.ts b/packages/web/src/core/subscriptions.ts index 893e1dc1..4c1791f0 100644 --- a/packages/web/src/core/subscriptions.ts +++ b/packages/web/src/core/subscriptions.ts @@ -74,19 +74,22 @@ export const subscribeAll = ( connection.events.onNodeInfoPacket.subscribe((nodeInfo) => { const nodeWithUser = ensureDefaultUser(nodeInfo); - if ( - nodeWithUser.num !== myNodeNum && // Ignore my own node - nodeDB.getNode(nodeWithUser.num)?.user?.publicKey !== undefined && // Only flag if the old key is not undefined - fromByteArray( - nodeDB.getNode(nodeWithUser.num)?.user?.publicKey ?? new Uint8Array(), // ... and equal to the new key - ) !== fromByteArray(nodeWithUser.user?.publicKey ?? new Uint8Array()) - ) { - console.warn( - `Node ${nodeWithUser.num} has a different public key than expected`, + if (nodeWithUser.num !== myNodeNum && nodeDB.getNode(nodeWithUser.num)) { + const oldPublicKey = fromByteArray( + nodeDB.getNode(nodeWithUser.num)?.user?.publicKey ?? new Uint8Array(), ); - nodeDB.setNodeError(nodeWithUser.num, "MISMATCH_PKI"); + const newPublicKey = fromByteArray( + nodeWithUser.user?.publicKey ?? new Uint8Array(), + ); + + if (oldPublicKey !== newPublicKey) { + console.warn( + `Node ${nodeWithUser.user?.longName} (${nodeWithUser.num}) has a different public key than expected: Expected ${oldPublicKey} but got ${newPublicKey}`, + ); + nodeDB.setNodeError(nodeWithUser.num, "MISMATCH_PKI"); - // TODO: Handle this error case properly (refactor PKI dialog?) + // TODO: Handle this error case properly (refactor PKI dialog?) + } } nodeDB.addNode(nodeWithUser);