Skip to content

Commit d3dbb16

Browse files
committed
Separate individual HD Keypath serialization into separate functions
1 parent a69332f commit d3dbb16

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/psbt.h

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ void UnserializeFromVector(Stream& s, X&... args)
9393
}
9494
}
9595

96+
// Deserialize an individual HD keypath to a stream
97+
template<typename Stream>
98+
void DeserializeHDKeypath(Stream& s, KeyOriginInfo& hd_keypath)
99+
{
100+
// Read in key path
101+
uint64_t value_len = ReadCompactSize(s);
102+
if (value_len % 4 || value_len == 0) {
103+
throw std::ios_base::failure("Invalid length for HD key path");
104+
}
105+
106+
s >> hd_keypath.fingerprint;
107+
for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
108+
uint32_t index;
109+
s >> index;
110+
hd_keypath.path.push_back(index);
111+
}
112+
}
113+
96114
// Deserialize HD keypaths into a map
97115
template<typename Stream>
98116
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
110128
throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
111129
}
112130

113-
// Read in key path
114-
uint64_t value_len = ReadCompactSize(s);
115-
if (value_len % 4 || value_len == 0) {
116-
throw std::ios_base::failure("Invalid length for HD key path");
117-
}
118-
119131
KeyOriginInfo keypath;
120-
s >> keypath.fingerprint;
121-
for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
122-
uint32_t index;
123-
s >> index;
124-
keypath.path.push_back(index);
125-
}
132+
DeserializeHDKeypath(s, keypath);
126133

127134
// Add to map
128135
hd_keypaths.emplace(pubkey, std::move(keypath));
129136
}
130137

138+
// Serialize an individual HD keypath to a stream
139+
template<typename Stream>
140+
void SerializeHDKeypath(Stream& s, KeyOriginInfo hd_keypath)
141+
{
142+
WriteCompactSize(s, (hd_keypath.path.size() + 1) * sizeof(uint32_t));
143+
s << hd_keypath.fingerprint;
144+
for (const auto& path : hd_keypath.path) {
145+
s << path;
146+
}
147+
}
148+
131149
// Serialize HD keypaths to a stream from a map
132150
template<typename Stream>
133151
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
137155
throw std::ios_base::failure("Invalid CPubKey being serialized");
138156
}
139157
SerializeToVector(s, type, Span{keypath_pair.first});
140-
WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
141-
s << keypath_pair.second.fingerprint;
142-
for (const auto& path : keypath_pair.second.path) {
143-
s << path;
144-
}
158+
SerializeHDKeypath(s, keypath_pair.second);
145159
}
146160
}
147161

0 commit comments

Comments
 (0)