Skip to content

Commit ce91120

Browse files
committed
Move individual KeyOriginInfo de/ser to separate function
To make it easier to de/serialize individual KeyOriginInfo for PSBTs, separate the actual de/serialization of KeyOriginInfo to its own function. This is an additional separation where any length prefix is processed by the caller.
1 parent 2111f32 commit ce91120

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/psbt.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,30 @@ void UnserializeFromVector(Stream& s, X&... args)
9797
}
9898
}
9999

100-
// Deserialize an individual HD keypath to a stream
100+
// Deserialize bytes of given length from the stream as a KeyOriginInfo
101101
template<typename Stream>
102-
void DeserializeHDKeypath(Stream& s, KeyOriginInfo& hd_keypath)
102+
KeyOriginInfo DeserializeKeyOrigin(Stream& s, uint64_t length)
103103
{
104104
// Read in key path
105-
uint64_t value_len = ReadCompactSize(s);
106-
if (value_len % 4 || value_len == 0) {
105+
if (length % 4 || length == 0) {
107106
throw std::ios_base::failure("Invalid length for HD key path");
108107
}
109108

109+
KeyOriginInfo hd_keypath;
110110
s >> hd_keypath.fingerprint;
111-
for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
111+
for (unsigned int i = 4; i < length; i += sizeof(uint32_t)) {
112112
uint32_t index;
113113
s >> index;
114114
hd_keypath.path.push_back(index);
115115
}
116+
return hd_keypath;
117+
}
118+
119+
// Deserialize a length prefixed KeyOriginInfo from a stream
120+
template<typename Stream>
121+
void DeserializeHDKeypath(Stream& s, KeyOriginInfo& hd_keypath)
122+
{
123+
hd_keypath = DeserializeKeyOrigin(s, ReadCompactSize(s));
116124
}
117125

118126
// Deserialize HD keypaths into a map
@@ -139,17 +147,24 @@ void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std
139147
hd_keypaths.emplace(pubkey, std::move(keypath));
140148
}
141149

142-
// Serialize an individual HD keypath to a stream
150+
// Serialize a KeyOriginInfo to a stream
143151
template<typename Stream>
144-
void SerializeHDKeypath(Stream& s, KeyOriginInfo hd_keypath)
152+
void SerializeKeyOrigin(Stream& s, KeyOriginInfo hd_keypath)
145153
{
146-
WriteCompactSize(s, (hd_keypath.path.size() + 1) * sizeof(uint32_t));
147154
s << hd_keypath.fingerprint;
148155
for (const auto& path : hd_keypath.path) {
149156
s << path;
150157
}
151158
}
152159

160+
// Serialize a length prefixed KeyOriginInfo to a stream
161+
template<typename Stream>
162+
void SerializeHDKeypath(Stream& s, KeyOriginInfo hd_keypath)
163+
{
164+
WriteCompactSize(s, (hd_keypath.path.size() + 1) * sizeof(uint32_t));
165+
SerializeKeyOrigin(s, hd_keypath);
166+
}
167+
153168
// Serialize HD keypaths to a stream from a map
154169
template<typename Stream>
155170
void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)

0 commit comments

Comments
 (0)