LCOV - code coverage report
Current view: top level - src - psbt.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 300 321 93.5 %
Date: 2021-06-29 14:35:33 Functions: 9 9 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 147 164 89.6 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2009-2020 The Bitcoin Core developers
#       2                 :            : // Distributed under the MIT software license, see the accompanying
#       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       4                 :            : 
#       5                 :            : #ifndef BITCOIN_PSBT_H
#       6                 :            : #define BITCOIN_PSBT_H
#       7                 :            : 
#       8                 :            : #include <attributes.h>
#       9                 :            : #include <node/transaction.h>
#      10                 :            : #include <policy/feerate.h>
#      11                 :            : #include <primitives/transaction.h>
#      12                 :            : #include <pubkey.h>
#      13                 :            : #include <script/sign.h>
#      14                 :            : #include <script/signingprovider.h>
#      15                 :            : 
#      16                 :            : #include <optional>
#      17                 :            : 
#      18                 :            : // Magic bytes
#      19                 :            : static constexpr uint8_t PSBT_MAGIC_BYTES[5] = {'p', 's', 'b', 't', 0xff};
#      20                 :            : 
#      21                 :            : // Global types
#      22                 :            : static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00;
#      23                 :            : 
#      24                 :            : // Input types
#      25                 :            : static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00;
#      26                 :            : static constexpr uint8_t PSBT_IN_WITNESS_UTXO = 0x01;
#      27                 :            : static constexpr uint8_t PSBT_IN_PARTIAL_SIG = 0x02;
#      28                 :            : static constexpr uint8_t PSBT_IN_SIGHASH = 0x03;
#      29                 :            : static constexpr uint8_t PSBT_IN_REDEEMSCRIPT = 0x04;
#      30                 :            : static constexpr uint8_t PSBT_IN_WITNESSSCRIPT = 0x05;
#      31                 :            : static constexpr uint8_t PSBT_IN_BIP32_DERIVATION = 0x06;
#      32                 :            : static constexpr uint8_t PSBT_IN_SCRIPTSIG = 0x07;
#      33                 :            : static constexpr uint8_t PSBT_IN_SCRIPTWITNESS = 0x08;
#      34                 :            : 
#      35                 :            : // Output types
#      36                 :            : static constexpr uint8_t PSBT_OUT_REDEEMSCRIPT = 0x00;
#      37                 :            : static constexpr uint8_t PSBT_OUT_WITNESSSCRIPT = 0x01;
#      38                 :            : static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02;
#      39                 :            : 
#      40                 :            : // The separator is 0x00. Reading this in means that the unserializer can interpret it
#      41                 :            : // as a 0 length key which indicates that this is the separator. The separator has no value.
#      42                 :            : static constexpr uint8_t PSBT_SEPARATOR = 0x00;
#      43                 :            : 
#      44                 :            : // BIP 174 does not specify a maximum file size, but we set a limit anyway
#      45                 :            : // to prevent reading a stream indefinitely and running out of memory.
#      46                 :            : const std::streamsize MAX_FILE_SIZE_PSBT = 100000000; // 100 MiB
#      47                 :            : 
#      48                 :            : /** A structure for PSBTs which contain per-input information */
#      49                 :            : struct PSBTInput
#      50                 :            : {
#      51                 :            :     CTransactionRef non_witness_utxo;
#      52                 :            :     CTxOut witness_utxo;
#      53                 :            :     CScript redeem_script;
#      54                 :            :     CScript witness_script;
#      55                 :            :     CScript final_script_sig;
#      56                 :            :     CScriptWitness final_script_witness;
#      57                 :            :     std::map<CPubKey, KeyOriginInfo> hd_keypaths;
#      58                 :            :     std::map<CKeyID, SigPair> partial_sigs;
#      59                 :            :     std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
#      60                 :            :     int sighash_type = 0;
#      61                 :            : 
#      62                 :            :     bool IsNull() const;
#      63                 :            :     void FillSignatureData(SignatureData& sigdata) const;
#      64                 :            :     void FromSignatureData(const SignatureData& sigdata);
#      65                 :            :     void Merge(const PSBTInput& input);
#      66                 :       1034 :     PSBTInput() {}
#      67                 :            : 
#      68                 :            :     template <typename Stream>
#      69                 :        665 :     inline void Serialize(Stream& s) const {
#      70                 :            :         // Write the utxo
#      71         [ +  + ]:        665 :         if (non_witness_utxo) {
#      72                 :        468 :             SerializeToVector(s, PSBT_IN_NON_WITNESS_UTXO);
#      73                 :        468 :             OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
#      74                 :        468 :             SerializeToVector(os, non_witness_utxo);
#      75                 :        468 :         }
#      76         [ +  + ]:        665 :         if (!witness_utxo.IsNull()) {
#      77                 :        291 :             SerializeToVector(s, PSBT_IN_WITNESS_UTXO);
#      78                 :        291 :             SerializeToVector(s, witness_utxo);
#      79                 :        291 :         }
#      80                 :            : 
#      81 [ +  + ][ +  + ]:        665 :         if (final_script_sig.empty() && final_script_witness.IsNull()) {
#      82                 :            :             // Write any partial signatures
#      83         [ +  + ]:        530 :             for (auto sig_pair : partial_sigs) {
#      84                 :         34 :                 SerializeToVector(s, PSBT_IN_PARTIAL_SIG, MakeSpan(sig_pair.second.first));
#      85                 :         34 :                 s << sig_pair.second.second;
#      86                 :         34 :             }
#      87                 :            : 
#      88                 :            :             // Write the sighash type
#      89         [ +  + ]:        530 :             if (sighash_type > 0) {
#      90                 :         24 :                 SerializeToVector(s, PSBT_IN_SIGHASH);
#      91                 :         24 :                 SerializeToVector(s, sighash_type);
#      92                 :         24 :             }
#      93                 :            : 
#      94                 :            :             // Write the redeem script
#      95         [ +  + ]:        530 :             if (!redeem_script.empty()) {
#      96                 :        137 :                 SerializeToVector(s, PSBT_IN_REDEEMSCRIPT);
#      97                 :        137 :                 s << redeem_script;
#      98                 :        137 :             }
#      99                 :            : 
#     100                 :            :             // Write the witness script
#     101         [ +  + ]:        530 :             if (!witness_script.empty()) {
#     102                 :         48 :                 SerializeToVector(s, PSBT_IN_WITNESSSCRIPT);
#     103                 :         48 :                 s << witness_script;
#     104                 :         48 :             }
#     105                 :            : 
#     106                 :            :             // Write any hd keypaths
#     107                 :        530 :             SerializeHDKeypaths(s, hd_keypaths, PSBT_IN_BIP32_DERIVATION);
#     108                 :        530 :         }
#     109                 :            : 
#     110                 :            :         // Write script sig
#     111         [ +  + ]:        665 :         if (!final_script_sig.empty()) {
#     112                 :        101 :             SerializeToVector(s, PSBT_IN_SCRIPTSIG);
#     113                 :        101 :             s << final_script_sig;
#     114                 :        101 :         }
#     115                 :            :         // write script witness
#     116         [ +  + ]:        665 :         if (!final_script_witness.IsNull()) {
#     117                 :         48 :             SerializeToVector(s, PSBT_IN_SCRIPTWITNESS);
#     118                 :         48 :             SerializeToVector(s, final_script_witness.stack);
#     119                 :         48 :         }
#     120                 :            : 
#     121                 :            :         // Write unknown things
#     122         [ +  + ]:        665 :         for (auto& entry : unknown) {
#     123                 :          6 :             s << entry.first;
#     124                 :          6 :             s << entry.second;
#     125                 :          6 :         }
#     126                 :            : 
#     127                 :        665 :         s << PSBT_SEPARATOR;
#     128                 :        665 :     }
#     129                 :            : 
#     130                 :            : 
#     131                 :            :     template <typename Stream>
#     132                 :        646 :     inline void Unserialize(Stream& s) {
#     133                 :            :         // Used for duplicate key detection
#     134                 :        646 :         std::set<std::vector<unsigned char>> key_lookup;
#     135                 :            : 
#     136                 :            :         // Read loop
#     137                 :        646 :         bool found_sep = false;
#     138         [ +  - ]:       2015 :         while(!s.empty()) {
#     139                 :            :             // Read
#     140                 :       2015 :             std::vector<unsigned char> key;
#     141                 :       2015 :             s >> key;
#     142                 :            : 
#     143                 :            :             // the key is empty if that was actually a separator byte
#     144                 :            :             // This is a special case for key lengths 0 as those are not allowed (except for separator)
#     145         [ +  + ]:       2015 :             if (key.empty()) {
#     146                 :        614 :                 found_sep = true;
#     147                 :        614 :                 break;
#     148                 :        614 :             }
#     149                 :            : 
#     150                 :            :             // First byte of key is the type
#     151                 :       1401 :             unsigned char type = key[0];
#     152                 :            : 
#     153                 :            :             // Do stuff based on type
#     154                 :       1401 :             switch(type) {
#     155         [ +  + ]:        357 :                 case PSBT_IN_NON_WITNESS_UTXO:
#     156                 :        357 :                 {
#     157         [ +  + ]:        357 :                     if (!key_lookup.emplace(key).second) {
#     158                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input non-witness utxo already provided");
#     159         [ +  + ]:        355 :                     } else if (key.size() != 1) {
#     160                 :          2 :                         throw std::ios_base::failure("Non-witness utxo key is more than one byte type");
#     161                 :          2 :                     }
#     162                 :            :                     // Set the stream to unserialize with witness since this is always a valid network transaction
#     163                 :        353 :                     OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() & ~SERIALIZE_TRANSACTION_NO_WITNESS);
#     164                 :        353 :                     UnserializeFromVector(os, non_witness_utxo);
#     165                 :        353 :                     break;
#     166                 :        353 :                 }
#     167         [ +  + ]:        353 :                 case PSBT_IN_WITNESS_UTXO:
#     168         [ +  + ]:        265 :                     if (!key_lookup.emplace(key).second) {
#     169                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input witness utxo already provided");
#     170         [ +  + ]:        263 :                     } else if (key.size() != 1) {
#     171                 :          2 :                         throw std::ios_base::failure("Witness utxo key is more than one byte type");
#     172                 :          2 :                     }
#     173                 :        261 :                     UnserializeFromVector(s, witness_utxo);
#     174                 :        261 :                     break;
#     175         [ +  + ]:        261 :                 case PSBT_IN_PARTIAL_SIG:
#     176                 :         46 :                 {
#     177                 :            :                     // Make sure that the key is the size of pubkey + 1
#     178 [ +  - ][ +  + ]:         46 :                     if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
#     179                 :          2 :                         throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey");
#     180                 :          2 :                     }
#     181                 :            :                     // Read in the pubkey from key
#     182                 :         44 :                     CPubKey pubkey(key.begin() + 1, key.end());
#     183         [ -  + ]:         44 :                     if (!pubkey.IsFullyValid()) {
#     184                 :          0 :                        throw std::ios_base::failure("Invalid pubkey");
#     185                 :          0 :                     }
#     186         [ -  + ]:         44 :                     if (partial_sigs.count(pubkey.GetID()) > 0) {
#     187                 :          0 :                         throw std::ios_base::failure("Duplicate Key, input partial signature for pubkey already provided");
#     188                 :          0 :                     }
#     189                 :            : 
#     190                 :            :                     // Read in the signature from value
#     191                 :         44 :                     std::vector<unsigned char> sig;
#     192                 :         44 :                     s >> sig;
#     193                 :            : 
#     194                 :            :                     // Add to list
#     195                 :         44 :                     partial_sigs.emplace(pubkey.GetID(), SigPair(pubkey, std::move(sig)));
#     196                 :         44 :                     break;
#     197                 :         44 :                 }
#     198         [ +  + ]:         44 :                 case PSBT_IN_SIGHASH:
#     199         [ +  + ]:         42 :                     if (!key_lookup.emplace(key).second) {
#     200                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input sighash type already provided");
#     201         [ +  + ]:         40 :                     } else if (key.size() != 1) {
#     202                 :          2 :                         throw std::ios_base::failure("Sighash type key is more than one byte type");
#     203                 :          2 :                     }
#     204                 :         38 :                     UnserializeFromVector(s, sighash_type);
#     205                 :         38 :                     break;
#     206         [ +  + ]:        131 :                 case PSBT_IN_REDEEMSCRIPT:
#     207                 :        131 :                 {
#     208         [ +  + ]:        131 :                     if (!key_lookup.emplace(key).second) {
#     209                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input redeemScript already provided");
#     210         [ +  + ]:        129 :                     } else if (key.size() != 1) {
#     211                 :          2 :                         throw std::ios_base::failure("Input redeemScript key is more than one byte type");
#     212                 :          2 :                     }
#     213                 :        127 :                     s >> redeem_script;
#     214                 :        127 :                     break;
#     215                 :        127 :                 }
#     216         [ +  + ]:        127 :                 case PSBT_IN_WITNESSSCRIPT:
#     217                 :         61 :                 {
#     218         [ +  + ]:         61 :                     if (!key_lookup.emplace(key).second) {
#     219                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input witnessScript already provided");
#     220         [ +  + ]:         59 :                     } else if (key.size() != 1) {
#     221                 :          2 :                         throw std::ios_base::failure("Input witnessScript key is more than one byte type");
#     222                 :          2 :                     }
#     223                 :         57 :                     s >> witness_script;
#     224                 :         57 :                     break;
#     225                 :         57 :                 }
#     226         [ +  + ]:        325 :                 case PSBT_IN_BIP32_DERIVATION:
#     227                 :        325 :                 {
#     228                 :        325 :                     DeserializeHDKeypaths(s, key, hd_keypaths);
#     229                 :        325 :                     break;
#     230                 :         57 :                 }
#     231         [ +  + ]:        111 :                 case PSBT_IN_SCRIPTSIG:
#     232                 :        111 :                 {
#     233         [ +  + ]:        111 :                     if (!key_lookup.emplace(key).second) {
#     234                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input final scriptSig already provided");
#     235         [ +  + ]:        109 :                     } else if (key.size() != 1) {
#     236                 :          2 :                         throw std::ios_base::failure("Final scriptSig key is more than one byte type");
#     237                 :          2 :                     }
#     238                 :        107 :                     s >> final_script_sig;
#     239                 :        107 :                     break;
#     240                 :        107 :                 }
#     241         [ +  + ]:        107 :                 case PSBT_IN_SCRIPTWITNESS:
#     242                 :         57 :                 {
#     243         [ +  + ]:         57 :                     if (!key_lookup.emplace(key).second) {
#     244                 :          2 :                         throw std::ios_base::failure("Duplicate Key, input final scriptWitness already provided");
#     245         [ +  + ]:         55 :                     } else if (key.size() != 1) {
#     246                 :          2 :                         throw std::ios_base::failure("Final scriptWitness key is more than one byte type");
#     247                 :          2 :                     }
#     248                 :         53 :                     UnserializeFromVector(s, final_script_witness.stack);
#     249                 :         53 :                     break;
#     250                 :         53 :                 }
#     251                 :            :                 // Unknown stuff
#     252         [ +  + ]:         53 :                 default:
#     253         [ -  + ]:          6 :                     if (unknown.count(key) > 0) {
#     254                 :          0 :                         throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
#     255                 :          0 :                     }
#     256                 :            :                     // Read in the value
#     257                 :          6 :                     std::vector<unsigned char> val_bytes;
#     258                 :          6 :                     s >> val_bytes;
#     259                 :          6 :                     unknown.emplace(std::move(key), std::move(val_bytes));
#     260                 :          6 :                     break;
#     261                 :       1401 :             }
#     262                 :       1401 :         }
#     263                 :            : 
#     264         [ -  + ]:        646 :         if (!found_sep) {
#     265                 :          0 :             throw std::ios_base::failure("Separator is missing at the end of an input map");
#     266                 :          0 :         }
#     267                 :        614 :     }
#     268                 :            : 
#     269                 :            :     template <typename Stream>
#     270                 :            :     PSBTInput(deserialize_type, Stream& s) {
#     271                 :            :         Unserialize(s);
#     272                 :            :     }
#     273                 :            : };
#     274                 :            : 
#     275                 :            : /** A structure for PSBTs which contains per output information */
#     276                 :            : struct PSBTOutput
#     277                 :            : {
#     278                 :            :     CScript redeem_script;
#     279                 :            :     CScript witness_script;
#     280                 :            :     std::map<CPubKey, KeyOriginInfo> hd_keypaths;
#     281                 :            :     std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
#     282                 :            : 
#     283                 :            :     bool IsNull() const;
#     284                 :            :     void FillSignatureData(SignatureData& sigdata) const;
#     285                 :            :     void FromSignatureData(const SignatureData& sigdata);
#     286                 :            :     void Merge(const PSBTOutput& output);
#     287                 :       1040 :     PSBTOutput() {}
#     288                 :            : 
#     289                 :            :     template <typename Stream>
#     290                 :        667 :     inline void Serialize(Stream& s) const {
#     291                 :            :         // Write the redeem script
#     292         [ +  + ]:        667 :         if (!redeem_script.empty()) {
#     293                 :         88 :             SerializeToVector(s, PSBT_OUT_REDEEMSCRIPT);
#     294                 :         88 :             s << redeem_script;
#     295                 :         88 :         }
#     296                 :            : 
#     297                 :            :         // Write the witness script
#     298         [ +  + ]:        667 :         if (!witness_script.empty()) {
#     299                 :         30 :             SerializeToVector(s, PSBT_OUT_WITNESSSCRIPT);
#     300                 :         30 :             s << witness_script;
#     301                 :         30 :         }
#     302                 :            : 
#     303                 :            :         // Write any hd keypaths
#     304                 :        667 :         SerializeHDKeypaths(s, hd_keypaths, PSBT_OUT_BIP32_DERIVATION);
#     305                 :            : 
#     306                 :            :         // Write unknown things
#     307         [ +  + ]:        667 :         for (auto& entry : unknown) {
#     308                 :          4 :             s << entry.first;
#     309                 :          4 :             s << entry.second;
#     310                 :          4 :         }
#     311                 :            : 
#     312                 :        667 :         s << PSBT_SEPARATOR;
#     313                 :        667 :     }
#     314                 :            : 
#     315                 :            : 
#     316                 :            :     template <typename Stream>
#     317                 :        607 :     inline void Unserialize(Stream& s) {
#     318                 :            :         // Used for duplicate key detection
#     319                 :        607 :         std::set<std::vector<unsigned char>> key_lookup;
#     320                 :            : 
#     321                 :            :         // Read loop
#     322                 :        607 :         bool found_sep = false;
#     323         [ +  - ]:       1046 :         while(!s.empty()) {
#     324                 :            :             // Read
#     325                 :       1046 :             std::vector<unsigned char> key;
#     326                 :       1046 :             s >> key;
#     327                 :            : 
#     328                 :            :             // the key is empty if that was actually a separator byte
#     329                 :            :             // This is a special case for key lengths 0 as those are not allowed (except for separator)
#     330         [ +  + ]:       1046 :             if (key.empty()) {
#     331                 :        597 :                 found_sep = true;
#     332                 :        597 :                 break;
#     333                 :        597 :             }
#     334                 :            : 
#     335                 :            :             // First byte of key is the type
#     336                 :        449 :             unsigned char type = key[0];
#     337                 :            : 
#     338                 :            :             // Do stuff based on type
#     339                 :        449 :             switch(type) {
#     340         [ +  + ]:         98 :                 case PSBT_OUT_REDEEMSCRIPT:
#     341                 :         98 :                 {
#     342         [ +  + ]:         98 :                     if (!key_lookup.emplace(key).second) {
#     343                 :          2 :                         throw std::ios_base::failure("Duplicate Key, output redeemScript already provided");
#     344         [ +  + ]:         96 :                     } else if (key.size() != 1) {
#     345                 :          2 :                         throw std::ios_base::failure("Output redeemScript key is more than one byte type");
#     346                 :          2 :                     }
#     347                 :         94 :                     s >> redeem_script;
#     348                 :         94 :                     break;
#     349                 :         94 :                 }
#     350         [ +  + ]:         94 :                 case PSBT_OUT_WITNESSSCRIPT:
#     351                 :         36 :                 {
#     352         [ +  + ]:         36 :                     if (!key_lookup.emplace(key).second) {
#     353                 :          2 :                         throw std::ios_base::failure("Duplicate Key, output witnessScript already provided");
#     354         [ +  + ]:         34 :                     } else if (key.size() != 1) {
#     355                 :          2 :                         throw std::ios_base::failure("Output witnessScript key is more than one byte type");
#     356                 :          2 :                     }
#     357                 :         32 :                     s >> witness_script;
#     358                 :         32 :                     break;
#     359                 :         32 :                 }
#     360         [ +  + ]:        311 :                 case PSBT_OUT_BIP32_DERIVATION:
#     361                 :        311 :                 {
#     362                 :        311 :                     DeserializeHDKeypaths(s, key, hd_keypaths);
#     363                 :        311 :                     break;
#     364                 :         32 :                 }
#     365                 :            :                 // Unknown stuff
#     366         [ +  + ]:         32 :                 default: {
#     367         [ -  + ]:          4 :                     if (unknown.count(key) > 0) {
#     368                 :          0 :                         throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
#     369                 :          0 :                     }
#     370                 :            :                     // Read in the value
#     371                 :          4 :                     std::vector<unsigned char> val_bytes;
#     372                 :          4 :                     s >> val_bytes;
#     373                 :          4 :                     unknown.emplace(std::move(key), std::move(val_bytes));
#     374                 :          4 :                     break;
#     375                 :          4 :                 }
#     376                 :        449 :             }
#     377                 :        449 :         }
#     378                 :            : 
#     379         [ -  + ]:        607 :         if (!found_sep) {
#     380                 :          0 :             throw std::ios_base::failure("Separator is missing at the end of an output map");
#     381                 :          0 :         }
#     382                 :        597 :     }
#     383                 :            : 
#     384                 :            :     template <typename Stream>
#     385                 :            :     PSBTOutput(deserialize_type, Stream& s) {
#     386                 :            :         Unserialize(s);
#     387                 :            :     }
#     388                 :            : };
#     389                 :            : 
#     390                 :            : /** A version of CTransaction with the PSBT format*/
#     391                 :            : struct PartiallySignedTransaction
#     392                 :            : {
#     393                 :            :     std::optional<CMutableTransaction> tx;
#     394                 :            :     std::vector<PSBTInput> inputs;
#     395                 :            :     std::vector<PSBTOutput> outputs;
#     396                 :            :     std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
#     397                 :            : 
#     398                 :            :     bool IsNull() const;
#     399                 :            : 
#     400                 :            :     /** Merge psbt into this. The two psbts must have the same underlying CTransaction (i.e. the
#     401                 :            :       * same actual Bitcoin transaction.) Returns true if the merge succeeded, false otherwise. */
#     402                 :            :     [[nodiscard]] bool Merge(const PartiallySignedTransaction& psbt);
#     403                 :            :     bool AddInput(const CTxIn& txin, PSBTInput& psbtin);
#     404                 :            :     bool AddOutput(const CTxOut& txout, const PSBTOutput& psbtout);
#     405                 :        668 :     PartiallySignedTransaction() {}
#     406                 :            :     explicit PartiallySignedTransaction(const CMutableTransaction& tx);
#     407                 :            :     /**
#     408                 :            :      * Finds the UTXO for a given input index
#     409                 :            :      *
#     410                 :            :      * @param[out] utxo The UTXO of the input if found
#     411                 :            :      * @param[in] input_index Index of the input to retrieve the UTXO of
#     412                 :            :      * @return Whether the UTXO for the specified input was found
#     413                 :            :      */
#     414                 :            :     bool GetInputUTXO(CTxOut& utxo, int input_index) const;
#     415                 :            : 
#     416                 :            :     template <typename Stream>
#     417                 :        490 :     inline void Serialize(Stream& s) const {
#     418                 :            : 
#     419                 :            :         // magic bytes
#     420                 :        490 :         s << PSBT_MAGIC_BYTES;
#     421                 :            : 
#     422                 :            :         // unsigned tx flag
#     423                 :        490 :         SerializeToVector(s, PSBT_GLOBAL_UNSIGNED_TX);
#     424                 :            : 
#     425                 :            :         // Write serialized tx to a stream
#     426                 :        490 :         OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
#     427                 :        490 :         SerializeToVector(os, *tx);
#     428                 :            : 
#     429                 :            :         // Write the unknown things
#     430         [ +  + ]:        490 :         for (auto& entry : unknown) {
#     431                 :          4 :             s << entry.first;
#     432                 :          4 :             s << entry.second;
#     433                 :          4 :         }
#     434                 :            : 
#     435                 :            :         // Separator
#     436                 :        490 :         s << PSBT_SEPARATOR;
#     437                 :            : 
#     438                 :            :         // Write inputs
#     439         [ +  + ]:        665 :         for (const PSBTInput& input : inputs) {
#     440                 :        665 :             s << input;
#     441                 :        665 :         }
#     442                 :            :         // Write outputs
#     443         [ +  + ]:        667 :         for (const PSBTOutput& output : outputs) {
#     444                 :        667 :             s << output;
#     445                 :        667 :         }
#     446                 :        490 :     }
#     447                 :            : 
#     448                 :            : 
#     449                 :            :     template <typename Stream>
#     450                 :        517 :     inline void Unserialize(Stream& s) {
#     451                 :            :         // Read the magic bytes
#     452                 :        517 :         uint8_t magic[5];
#     453                 :        517 :         s >> magic;
#     454         [ +  + ]:        517 :         if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) {
#     455                 :          4 :             throw std::ios_base::failure("Invalid PSBT magic bytes");
#     456                 :          4 :         }
#     457                 :            : 
#     458                 :            :         // Used for duplicate key detection
#     459                 :        513 :         std::set<std::vector<unsigned char>> key_lookup;
#     460                 :            : 
#     461                 :            :         // Read global data
#     462                 :        513 :         bool found_sep = false;
#     463         [ +  - ]:       1024 :         while(!s.empty()) {
#     464                 :            :             // Read
#     465                 :       1024 :             std::vector<unsigned char> key;
#     466                 :       1024 :             s >> key;
#     467                 :            : 
#     468                 :            :             // the key is empty if that was actually a separator byte
#     469                 :            :             // This is a special case for key lengths 0 as those are not allowed (except for separator)
#     470         [ +  + ]:       1024 :             if (key.empty()) {
#     471                 :        509 :                 found_sep = true;
#     472                 :        509 :                 break;
#     473                 :        509 :             }
#     474                 :            : 
#     475                 :            :             // First byte of key is the type
#     476                 :        515 :             unsigned char type = key[0];
#     477                 :            : 
#     478                 :            :             // Do stuff based on type
#     479                 :        515 :             switch(type) {
#     480         [ +  + ]:        511 :                 case PSBT_GLOBAL_UNSIGNED_TX:
#     481                 :        511 :                 {
#     482         [ -  + ]:        511 :                     if (!key_lookup.emplace(key).second) {
#     483                 :          0 :                         throw std::ios_base::failure("Duplicate Key, unsigned tx already provided");
#     484         [ +  + ]:        511 :                     } else if (key.size() != 1) {
#     485                 :          2 :                         throw std::ios_base::failure("Global unsigned tx key is more than one byte type");
#     486                 :          2 :                     }
#     487                 :        509 :                     CMutableTransaction mtx;
#     488                 :            :                     // Set the stream to serialize with non-witness since this should always be non-witness
#     489                 :        509 :                     OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
#     490                 :        509 :                     UnserializeFromVector(os, mtx);
#     491                 :        509 :                     tx = std::move(mtx);
#     492                 :            :                     // Make sure that all scriptSigs and scriptWitnesses are empty
#     493         [ +  + ]:        652 :                     for (const CTxIn& txin : tx->vin) {
#     494 [ +  + ][ -  + ]:        652 :                         if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) {
#     495                 :          2 :                             throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses.");
#     496                 :          2 :                         }
#     497                 :        652 :                     }
#     498                 :        509 :                     break;
#     499                 :        509 :                 }
#     500                 :            :                 // Unknown stuff
#     501         [ +  + ]:        509 :                 default: {
#     502         [ -  + ]:          4 :                     if (unknown.count(key) > 0) {
#     503                 :          0 :                         throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
#     504                 :          0 :                     }
#     505                 :            :                     // Read in the value
#     506                 :          4 :                     std::vector<unsigned char> val_bytes;
#     507                 :          4 :                     s >> val_bytes;
#     508                 :          4 :                     unknown.emplace(std::move(key), std::move(val_bytes));
#     509                 :          4 :                 }
#     510                 :        515 :             }
#     511                 :        515 :         }
#     512                 :            : 
#     513         [ -  + ]:        513 :         if (!found_sep) {
#     514                 :          0 :             throw std::ios_base::failure("Separator is missing at the end of the global map");
#     515                 :          0 :         }
#     516                 :            : 
#     517                 :            :         // Make sure that we got an unsigned tx
#     518         [ +  + ]:        509 :         if (!tx) {
#     519                 :          2 :             throw std::ios_base::failure("No unsigned transcation was provided");
#     520                 :          2 :         }
#     521                 :            : 
#     522                 :            :         // Read input data
#     523                 :        507 :         unsigned int i = 0;
#     524 [ +  + ][ +  + ]:       1153 :         while (!s.empty() && i < tx->vin.size()) {
#     525                 :        646 :             PSBTInput input;
#     526                 :        646 :             s >> input;
#     527                 :        646 :             inputs.push_back(input);
#     528                 :            : 
#     529                 :            :             // Make sure the non-witness utxo matches the outpoint
#     530 [ +  + ][ -  + ]:        646 :             if (input.non_witness_utxo && input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) {
#     531                 :          0 :                 throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash");
#     532                 :          0 :             }
#     533                 :        646 :             ++i;
#     534                 :        646 :         }
#     535                 :            :         // Make sure that the number of inputs matches the number of inputs in the transaction
#     536         [ -  + ]:        507 :         if (inputs.size() != tx->vin.size()) {
#     537                 :          0 :             throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction.");
#     538                 :          0 :         }
#     539                 :            : 
#     540                 :            :         // Read output data
#     541                 :        507 :         i = 0;
#     542 [ +  + ][ +  - ]:       1114 :         while (!s.empty() && i < tx->vout.size()) {
#     543                 :        607 :             PSBTOutput output;
#     544                 :        607 :             s >> output;
#     545                 :        607 :             outputs.push_back(output);
#     546                 :        607 :             ++i;
#     547                 :        607 :         }
#     548                 :            :         // Make sure that the number of outputs matches the number of outputs in the transaction
#     549         [ +  + ]:        507 :         if (outputs.size() != tx->vout.size()) {
#     550                 :          2 :             throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction.");
#     551                 :          2 :         }
#     552                 :        507 :     }
#     553                 :            : 
#     554                 :            :     template <typename Stream>
#     555                 :            :     PartiallySignedTransaction(deserialize_type, Stream& s) {
#     556                 :            :         Unserialize(s);
#     557                 :            :     }
#     558                 :            : };
#     559                 :            : 
#     560                 :            : enum class PSBTRole {
#     561                 :            :     CREATOR,
#     562                 :            :     UPDATER,
#     563                 :            :     SIGNER,
#     564                 :            :     FINALIZER,
#     565                 :            :     EXTRACTOR
#     566                 :            : };
#     567                 :            : 
#     568                 :            : std::string PSBTRoleName(PSBTRole role);
#     569                 :            : 
#     570                 :            : /** Checks whether a PSBTInput is already signed. */
#     571                 :            : bool PSBTInputSigned(const PSBTInput& input);
#     572                 :            : 
#     573                 :            : /** Signs a PSBTInput, verifying that all provided data matches what is being signed. */
#     574                 :            : bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, int sighash = SIGHASH_ALL, SignatureData* out_sigdata = nullptr, bool use_dummy = false);
#     575                 :            : 
#     576                 :            : /** Counts the unsigned inputs of a PSBT. */
#     577                 :            : size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt);
#     578                 :            : 
#     579                 :            : /** Updates a PSBTOutput with information from provider.
#     580                 :            :  *
#     581                 :            :  * This fills in the redeem_script, witness_script, and hd_keypaths where possible.
#     582                 :            :  */
#     583                 :            : void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index);
#     584                 :            : 
#     585                 :            : /**
#     586                 :            :  * Finalizes a PSBT if possible, combining partial signatures.
#     587                 :            :  *
#     588                 :            :  * @param[in,out] psbtx PartiallySignedTransaction to finalize
#     589                 :            :  * return True if the PSBT is now complete, false otherwise
#     590                 :            :  */
#     591                 :            : bool FinalizePSBT(PartiallySignedTransaction& psbtx);
#     592                 :            : 
#     593                 :            : /**
#     594                 :            :  * Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.
#     595                 :            :  *
#     596                 :            :  * @param[in]  psbtx PartiallySignedTransaction
#     597                 :            :  * @param[out] result CMutableTransaction representing the complete transaction, if successful
#     598                 :            :  * @return True if we successfully extracted the transaction, false otherwise
#     599                 :            :  */
#     600                 :            : bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result);
#     601                 :            : 
#     602                 :            : /**
#     603                 :            :  * Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input.
#     604                 :            :  *
#     605                 :            :  * @param[out] out   the combined PSBT, if successful
#     606                 :            :  * @param[in]  psbtxs the PSBTs to combine
#     607                 :            :  * @return error (OK if we successfully combined the transactions, other error if they were not compatible)
#     608                 :            :  */
#     609                 :            : [[nodiscard]] TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs);
#     610                 :            : 
#     611                 :            : //! Decode a base64ed PSBT into a PartiallySignedTransaction
#     612                 :            : [[nodiscard]] bool DecodeBase64PSBT(PartiallySignedTransaction& decoded_psbt, const std::string& base64_psbt, std::string& error);
#     613                 :            : //! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction
#     614                 :            : [[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, const std::string& raw_psbt, std::string& error);
#     615                 :            : 
#     616                 :            : #endif // BITCOIN_PSBT_H

Generated by: LCOV version 1.14