@@ -97,22 +97,30 @@ void UnserializeFromVector(Stream& s, X&... args)
97
97
}
98
98
}
99
99
100
- // Deserialize an individual HD keypath to a stream
100
+ // Deserialize bytes of given length from the stream as a KeyOriginInfo
101
101
template <typename Stream>
102
- void DeserializeHDKeypath (Stream& s, KeyOriginInfo& hd_keypath )
102
+ KeyOriginInfo DeserializeKeyOrigin (Stream& s, uint64_t length )
103
103
{
104
104
// 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 ) {
107
106
throw std::ios_base::failure (" Invalid length for HD key path" );
108
107
}
109
108
109
+ KeyOriginInfo hd_keypath;
110
110
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 )) {
112
112
uint32_t index;
113
113
s >> index;
114
114
hd_keypath.path .push_back (index);
115
115
}
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));
116
124
}
117
125
118
126
// Deserialize HD keypaths into a map
@@ -139,17 +147,24 @@ void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std
139
147
hd_keypaths.emplace (pubkey, std::move (keypath));
140
148
}
141
149
142
- // Serialize an individual HD keypath to a stream
150
+ // Serialize a KeyOriginInfo to a stream
143
151
template <typename Stream>
144
- void SerializeHDKeypath (Stream& s, KeyOriginInfo hd_keypath)
152
+ void SerializeKeyOrigin (Stream& s, KeyOriginInfo hd_keypath)
145
153
{
146
- WriteCompactSize (s, (hd_keypath.path .size () + 1 ) * sizeof (uint32_t ));
147
154
s << hd_keypath.fingerprint ;
148
155
for (const auto & path : hd_keypath.path ) {
149
156
s << path;
150
157
}
151
158
}
152
159
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
+
153
168
// Serialize HD keypaths to a stream from a map
154
169
template <typename Stream>
155
170
void SerializeHDKeypaths (Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
0 commit comments