@@ -93,6 +93,24 @@ void UnserializeFromVector(Stream& s, X&... args)
93
93
}
94
94
}
95
95
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
+
96
114
// Deserialize HD keypaths into a map
97
115
template <typename Stream>
98
116
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
110
128
throw std::ios_base::failure (" Duplicate Key, pubkey derivation path already provided" );
111
129
}
112
130
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
-
119
131
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);
126
133
127
134
// Add to map
128
135
hd_keypaths.emplace (pubkey, std::move (keypath));
129
136
}
130
137
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
+
131
149
// Serialize HD keypaths to a stream from a map
132
150
template <typename Stream>
133
151
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
137
155
throw std::ios_base::failure (" Invalid CPubKey being serialized" );
138
156
}
139
157
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 );
145
159
}
146
160
}
147
161
0 commit comments