From c3eb416b882522dffa4254b52d2da5b53c970efe Mon Sep 17 00:00:00 2001 From: Andrew Chow Date: Wed, 2 Oct 2019 16:11:34 -0400 Subject: Implement PSBT versions --- src/psbt.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/psbt.h') diff --git a/src/psbt.h b/src/psbt.h index a752e99e74..834af26219 100644 --- a/src/psbt.h +++ b/src/psbt.h @@ -20,6 +20,7 @@ static constexpr uint8_t PSBT_MAGIC_BYTES[5] = {'p', 's', 'b', 't', 0xff}; // Global types static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00; +static constexpr uint8_t PSBT_GLOBAL_VERSION = 0xFB; // Input types static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00; @@ -45,6 +46,9 @@ static constexpr uint8_t PSBT_SEPARATOR = 0x00; // to prevent reading a stream indefinitely and running out of memory. const std::streamsize MAX_FILE_SIZE_PSBT = 100000000; // 100 MiB +// PSBT version number +static constexpr uint32_t PSBT_HIGHEST_VERSION = 0; + /** A structure for PSBTs which contain per-input information */ struct PSBTInput { @@ -398,6 +402,7 @@ struct PartiallySignedTransaction std::vector inputs; std::vector outputs; std::map, std::vector> unknown; + std::optional m_version; bool IsNull() const; @@ -430,6 +435,12 @@ struct PartiallySignedTransaction OverrideStream os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS); SerializeToVector(os, *tx); + // PSBT version + if (m_version != std::nullopt && *m_version > 0) { + SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_VERSION)); + SerializeToVector(s, *m_version); + } + // Write the unknown things for (auto& entry : unknown) { s << entry.first; @@ -502,6 +513,21 @@ struct PartiallySignedTransaction } break; } + case PSBT_GLOBAL_VERSION: + { + if (m_version) { + throw std::ios_base::failure("Duplicate Key, version already provided"); + } else if (key.size() != 1) { + throw std::ios_base::failure("Global version key is more than one byte type"); + } + uint32_t v; + UnserializeFromVector(s, v); + m_version = v; + if (*m_version > PSBT_HIGHEST_VERSION) { + throw std::ios_base::failure("Unsupported version number"); + } + break; + } // Unknown stuff default: { if (unknown.count(key) > 0) { -- cgit v1.2.3