diff options
author | Andrew Chow <achow101-github@achow101.com> | 2019-07-25 11:32:13 -0400 |
---|---|---|
committer | Andrew Chow <achow101-github@achow101.com> | 2021-12-10 08:29:47 -0500 |
commit | d3dbb16168145ccbcc7ef0a8e150695711b661b7 (patch) | |
tree | 0a14a612f8461d93a00737858f9054ce350c5fcd | |
parent | a69332fd89a5c1e293113b641fbe6b23cf279741 (diff) |
Separate individual HD Keypath serialization into separate functions
-rw-r--r-- | src/psbt.h | 48 |
1 files changed, 31 insertions, 17 deletions
diff --git a/src/psbt.h b/src/psbt.h index 36a3d2ca16..92ead2d878 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -93,6 +93,24 @@ void UnserializeFromVector(Stream& s, X&... args) } } +// Deserialize an individual HD keypath to a stream +template<typename Stream> +void DeserializeHDKeypath(Stream& s, KeyOriginInfo& hd_keypath) +{ + // Read in key path + uint64_t value_len = ReadCompactSize(s); + if (value_len % 4 || value_len == 0) { + throw std::ios_base::failure("Invalid length for HD key path"); + } + + s >> hd_keypath.fingerprint; + for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) { + uint32_t index; + s >> index; + hd_keypath.path.push_back(index); + } +} + // Deserialize HD keypaths into a map template<typename Stream> void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths) @@ -110,24 +128,24 @@ void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided"); } - // Read in key path - uint64_t value_len = ReadCompactSize(s); - if (value_len % 4 || value_len == 0) { - throw std::ios_base::failure("Invalid length for HD key path"); - } - KeyOriginInfo keypath; - s >> keypath.fingerprint; - for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) { - uint32_t index; - s >> index; - keypath.path.push_back(index); - } + DeserializeHDKeypath(s, keypath); // Add to map hd_keypaths.emplace(pubkey, std::move(keypath)); } +// Serialize an individual HD keypath to a stream +template<typename Stream> +void SerializeHDKeypath(Stream& s, KeyOriginInfo hd_keypath) +{ + WriteCompactSize(s, (hd_keypath.path.size() + 1) * sizeof(uint32_t)); + s << hd_keypath.fingerprint; + for (const auto& path : hd_keypath.path) { + s << path; + } +} + // Serialize HD keypaths to a stream from a map template<typename Stream> void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type) @@ -137,11 +155,7 @@ void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_k throw std::ios_base::failure("Invalid CPubKey being serialized"); } SerializeToVector(s, type, Span{keypath_pair.first}); - WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t)); - s << keypath_pair.second.fingerprint; - for (const auto& path : keypath_pair.second.path) { - s << path; - } + SerializeHDKeypath(s, keypath_pair.second); } } |