mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-07 05:31:03 +02:00
Separate individual HD Keypath serialization into separate functions
This commit is contained in:
parent
a69332fd89
commit
d3dbb16168
48
src/psbt.h
48
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
|
// Deserialize HD keypaths into a map
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
|
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");
|
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;
|
KeyOriginInfo keypath;
|
||||||
s >> keypath.fingerprint;
|
DeserializeHDKeypath(s, keypath);
|
||||||
for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
|
|
||||||
uint32_t index;
|
|
||||||
s >> index;
|
|
||||||
keypath.path.push_back(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add to map
|
// Add to map
|
||||||
hd_keypaths.emplace(pubkey, std::move(keypath));
|
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
|
// Serialize HD keypaths to a stream from a map
|
||||||
template<typename Stream>
|
template<typename Stream>
|
||||||
void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
|
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");
|
throw std::ios_base::failure("Invalid CPubKey being serialized");
|
||||||
}
|
}
|
||||||
SerializeToVector(s, type, Span{keypath_pair.first});
|
SerializeToVector(s, type, Span{keypath_pair.first});
|
||||||
WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
|
SerializeHDKeypath(s, keypath_pair.second);
|
||||||
s << keypath_pair.second.fingerprint;
|
|
||||||
for (const auto& path : keypath_pair.second.path) {
|
|
||||||
s << path;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user