fix(dm): resolve short pubkey prefix to full key on incoming DM

When a DM arrives with only pubkey_prefix (short hex) and the sender
is not in mc.contacts, fall back to DB prefix lookup to get the full
64-char public key. Prevents ghost contact entries and "Unknown" DM
conversations.

Also adds get_contact_by_prefix() database helper.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
MarekWo
2026-03-07 07:19:46 +01:00
parent 5b757e9548
commit 8f31c27360
2 changed files with 15 additions and 1 deletions
+9
View File
@@ -128,6 +128,15 @@ class Database:
).fetchone()
return dict(row) if row else None
def get_contact_by_prefix(self, prefix: str) -> Optional[Dict]:
"""Find a contact by public key prefix (LIKE match)."""
with self._connect() as conn:
row = conn.execute(
"SELECT * FROM contacts WHERE public_key LIKE ? AND length(public_key) = 64 LIMIT 1",
(prefix.lower() + '%',)
).fetchone()
return dict(row) if row else None
def delete_contact(self, public_key: str) -> bool:
with self._connect() as conn:
cursor = conn.execute(
+6 -1
View File
@@ -389,10 +389,15 @@ class DeviceManager:
contact = self.mc.get_contact_by_key_prefix(sender_key)
if contact:
sender_name = contact.get('name', '')
# Use the full public key from contacts (not the short prefix)
full_key = contact.get('public_key', '')
if full_key:
sender_key = full_key
elif len(sender_key) < 64:
# Prefix not resolved from in-memory contacts — try DB
db_contact = self.db.get_contact_by_prefix(sender_key)
if db_contact and len(db_contact['public_key']) == 64:
sender_key = db_contact['public_key']
sender_name = db_contact.get('name', '')
# Receiver-side dedup: skip duplicate retries
sender_ts = data.get('sender_timestamp')