wallet: decrypt_message: nicer error msg if pubkey is unrelated
What changed, and why it matters
This commit improves the error handling in Electrum's message decryption feature. Previously, if a user tried to decrypt a message using a public key that did not belong to their wallet, the code could proceed and likely fail with a confusing internal error. Now it explicitly checks whether the public key is wallet-related and shows a clear, user-friendly error message ('Pubkey unrelated to wallet.'). This is a defensive hardening change, not a fix for an active security vulnerability.
No urgent action required. This is a minor hardening improvement. Users and integrators may benefit from the clearer error message during normal operation. Review whether similar input validation is needed in related wallet operations, such as sign_message or other keystore methods accepting key identifiers.
Security signals we found
Input validation added for public key before cryptographic operation
User-facing exception replaces potential internal failure
Defensive check prevents use of unrelated key material
Evidence from the diff
In Abstract_Wallet.decrypt_message(), the patch adds explicit validation before calling keystore.decrypt_message(). For Imported_Wallet, it asserts the keystore type and checks that the supplied pubkey exists in self.keystore.keypairs. For other wallet types, it checks that pubkeys_to_address() returns an address for which get_address_index() finds an index. In both cases, a UserFacingException is raised with a localized message if the pubkey is unrelated. This prevents passing invalid addr_index values into the keystore’s decrypt_message method and replaces a likely downstream exception with a controlled, informative error.
Changed components
electrum/wallet.pyAbstract_Wallet.decrypt_message()Imported_Wallet message decryption pathStandard wallet message decryption pathInspect captured patch +5 / −0
diff --git a/electrum/wallet.py b/electrum/wallet.py
index 1a72de1..55d8a73 100644
--- a/electrum/wallet.py
+++ b/electrum/wallet.py
@@ -3289,9 +3289,14 @@ class Abstract_Wallet(ABC, Logger, EventListener):
if isinstance(self, Imported_Wallet):
# this branch is significantly faster. Imported_Wallet.pubkeys_to_address is slow.
addr_index = pubkey
+ assert isinstance(self.keystore, keystore.Imported_KeyStore)
+ if pubkey not in self.keystore.keypairs:
+ raise UserFacingException(_("Pubkey unrelated to wallet."))
else:
addr = self.pubkeys_to_address([pubkey]) # note: broken for multisig
addr_index = self.get_address_index(addr)
+ if addr_index is None:
+ raise UserFacingException(_("Pubkey unrelated to wallet."))
return self.keystore.decrypt_message(addr_index, message, password)
@classmethod
Why this scored 22/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.