@@ -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) {
0 commit comments