@@ -69,52 +69,52 @@ struct PSBTInput
69
69
inline void Serialize (Stream& s) const {
70
70
// Write the utxo
71
71
if (non_witness_utxo) {
72
- SerializeToVector (s, PSBT_IN_NON_WITNESS_UTXO);
72
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_NON_WITNESS_UTXO) );
73
73
OverrideStream<Stream> os (&s, s.GetType (), s.GetVersion () | SERIALIZE_TRANSACTION_NO_WITNESS);
74
74
SerializeToVector (os, non_witness_utxo);
75
75
}
76
76
if (!witness_utxo.IsNull ()) {
77
- SerializeToVector (s, PSBT_IN_WITNESS_UTXO);
77
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_WITNESS_UTXO) );
78
78
SerializeToVector (s, witness_utxo);
79
79
}
80
80
81
81
if (final_script_sig.empty () && final_script_witness.IsNull ()) {
82
82
// Write any partial signatures
83
83
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 });
85
85
s << sig_pair.second .second ;
86
86
}
87
87
88
88
// Write the sighash type
89
89
if (sighash_type != std::nullopt) {
90
- SerializeToVector (s, PSBT_IN_SIGHASH);
90
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_SIGHASH) );
91
91
SerializeToVector (s, *sighash_type);
92
92
}
93
93
94
94
// Write the redeem script
95
95
if (!redeem_script.empty ()) {
96
- SerializeToVector (s, PSBT_IN_REDEEMSCRIPT);
96
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_REDEEMSCRIPT) );
97
97
s << redeem_script;
98
98
}
99
99
100
100
// Write the witness script
101
101
if (!witness_script.empty ()) {
102
- SerializeToVector (s, PSBT_IN_WITNESSSCRIPT);
102
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_WITNESSSCRIPT) );
103
103
s << witness_script;
104
104
}
105
105
106
106
// Write any hd keypaths
107
- SerializeHDKeypaths (s, hd_keypaths, PSBT_IN_BIP32_DERIVATION);
107
+ SerializeHDKeypaths (s, hd_keypaths, CompactSizeWriter ( PSBT_IN_BIP32_DERIVATION) );
108
108
}
109
109
110
110
// Write script sig
111
111
if (!final_script_sig.empty ()) {
112
- SerializeToVector (s, PSBT_IN_SCRIPTSIG);
112
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_SCRIPTSIG) );
113
113
s << final_script_sig;
114
114
}
115
115
// write script witness
116
116
if (!final_script_witness.IsNull ()) {
117
- SerializeToVector (s, PSBT_IN_SCRIPTWITNESS);
117
+ SerializeToVector (s, CompactSizeWriter ( PSBT_IN_SCRIPTWITNESS) );
118
118
SerializeToVector (s, final_script_witness.stack );
119
119
}
120
120
@@ -147,8 +147,9 @@ struct PSBTInput
147
147
break ;
148
148
}
149
149
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);
152
153
153
154
// Do stuff based on type
154
155
switch (type) {
@@ -292,18 +293,18 @@ struct PSBTOutput
292
293
inline void Serialize (Stream& s) const {
293
294
// Write the redeem script
294
295
if (!redeem_script.empty ()) {
295
- SerializeToVector (s, PSBT_OUT_REDEEMSCRIPT);
296
+ SerializeToVector (s, CompactSizeWriter ( PSBT_OUT_REDEEMSCRIPT) );
296
297
s << redeem_script;
297
298
}
298
299
299
300
// Write the witness script
300
301
if (!witness_script.empty ()) {
301
- SerializeToVector (s, PSBT_OUT_WITNESSSCRIPT);
302
+ SerializeToVector (s, CompactSizeWriter ( PSBT_OUT_WITNESSSCRIPT) );
302
303
s << witness_script;
303
304
}
304
305
305
306
// Write any hd keypaths
306
- SerializeHDKeypaths (s, hd_keypaths, PSBT_OUT_BIP32_DERIVATION);
307
+ SerializeHDKeypaths (s, hd_keypaths, CompactSizeWriter ( PSBT_OUT_BIP32_DERIVATION) );
307
308
308
309
// Write unknown things
309
310
for (auto & entry : unknown) {
@@ -334,8 +335,9 @@ struct PSBTOutput
334
335
break ;
335
336
}
336
337
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);
339
341
340
342
// Do stuff based on type
341
343
switch (type) {
@@ -422,7 +424,7 @@ struct PartiallySignedTransaction
422
424
s << PSBT_MAGIC_BYTES;
423
425
424
426
// unsigned tx flag
425
- SerializeToVector (s, PSBT_GLOBAL_UNSIGNED_TX);
427
+ SerializeToVector (s, CompactSizeWriter ( PSBT_GLOBAL_UNSIGNED_TX) );
426
428
427
429
// Write serialized tx to a stream
428
430
OverrideStream<Stream> os (&s, s.GetType (), s.GetVersion () | SERIALIZE_TRANSACTION_NO_WITNESS);
@@ -474,8 +476,9 @@ struct PartiallySignedTransaction
474
476
break ;
475
477
}
476
478
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);
479
482
480
483
// Do stuff based on type
481
484
switch (type) {
0 commit comments