Imported_KeyStore: private keys rejected as "not implemented type" are still persisted in the keystore
Where: electrum/keystore.py, Imported_KeyStore.import_privkey() /
import_private_keys() (around lines 291-317).
What happens:
import_privkey() deserializes the WIF key, encrypts it and immediately writes
it into self.keypairs[pubkey]. Only afterwards import_private_keys() checks
the script type and rejects anything that is not p2pkh / p2wpkh /
p2wpkh-p2sh, appending it to bad_keys and reporting it to the user as
"not implemented type".
But the key has already been added to self.keypairs and is never removed.
Abstract_Wallet.import_private_keys() then calls self.save_keystore(),
so the rejected key ends up encrypted in the wallet file.
Reproduce:
- Create an "Import Bitcoin addresses or private keys" wallet.
- Import a WIF with a script-type prefix that
deserialize_privkeyaccepts but the keystore does not support, e.g.p2wsh:<WIF>(prefixesp2sh,p2wsh,p2wsh-p2share all valid perbitcoin.WIF_SCRIPT_TYPES). - UI reports the key as rejected ("not implemented type: p2wsh").
- Inspect the wallet file: the key is present in
keystore.keypairs.
Why it matters:
- The user is told the key was not imported, yet the secret is now stored on disk. That contradicts the UI and is surprising for anything involving keys.
- There is no address associated with the entry, so it cannot be deleted through the normal "delete address" path; it is orphaned.
- On password change the orphaned key is silently re-encrypted along with the real ones.
Suggested fix:
Validate txin_type in import_privkey() before touching self.keypairs
(raise so the caller lands in the existing bad_keys branch), or move the
script-type check ahead of the store in import_private_keys(). Either way
nothing should be written to self.keypairs for a key that is reported as
rejected. Happy to open a PR if the maintainers agree on the approach.
Source: spesmilo/electrum