In several places in the wallet DB module, byte strings containing serialized public keys and secret keys are created in order to be hashed. To avoid sensitive data lingering in memory (and potentially leaking), ensure that these hash preimages are cleared out by using don’t store the preimage, but hash both public key and secret key in one shot, using the overloaded secure_allocator for the std::vector<unsigned char> instances.Hash function:
https://github.com/bitcoin/bitcoin/blob/d198635fa2d48b7618789aedf112783935015d77/src/hash.h#L82-L88
See e.g. #31166 and #31774 for similarly themed PRs (Note that in #31166 we used the explicit memory_cleanse approach though, as changing the allocator was not possible.)
The first commit contains the minimal-diff fix. The second commit is a code deduplication refactor; it could be dropped and picked up in a separate PR if preferred (there may be some bikeshedding about naming and style).