@@ -20,6 +20,7 @@ static constexpr uint8_t PSBT_MAGIC_BYTES[5] = {'p', 's', 'b', 't', 0xff};
20
20
21
21
// Global types
22
22
static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00 ;
23
+ static constexpr uint8_t PSBT_GLOBAL_VERSION = 0xFB ;
23
24
24
25
// Input types
25
26
static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00 ;
@@ -45,6 +46,9 @@ static constexpr uint8_t PSBT_SEPARATOR = 0x00;
45
46
// to prevent reading a stream indefinitely and running out of memory.
46
47
const std::streamsize MAX_FILE_SIZE_PSBT = 100000000 ; // 100 MiB
47
48
49
+ // PSBT version number
50
+ static constexpr uint32_t PSBT_HIGHEST_VERSION = 0 ;
51
+
48
52
/* * A structure for PSBTs which contain per-input information */
49
53
struct PSBTInput
50
54
{
@@ -398,6 +402,7 @@ struct PartiallySignedTransaction
398
402
std::vector<PSBTInput> inputs;
399
403
std::vector<PSBTOutput> outputs;
400
404
std::map<std::vector<unsigned char >, std::vector<unsigned char >> unknown;
405
+ std::optional<uint32_t > m_version;
401
406
402
407
bool IsNull () const ;
403
408
@@ -430,6 +435,12 @@ struct PartiallySignedTransaction
430
435
OverrideStream<Stream> os (&s, s.GetType (), s.GetVersion () | SERIALIZE_TRANSACTION_NO_WITNESS);
431
436
SerializeToVector (os, *tx);
432
437
438
+ // PSBT version
439
+ if (m_version != std::nullopt && *m_version > 0 ) {
440
+ SerializeToVector (s, CompactSizeWriter (PSBT_GLOBAL_VERSION));
441
+ SerializeToVector (s, *m_version);
442
+ }
443
+
433
444
// Write the unknown things
434
445
for (auto & entry : unknown) {
435
446
s << entry.first ;
@@ -502,6 +513,21 @@ struct PartiallySignedTransaction
502
513
}
503
514
break ;
504
515
}
516
+ case PSBT_GLOBAL_VERSION:
517
+ {
518
+ if (m_version) {
519
+ throw std::ios_base::failure (" Duplicate Key, version already provided" );
520
+ } else if (key.size () != 1 ) {
521
+ throw std::ios_base::failure (" Global version key is more than one byte type" );
522
+ }
523
+ uint32_t v;
524
+ UnserializeFromVector (s, v);
525
+ m_version = v;
526
+ if (*m_version > PSBT_HIGHEST_VERSION) {
527
+ throw std::ios_base::failure (" Unsupported version number" );
528
+ }
529
+ break ;
530
+ }
505
531
// Unknown stuff
506
532
default : {
507
533
if (unknown.count (key) > 0 ) {
0 commit comments