27 if (
tx->GetHash() != psbt.
tx->GetHash()) {
31 for (
unsigned int i = 0; i <
inputs.size(); ++i) {
34 for (
unsigned int i = 0; i <
outputs.size(); ++i) {
37 for (
auto& xpub_pair : psbt.
m_xpubs) {
38 if (
m_xpubs.count(xpub_pair.first) == 0) {
39 m_xpubs[xpub_pair.first] = xpub_pair.second;
41 m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
51 if (std::find(
tx->vin.begin(),
tx->vin.end(), txin) !=
tx->vin.end()) {
54 tx->vin.push_back(txin);
64 tx->vout.push_back(txout);
72 uint32_t prevout_index =
tx->vin[input_index].prevout.n;
116 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
130 for (
const auto& [leaf_script, control_block] :
m_tap_scripts) {
138 sigdata.
ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
141 sigdata.
sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
144 sigdata.
hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
147 sigdata.
hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
234 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
239 builder.
Add((
int)depth,
script, (
int)leaf_ver,
true);
306 COutPoint prevout = psbt.
tx->vin[input_index].prevout;
329 for (
const auto& input : psbt.
inputs) {
361 bool have_all_spent_outputs =
true;
362 std::vector<CTxOut> utxos(tx.
vin.size());
363 for (
size_t idx = 0; idx < tx.
vin.size(); ++idx) {
364 if (!psbt.
GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs =
false;
367 if (have_all_spent_outputs) {
368 txdata.
Init(tx, std::move(utxos),
true);
370 txdata.
Init(tx, {},
true);
389 bool require_witness_sig =
false;
408 require_witness_sig =
true;
415 if (txdata ==
nullptr) {
422 if (require_witness_sig && !sigdata.
witness)
return false;
453 std::vector<unsigned int> to_drop;
454 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
455 const auto& input = psbtx.
inputs.at(i);
457 std::vector<unsigned char> wit_prog;
458 if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
468 if (input.non_witness_utxo) {
469 to_drop.push_back(i);
474 for (
unsigned int i : to_drop) {
475 psbtx.
inputs.at(i).non_witness_utxo =
nullptr;
486 bool complete =
true;
488 for (
unsigned int i = 0; i < psbtx.
tx->vin.size(); ++i) {
504 for (
unsigned int i = 0; i < result.
vin.size(); ++i) {
505 result.
vin[i].scriptSig = psbtx.
inputs[i].final_script_sig;
506 result.
vin[i].scriptWitness = psbtx.
inputs[i].final_script_witness;
516 for (
auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
517 if (!
out.Merge(*it)) {
540 error =
"invalid base64";
551 if (!ss_data.empty()) {
552 error =
"extra data after PSBT";
555 }
catch (
const std::exception& e) {
#define Assert(val)
Identity function.
An outpoint - a combination of a transaction hash and an index n into its vout.
An input of a transaction.
An output of a transaction.
Double ended buffer combining vector and stream-like interfaces.
A signature creator for transactions.
An interface to be implemented by keystores that support signing.
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, 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
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
is a home for public enum and struct type definitions that are used by internally by node code,...
static constexpr unsigned int STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
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)
bool CombinePSBTs(PartiallySignedTransaction &out, const std::vector< PartiallySignedTransaction > &psbtxs)
Combines PSBTs with the same underlying transaction, resulting in a single PSBT with all partial sign...
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.
void RemoveUnnecessaryTransactions(PartiallySignedTransaction &psbtx, const int &sighash_type)
Reduces the size of the PSBT by dropping unnecessary non_witness_utxos (i.e.
bool DecodeRawPSBT(PartiallySignedTransaction &psbt, Span< const std::byte > tx_data, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
bool SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, const PrecomputedTransactionData *txdata, int sighash, SignatureData *out_sigdata, bool finalize)
Signs a PSBTInput, verifying that all provided data matches what is being signed.
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
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
Span< const std::byte > MakeByteSpan(V &&v) noexcept
A mutable version of CTransaction.
std::vector< CTxOut > vout
A structure for PSBTs which contains per output information.
XOnlyPubKey m_tap_internal_key
std::map< XOnlyPubKey, std::pair< std::set< uint256 >, KeyOriginInfo > > m_tap_bip32_paths
void 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.
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
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
bool AddOutput(const CTxOut &txout, const PSBTOutput &psbtout)
std::vector< PSBTInput > inputs
PartiallySignedTransaction()=default
std::optional< CMutableTransaction > tx
bool AddInput(const CTxIn &txin, PSBTInput &psbtin)
std::vector< PSBTOutput > outputs
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< 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.
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.
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.
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)