This is a partial backport of core#19760 [3/4]
https://github.com/bitcoin/bitcoin/pull/19760/commits/5e8df3312e47a73e747ee892face55ed9ababeea
Some changes are debatable (e.g. assert_is_hex_string > assert_is_hash_string), but I think it is best to deviate as little as possible from the Core code for such minor points of detail.
Depends on D9573