Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1501default: {1502// Read in the value1503std::vector<unsigned char> val_bytes;1504s >> val_bytes;1505unknown.emplace(std::move(key), std::move(val_bytes));1506}1507}1508}1510if (!found_sep) {1511throw std::ios_base::failure("Separator is missing at the end of the global map");1512}1514const uint32_t psbt_ver = GetVersion();1516// Check PSBT version constraints1517if (psbt_ver == 0) {1518// Make sure that we got an unsigned tx for PSBTv01519if (!tx) {1520throw std::ios_base::failure("No unsigned transaction was provided");1521}1522// Make sure no PSBTv2 fields are present1523if (found_tx_version) {1524throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is not allowed in PSBTv0");1525}1526if (found_fallback_locktime) {1527throw std::ios_base::failure("PSBT_GLOBAL_FALLBACK_LOCKTIME is not allowed in PSBTv0");1528}1529if (found_input_count) {1530throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is not allowed in PSBTv0");1531}1532if (found_output_count) {1533throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is not allowed in PSBTv0");1534}1535if (m_tx_modifiable != std::nullopt) {1536throw std::ios_base::failure("PSBT_GLOBAL_TX_MODIFIABLE is not allowed in PSBTv0");1537}1538}1539// Disallow v11540if (psbt_ver == 1) {1541throw std::ios_base::failure("There is no PSBT version 1");1542}1543if (psbt_ver == 2) {1544// Tx version, input, and output counts are required1545if (!found_tx_version) {1546throw std::ios_base::failure("PSBT_GLOBAL_TX_VERSION is required in PSBTv2");1547}1548if (!found_input_count) {1549throw std::ios_base::failure("PSBT_GLOBAL_INPUT_COUNT is required in PSBTv2");1550}1551if (!found_output_count) {1552throw std::ios_base::failure("PSBT_GLOBAL_OUTPUT_COUNT is required in PSBTv2");1553}1554// Unsigned tx is disallowed1555if (tx) {1556throw std::ios_base::failure("PSBT_GLOBAL_UNSIGNED_TX is not allowed in PSBTv2");1557}1558}1559if (psbt_ver > 2) {1560throw std::ios_base::failure("Unknown PSBT version");1561}1563// Read input data1564unsigned int i = 0;1565while (!s.empty() && i < input_count) {1566if (psbt_ver < 2) {1567inputs.emplace_back(psbt_ver, tx->vin[i].prevout.hash, tx->vin[i].prevout.n, tx->vin[i].nSequence);1568s >> inputs.back();1569} else {1570inputs.emplace_back(deserialize, s, psbt_ver);1571}1573// Make sure the non-witness utxo matches the outpoint1574const PSBTInput& input = inputs.back();1575if (input.non_witness_utxo) {1576if (psbt_ver < 2) {1577if (input.non_witness_utxo->GetHash() != tx->vin[i].prevout.hash) {1578throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash");1579}1580if (tx->vin[i].prevout.n >= input.non_witness_utxo->vout.size()) {1581throw std::ios_base::failure("Input specifies output index that does not exist");1582}1583} else {1584if (input.non_witness_utxo->GetHash() != input.prev_txid) {1585throw std::ios_base::failure("Non-witness UTXO does not match outpoint hash");1586}1587if (input.prev_out >= input.non_witness_utxo->vout.size()) {1588throw std::ios_base::failure("Input specifies output index that does not exist");1589}1590}1591}1592++i;1593}1594// Make sure that the number of inputs matches the number of inputs in the transaction1595if (inputs.size() != input_count) {1596throw std::ios_base::failure("Inputs provided does not match the number of inputs in transaction.");1597}1599// Read output data1600i = 0;1601while (!s.empty() && i < output_count) {1602if (psbt_ver < 2) {1603outputs.emplace_back(psbt_ver, tx->vout[i].nValue, tx->vout[i].scriptPubKey);1604s >> outputs.back();1605} else {1606outputs.emplace_back(deserialize, s, psbt_ver);1607}1608++i;1609}1610// Make sure that the number of outputs matches the number of outputs in the transaction1611if (outputs.size() != output_count) {1612throw std::ios_base::failure("Outputs provided does not match the number of outputs in transaction.");1613}1614}1616template <typename Stream>1617PartiallySignedTransaction(deserialize_type, Stream& s) {1618Unserialize(s);1619}1620};1622enum class PSBTRole {1623CREATOR,1624UPDATER,1625SIGNER,1626FINALIZER,1627EXTRACTOR1628};1630std::string PSBTRoleName(PSBTRole role);1632/** Compute a PrecomputedTransactionData object from a psbt. */1633std::optional<PrecomputedTransactionData> PrecomputePSBTData(const PartiallySignedTransaction& psbt);1635/** Checks whether a PSBTInput is already signed by checking for non-null finalized fields. */1636bool PSBTInputSigned(const PSBTInput& input);1638/** Checks whether a PSBTInput is already signed by doing script verification using final fields. */1639bool PSBTInputSignedAndVerified(const PartiallySignedTransaction& psbt, unsigned int input_index, const PrecomputedTransactionData* txdata);1641/** Signs a PSBTInput, verifying that all provided data matches what is being signed.1642*1643* txdata should be the output of PrecomputePSBTData (which can be shared across1644* multiple SignPSBTInput calls). If it is nullptr, a dummy signature will be created.1645**/1646[[nodiscard]] util::Expected<void, PSBTError> SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, const common::PSBTFillOptions& options, SignatureData* out_sigdata = nullptr);1648/** Reduces the size of the PSBT by dropping unnecessary `non_witness_utxos` (i.e. complete previous transactions) from a psbt when all inputs are segwit v1. */1649void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx);1651/** Counts the unsigned inputs of a PSBT. */1652size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt);1654/** Updates a PSBTOutput with information from provider.1655*1656* This fills in the redeem_script, witness_script, and hd_keypaths where possible.1657*/1658void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index);1660/**1661* Finalizes a PSBT if possible, combining partial signatures.1662*1663* @param[in,out] psbtx PartiallySignedTransaction to finalize1664* return True if the PSBT is now complete, false otherwise1665*/1666bool FinalizePSBT(PartiallySignedTransaction& psbtx);1668/**1669* Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.1670*1671* @param[in] psbtx PartiallySignedTransaction1672* @param[out] result CMutableTransaction representing the complete transaction, if successful1673* @return True if we successfully extracted the transaction, false otherwise1674*/1675bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result);1677/**1678* Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial signatures from each input.1679*1680* @param[in] psbtxs the PSBTs to combine1681* @return The combined PSBT or std::nullopt if the PSBTs cannot be combined1682*/1683[[nodiscard]] std::optional<PartiallySignedTransaction> CombinePSBTs(const std::vector<PartiallySignedTransaction>& psbtxs);1685//! Decode a base64ed PSBT into a PartiallySignedTransaction1686[[nodiscard]] util::Result<PartiallySignedTransaction> DecodeBase64PSBT(const std::string& base64_tx);1687//! Decode a raw (binary blob) PSBT into a PartiallySignedTransaction1688[[nodiscard]] util::Result<PartiallySignedTransaction> DecodeRawPSBT(std::span<const std::byte> tx_data);1690#endif // BITCOIN_PSBT_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.