Refactor keymetadata writing to a separate method
Summary:
Partial backport of Core PR14021
https://github.com/bitcoin/bitcoin/pull/14021/commits/c45415f73a095665a8ff6172b57eb818cf572547
Test Plan:
ninja ninja check ninja check-functional
Reviewers: O1 Bitcoin ABC, #bitcoin_abc, Fabien
Reviewed By: O1 Bitcoin ABC, #bitcoin_abc, Fabien
Subscribers: Fabien
Differential Revision: https://reviews.bitcoinabc.org/D6004