42 if (!this_id || !psbt_id || this_id != psbt_id) {
49 for (
unsigned int i = 0; i <
inputs.size(); ++i) {
54 for (
unsigned int i = 0; i <
outputs.size(); ++i) {
67 std::bitset<8> final_modifiable = this_modifiable & psbt_modifiable;
69 final_modifiable.set(2, this_modifiable[2] || psbt_modifiable[2]);
82 for (
const auto& [origin, xpubs] : psbt.
m_xpubs) {
84 const bool known{std::ranges::any_of(
m_xpubs, [&](
const auto& entry) {
return entry.second.contains(xpub); })};
85 if (!known)
m_xpubs[origin].insert(xpub);
93 std::optional<uint32_t> time_lock{0};
94 std::optional<uint32_t> height_lock{0};
96 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
98 if (!time_lock.has_value()) {
101 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
103 if (!height_lock.has_value()) {
107 if (input.time_locktime && time_lock.has_value()) {
108 time_lock = std::max(time_lock, input.time_locktime);
110 if (input.height_locktime && height_lock.has_value()) {
111 height_lock = std::max(height_lock, input.height_locktime);
114 if (height_lock.has_value() && *height_lock > 0) {
117 if (time_lock.has_value() && *time_lock > 0) {
138 txin.
nSequence = input.sequence.value_or(max_sequence);
139 mtx.
vin.push_back(txin);
143 txout.
nValue = output.amount;
145 mtx.
vout.push_back(txout);
158 for (
CTxIn& txin : mtx->vin) {
162 return mtx->GetHash();
175 return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out;
184 inputs.back().partial_sigs.clear();
185 inputs.back().final_script_sig.clear();
186 inputs.back().final_script_witness.SetNull();
201 if (iterate_inputs) {
209 bool has_sigs =
false;
211 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
213 if (time_lock == std::nullopt) {
216 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
218 if (height_lock == std::nullopt) {
222 if (input.time_locktime && time_lock.has_value()) {
223 time_lock = std::max(time_lock, input.time_locktime);
225 if (input.height_locktime && height_lock.has_value()) {
226 height_lock = std::max(height_lock, input.height_locktime);
228 if (input.HasSignatures()) {
233 if (height_lock.has_value() && *height_lock > 0) {
234 new_timelock = *height_lock;
235 }
else if (time_lock.has_value() && *time_lock > 0) {
236 new_timelock = *time_lock;
238 if (has_sigs && *old_timelock != new_timelock) {
316 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
330 for (
const auto& [leaf_script, control_block] :
m_tap_scripts) {
338 sigdata.
ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
341 sigdata.
sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
344 sigdata.
hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
347 sigdata.
hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
351 sigdata.
musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
411 ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
414 sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
417 hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
420 hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
442 std::set<std::vector<unsigned char>> seen_control_blocks;
444 seen_control_blocks.insert(control_blocks.begin(), control_blocks.end());
446 for (
const auto& [leaf, control_blocks] : input.
m_tap_scripts) {
447 for (
const auto& control_block : control_blocks) {
448 if (seen_control_blocks.insert(control_block).second)
m_tap_scripts[leaf].insert(control_block);
493 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
498 builder.
Add((
int)depth,
script, (
int)leaf_ver,
true);
581 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
595 for (
const auto& input : psbt.
inputs) {
606 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
610 const CTxOut&
out = unsigned_tx->vout.at(index);
621 tx.
vin.emplace_back();
631 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
636 bool have_all_spent_outputs =
true;
637 std::vector<CTxOut> utxos;
639 if (!input.
GetUTXO(utxos.emplace_back())) have_all_spent_outputs =
false;
642 if (have_all_spent_outputs) {
643 txdata.
Init(tx, std::move(utxos),
true);
645 txdata.
Init(tx, {},
true);
653 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
668 bool require_witness_sig =
false;
687 require_witness_sig =
true;
724 if (sig.size() != 65 || sig.back() != sighash)
return util::Unexpected{PSBTError::SIGHASH_MISMATCH};
727 if (sig.second.back() != sighash)
return util::Unexpected{PSBTError::SIGHASH_MISMATCH};
733 if (txdata ==
nullptr) {
770 std::vector<unsigned int> to_drop;
771 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
772 const auto& input = psbtx.
inputs.at(i);
774 std::vector<unsigned char> wit_prog;
775 if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
788 if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) ==
SIGHASH_ANYONECANPAY) {
793 if (input.non_witness_utxo) {
794 to_drop.push_back(i);
799 for (
unsigned int i : to_drop) {
800 psbtx.
inputs.at(i).non_witness_utxo =
nullptr;
810 bool complete =
true;
816 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
819 complete &= sign_result.has_value();
833 std::optional<CMutableTransaction> unsigned_tx = psbtx.
GetUnsignedTx();
837 result = *unsigned_tx;
838 for (
unsigned int i = 0; i < result.
vin.size(); ++i) {
839 result.
vin[i].scriptSig = psbtx.
inputs[i].final_script_sig;
840 result.
vin[i].scriptWitness = psbtx.
inputs[i].final_script_witness;
845std::optional<PartiallySignedTransaction>
CombinePSBTs(
const std::vector<PartiallySignedTransaction>& psbtxs)
850 for (
auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
851 if (!
out.Merge(*it)) {
883 if (!ss_data.empty()) {
887 }
catch (
const std::exception& e) {
An outpoint - a combination of a transaction hash and an index n into its vout.
bool IsPayToTaproot() const
An input of a transaction.
static constexpr uint32_t SEQUENCE_FINAL
Setting nSequence to this value for every input in a transaction disables nLockTime/IsFinalTx().
An output of a transaction.
A signature creator for transactions.
A structure for PSBTs which contains per output information.
std::map< CPubKey, std::vector< CPubKey > > m_musig2_participants
XOnlyPubKey m_tap_internal_key
std::map< XOnlyPubKey, std::pair< std::set< uint256 >, KeyOriginInfo > > m_tap_bip32_paths
std::set< PSBTProprietary > m_proprietary
uint32_t GetVersion() const
bool Merge(const PSBTOutput &output)
std::map< CPubKey, KeyOriginInfo > hd_keypaths
std::vector< std::tuple< uint8_t, uint8_t, std::vector< unsigned char > > > m_tap_tree
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
void FillSignatureData(SignatureData &sigdata) const
void FromSignatureData(const SignatureData &sigdata)
A version of CTransaction with the PSBT format.
std::optional< std::bitset< 8 > > m_tx_modifiable
uint32_t GetVersion() const
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
std::map< KeyOriginInfo, std::set< CExtPubKey > > m_xpubs
std::optional< uint32_t > m_version
std::optional< Txid > GetUniqueID() const
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
std::vector< PSBTInput > inputs
void MergeGlobalXPubs(const PartiallySignedTransaction &psbt)
Merge the global xpubs of psbt into this, keeping the existing origin for an xpub seen again with a d...
std::optional< CMutableTransaction > GetUnsignedTx() const
std::optional< uint32_t > ComputeTimeLock() const
std::vector< PSBTOutput > outputs
std::set< PSBTProprietary > m_proprietary
bool AddOutput(const PSBTOutput &psbtout)
std::optional< uint32_t > fallback_locktime
PartiallySignedTransaction(const CMutableTransaction &tx, uint32_t version=2)
bool AddInput(const PSBTInput &psbtin)
An interface to be implemented by keystores that support signing.
Minimal stream for reading from an existing byte array by std::span.
Utility class to construct Taproot outputs from internal key and script tree.
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
TaprootBuilder & Add(int depth, std::span< const unsigned char > script, int leaf_version, bool track=true)
Add a new script at a certain depth in the tree.
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
bool IsFullyValid() const
Determine if this pubkey is fully valid.
constexpr bool IsNull() const
The util::Expected class provides a standard way for low-level functions to return either error value...
The util::Unexpected class represents an unexpected value stored in util::Expected.
is a home for simple enum and struct type definitions that can be used internally by functions in the...
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
@ SIGHASH_DEFAULT
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
is a home for public enum and struct type definitions that are used internally by node code,...
constexpr script_verify_flags STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
util::Expected< void, PSBTError > SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, const PrecomputedTransactionData *txdata, const common::PSBTFillOptions &options, SignatureData *out_sigdata)
Signs a PSBTInput, verifying that all provided data matches what is being signed.
void UpdatePSBTOutput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index)
Updates a PSBTOutput with information from provider.
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction &psbt, unsigned int input_index, const PrecomputedTransactionData *txdata)
Checks whether a PSBTInput is already signed by doing script verification using final fields.
std::string PSBTRoleName(PSBTRole role)
util::Result< PartiallySignedTransaction > DecodeBase64PSBT(const std::string &base64_tx)
Decode a base64ed PSBT into a PartiallySignedTransaction.
std::optional< PartiallySignedTransaction > CombinePSBTs(const std::vector< PartiallySignedTransaction > &psbtxs)
Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial sign...
void RemoveUnnecessaryTransactions(PartiallySignedTransaction &psbtx)
Reduces the size of the PSBT by dropping unnecessary non_witness_utxos (i.e.
std::optional< PrecomputedTransactionData > PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
bool FinalizeAndExtractPSBT(PartiallySignedTransaction &psbtx, CMutableTransaction &result)
Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized.
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
util::Result< PartiallySignedTransaction > DecodeRawPSBT(std::span< const std::byte > tx_data)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
constexpr deserialize_type deserialize
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
const SigningProvider & DUMMY_SIGNING_PROVIDER
auto MakeByteSpan(const V &v) noexcept
A mutable version of CTransaction.
std::vector< CTxOut > vout
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
std::map< std::vector< uint8_t >, std::vector< uint8_t > > ripemd160_preimages
Mapping from a RIPEMD160 hash to its preimage provided to solve a Script.
std::map< CKeyID, XOnlyPubKey > tap_pubkeys
Misc Taproot pubkeys involved in this input, by hash. (Equivalent of misc_pubkeys but for Taproot....
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
TaprootSpendData tr_spenddata
Taproot spending data.
bool witness
Stores whether the input this SigData corresponds to is a witness input.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
std::optional< TaprootBuilder > tr_builder
Taproot tree used to build tr_spenddata.
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format.
std::map< std::vector< uint8_t >, std::vector< uint8_t > > sha256_preimages
Mapping from a SHA256 hash to its preimage provided to solve a Script.
std::vector< unsigned char > taproot_key_path_sig
std::map< std::pair< CPubKey, uint256 >, std::map< CPubKey, std::vector< uint8_t > > > musig2_pubnonces
Mapping from pair of MuSig2 aggregate pubkey, and tapleaf hash to map of MuSig2 participant pubkeys t...
std::map< std::pair< XOnlyPubKey, uint256 >, std::vector< unsigned char > > taproot_script_sigs
Schnorr signature for key path spending.
std::map< XOnlyPubKey, std::pair< std::set< uint256 >, KeyOriginInfo > > taproot_misc_pubkeys
Miscellaneous Taproot pubkeys involved in this input along with their leaf script hashes and key orig...
std::map< std::vector< uint8_t >, std::vector< uint8_t > > hash256_preimages
Mapping from a HASH256 hash to its preimage provided to solve a Script.
CScript redeem_script
The redeemScript (if any) for the input.
std::map< std::pair< CPubKey, uint256 >, std::map< CPubKey, uint256 > > musig2_partial_sigs
Mapping from pair of MuSig2 aggregate pubkey, and tapleaf hash to map of MuSig2 participant pubkeys t...
uint256 missing_witness_script
SHA256 of the missing witnessScript (if any)
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
std::map< CPubKey, std::vector< CPubKey > > musig2_pubkeys
Map MuSig2 aggregate pubkeys to its participants.
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
bool complete
Stores whether the scriptSig and scriptWitness are complete.
std::map< std::vector< uint8_t >, std::vector< uint8_t > > hash160_preimages
Mapping from a HASH160 hash to its preimage provided to solve a Script.
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
std::map< std::pair< std::vector< unsigned char >, int >, std::set< std::vector< unsigned char >, ShortestVectorFirstComparator > > scripts
Map from (script, leaf_version) to (sets of) control blocks.
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
XOnlyPubKey internal_key
The BIP341 internal key.
Instructions for how a PSBT should be signed or filled with information.
std::optional< int > sighash_type
The sighash type to use when signing (if PSBT does not specify).
bool finalize
Whether to create the final scriptSig or scriptWitness if possible.
FuzzedDataProvider provider
consteval auto _(util::TranslatedLiteral str)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)