Skip to content

wallet: EncryptWallet derives the new keypool before VACUUM, leaving deleted plaintext keys unvacuumed for ~30 s #285

Closed

Description

@kiwidream

Existing issue search

  • I have searched the existing issues.

Summary

CWallet::EncryptWallet commits the encryption, which deletes the plaintext key records. It then derives a full P2MR keypool for the new seed, and only after that runs GetDatabase().Rewrite() (VACUUM).

With the default keypool this gap is about 30 s. A crash or kill during it leaves the deleted plaintext seed and PQC key records in the database's free pages permanently.

Current behaviour

The order in wallet.cpp#L1211-L1233 is:

  1. commit the encryption;
  2. Unlock;
  3. SetupDescriptorScriptPubKeyMans(use_create_keypool_warmup=false), which runs 1000 SLH-DSA key generations per new descriptor;
  4. Rewrite().

Measured on regtest with -keypool=1000 -debug=walletdb, the encryption COMMIT came 31.7 s before the VACUUM. The encryptwallet RPC took 32 s.

Release builds use the SQLite from depends, built without SQLITE_SECURE_DELETE, so deleted cells remain until a VACUUM. The only other Rewrite() call is the legacy NEED_REWRITE load path, so nothing runs the VACUUM after a crash.

Bitcoin Core v30.2 uses the same order, but its EC keypool derivation takes about 1–2 s.

Expected behaviour

Plaintext key material should not stay in an encrypted wallet's file, and a crash should not be able to make that permanent.

Suggested fix

Run Rewrite() immediately after the encryption commit, before setting up the new descriptors. Alternatively, enable PRAGMA secure_delete for the encryption transaction, or persist a "needs vacuum" marker and act on it at the next load.

Version

1.x.x at e374ba24cf; v1.0.0 uses the same order.

Activity

  1. added
    bug
    on Nov 3, 2026
  2. kiwidream commented on Nov 11, 2026

    @kiwidream
    MemberAuthor

    Fixed on 1.x.x by #300 (ea571b86f5), which ships in v1.0.1: EncryptWallet now rewrites the wallet file right after the encryption commit, before it derives the new keypool, and a postencryptionrewrite record makes the next load retry a rewrite that did not finish.

    Closing by hand, because a merge into 1.x.x does not close issues automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions