Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2021 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/keyorigin.h>
# 14 : : #include <script/sign.h>
# 15 : : #include <script/signingprovider.h>
# 16 : : #include <span.h>
# 17 : : #include <streams.h>
# 18 : :
# 19 : : #include <optional>
# 20 : :
# 21 : : // Magic bytes
# 22 : : static constexpr uint8_t PSBT_MAGIC_BYTES[5] = {'p', 's', 'b', 't', 0xff};
# 23 : :
# 24 : : // Global types
# 25 : : static constexpr uint8_t PSBT_GLOBAL_UNSIGNED_TX = 0x00;
# 26 : : static constexpr uint8_t PSBT_GLOBAL_XPUB = 0x01;
# 27 : : static constexpr uint8_t PSBT_GLOBAL_VERSION = 0xFB;
# 28 : : static constexpr uint8_t PSBT_GLOBAL_PROPRIETARY = 0xFC;
# 29 : :
# 30 : : // Input types
# 31 : : static constexpr uint8_t PSBT_IN_NON_WITNESS_UTXO = 0x00;
# 32 : : static constexpr uint8_t PSBT_IN_WITNESS_UTXO = 0x01;
# 33 : : static constexpr uint8_t PSBT_IN_PARTIAL_SIG = 0x02;
# 34 : : static constexpr uint8_t PSBT_IN_SIGHASH = 0x03;
# 35 : : static constexpr uint8_t PSBT_IN_REDEEMSCRIPT = 0x04;
# 36 : : static constexpr uint8_t PSBT_IN_WITNESSSCRIPT = 0x05;
# 37 : : static constexpr uint8_t PSBT_IN_BIP32_DERIVATION = 0x06;
# 38 : : static constexpr uint8_t PSBT_IN_SCRIPTSIG = 0x07;
# 39 : : static constexpr uint8_t PSBT_IN_SCRIPTWITNESS = 0x08;
# 40 : : static constexpr uint8_t PSBT_IN_RIPEMD160 = 0x0A;
# 41 : : static constexpr uint8_t PSBT_IN_SHA256 = 0x0B;
# 42 : : static constexpr uint8_t PSBT_IN_HASH160 = 0x0C;
# 43 : : static constexpr uint8_t PSBT_IN_HASH256 = 0x0D;
# 44 : : static constexpr uint8_t PSBT_IN_PROPRIETARY = 0xFC;
# 45 : :
# 46 : : // Output types
# 47 : : static constexpr uint8_t PSBT_OUT_REDEEMSCRIPT = 0x00;
# 48 : : static constexpr uint8_t PSBT_OUT_WITNESSSCRIPT = 0x01;
# 49 : : static constexpr uint8_t PSBT_OUT_BIP32_DERIVATION = 0x02;
# 50 : : static constexpr uint8_t PSBT_OUT_PROPRIETARY = 0xFC;
# 51 : :
# 52 : : // The separator is 0x00. Reading this in means that the unserializer can interpret it
# 53 : : // as a 0 length key which indicates that this is the separator. The separator has no value.
# 54 : : static constexpr uint8_t PSBT_SEPARATOR = 0x00;
# 55 : :
# 56 : : // BIP 174 does not specify a maximum file size, but we set a limit anyway
# 57 : : // to prevent reading a stream indefinitely and running out of memory.
# 58 : : const std::streamsize MAX_FILE_SIZE_PSBT = 100000000; // 100 MiB
# 59 : :
# 60 : : // PSBT version number
# 61 : : static constexpr uint32_t PSBT_HIGHEST_VERSION = 0;
# 62 : :
# 63 : : /** A structure for PSBT proprietary types */
# 64 : : struct PSBTProprietary
# 65 : : {
# 66 : : uint64_t subtype;
# 67 : : std::vector<unsigned char> identifier;
# 68 : : std::vector<unsigned char> key;
# 69 : : std::vector<unsigned char> value;
# 70 : :
# 71 : 8 : bool operator<(const PSBTProprietary &b) const {
# 72 : 8 : return key < b.key;
# 73 : 8 : }
# 74 : 0 : bool operator==(const PSBTProprietary &b) const {
# 75 : 0 : return key == b.key;
# 76 : 0 : }
# 77 : : };
# 78 : :
# 79 : : // Takes a stream and multiple arguments and serializes them as if first serialized into a vector and then into the stream
# 80 : : // The resulting output into the stream has the total serialized length of all of the objects followed by all objects concatenated with each other.
# 81 : : template<typename Stream, typename... X>
# 82 : : void SerializeToVector(Stream& s, const X&... args)
# 83 : 6496 : {
# 84 : 6496 : WriteCompactSize(s, GetSerializeSizeMany(s.GetVersion(), args...));
# 85 : 6496 : SerializeMany(s, args...);
# 86 : 6496 : }
# 87 : :
# 88 : : // Takes a stream and multiple arguments and unserializes them first as a vector then each object individually in the order provided in the arguments
# 89 : : template<typename Stream, typename... X>
# 90 : : void UnserializeFromVector(Stream& s, X&... args)
# 91 : 2236 : {
# 92 : 2236 : size_t expected_size = ReadCompactSize(s);
# 93 : 2236 : size_t remaining_before = s.size();
# 94 : 2236 : UnserializeMany(s, args...);
# 95 : 2236 : size_t remaining_after = s.size();
# 96 [ - + ][ - + ]: 2236 : if (remaining_after + expected_size != remaining_before) {
# [ - + ][ - + ]
# [ - + ][ - + ]
# 97 : 0 : throw std::ios_base::failure("Size of value was not the stated size");
# 98 : 0 : }
# 99 : 2236 : }
# 100 : :
# 101 : : // Deserialize an individual HD keypath to a stream
# 102 : : template<typename Stream>
# 103 : : void DeserializeHDKeypath(Stream& s, KeyOriginInfo& hd_keypath)
# 104 : 1101 : {
# 105 : : // Read in key path
# 106 : 1101 : uint64_t value_len = ReadCompactSize(s);
# 107 [ - + ][ - + ]: 1101 : if (value_len % 4 || value_len == 0) {
# 108 : 0 : throw std::ios_base::failure("Invalid length for HD key path");
# 109 : 0 : }
# 110 : :
# 111 : 1101 : s >> hd_keypath.fingerprint;
# 112 [ + + ]: 3783 : for (unsigned int i = 4; i < value_len; i += sizeof(uint32_t)) {
# 113 : 2682 : uint32_t index;
# 114 : 2682 : s >> index;
# 115 : 2682 : hd_keypath.path.push_back(index);
# 116 : 2682 : }
# 117 : 1101 : }
# 118 : :
# 119 : : // Deserialize HD keypaths into a map
# 120 : : template<typename Stream>
# 121 : : void DeserializeHDKeypaths(Stream& s, const std::vector<unsigned char>& key, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
# 122 : 1099 : {
# 123 : : // Make sure that the key is the size of pubkey + 1
# 124 [ + - ][ + + ]: 1099 : if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
# 125 : 4 : throw std::ios_base::failure("Size of key was not the expected size for the type BIP32 keypath");
# 126 : 4 : }
# 127 : : // Read in the pubkey from key
# 128 : 1095 : CPubKey pubkey(key.begin() + 1, key.end());
# 129 [ - + ]: 1095 : if (!pubkey.IsFullyValid()) {
# 130 : 0 : throw std::ios_base::failure("Invalid pubkey");
# 131 : 0 : }
# 132 [ - + ]: 1095 : if (hd_keypaths.count(pubkey) > 0) {
# 133 : 0 : throw std::ios_base::failure("Duplicate Key, pubkey derivation path already provided");
# 134 : 0 : }
# 135 : :
# 136 : 1095 : KeyOriginInfo keypath;
# 137 : 1095 : DeserializeHDKeypath(s, keypath);
# 138 : :
# 139 : : // Add to map
# 140 : 1095 : hd_keypaths.emplace(pubkey, std::move(keypath));
# 141 : 1095 : }
# 142 : :
# 143 : : // Serialize an individual HD keypath to a stream
# 144 : : template<typename Stream>
# 145 : : void SerializeHDKeypath(Stream& s, KeyOriginInfo hd_keypath)
# 146 : 1242 : {
# 147 : 1242 : WriteCompactSize(s, (hd_keypath.path.size() + 1) * sizeof(uint32_t));
# 148 : 1242 : s << hd_keypath.fingerprint;
# 149 [ + + ]: 3297 : for (const auto& path : hd_keypath.path) {
# 150 : 3297 : s << path;
# 151 : 3297 : }
# 152 : 1242 : }
# 153 : :
# 154 : : // Serialize HD keypaths to a stream from a map
# 155 : : template<typename Stream>
# 156 : : void SerializeHDKeypaths(Stream& s, const std::map<CPubKey, KeyOriginInfo>& hd_keypaths, CompactSizeWriter type)
# 157 : 1756 : {
# 158 [ + + ]: 1756 : for (auto keypath_pair : hd_keypaths) {
# 159 [ - + ]: 1242 : if (!keypath_pair.first.IsValid()) {
# 160 : 0 : throw std::ios_base::failure("Invalid CPubKey being serialized");
# 161 : 0 : }
# 162 : 1242 : SerializeToVector(s, type, Span{keypath_pair.first});
# 163 : 1242 : SerializeHDKeypath(s, keypath_pair.second);
# 164 : 1242 : }
# 165 : 1756 : }
# 166 : :
# 167 : : /** A structure for PSBTs which contain per-input information */
# 168 : : struct PSBTInput
# 169 : : {
# 170 : : CTransactionRef non_witness_utxo;
# 171 : : CTxOut witness_utxo;
# 172 : : CScript redeem_script;
# 173 : : CScript witness_script;
# 174 : : CScript final_script_sig;
# 175 : : CScriptWitness final_script_witness;
# 176 : : std::map<CPubKey, KeyOriginInfo> hd_keypaths;
# 177 : : std::map<CKeyID, SigPair> partial_sigs;
# 178 : : std::map<uint160, std::vector<unsigned char>> ripemd160_preimages;
# 179 : : std::map<uint256, std::vector<unsigned char>> sha256_preimages;
# 180 : : std::map<uint160, std::vector<unsigned char>> hash160_preimages;
# 181 : : std::map<uint256, std::vector<unsigned char>> hash256_preimages;
# 182 : : std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
# 183 : : std::set<PSBTProprietary> m_proprietary;
# 184 : : std::optional<int> sighash_type;
# 185 : :
# 186 : : bool IsNull() const;
# 187 : : void FillSignatureData(SignatureData& sigdata) const;
# 188 : : void FromSignatureData(const SignatureData& sigdata);
# 189 : : void Merge(const PSBTInput& input);
# 190 : 1639 : PSBTInput() {}
# 191 : :
# 192 : : template <typename Stream>
# 193 : 1023 : inline void Serialize(Stream& s) const {
# 194 : : // Write the utxo
# 195 [ + + ]: 1023 : if (non_witness_utxo) {
# 196 : 801 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_NON_WITNESS_UTXO));
# 197 : 801 : OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
# 198 : 801 : SerializeToVector(os, non_witness_utxo);
# 199 : 801 : }
# 200 [ + + ]: 1023 : if (!witness_utxo.IsNull()) {
# 201 : 609 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESS_UTXO));
# 202 : 609 : SerializeToVector(s, witness_utxo);
# 203 : 609 : }
# 204 : :
# 205 [ + + ][ + + ]: 1023 : if (final_script_sig.empty() && final_script_witness.IsNull()) {
# 206 : : // Write any partial signatures
# 207 [ + + ]: 714 : for (auto sig_pair : partial_sigs) {
# 208 : 43 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_PARTIAL_SIG), Span{sig_pair.second.first});
# 209 : 43 : s << sig_pair.second.second;
# 210 : 43 : }
# 211 : :
# 212 : : // Write the sighash type
# 213 [ + + ]: 714 : if (sighash_type != std::nullopt) {
# 214 : 24 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_SIGHASH));
# 215 : 24 : SerializeToVector(s, *sighash_type);
# 216 : 24 : }
# 217 : :
# 218 : : // Write the redeem script
# 219 [ + + ]: 714 : if (!redeem_script.empty()) {
# 220 : 139 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_REDEEMSCRIPT));
# 221 : 139 : s << redeem_script;
# 222 : 139 : }
# 223 : :
# 224 : : // Write the witness script
# 225 [ + + ]: 714 : if (!witness_script.empty()) {
# 226 : 102 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_WITNESSSCRIPT));
# 227 : 102 : s << witness_script;
# 228 : 102 : }
# 229 : :
# 230 : : // Write any hd keypaths
# 231 : 714 : SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_IN_BIP32_DERIVATION));
# 232 : :
# 233 : : // Write any ripemd160 preimage
# 234 [ - + ]: 714 : for (const auto& [hash, preimage] : ripemd160_preimages) {
# 235 : 0 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_RIPEMD160), Span{hash});
# 236 : 0 : s << preimage;
# 237 : 0 : }
# 238 : :
# 239 : : // Write any sha256 preimage
# 240 [ - + ]: 714 : for (const auto& [hash, preimage] : sha256_preimages) {
# 241 : 0 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_SHA256), Span{hash});
# 242 : 0 : s << preimage;
# 243 : 0 : }
# 244 : :
# 245 : : // Write any hash160 preimage
# 246 [ - + ]: 714 : for (const auto& [hash, preimage] : hash160_preimages) {
# 247 : 0 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH160), Span{hash});
# 248 : 0 : s << preimage;
# 249 : 0 : }
# 250 : :
# 251 : : // Write any hash256 preimage
# 252 [ - + ]: 714 : for (const auto& [hash, preimage] : hash256_preimages) {
# 253 : 0 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_HASH256), Span{hash});
# 254 : 0 : s << preimage;
# 255 : 0 : }
# 256 : 714 : }
# 257 : :
# 258 : : // Write script sig
# 259 [ + + ]: 1023 : if (!final_script_sig.empty()) {
# 260 : 109 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_SCRIPTSIG));
# 261 : 109 : s << final_script_sig;
# 262 : 109 : }
# 263 : : // write script witness
# 264 [ + + ]: 1023 : if (!final_script_witness.IsNull()) {
# 265 : 214 : SerializeToVector(s, CompactSizeWriter(PSBT_IN_SCRIPTWITNESS));
# 266 : 214 : SerializeToVector(s, final_script_witness.stack);
# 267 : 214 : }
# 268 : :
# 269 : : // Write proprietary things
# 270 [ - + ]: 1023 : for (const auto& entry : m_proprietary) {
# 271 : 0 : s << entry.key;
# 272 : 0 : s << entry.value;
# 273 : 0 : }
# 274 : :
# 275 : : // Write unknown things
# 276 [ + + ]: 1023 : for (auto& entry : unknown) {
# 277 : 6 : s << entry.first;
# 278 : 6 : s << entry.second;
# 279 : 6 : }
# 280 : :
# 281 : 1023 : s << PSBT_SEPARATOR;
# 282 : 1023 : }
# 283 : :
# 284 : :
# 285 : : template <typename Stream>
# 286 : 1014 : inline void Unserialize(Stream& s) {
# 287 : : // Used for duplicate key detection
# 288 : 1014 : std::set<std::vector<unsigned char>> key_lookup;
# 289 : :
# 290 : : // Read loop
# 291 : 1014 : bool found_sep = false;
# 292 [ + - ]: 3462 : while(!s.empty()) {
# 293 : : // Read
# 294 : 3462 : std::vector<unsigned char> key;
# 295 : 3462 : s >> key;
# 296 : :
# 297 : : // the key is empty if that was actually a separator byte
# 298 : : // This is a special case for key lengths 0 as those are not allowed (except for separator)
# 299 [ + + ]: 3462 : if (key.empty()) {
# 300 : 982 : found_sep = true;
# 301 : 982 : break;
# 302 : 982 : }
# 303 : :
# 304 : : // Type is compact size uint at beginning of key
# 305 : 2480 : SpanReader skey(s.GetType(), s.GetVersion(), key);
# 306 : 2480 : uint64_t type = ReadCompactSize(skey);
# 307 : :
# 308 : : // Do stuff based on type
# 309 : 2480 : switch(type) {
# 310 [ + + ]: 700 : case PSBT_IN_NON_WITNESS_UTXO:
# 311 : 700 : {
# 312 [ + + ]: 700 : if (!key_lookup.emplace(key).second) {
# 313 : 2 : throw std::ios_base::failure("Duplicate Key, input non-witness utxo already provided");
# 314 [ + + ]: 698 : } else if (key.size() != 1) {
# 315 : 2 : throw std::ios_base::failure("Non-witness utxo key is more than one byte type");
# 316 : 2 : }
# 317 : : // Set the stream to unserialize with witness since this is always a valid network transaction
# 318 : 696 : OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() & ~SERIALIZE_TRANSACTION_NO_WITNESS);
# 319 : 696 : UnserializeFromVector(os, non_witness_utxo);
# 320 : 696 : break;
# 321 : 700 : }
# 322 [ + + ]: 579 : case PSBT_IN_WITNESS_UTXO:
# 323 [ + + ]: 579 : if (!key_lookup.emplace(key).second) {
# 324 : 2 : throw std::ios_base::failure("Duplicate Key, input witness utxo already provided");
# 325 [ + + ]: 577 : } else if (key.size() != 1) {
# 326 : 2 : throw std::ios_base::failure("Witness utxo key is more than one byte type");
# 327 : 2 : }
# 328 : 575 : UnserializeFromVector(s, witness_utxo);
# 329 : 575 : break;
# 330 [ + + ]: 56 : case PSBT_IN_PARTIAL_SIG:
# 331 : 56 : {
# 332 : : // Make sure that the key is the size of pubkey + 1
# 333 [ + - ][ + + ]: 56 : if (key.size() != CPubKey::SIZE + 1 && key.size() != CPubKey::COMPRESSED_SIZE + 1) {
# 334 : 2 : throw std::ios_base::failure("Size of key was not the expected size for the type partial signature pubkey");
# 335 : 2 : }
# 336 : : // Read in the pubkey from key
# 337 : 54 : CPubKey pubkey(key.begin() + 1, key.end());
# 338 [ - + ]: 54 : if (!pubkey.IsFullyValid()) {
# 339 : 0 : throw std::ios_base::failure("Invalid pubkey");
# 340 : 0 : }
# 341 [ - + ]: 54 : if (partial_sigs.count(pubkey.GetID()) > 0) {
# 342 : 0 : throw std::ios_base::failure("Duplicate Key, input partial signature for pubkey already provided");
# 343 : 0 : }
# 344 : :
# 345 : : // Read in the signature from value
# 346 : 54 : std::vector<unsigned char> sig;
# 347 : 54 : s >> sig;
# 348 : :
# 349 : : // Add to list
# 350 : 54 : partial_sigs.emplace(pubkey.GetID(), SigPair(pubkey, std::move(sig)));
# 351 : 54 : break;
# 352 : 54 : }
# 353 [ + + ]: 44 : case PSBT_IN_SIGHASH:
# 354 [ + + ]: 44 : if (!key_lookup.emplace(key).second) {
# 355 : 2 : throw std::ios_base::failure("Duplicate Key, input sighash type already provided");
# 356 [ + + ]: 42 : } else if (key.size() != 1) {
# 357 : 2 : throw std::ios_base::failure("Sighash type key is more than one byte type");
# 358 : 2 : }
# 359 : 40 : int sighash;
# 360 : 40 : UnserializeFromVector(s, sighash);
# 361 : 40 : sighash_type = sighash;
# 362 : 40 : break;
# 363 [ + + ]: 131 : case PSBT_IN_REDEEMSCRIPT:
# 364 : 131 : {
# 365 [ + + ]: 131 : if (!key_lookup.emplace(key).second) {
# 366 : 2 : throw std::ios_base::failure("Duplicate Key, input redeemScript already provided");
# 367 [ + + ]: 129 : } else if (key.size() != 1) {
# 368 : 2 : throw std::ios_base::failure("Input redeemScript key is more than one byte type");
# 369 : 2 : }
# 370 : 127 : s >> redeem_script;
# 371 : 127 : break;
# 372 : 131 : }
# 373 [ + + ]: 122 : case PSBT_IN_WITNESSSCRIPT:
# 374 : 122 : {
# 375 [ + + ]: 122 : if (!key_lookup.emplace(key).second) {
# 376 : 2 : throw std::ios_base::failure("Duplicate Key, input witnessScript already provided");
# 377 [ + + ]: 120 : } else if (key.size() != 1) {
# 378 : 2 : throw std::ios_base::failure("Input witnessScript key is more than one byte type");
# 379 : 2 : }
# 380 : 118 : s >> witness_script;
# 381 : 118 : break;
# 382 : 122 : }
# 383 [ + + ]: 488 : case PSBT_IN_BIP32_DERIVATION:
# 384 : 488 : {
# 385 : 488 : DeserializeHDKeypaths(s, key, hd_keypaths);
# 386 : 488 : break;
# 387 : 122 : }
# 388 [ + + ]: 115 : case PSBT_IN_SCRIPTSIG:
# 389 : 115 : {
# 390 [ + + ]: 115 : if (!key_lookup.emplace(key).second) {
# 391 : 2 : throw std::ios_base::failure("Duplicate Key, input final scriptSig already provided");
# 392 [ + + ]: 113 : } else if (key.size() != 1) {
# 393 : 2 : throw std::ios_base::failure("Final scriptSig key is more than one byte type");
# 394 : 2 : }
# 395 : 111 : s >> final_script_sig;
# 396 : 111 : break;
# 397 : 115 : }
# 398 [ + + ]: 221 : case PSBT_IN_SCRIPTWITNESS:
# 399 : 221 : {
# 400 [ + + ]: 221 : if (!key_lookup.emplace(key).second) {
# 401 : 2 : throw std::ios_base::failure("Duplicate Key, input final scriptWitness already provided");
# 402 [ + + ]: 219 : } else if (key.size() != 1) {
# 403 : 2 : throw std::ios_base::failure("Final scriptWitness key is more than one byte type");
# 404 : 2 : }
# 405 : 217 : UnserializeFromVector(s, final_script_witness.stack);
# 406 : 217 : break;
# 407 : 221 : }
# 408 [ + + ]: 4 : case PSBT_IN_RIPEMD160:
# 409 : 4 : {
# 410 : : // Make sure that the key is the size of a ripemd160 hash + 1
# 411 [ - + ]: 4 : if (key.size() != CRIPEMD160::OUTPUT_SIZE + 1) {
# 412 : 0 : throw std::ios_base::failure("Size of key was not the expected size for the type ripemd160 preimage");
# 413 : 0 : }
# 414 : : // Read in the hash from key
# 415 : 4 : std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
# 416 : 4 : uint160 hash(hash_vec);
# 417 [ - + ]: 4 : if (ripemd160_preimages.count(hash) > 0) {
# 418 : 0 : throw std::ios_base::failure("Duplicate Key, input ripemd160 preimage already provided");
# 419 : 0 : }
# 420 : :
# 421 : : // Read in the preimage from value
# 422 : 4 : std::vector<unsigned char> preimage;
# 423 : 4 : s >> preimage;
# 424 : :
# 425 : : // Add to preimages list
# 426 : 4 : ripemd160_preimages.emplace(hash, std::move(preimage));
# 427 : 4 : break;
# 428 : 4 : }
# 429 [ + + ]: 4 : case PSBT_IN_SHA256:
# 430 : 4 : {
# 431 : : // Make sure that the key is the size of a sha256 hash + 1
# 432 [ - + ]: 4 : if (key.size() != CSHA256::OUTPUT_SIZE + 1) {
# 433 : 0 : throw std::ios_base::failure("Size of key was not the expected size for the type sha256 preimage");
# 434 : 0 : }
# 435 : : // Read in the hash from key
# 436 : 4 : std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
# 437 : 4 : uint256 hash(hash_vec);
# 438 [ - + ]: 4 : if (sha256_preimages.count(hash) > 0) {
# 439 : 0 : throw std::ios_base::failure("Duplicate Key, input sha256 preimage already provided");
# 440 : 0 : }
# 441 : :
# 442 : : // Read in the preimage from value
# 443 : 4 : std::vector<unsigned char> preimage;
# 444 : 4 : s >> preimage;
# 445 : :
# 446 : : // Add to preimages list
# 447 : 4 : sha256_preimages.emplace(hash, std::move(preimage));
# 448 : 4 : break;
# 449 : 4 : }
# 450 [ + + ]: 4 : case PSBT_IN_HASH160:
# 451 : 4 : {
# 452 : : // Make sure that the key is the size of a hash160 hash + 1
# 453 [ - + ]: 4 : if (key.size() != CHash160::OUTPUT_SIZE + 1) {
# 454 : 0 : throw std::ios_base::failure("Size of key was not the expected size for the type hash160 preimage");
# 455 : 0 : }
# 456 : : // Read in the hash from key
# 457 : 4 : std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
# 458 : 4 : uint160 hash(hash_vec);
# 459 [ - + ]: 4 : if (hash160_preimages.count(hash) > 0) {
# 460 : 0 : throw std::ios_base::failure("Duplicate Key, input hash160 preimage already provided");
# 461 : 0 : }
# 462 : :
# 463 : : // Read in the preimage from value
# 464 : 4 : std::vector<unsigned char> preimage;
# 465 : 4 : s >> preimage;
# 466 : :
# 467 : : // Add to preimages list
# 468 : 4 : hash160_preimages.emplace(hash, std::move(preimage));
# 469 : 4 : break;
# 470 : 4 : }
# 471 [ + + ]: 4 : case PSBT_IN_HASH256:
# 472 : 4 : {
# 473 : : // Make sure that the key is the size of a hash256 hash + 1
# 474 [ - + ]: 4 : if (key.size() != CHash256::OUTPUT_SIZE + 1) {
# 475 : 0 : throw std::ios_base::failure("Size of key was not the expected size for the type hash256 preimage");
# 476 : 0 : }
# 477 : : // Read in the hash from key
# 478 : 4 : std::vector<unsigned char> hash_vec(key.begin() + 1, key.end());
# 479 : 4 : uint256 hash(hash_vec);
# 480 [ - + ]: 4 : if (hash256_preimages.count(hash) > 0) {
# 481 : 0 : throw std::ios_base::failure("Duplicate Key, input hash256 preimage already provided");
# 482 : 0 : }
# 483 : :
# 484 : : // Read in the preimage from value
# 485 : 4 : std::vector<unsigned char> preimage;
# 486 : 4 : s >> preimage;
# 487 : :
# 488 : : // Add to preimages list
# 489 : 4 : hash256_preimages.emplace(hash, std::move(preimage));
# 490 : 4 : break;
# 491 : 4 : }
# 492 [ + + ]: 2 : case PSBT_IN_PROPRIETARY:
# 493 : 2 : {
# 494 : 2 : PSBTProprietary this_prop;
# 495 : 2 : skey >> this_prop.identifier;
# 496 : 2 : this_prop.subtype = ReadCompactSize(skey);
# 497 : 2 : this_prop.key = key;
# 498 : :
# 499 [ - + ]: 2 : if (m_proprietary.count(this_prop) > 0) {
# 500 : 0 : throw std::ios_base::failure("Duplicate Key, proprietary key already found");
# 501 : 0 : }
# 502 : 2 : s >> this_prop.value;
# 503 : 2 : m_proprietary.insert(this_prop);
# 504 : 2 : break;
# 505 : 2 : }
# 506 : : // Unknown stuff
# 507 [ + + ]: 6 : default:
# 508 [ - + ]: 6 : if (unknown.count(key) > 0) {
# 509 : 0 : throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
# 510 : 0 : }
# 511 : : // Read in the value
# 512 : 6 : std::vector<unsigned char> val_bytes;
# 513 : 6 : s >> val_bytes;
# 514 : 6 : unknown.emplace(std::move(key), std::move(val_bytes));
# 515 : 6 : break;
# 516 : 2480 : }
# 517 : 2480 : }
# 518 : :
# 519 [ - + ]: 982 : if (!found_sep) {
# 520 : 0 : throw std::ios_base::failure("Separator is missing at the end of an input map");
# 521 : 0 : }
# 522 : 982 : }
# 523 : :
# 524 : : template <typename Stream>
# 525 : : PSBTInput(deserialize_type, Stream& s) {
# 526 : : Unserialize(s);
# 527 : : }
# 528 : : };
# 529 : :
# 530 : : /** A structure for PSBTs which contains per output information */
# 531 : : struct PSBTOutput
# 532 : : {
# 533 : : CScript redeem_script;
# 534 : : CScript witness_script;
# 535 : : std::map<CPubKey, KeyOriginInfo> hd_keypaths;
# 536 : : std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
# 537 : : std::set<PSBTProprietary> m_proprietary;
# 538 : :
# 539 : : bool IsNull() const;
# 540 : : void FillSignatureData(SignatureData& sigdata) const;
# 541 : : void FromSignatureData(const SignatureData& sigdata);
# 542 : : void Merge(const PSBTOutput& output);
# 543 : 1653 : PSBTOutput() {}
# 544 : :
# 545 : : template <typename Stream>
# 546 : 1042 : inline void Serialize(Stream& s) const {
# 547 : : // Write the redeem script
# 548 [ + + ]: 1042 : if (!redeem_script.empty()) {
# 549 : 88 : SerializeToVector(s, CompactSizeWriter(PSBT_OUT_REDEEMSCRIPT));
# 550 : 88 : s << redeem_script;
# 551 : 88 : }
# 552 : :
# 553 : : // Write the witness script
# 554 [ + + ]: 1042 : if (!witness_script.empty()) {
# 555 : 133 : SerializeToVector(s, CompactSizeWriter(PSBT_OUT_WITNESSSCRIPT));
# 556 : 133 : s << witness_script;
# 557 : 133 : }
# 558 : :
# 559 : : // Write any hd keypaths
# 560 : 1042 : SerializeHDKeypaths(s, hd_keypaths, CompactSizeWriter(PSBT_OUT_BIP32_DERIVATION));
# 561 : :
# 562 : : // Write proprietary things
# 563 [ - + ]: 1042 : for (const auto& entry : m_proprietary) {
# 564 : 0 : s << entry.key;
# 565 : 0 : s << entry.value;
# 566 : 0 : }
# 567 : :
# 568 : : // Write unknown things
# 569 [ + + ]: 1042 : for (auto& entry : unknown) {
# 570 : 4 : s << entry.first;
# 571 : 4 : s << entry.second;
# 572 : 4 : }
# 573 : :
# 574 : 1042 : s << PSBT_SEPARATOR;
# 575 : 1042 : }
# 576 : :
# 577 : :
# 578 : : template <typename Stream>
# 579 : 1006 : inline void Unserialize(Stream& s) {
# 580 : : // Used for duplicate key detection
# 581 : 1006 : std::set<std::vector<unsigned char>> key_lookup;
# 582 : :
# 583 : : // Read loop
# 584 : 1006 : bool found_sep = false;
# 585 [ + - ]: 1855 : while(!s.empty()) {
# 586 : : // Read
# 587 : 1855 : std::vector<unsigned char> key;
# 588 : 1855 : s >> key;
# 589 : :
# 590 : : // the key is empty if that was actually a separator byte
# 591 : : // This is a special case for key lengths 0 as those are not allowed (except for separator)
# 592 [ + + ]: 1855 : if (key.empty()) {
# 593 : 996 : found_sep = true;
# 594 : 996 : break;
# 595 : 996 : }
# 596 : :
# 597 : : // Type is compact size uint at beginning of key
# 598 : 859 : SpanReader skey(s.GetType(), s.GetVersion(), key);
# 599 : 859 : uint64_t type = ReadCompactSize(skey);
# 600 : :
# 601 : : // Do stuff based on type
# 602 : 859 : switch(type) {
# 603 [ + + ]: 98 : case PSBT_OUT_REDEEMSCRIPT:
# 604 : 98 : {
# 605 [ + + ]: 98 : if (!key_lookup.emplace(key).second) {
# 606 : 2 : throw std::ios_base::failure("Duplicate Key, output redeemScript already provided");
# 607 [ + + ]: 96 : } else if (key.size() != 1) {
# 608 : 2 : throw std::ios_base::failure("Output redeemScript key is more than one byte type");
# 609 : 2 : }
# 610 : 94 : s >> redeem_script;
# 611 : 94 : break;
# 612 : 98 : }
# 613 [ + + ]: 144 : case PSBT_OUT_WITNESSSCRIPT:
# 614 : 144 : {
# 615 [ + + ]: 144 : if (!key_lookup.emplace(key).second) {
# 616 : 2 : throw std::ios_base::failure("Duplicate Key, output witnessScript already provided");
# 617 [ + + ]: 142 : } else if (key.size() != 1) {
# 618 : 2 : throw std::ios_base::failure("Output witnessScript key is more than one byte type");
# 619 : 2 : }
# 620 : 140 : s >> witness_script;
# 621 : 140 : break;
# 622 : 144 : }
# 623 [ + + ]: 611 : case PSBT_OUT_BIP32_DERIVATION:
# 624 : 611 : {
# 625 : 611 : DeserializeHDKeypaths(s, key, hd_keypaths);
# 626 : 611 : break;
# 627 : 144 : }
# 628 [ + + ]: 2 : case PSBT_OUT_PROPRIETARY:
# 629 : 2 : {
# 630 : 2 : PSBTProprietary this_prop;
# 631 : 2 : skey >> this_prop.identifier;
# 632 : 2 : this_prop.subtype = ReadCompactSize(skey);
# 633 : 2 : this_prop.key = key;
# 634 : :
# 635 [ - + ]: 2 : if (m_proprietary.count(this_prop) > 0) {
# 636 : 0 : throw std::ios_base::failure("Duplicate Key, proprietary key already found");
# 637 : 0 : }
# 638 : 2 : s >> this_prop.value;
# 639 : 2 : m_proprietary.insert(this_prop);
# 640 : 2 : break;
# 641 : 2 : }
# 642 : : // Unknown stuff
# 643 [ + + ]: 4 : default: {
# 644 [ - + ]: 4 : if (unknown.count(key) > 0) {
# 645 : 0 : throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
# 646 : 0 : }
# 647 : : // Read in the value
# 648 : 4 : std::vector<unsigned char> val_bytes;
# 649 : 4 : s >> val_bytes;
# 650 : 4 : unknown.emplace(std::move(key), std::move(val_bytes));
# 651 : 4 : break;
# 652 : 4 : }
# 653 : 859 : }
# 654 : 859 : }
# 655 : :
# 656 [ - + ]: 996 : if (!found_sep) {
# 657 : 0 : throw std::ios_base::failure("Separator is missing at the end of an output map");
# 658 : 0 : }
# 659 : 996 : }
# 660 : :
# 661 : : template <typename Stream>
# 662 : : PSBTOutput(deserialize_type, Stream& s) {
# 663 : : Unserialize(s);
# 664 : : }
# 665 : : };
# 666 : :
# 667 : : /** A version of CTransaction with the PSBT format*/
# 668 : : struct PartiallySignedTransaction
# 669 : : {
# 670 : : std::optional<CMutableTransaction> tx;
# 671 : : // We use a vector of CExtPubKey in the event that there happens to be the same KeyOriginInfos for different CExtPubKeys
# 672 : : // Note that this map swaps the key and values from the serialization
# 673 : : std::map<KeyOriginInfo, std::set<CExtPubKey>> m_xpubs;
# 674 : : std::vector<PSBTInput> inputs;
# 675 : : std::vector<PSBTOutput> outputs;
# 676 : : std::map<std::vector<unsigned char>, std::vector<unsigned char>> unknown;
# 677 : : std::optional<uint32_t> m_version;
# 678 : : std::set<PSBTProprietary> m_proprietary;
# 679 : :
# 680 : : bool IsNull() const;
# 681 : : uint32_t GetVersion() const;
# 682 : :
# 683 : : /** Merge psbt into this. The two psbts must have the same underlying CTransaction (i.e. the
# 684 : : * same actual Bitcoin transaction.) Returns true if the merge succeeded, false otherwise. */
# 685 : : [[nodiscard]] bool Merge(const PartiallySignedTransaction& psbt);
# 686 : : bool AddInput(const CTxIn& txin, PSBTInput& psbtin);
# 687 : : bool AddOutput(const CTxOut& txout, const PSBTOutput& psbtout);
# 688 : 861 : PartiallySignedTransaction() {}
# 689 : : explicit PartiallySignedTransaction(const CMutableTransaction& tx);
# 690 : : /**
# 691 : : * Finds the UTXO for a given input index
# 692 : : *
# 693 : : * @param[out] utxo The UTXO of the input if found
# 694 : : * @param[in] input_index Index of the input to retrieve the UTXO of
# 695 : : * @return Whether the UTXO for the specified input was found
# 696 : : */
# 697 : : bool GetInputUTXO(CTxOut& utxo, int input_index) const;
# 698 : :
# 699 : : template <typename Stream>
# 700 : 672 : inline void Serialize(Stream& s) const {
# 701 : :
# 702 : : // magic bytes
# 703 : 672 : s << PSBT_MAGIC_BYTES;
# 704 : :
# 705 : : // unsigned tx flag
# 706 : 672 : SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_UNSIGNED_TX));
# 707 : :
# 708 : : // Write serialized tx to a stream
# 709 : 672 : OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
# 710 : 672 : SerializeToVector(os, *tx);
# 711 : :
# 712 : : // Write xpubs
# 713 [ - + ]: 672 : for (const auto& xpub_pair : m_xpubs) {
# 714 [ # # ]: 0 : for (const auto& xpub : xpub_pair.second) {
# 715 : 0 : unsigned char ser_xpub[BIP32_EXTKEY_WITH_VERSION_SIZE];
# 716 : 0 : xpub.EncodeWithVersion(ser_xpub);
# 717 : : // Note that the serialization swaps the key and value
# 718 : : // The xpub is the key (for uniqueness) while the path is the value
# 719 : 0 : SerializeToVector(s, PSBT_GLOBAL_XPUB, ser_xpub);
# 720 : 0 : SerializeHDKeypath(s, xpub_pair.first);
# 721 : 0 : }
# 722 : 0 : }
# 723 : :
# 724 : : // PSBT version
# 725 [ - + ]: 672 : if (GetVersion() > 0) {
# 726 : 0 : SerializeToVector(s, CompactSizeWriter(PSBT_GLOBAL_VERSION));
# 727 : 0 : SerializeToVector(s, *m_version);
# 728 : 0 : }
# 729 : :
# 730 : : // Write proprietary things
# 731 [ + + ]: 672 : for (const auto& entry : m_proprietary) {
# 732 : 2 : s << entry.key;
# 733 : 2 : s << entry.value;
# 734 : 2 : }
# 735 : :
# 736 : : // Write the unknown things
# 737 [ + + ]: 672 : for (auto& entry : unknown) {
# 738 : 4 : s << entry.first;
# 739 : 4 : s << entry.second;
# 740 : 4 : }
# 741 : :
# 742 : : // Separator
# 743 : 672 : s << PSBT_SEPARATOR;
# 744 : :
# 745 : : // Write inputs
# 746 [ + + ]: 1023 : for (const PSBTInput& input : inputs) {
# 747 : 1023 : s << input;
# 748 : 1023 : }
# 749 : : // Write outputs
# 750 [ + + ]: 1042 : for (const PSBTOutput& output : outputs) {
# 751 : 1042 : s << output;
# 752 : 1042 : }
# 753 : 672 : }
# 754 : :
# 755 : :
# 756 : : template <typename Stream>
# 757 : 714 : inline void Unserialize(Stream& s) {
# 758 : : // Read the magic bytes
# 759 : 714 : uint8_t magic[5];
# 760 : 714 : s >> magic;
# 761 [ + + ]: 714 : if (!std::equal(magic, magic + 5, PSBT_MAGIC_BYTES)) {
# 762 : 4 : throw std::ios_base::failure("Invalid PSBT magic bytes");
# 763 : 4 : }
# 764 : :
# 765 : : // Used for duplicate key detection
# 766 : 710 : std::set<std::vector<unsigned char>> key_lookup;
# 767 : :
# 768 : : // Track the global xpubs we have already seen. Just for sanity checking
# 769 : 710 : std::set<CExtPubKey> global_xpubs;
# 770 : :
# 771 : : // Read global data
# 772 : 710 : bool found_sep = false;
# 773 [ + - ]: 1430 : while(!s.empty()) {
# 774 : : // Read
# 775 : 1430 : std::vector<unsigned char> key;
# 776 : 1430 : s >> key;
# 777 : :
# 778 : : // the key is empty if that was actually a separator byte
# 779 : : // This is a special case for key lengths 0 as those are not allowed (except for separator)
# 780 [ + + ]: 1430 : if (key.empty()) {
# 781 : 704 : found_sep = true;
# 782 : 704 : break;
# 783 : 704 : }
# 784 : :
# 785 : : // Type is compact size uint at beginning of key
# 786 : 726 : SpanReader skey(s.GetType(), s.GetVersion(), key);
# 787 : 726 : uint64_t type = ReadCompactSize(skey);
# 788 : :
# 789 : : // Do stuff based on type
# 790 : 726 : switch(type) {
# 791 [ + + ]: 706 : case PSBT_GLOBAL_UNSIGNED_TX:
# 792 : 706 : {
# 793 [ - + ]: 706 : if (!key_lookup.emplace(key).second) {
# 794 : 0 : throw std::ios_base::failure("Duplicate Key, unsigned tx already provided");
# 795 [ + + ]: 706 : } else if (key.size() != 1) {
# 796 : 2 : throw std::ios_base::failure("Global unsigned tx key is more than one byte type");
# 797 : 2 : }
# 798 : 704 : CMutableTransaction mtx;
# 799 : : // Set the stream to serialize with non-witness since this should always be non-witness
# 800 : 704 : OverrideStream<Stream> os(&s, s.GetType(), s.GetVersion() | SERIALIZE_TRANSACTION_NO_WITNESS);
# 801 : 704 : UnserializeFromVector(os, mtx);
# 802 : 704 : tx = std::move(mtx);
# 803 : : // Make sure that all scriptSigs and scriptWitnesses are empty
# 804 [ + + ]: 1020 : for (const CTxIn& txin : tx->vin) {
# 805 [ + + ][ - + ]: 1020 : if (!txin.scriptSig.empty() || !txin.scriptWitness.IsNull()) {
# 806 : 2 : throw std::ios_base::failure("Unsigned tx does not have empty scriptSigs and scriptWitnesses.");
# 807 : 2 : }
# 808 : 1020 : }
# 809 : 702 : break;
# 810 : 704 : }
# 811 [ + + ]: 702 : case PSBT_GLOBAL_XPUB:
# 812 : 6 : {
# 813 [ - + ]: 6 : if (key.size() != BIP32_EXTKEY_WITH_VERSION_SIZE + 1) {
# 814 : 0 : throw std::ios_base::failure("Size of key was not the expected size for the type global xpub");
# 815 : 0 : }
# 816 : : // Read in the xpub from key
# 817 : 6 : CExtPubKey xpub;
# 818 : 6 : xpub.DecodeWithVersion(&key.data()[1]);
# 819 [ - + ]: 6 : if (!xpub.pubkey.IsFullyValid()) {
# 820 : 0 : throw std::ios_base::failure("Invalid pubkey");
# 821 : 0 : }
# 822 [ - + ]: 6 : if (global_xpubs.count(xpub) > 0) {
# 823 : 0 : throw std::ios_base::failure("Duplicate key, global xpub already provided");
# 824 : 0 : }
# 825 : 6 : global_xpubs.insert(xpub);
# 826 : : // Read in the keypath from stream
# 827 : 6 : KeyOriginInfo keypath;
# 828 : 6 : DeserializeHDKeypath(s, keypath);
# 829 : :
# 830 : : // Note that we store these swapped to make searches faster.
# 831 : : // Serialization uses xpub -> keypath to enqure key uniqueness
# 832 [ + - ]: 6 : if (m_xpubs.count(keypath) == 0) {
# 833 : : // Make a new set to put the xpub in
# 834 : 6 : m_xpubs[keypath] = {xpub};
# 835 : 6 : } else {
# 836 : : // Insert xpub into existing set
# 837 : 0 : m_xpubs[keypath].insert(xpub);
# 838 : 0 : }
# 839 : 6 : break;
# 840 : 6 : }
# 841 [ + + ]: 4 : case PSBT_GLOBAL_VERSION:
# 842 : 4 : {
# 843 [ - + ]: 4 : if (m_version) {
# 844 : 0 : throw std::ios_base::failure("Duplicate Key, version already provided");
# 845 [ - + ]: 4 : } else if (key.size() != 1) {
# 846 : 0 : throw std::ios_base::failure("Global version key is more than one byte type");
# 847 : 0 : }
# 848 : 4 : uint32_t v;
# 849 : 4 : UnserializeFromVector(s, v);
# 850 : 4 : m_version = v;
# 851 [ + + ]: 4 : if (*m_version > PSBT_HIGHEST_VERSION) {
# 852 : 2 : throw std::ios_base::failure("Unsupported version number");
# 853 : 2 : }
# 854 : 2 : break;
# 855 : 4 : }
# 856 [ + + ]: 6 : case PSBT_GLOBAL_PROPRIETARY:
# 857 : 6 : {
# 858 : 6 : PSBTProprietary this_prop;
# 859 : 6 : skey >> this_prop.identifier;
# 860 : 6 : this_prop.subtype = ReadCompactSize(skey);
# 861 : 6 : this_prop.key = key;
# 862 : :
# 863 [ - + ]: 6 : if (m_proprietary.count(this_prop) > 0) {
# 864 : 0 : throw std::ios_base::failure("Duplicate Key, proprietary key already found");
# 865 : 0 : }
# 866 : 6 : s >> this_prop.value;
# 867 : 6 : m_proprietary.insert(this_prop);
# 868 : 6 : break;
# 869 : 6 : }
# 870 : : // Unknown stuff
# 871 [ + + ]: 4 : default: {
# 872 [ - + ]: 4 : if (unknown.count(key) > 0) {
# 873 : 0 : throw std::ios_base::failure("Duplicate Key, key for unknown value already provided");
# 874 : 0 : }
# 875 : : // Read in the value
# 876 : 4 : std::vector<unsigned char> val_bytes;
# 877 : 4 : s >> val_bytes;
# 878 : 4 : unknown.emplace(std::move(key), std::move(val_bytes));
# 879 : 4 : }
# 880 : 726 : }
# 881 : 726 : }
# 882 : :
# 883 [ - + ]: 704 : if (!found_sep) {
# 884 : 0 : throw std::ios_base::failure("Separator is missing at the end of the global map");
# 885 : 0 : }
# 886 : :
# 887 : : // Make sure that we got an unsigned tx
# 888 [ + + ]: 704 : if (!tx) {
# 889 : 2 : throw std::ios_base::failure("No unsigned transcation was provided");
# 890 : 2 : }
# 891 : :
# 892 : : // Read input data
# 893 : 702 : unsigned int i = 0;
# 894 [ + + ][ + + ]: 1716 : while (!s.empty() && i < tx->vin.size()) {
# 895 : 1014 : PSBTInput input;
# 896 : 1014 : s >> input;
# 897 : 1014 : inputs.push_back(input);
# 898 : :
# 899 : : // Make sure the non-witness utxo matches the outpoint
# 900 [ + + ][ - + ]: 1014 : if (input.non_witness_utxo && input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) {
# 901 : 0 : throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash");
# 902 : 0 : }
# 903 : 1014 : ++i;
# 904 : 1014 : }
# 905 : : // Make sure that the number of inputs matches the number of inputs in the transaction
# 906 [ - + ]: 702 : if (inputs.size() != tx->vin.size()) {
# 907 : 0 : throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction.");
# 908 : 0 : }
# 909 : :
# 910 : : // Read output data
# 911 : 702 : i = 0;
# 912 [ + + ][ + - ]: 1708 : while (!s.empty() && i < tx->vout.size()) {
# 913 : 1006 : PSBTOutput output;
# 914 : 1006 : s >> output;
# 915 : 1006 : outputs.push_back(output);
# 916 : 1006 : ++i;
# 917 : 1006 : }
# 918 : : // Make sure that the number of outputs matches the number of outputs in the transaction
# 919 [ + + ]: 702 : if (outputs.size() != tx->vout.size()) {
# 920 : 2 : throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction.");
# 921 : 2 : }
# 922 : 702 : }
# 923 : :
# 924 : : template <typename Stream>
# 925 : : PartiallySignedTransaction(deserialize_type, Stream& s) {
# 926 : : Unserialize(s);
# 927 : : }
# 928 : : };
# 929 : :
# 930 : : enum class PSBTRole {
# 931 : : CREATOR,
# 932 : : UPDATER,
# 933 : : SIGNER,
# 934 : : FINALIZER,
# 935 : : EXTRACTOR
# 936 : : };
# 937 : :
# 938 : : std::string PSBTRoleName(PSBTRole role);
# 939 : :
# 940 : : /** Compute a PrecomputedTransactionData object from a psbt. */
# 941 : : PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt);
# 942 : :
# 943 : : /** Checks whether a PSBTInput is already signed. */
# 944 : : bool PSBTInputSigned(const PSBTInput& input);
# 945 : :
# 946 : : /** Signs a PSBTInput, verifying that all provided data matches what is being signed.
# 947 : : *
# 948 : : * txdata should be the output of PrecomputePSBTData (which can be shared across
# 949 : : * multiple SignPSBTInput calls). If it is nullptr, a dummy signature will be created.
# 950 : : **/
# 951 : : bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, int sighash = SIGHASH_ALL, SignatureData* out_sigdata = nullptr, bool finalize = true);
# 952 : :
# 953 : : /** Counts the unsigned inputs of a PSBT. */
# 954 : : size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt);
# 955 : :
# 956 : : /** Updates a PSBTOutput with information from provider.
# 957 : : *
# 958 : : * This fills in the redeem_script, witness_script, and hd_keypaths where possible.
# 959 : : */
# 960 : : void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index);
# 961 : :
# 962 : : /**
# 963 : : * Finalizes a PSBT if possible, combining partial signatures.
# 964 : : *
# 965 : : * @param[in,out] psbtx PartiallySignedTransaction to finalize
# 966 : : * return True if the PSBT is now complete, false otherwise
# 967 : : */
# 968 : : bool FinalizePSBT(PartiallySignedTransaction& psbtx);
# 969 : :
# 970 : : /**
# 971 : : * Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.
# 972 : : *
# 973 : : * @param[in] psbtx PartiallySignedTransaction
# 974 : : * @param[out] result CMutableTransaction representing the complete transaction, if successful
# 975 : : * @return True if we successfully extracted the transaction, false otherwise
# 976 : : */
# 977 : : bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result);
# 978 : :
# 979 : : /**
# 980 : : * Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input.
# 981 : : *
# 982 : : * @param[out] out the combined PSBT, if successful
# 983 : : * @param[in] psbtxs the PSBTs to combine
# 984 : : * @return error (OK if we successfully combined the transactions, other error if they were not compatible)
# 985 : : */
# 986 : : [[nodiscard]] TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs);
# 987 : :
# 988 : : //! Decode a base64ed PSBT into a PartiallySignedTransaction
# 989 : : [[nodiscard]] bool DecodeBase64PSBT(PartiallySignedTransaction& decoded_psbt, const std::string& base64_psbt, std::string& error);
# 990 : : //! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction
# 991 : : [[nodiscard]] bool DecodeRawPSBT(PartiallySignedTransaction& decoded_psbt, const std::string& raw_psbt, std::string& error);
# 992 : :
# 993 : : #endif // BITCOIN_PSBT_H
|