Skip to content

Commit 3235847

Browse files
committed
Types are compact size uints
1 parent 65b49f6 commit 3235847

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

src/psbt.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,52 +69,52 @@ struct PSBTInput
6969
inline void Serialize(Stream& s) const {
7070
// Write the utxo
7171
if (non_witness_utxo) {
72-
SerializeToVector(s, PSBT_IN_NON_WITNESS_UTXO);
72+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_NON_WITNESS_UTXO));
7373
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
7474
SerializeToVector(os, non_witness_utxo);
7575
}
7676
if (!witness_utxo.IsNull()) {
77-
SerializeToVector(s, PSBT_IN_WITNESS_UTXO);
77+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESS_UTXO));
7878
SerializeToVector(s, witness_utxo);
7979
}
8080

8181
if (final_script_sig.empty() && final_script_witness.IsNull()) {
8282
// Write any partial signatures
8383
for (auto sig_pair : partial_sigs) {
84-
SerializeToVector(s, PSBT_IN_PARTIAL_SIG, Span{sig_pair.second.first});
84+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_PARTIAL_SIG), Span{sig_pair.second.first});
8585
s << sig_pair.second.second;
8686
}
8787

8888
// Write the sighash type
8989
if (sighash_type != std::nullopt) {
90-
SerializeToVector(s, PSBT_IN_SIGHASH);
90+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_SIGHASH));
9191
SerializeToVector(s, *sighash_type);
9292
}
9393

9494
// Write the redeem script
9595
if (!redeem_script.empty()) {
96-
SerializeToVector(s, PSBT_IN_REDEEMSCRIPT);
96+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_REDEEMSCRIPT));
9797
s << redeem_script;
9898
}
9999

100100
// Write the witness script
101101
if (!witness_script.empty()) {
102-
SerializeToVector(s, PSBT_IN_WITNESSSCRIPT);
102+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESSSCRIPT));
103103
s << witness_script;
104104
}
105105

106106
// Write any hd keypaths
107-
SerializeHDKeypaths(s, hd_keypaths, PSBT_IN_BIP32_DERIVATION);
107+
SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_IN_BIP32_DERIVATION));
108108
}
109109

110110
// Write script sig
111111
if (!final_script_sig.empty()) {
112-
SerializeToVector(s, PSBT_IN_SCRIPTSIG);
112+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_SCRIPTSIG));
113113
s << final_script_sig;
114114
}
115115
// write script witness
116116
if (!final_script_witness.IsNull()) {
117-
SerializeToVector(s, PSBT_IN_SCRIPTWITNESS);
117+
SerializeToVector(s, CompactSizeWriter(PSBT_IN_SCRIPTWITNESS));
118118
SerializeToVector(s, final_script_witness.stack);
119119
}
120120

@@ -147,8 +147,9 @@ struct PSBTInput
147147
break;
148148
}
149149

150-
// First byte of key is the type
151-
unsigned char type = key[0];
150+
// Type is compact size uint at beginning of key
151+
SpanReader skey(s.GetType(), s.GetVersion(), key);
152+
uint64_t type = ReadCompactSize(skey);
152153

153154
// Do stuff based on type
154155
switch(type) {
@@ -292,18 +293,18 @@ struct PSBTOutput
292293
inline void Serialize(Stream& s) const {
293294
// Write the redeem script
294295
if (!redeem_script.empty()) {
295-
SerializeToVector(s, PSBT_OUT_REDEEMSCRIPT);
296+
SerializeToVector(s, CompactSizeWriter(PSBT_OUT_REDEEMSCRIPT));
296297
s << redeem_script;
297298
}
298299

299300
// Write the witness script
300301
if (!witness_script.empty()) {
301-
SerializeToVector(s, PSBT_OUT_WITNESSSCRIPT);
302+
SerializeToVector(s, CompactSizeWriter(PSBT_OUT_WITNESSSCRIPT));
302303
s << witness_script;
303304
}
304305

305306
// Write any hd keypaths
306-
SerializeHDKeypaths(s, hd_keypaths, PSBT_OUT_BIP32_DERIVATION);
307+
SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_OUT_BIP32_DERIVATION));
307308

308309
// Write unknown things
309310
for (auto& entry : unknown) {
@@ -334,8 +335,9 @@ struct PSBTOutput
334335
break;
335336
}
336337

337-
// First byte of key is the type
338-
unsigned char type = key[0];
338+
// Type is compact size uint at beginning of key
339+
SpanReader skey(s.GetType(), s.GetVersion(), key);
340+
uint64_t type = ReadCompactSize(skey);
339341

340342
// Do stuff based on type
341343
switch(type) {
@@ -422,7 +424,7 @@ struct PartiallySignedTransaction
422424
s << PSBT_MAGIC_BYTES;
423425

424426
// unsigned tx flag
425-
SerializeToVector(s, PSBT_GLOBAL_UNSIGNED_TX);
427+
SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_UNSIGNED_TX));
426428

427429
// Write serialized tx to a stream
428430
OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
@@ -474,8 +476,9 @@ struct PartiallySignedTransaction
474476
break;
475477
}
476478

477-
// First byte of key is the type
478-
unsigned char type = key[0];
479+
// Type is compact size uint at beginning of key
480+
SpanReader skey(s.GetType(), s.GetVersion(), key);
481+
uint64_t type = ReadCompactSize(skey);
479482

480483
// Do stuff based on type
481484
switch(type) {

src/script/sign.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std
143143

144144
// Serialize HD keypaths to a stream from a map
145145
template<typename Stream>
146-
void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, uint8_t type)
146+
void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
147147
{
148148
for (auto keypath_pair : hd_keypaths) {
149149
if (!keypath_pair.first.IsValid()) {

src/serialize.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ struct CompactSizeFormatter
527527
}
528528
};
529529

530+
class CompactSizeWriter
531+
{
532+
protected:
533+
uint64_t n;
534+
public:
535+
explicit CompactSizeWriter(uint64_t n_in) : n(n_in) { }
536+
537+
template<typename Stream>
538+
void Serialize(Stream &s) const {
539+
WriteCompactSize<Stream>(s, n);
540+
}
541+
};
542+
530543
template<size_t Limit>
531544
struct LimitedStringFormatter
532545
{

src/test/fuzz/script_sign.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ FUZZ_TARGET_INIT(script_sign, initialize_script_sign)
4343
} catch (const std::ios_base::failure&) {
4444
}
4545
CDataStream serialized{SER_NETWORK, PROTOCOL_VERSION};
46-
SerializeHDKeypaths(serialized, hd_keypaths, fuzzed_data_provider.ConsumeIntegral<uint8_t>());
46+
SerializeHDKeypaths(serialized, hd_keypaths, CompactSizeWriter(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
4747
}
4848

4949
{
@@ -61,7 +61,7 @@ FUZZ_TARGET_INIT(script_sign, initialize_script_sign)
6161
}
6262
CDataStream serialized{SER_NETWORK, PROTOCOL_VERSION};
6363
try {
64-
SerializeHDKeypaths(serialized, hd_keypaths, fuzzed_data_provider.ConsumeIntegral<uint8_t>());
64+
SerializeHDKeypaths(serialized, hd_keypaths, CompactSizeWriter(fuzzed_data_provider.ConsumeIntegral<uint8_t>()));
6565
} catch (const std::ios_base::failure&) {
6666
}
6767
std::map<CPubKey, KeyOriginInfo> deserialized_hd_keypaths;

0 commit comments

Comments
 (0)