This PR is based on #22787 ("refactor: actual immutable pointing"), which should be reviewed first. (merged by now)
It aims to make the CWallet shared pointers actually immutable also for the dumpprivkey and dumpwallet RPC methods. For doing that, some more preparations are needed; we need a const-counterpart to the helper EnsureLegacyScriptPubKeyMan that accepts a const CWallet pointer and accordingly also returns a const LegacyScriptPubKeyMan instance. The metadata lookup in dumpwallet is changed to not need a mutable ScriptPubKeyMan instance by avoiding using the operator[] in its mapKeyMetadata map, which also avoids repeated lookups.