Skip to content

Commit 5fdaf6a

Browse files
committed
moveonly: Move (Un)Serialize(To/From)Vector, (De)SerializeHDKeypaths to psbt module
SerializeToVector, UnserializeFromVector, DeserializeHDKeypaths, and SerializeHDKeypaths were in sign.h where PSBT was originally implemented. Since all of the PSBT serialization has moved to its own file, these functions should follow.
1 parent 94065cc commit 5fdaf6a

File tree

5 files changed

+80
-78
lines changed

5 files changed

+80
-78
lines changed

src/psbt.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
#include <policy/feerate.h>
1111
#include <primitives/transaction.h>
1212
#include <pubkey.h>
13+
#include <script/keyorigin.h>
1314
#include <script/sign.h>
1415
#include <script/signingprovider.h>
16+
#include <span.h>
17+
#include <streams.h>
1518

1619
#include <optional>
1720

@@ -68,6 +71,80 @@ struct PSBTProprietary
6871
}
6972
};
7073

74+
// Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
75+
// The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
76+
template<typename Stream, typename... X>
77+
void SerializeToVector(Stream& s, const X&... args)
78+
{
79+
WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
80+
SerializeMany(s, args...);
81+
}
82+
83+
// Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
84+
template<typename Stream, typename... X>
85+
void UnserializeFromVector(Stream& s, X&... args)
86+
{
87+
size_t expected_size = ReadCompactSize(s);
88+
size_t remaining_before = s.size();
89+
UnserializeMany(s, args...);
90+
size_t remaining_after = s.size();
91+
if (remaining_after + expected_size != remaining_before) {
92+
throw std::ios_base::failure("Size of value was not the stated size");
93+
}
94+
}
95+
96+
// Deserialize HD keypaths into a map
97+
template<typename Stream>
98+
void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
99+
{
100+
// Make sure that the key is the size of pubkey + 1
101+
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
102+
throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
103+
}
104+
// Read in the pubkey from key
105+
CPubKey pubkey(key.begin() + 1, key.end());
106+
if (!pubkey.IsFullyValid()) {
107+
throw std::ios_base::failure("Invalid pubkey");
108+
}
109+
if (hd_keypaths.count(pubkey) > 0) {
110+
throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
111+
}
112+
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+
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+
}
126+
127+
// Add to map
128+
hd_keypaths.emplace(pubkey, std::move(keypath));
129+
}
130+
131+
// Serialize HD keypaths to a stream from a map
132+
template<typename Stream>
133+
void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
134+
{
135+
for (auto keypath_pair : hd_keypaths) {
136+
if (!keypath_pair.first.IsValid()) {
137+
throw std::ios_base::failure("Invalid CPubKey being serialized");
138+
}
139+
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+
}
145+
}
146+
}
147+
71148
/** A structure for PSBTs which contain per-input information */
72149
struct PSBTInput
73150
{

src/script/sign.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <key.h>
1010
#include <policy/policy.h>
1111
#include <primitives/transaction.h>
12+
#include <script/keyorigin.h>
1213
#include <script/signingprovider.h>
1314
#include <script/standard.h>
1415
#include <uint256.h>

src/script/sign.h

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <script/interpreter.h>
1313
#include <script/keyorigin.h>
1414
#include <script/standard.h>
15-
#include <span.h>
16-
#include <streams.h>
1715

1816
class CKey;
1917
class CKeyID;
@@ -84,80 +82,6 @@ struct SignatureData {
8482
void MergeSignatureData(SignatureData sigdata);
8583
};
8684

87-
// Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
88-
// The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
89-
template<typename Stream, typename... X>
90-
void SerializeToVector(Stream& s, const X&... args)
91-
{
92-
WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
93-
SerializeMany(s, args...);
94-
}
95-
96-
// Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
97-
template<typename Stream, typename... X>
98-
void UnserializeFromVector(Stream& s, X&... args)
99-
{
100-
size_t expected_size = ReadCompactSize(s);
101-
size_t remaining_before = s.size();
102-
UnserializeMany(s, args...);
103-
size_t remaining_after = s.size();
104-
if (remaining_after + expected_size != remaining_before) {
105-
throw std::ios_base::failure("Size of value was not the stated size");
106-
}
107-
}
108-
109-
// Deserialize HD keypaths into a map
110-
template<typename Stream>
111-
void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
112-
{
113-
// Make sure that the key is the size of pubkey + 1
114-
if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
115-
throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
116-
}
117-
// Read in the pubkey from key
118-
CPubKey pubkey(key.begin() + 1, key.end());
119-
if (!pubkey.IsFullyValid()) {
120-
throw std::ios_base::failure("Invalid pubkey");
121-
}
122-
if (hd_keypaths.count(pubkey) > 0) {
123-
throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
124-
}
125-
126-
// Read in key path
127-
uint64_t value_len = ReadCompactSize(s);
128-
if (value_len % 4 || value_len == 0) {
129-
throw std::ios_base::failure("Invalid length for HD key path");
130-
}
131-
132-
KeyOriginInfo keypath;
133-
s >> keypath.fingerprint;
134-
for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
135-
uint32_t index;
136-
s >> index;
137-
keypath.path.push_back(index);
138-
}
139-
140-
// Add to map
141-
hd_keypaths.emplace(pubkey, std::move(keypath));
142-
}
143-
144-
// Serialize HD keypaths to a stream from a map
145-
template<typename Stream>
146-
void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
147-
{
148-
for (auto keypath_pair : hd_keypaths) {
149-
if (!keypath_pair.first.IsValid()) {
150-
throw std::ios_base::failure("Invalid CPubKey being serialized");
151-
}
152-
SerializeToVector(s, type, Span{keypath_pair.first});
153-
WriteCompactSize(s, (keypath_pair.second.path.size() + 1) * sizeof(uint32_t));
154-
s << keypath_pair.second.fingerprint;
155-
for (const auto& path : keypath_pair.second.path) {
156-
s << path;
157-
}
158-
}
159-
}
160-
16185
/** Produce a script signature using a generic signature creator. */
16286
bool ProduceSignature(const SigningProvider& provider, const BaseSignatureCreator& creator, const CScript& scriptPubKey, SignatureData& sigdata);
16387

src/script/signingprovider.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88

99
#include <key.h>
1010
#include <pubkey.h>
11+
#include <script/keyorigin.h>
1112
#include <script/script.h>
1213
#include <script/standard.h>
1314
#include <sync.h>
1415

15-
struct KeyOriginInfo;
16-
1716
/** An interface to be implemented by keystores that support signing. */
1817
class SigningProvider
1918
{

src/test/fuzz/script_sign.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <chainparams.h>
66
#include <chainparamsbase.h>
77
#include <key.h>
8+
#include <psbt.h>
89
#include <pubkey.h>
910
#include <script/keyorigin.h>
1011
#include <script/sign.h>

0 commit comments

Comments
 (0)