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]);
90 std::optional<uint32_t> time_lock{0};
91 std::optional<uint32_t> height_lock{0};
93 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
95 if (!time_lock.has_value()) {
98 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
100 if (!height_lock.has_value()) {
104 if (input.time_locktime && time_lock.has_value()) {
105 time_lock = std::max(time_lock, input.time_locktime);
107 if (input.height_locktime && height_lock.has_value()) {
108 height_lock = std::max(height_lock, input.height_locktime);
111 if (height_lock.has_value() && *height_lock > 0) {
114 if (time_lock.has_value() && *time_lock > 0) {
135 txin.
nSequence = input.sequence.value_or(max_sequence);
136 mtx.
vin.push_back(txin);
140 txout.
nValue = output.amount;
142 mtx.
vout.push_back(txout);
155 for (
CTxIn& txin : mtx->vin) {
159 return mtx->GetHash();
172 return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out;
181 inputs.back().partial_sigs.clear();
182 inputs.back().final_script_sig.clear();
183 inputs.back().final_script_witness.SetNull();
198 if (iterate_inputs) {
206 bool has_sigs =
false;
208 if (input.time_locktime.has_value() && !input.height_locktime.has_value()) {
210 if (time_lock == std::nullopt) {
213 }
else if (!input.time_locktime.has_value() && input.height_locktime.has_value()) {
215 if (height_lock == std::nullopt) {
219 if (input.time_locktime && time_lock.has_value()) {
220 time_lock = std::max(time_lock, input.time_locktime);
222 if (input.height_locktime && height_lock.has_value()) {
223 height_lock = std::max(height_lock, input.height_locktime);
225 if (input.HasSignatures()) {
230 if (height_lock.has_value() && *height_lock > 0) {
231 new_timelock = *height_lock;
232 }
else if (time_lock.has_value() && *time_lock > 0) {
233 new_timelock = *time_lock;
235 if (has_sigs && *old_timelock != new_timelock) {
318 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
332 for (
const auto& [leaf_script, control_block] :
m_tap_scripts) {
340 sigdata.
ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
343 sigdata.
sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
346 sigdata.
hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
349 sigdata.
hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
353 sigdata.
musig2_pubnonces[agg_key_lh].insert(pubnonces.begin(), pubnonces.end());
413 ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
416 sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
419 hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
422 hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
484 sigdata.
misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
489 builder.
Add((
int)depth,
script, (
int)leaf_ver,
true);
576 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
590 for (
const auto& input : psbt.
inputs) {
601 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
625 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
630 bool have_all_spent_outputs =
true;
631 std::vector<CTxOut> utxos;
633 if (!input.
GetUTXO(utxos.emplace_back())) have_all_spent_outputs =
false;
636 if (have_all_spent_outputs) {
637 txdata.
Init(tx, std::move(utxos),
true);
639 txdata.
Init(tx, {},
true);
647 std::optional<CMutableTransaction> unsigned_tx = psbt.
GetUnsignedTx();
649 return PSBTError::INVALID_TX;
654 return PSBTError::OK;
662 bool require_witness_sig =
false;
669 return PSBTError::MISSING_INPUTS;
672 return PSBTError::MISSING_INPUTS;
681 require_witness_sig =
true;
683 return PSBTError::MISSING_INPUTS;
695 return PSBTError::SIGHASH_MISMATCH;
708 return PSBTError::SIGHASH_MISMATCH;
711 if (sig.size() != 64)
return PSBTError::SIGHASH_MISMATCH;
715 return PSBTError::SIGHASH_MISMATCH;
718 if (sig.size() != 65 || sig.back() != sighash)
return PSBTError::SIGHASH_MISMATCH;
721 if (sig.second.back() != sighash)
return PSBTError::SIGHASH_MISMATCH;
727 if (txdata ==
nullptr) {
734 if (require_witness_sig && !sigdata.
witness)
return PSBTError::INCOMPLETE;
757 return sig_complete ? PSBTError::OK : PSBTError::INCOMPLETE;
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) {
825 std::optional<CMutableTransaction> unsigned_tx = psbtx.
GetUnsignedTx();
829 result = *unsigned_tx;
830 for (
unsigned int i = 0; i < result.
vin.size(); ++i) {
831 result.
vin[i].scriptSig = psbtx.
inputs[i].final_script_sig;
832 result.
vin[i].scriptWitness = psbtx.
inputs[i].final_script_witness;
837std::optional<PartiallySignedTransaction>
CombinePSBTs(
const std::vector<PartiallySignedTransaction>& psbtxs)
842 for (
auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
843 if (!
out.Merge(*it)) {
875 if (!ss_data.empty()) {
879 }
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
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
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.
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)