42 if (!this_id || !psbt_id || this_id != psbt_id) {
49 for (
unsigned int i = 0; i <
inputs.size(); ++i) {
52 for (
unsigned int i = 0; i <
outputs.size(); ++i) {
63 std::bitset<8> final_modifiable = this_modifiable & psbt_modifiable;
65 final_modifiable.set(2, this_modifiable[2] || psbt_modifiable[2]);
78 for (
const auto& [origin, xpubs] : psbt.
m_xpubs) {
80 const bool known{std::ranges::any_of(
m_xpubs, [&](
const auto& entry) {
return entry.second.contains(xpub); })};
81 if (!known)
m_xpubs[origin].insert(xpub);
89 std::optional<uint32_t> time_lock{0};
90 std::optional<uint32_t> height_lock{0};
92 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
94 if (!time_lock.has_value()) {
97 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
99 if (!height_lock.has_value()) {
103 if (input.time_locktime && time_lock.has_value()) {
104 time_lock = std::max(time_lock, input.time_locktime);
106 if (input.height_locktime && height_lock.has_value()) {
107 height_lock = std::max(height_lock, input.height_locktime);
110 if (height_lock.has_value() && *height_lock > 0) {
113 if (time_lock.has_value() && *time_lock > 0) {
134 txin.
nSequence = input.sequence.value_or(max_sequence);
135 mtx.
vin.push_back(txin);
139 txout.
nValue = output.amount;
141 mtx.
vout.push_back(txout);
154 for (
CTxIn& txin : mtx->vin) {
158 return mtx->GetHash();
171 return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out;
180 inputs.back().partial_sigs.clear();
181 inputs.back().final_script_sig.clear();
182 inputs.back().final_script_witness.SetNull();
197 if (iterate_inputs) {
205 bool has_sigs =
false;
207 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
209 if (time_lock == std::nullopt) {
212 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
214 if (height_lock == std::nullopt) {
218 if (input.time_locktime && time_lock.has_value()) {
219 time_lock = std::max(time_lock, input.time_locktime);
221 if (input.height_locktime && height_lock.has_value()) {
222 height_lock = std::max(height_lock, input.height_locktime);
224 if (input.HasSignatures()) {
229 if (height_lock.has_value() && *height_lock > 0) {
230 new_timelock = *height_lock;
231 }
else if (time_lock.has_value() && *time_lock > 0) {
232 new_timelock = *time_lock;
234 if (has_sigs && *old_timelock != new_timelock) {
312 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
326 for (
const auto& [leaf_script, control_block] :
m_tap_scripts) {
334 sigdata.
ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
337 sigdata.
sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
340 sigdata.
hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
343 sigdata.
hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
347 sigdata.
musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
407 ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
410 sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
413 hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
416 hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
438 std::set<std::vector<unsigned char>> seen_control_blocks;
440 seen_control_blocks.insert(control_blocks.begin(), control_blocks.end());
442 for (
const auto& [leaf, control_blocks] : input.
m_tap_scripts) {
443 for (
const auto& control_block : control_blocks) {
444 if (seen_control_blocks.insert(control_block).second)
m_tap_scripts[leaf].insert(control_block);
488 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
493 builder.
Add((
int)depth,
script, (
int)leaf_ver,
true);
574 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
588 for (
const auto& input : psbt.
inputs) {
599 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
603 const CTxOut&
out = unsigned_tx->vout.at(index);
614 tx.
vin.emplace_back();
624 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
629 bool have_all_spent_outputs =
true;
630 std::vector<CTxOut> utxos;
632 if (!input.
GetUTXO(utxos.emplace_back())) have_all_spent_outputs =
false;
635 if (have_all_spent_outputs) {
636 txdata.
Init(tx, std::move(utxos),
true);
638 txdata.
Init(tx, {},
true);
646 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
661 bool require_witness_sig =
false;
680 require_witness_sig =
true;
717 if (sig.size() != 65 || sig.back() != sighash)
return util::Unexpected{PSBTError::SIGHASH_MISMATCH};
720 if (sig.second.back() != sighash)
return util::Unexpected{PSBTError::SIGHASH_MISMATCH};
726 if (txdata ==
nullptr) {
763 std::vector<unsigned int> to_drop;
764 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
765 const auto& input = psbtx.
inputs.at(i);
767 std::vector<unsigned char> wit_prog;
768 if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
781 if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) ==
SIGHASH_ANYONECANPAY) {
786 if (input.non_witness_utxo) {
787 to_drop.push_back(i);
792 for (
unsigned int i : to_drop) {
793 psbtx.
inputs.at(i).non_witness_utxo =
nullptr;
803 bool complete =
true;
809 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
812 complete &= sign_result.has_value();
826 std::optional<CMutableTransaction> unsigned_tx = psbtx.
GetUnsignedTx();
830 result = *unsigned_tx;
831 for (
unsigned int i = 0; i < result.
vin.size(); ++i) {
832 result.
vin[i].scriptSig = psbtx.
inputs[i].final_script_sig;
833 result.
vin[i].scriptWitness = psbtx.
inputs[i].final_script_witness;
838std::optional<PartiallySignedTransaction>
CombinePSBTs(
const std::vector<PartiallySignedTransaction>& psbtxs)
843 for (
auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
844 if (!
out.Merge(*it)) {
876 if (!ss_data.empty()) {
880 }
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
void Merge(const PSBTOutput &output)
std::set< PSBTProprietary > m_proprietary
uint32_t GetVersion() const
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)