30 if (
tx->GetHash() != psbt.
tx->GetHash()) {
34 for (
unsigned int i = 0; i <
inputs.size(); ++i) {
37 for (
unsigned int i = 0; i <
outputs.size(); ++i) {
40 for (
auto& xpub_pair : psbt.
m_xpubs) {
41 if (!
m_xpubs.contains(xpub_pair.first)) {
42 m_xpubs[xpub_pair.first] = xpub_pair.second;
44 m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
54 if (std::find(
tx->vin.begin(),
tx->vin.end(), txin) !=
tx->vin.end()) {
57 tx->vin.push_back(txin);
67 tx->vout.push_back(txout);
75 uint32_t prevout_index =
tx->vin[input_index].prevout.n;
119 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
133 for (
const auto& [leaf_script, control_block] :
m_tap_scripts) {
141 sigdata.
ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
144 sigdata.
sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
147 sigdata.
hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
150 sigdata.
hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
154 sigdata.
musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
258 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
263 builder.
Add((
int)depth,
script, (
int)leaf_ver,
true);
331 if (input.non_witness_utxo) {
333 COutPoint prevout = psbt.
tx->vin[input_index].prevout;
334 if (prevout.
n >= input.non_witness_utxo->vout.size()) {
337 if (input.non_witness_utxo->GetHash() != prevout.
hash) {
340 utxo = input.non_witness_utxo->vout[prevout.
n];
341 }
else if (!input.witness_utxo.IsNull()) {
342 utxo = input.witness_utxo;
356 for (
const auto& input : psbt.
inputs) {
388 bool have_all_spent_outputs =
true;
389 std::vector<CTxOut> utxos(tx.
vin.size());
390 for (
size_t idx = 0; idx < tx.
vin.size(); ++idx) {
391 if (!psbt.
GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs =
false;
394 if (have_all_spent_outputs) {
395 txdata.
Init(tx, std::move(utxos),
true);
397 txdata.
Init(tx, {},
true);
408 return PSBTError::OK;
416 bool require_witness_sig =
false;
423 return PSBTError::MISSING_INPUTS;
426 return PSBTError::MISSING_INPUTS;
435 require_witness_sig =
true;
437 return PSBTError::MISSING_INPUTS;
446 Assert(sighash.has_value());
449 return PSBTError::SIGHASH_MISMATCH;
462 return PSBTError::SIGHASH_MISMATCH;
465 if (sig.size() != 64)
return PSBTError::SIGHASH_MISMATCH;
469 return PSBTError::SIGHASH_MISMATCH;
472 if (sig.size() != 65 || sig.back() != *sighash)
return PSBTError::SIGHASH_MISMATCH;
475 if (sig.second.back() != *sighash)
return PSBTError::SIGHASH_MISMATCH;
481 if (txdata ==
nullptr) {
488 if (require_witness_sig && !sigdata.
witness)
return PSBTError::INCOMPLETE;
511 return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
517 std::vector<unsigned int> to_drop;
518 for (
unsigned int i = 0; i < psbtx.
inputs.size(); ++i) {
519 const auto& input = psbtx.
inputs.at(i);
521 std::vector<unsigned char> wit_prog;
522 if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
535 if (input.sighash_type != std::nullopt && (*input.sighash_type & 0x80) ==
SIGHASH_ANYONECANPAY) {
540 if (input.non_witness_utxo) {
541 to_drop.push_back(i);
546 for (
unsigned int i : to_drop) {
547 psbtx.
inputs.at(i).non_witness_utxo =
nullptr;
557 bool complete =
true;
559 for (
unsigned int i = 0; i < psbtx.
tx->vin.size(); ++i) {
576 for (
unsigned int i = 0; i <
result.vin.size(); ++i) {
577 result.vin[i].scriptSig = psbtx.
inputs[i].final_script_sig;
578 result.vin[i].scriptWitness = psbtx.
inputs[i].final_script_witness;
588 for (
auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
589 if (!
out.Merge(*it)) {
612 error =
"invalid base64";
623 if (!ss_data.empty()) {
624 error =
"extra data after PSBT";
627 }
catch (
const std::exception& e) {
bool AddInput(const CTxIn &txin, PSBTInput &psbtin)
auto MakeByteSpan(const V &v) noexcept
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< 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...
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
CScript witness_script
The witnessScript (if any) for the input. witnessScripts are used in P2WSH outputs.
PartiallySignedTransaction()=default
PSBTError SignPSBTInput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index, const PrecomputedTransactionData *txdata, std::optional< int > sighash, SignatureData *out_sigdata, bool finalize)
Signs a PSBTInput, verifying that all provided data matches what is being signed. ...
std::map< CPubKey, std::vector< CPubKey > > musig2_pubkeys
Map MuSig2 aggregate pubkeys to its participants.
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
CScript scriptSig
The scriptSig of an input. Contains complete signatures or the traditional partial signatures format...
std::vector< CKeyID > missing_sigs
KeyIDs of pubkeys for signatures which could not be found.
bool FinalizePSBT(PartiallySignedTransaction &psbtx)
Finalizes a PSBT if possible, combining partial signatures.
std::vector< CKeyID > missing_pubkeys
KeyIDs of pubkeys which could not be found.
const BaseSignatureCreator & DUMMY_SIGNATURE_CREATOR
A signature creator that just produces 71-byte empty signatures.
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...
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...
void Merge(const PSBTOutput &output)
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.
A version of CTransaction with the PSBT format.
std::map< CKeyID, std::pair< CPubKey, KeyOriginInfo > > misc_pubkeys
A signature creator for transactions.
std::map< XOnlyPubKey, std::pair< std::set< uint256 >, KeyOriginInfo > > m_tap_bip32_paths
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...
consteval auto _(util::TranslatedLiteral str)
bool AddOutput(const CTxOut &txout, const PSBTOutput &psbtout)
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::map< KeyOriginInfo, std::set< CExtPubKey > > m_xpubs
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
bool FinalizeAndExtractPSBT(PartiallySignedTransaction &psbtx, CMutableTransaction &result)
Finalizes a PSBT if possible, and extracts it to a CMutableTransaction if it could be finalized...
bool IsPayToTaproot() const
XOnlyPubKey m_tap_internal_key
void FromSignatureData(const SignatureData &sigdata)
std::map< std::pair< XOnlyPubKey, uint256 >, std::vector< unsigned char > > taproot_script_sigs
Schnorr signature for key path spending.
std::string PSBTRoleName(PSBTRole role)
Minimal stream for reading from an existing byte array by std::span.
A structure for PSBTs which contains per output information.
std::optional< uint32_t > m_version
std::vector< PSBTOutput > outputs
std::map< CPubKey, KeyOriginInfo > hd_keypaths
uint160 missing_redeem_script
ScriptID of the missing redeemScript (if any)
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
std::map< CKeyID, XOnlyPubKey > tap_pubkeys
Misc Taproot pubkeys involved in this input, by hash. (Equivalent of misc_pubkeys but for Taproot...
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.
An input of a transaction.
uint32_t GetVersion() const
bool DecodeBase64PSBT(PartiallySignedTransaction &psbt, const std::string &base64_tx, std::string &error)
Decode a base64ed PSBT into a PartiallySignedTransaction.
std::optional< std::vector< unsigned char > > DecodeBase64(std::string_view str)
std::map< CPubKey, std::vector< CPubKey > > m_musig2_participants
const SigningProvider & DUMMY_SIGNING_PROVIDER
TaprootBuilder & Finalize(const XOnlyPubKey &internal_key)
Finalize the construction.
std::optional< TaprootBuilder > tr_builder
Taproot tree used to build tr_spenddata.
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
is a home for simple enum and struct type definitions that can be used internally by functions in the...
bool DecodeRawPSBT(PartiallySignedTransaction &psbt, std::span< const std::byte > tx_data, std::string &error)
Decode a raw (binary blob) PSBT into a PartiallySignedTransaction.
static constexpr script_verify_flags STANDARD_SCRIPT_VERIFY_FLAGS
Standard script verification flags that standard transactions will comply with.
An output of a transaction.
void UpdatePSBTOutput(const SigningProvider &provider, PartiallySignedTransaction &psbt, int index)
Updates a PSBTOutput with information from provider.
uint256 merkle_root
The Merkle root of the script tree (0 if no scripts).
An outpoint - a combination of a transaction hash and an index n into its vout.
std::vector< CTxOut > vout
constexpr bool IsNull() const
std::vector< PSBTInput > inputs
CScriptWitness scriptWitness
The scriptWitness of an input. Contains complete signatures or the traditional partial signatures for...
bool IsFullyValid() const
Determine if this pubkey is fully valid.
TaprootSpendData GetSpendData() const
Compute spending data (after Finalize()).
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.
Utility class to construct Taproot outputs from internal key and script tree.
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
std::vector< std::tuple< uint8_t, uint8_t, std::vector< unsigned char > > > m_tap_tree
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction &psbt)
Counts the unsigned inputs of a PSBT.
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.
void FillSignatureData(SignatureData &sigdata) const
bool Merge(const PartiallySignedTransaction &psbt)
Merge psbt into this.
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.
An interface to be implemented by keystores that support signing.
XOnlyPubKey internal_key
The BIP341 internal key.
bool ProduceSignature(const SigningProvider &provider, const BaseSignatureCreator &creator, const CScript &fromPubKey, SignatureData &sigdata)
Produce a script signature using a generic signature creator.
A mutable version of CTransaction.
bool PSBTInputSigned(const PSBTInput &input)
Checks whether a PSBTInput is already signed by checking for non-null finalized fields.
bool IsComplete() const
Return whether there were either no leaves, or the leaves form a Huffman tree.
std::optional< CMutableTransaction > tx
std::vector< unsigned char > taproot_key_path_sig
is a home for public enum and struct type definitions that are used internally by node code...
bool complete
Stores whether the scriptSig and scriptWitness are complete.
bool GetInputUTXO(CTxOut &utxo, int input_index) const
Finds the UTXO for a given input index.
CScript redeem_script
The redeemScript (if any) for the input.
std::map< std::vector< unsigned char >, std::vector< unsigned char > > unknown
bool witness
Stores whether the input this SigData corresponds to is a witness input.
#define Assert(val)
Identity function.
std::map< CKeyID, SigPair > signatures
BIP 174 style partial signatures for the input. May contain all signatures necessary for producing a ...
void Merge(TaprootSpendData other)
Merge other TaprootSpendData (for the same scriptPubKey) into this.
void RemoveUnnecessaryTransactions(PartiallySignedTransaction &psbtx)
Reduces the size of the PSBT by dropping unnecessary non_witness_utxos (i.e.
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction &psbt)
Compute a PrecomputedTransactionData object from a psbt.
TaprootSpendData tr_spenddata
Taproot spending data.