44 if (!this_id || !psbt_id || this_id != psbt_id) {
51 for (
unsigned int i = 0; i <
inputs.size(); ++i) {
56 for (
unsigned int i = 0; i <
outputs.size(); ++i) {
61 for (
auto& xpub_pair : psbt.
m_xpubs) {
62 if (!
m_xpubs.contains(xpub_pair.first)) {
63 m_xpubs[xpub_pair.first] = xpub_pair.second;
65 m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
75 std::bitset<8> final_modifiable = this_modifiable & psbt_modifiable;
77 final_modifiable.set(2, this_modifiable[2] || psbt_modifiable[2]);
91 std::optional<uint32_t> time_lock{0};
92 std::optional<uint32_t> height_lock{0};
94 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
96 if (!time_lock.has_value()) {
99 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
101 if (!height_lock.has_value()) {
105 if (input.time_locktime && time_lock.has_value()) {
106 time_lock = std::max(time_lock, input.time_locktime);
108 if (input.height_locktime && height_lock.has_value()) {
109 height_lock = std::max(height_lock, input.height_locktime);
112 if (height_lock.has_value() && *height_lock > 0) {
115 if (time_lock.has_value() && *time_lock > 0) {
136 txin.
nSequence = input.sequence.value_or(max_sequence);
137 mtx.
vin.push_back(txin);
141 txout.
nValue = output.amount;
143 mtx.
vout.push_back(txout);
156 for (
CTxIn& txin : mtx->vin) {
160 return mtx->GetHash();
173 return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out;
182 inputs.back().partial_sigs.clear();
183 inputs.back().final_script_sig.clear();
184 inputs.back().final_script_witness.SetNull();
199 if (iterate_inputs) {
207 bool has_sigs =
false;
209 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
211 if (time_lock == std::nullopt) {
214 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
216 if (height_lock == std::nullopt) {
220 if (input.time_locktime && time_lock.has_value()) {
221 time_lock = std::max(time_lock, input.time_locktime);
223 if (input.height_locktime && height_lock.has_value()) {
224 height_lock = std::max(height_lock, input.height_locktime);
226 if (input.HasSignatures()) {
231 if (height_lock.has_value() && *height_lock > 0) {
232 new_timelock = *height_lock;
233 }
else if (time_lock.has_value() && *time_lock > 0) {
234 new_timelock = *time_lock;
236 if (has_sigs && *old_timelock != new_timelock) {
319 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
333 for (
const auto& [leaf_script, control_block] :
m_tap_scripts) {
341 sigdata.
ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
344 sigdata.
sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
347 sigdata.
hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
350 sigdata.
hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
354 sigdata.
musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
414 ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
417 sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
420 hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
423 hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
486 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
491 builder.
Add((
int)depth,
script, (
int)leaf_ver,
true);
579 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
593 for (
const auto& input : psbt.
inputs) {
604 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
628 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
633 bool have_all_spent_outputs =
true;
634 std::vector<CTxOut> utxos;
636 if (!input.
GetUTXO(utxos.emplace_back())) have_all_spent_outputs =
false;
639 if (have_all_spent_outputs) {
640 txdata.
Init(tx, std::move(utxos),
true);
642 txdata.
Init(tx, {},
true);
650 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
652 return PSBTError::INVALID_TX;
657 return PSBTError::OK;
665 bool require_witness_sig =
false;
672 return PSBTError::MISSING_INPUTS;
675 return PSBTError::MISSING_INPUTS;
684 require_witness_sig =
true;
686 return PSBTError::MISSING_INPUTS;
698 return PSBTError::SIGHASH_MISMATCH;
711 return PSBTError::SIGHASH_MISMATCH;
714 if (sig.size() != 64)
return PSBTError::SIGHASH_MISMATCH;
718 return PSBTError::SIGHASH_MISMATCH;
721 if (sig.size() != 65 || sig.back() != sighash)
return PSBTError::SIGHASH_MISMATCH;
724 if (sig.second.back() != sighash)
return PSBTError::SIGHASH_MISMATCH;
730 if (txdata ==
nullptr) {
737 if (require_witness_sig && !sigdata.
witness)
return PSBTError::INCOMPLETE;
760 return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
766 std::vector<unsigned int> to_drop;
767 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
768 const auto& input = psbtx.
inputs.at(i);
770 std::vector<unsigned char> wit_prog;
771 if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
784 if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) ==
SIGHASH_ANYONECANPAY) {
789 if (input.non_witness_utxo) {
790 to_drop.push_back(i);
795 for (
unsigned int i : to_drop) {
796 psbtx.
inputs.at(i).non_witness_utxo =
nullptr;
806 bool complete =
true;
812 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
828 std::optional<CMutableTransaction> unsigned_tx = psbtx.
GetUnsignedTx();
832 result = *unsigned_tx;
833 for (
unsigned int i = 0; i < result.
vin.size(); ++i) {
834 result.
vin[i].scriptSig = psbtx.
inputs[i].final_script_sig;
835 result.
vin[i].scriptWitness = psbtx.
inputs[i].final_script_witness;
840std::optional<PartiallySignedTransaction>
CombinePSBTs(
const std::vector<PartiallySignedTransaction>& psbtxs)
845 for (
auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
846 if (!
out.Merge(*it)) {
878 if (!ss_data.empty()) {
882 }
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 const 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
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
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,...
static constexpr script_verify_flags STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
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.
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.
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)