2022-12-24 20:49:50 -03:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2019-01-09 07:06:29 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <psbt.h>
|
2020-12-16 07:28:22 -03:00
|
|
|
|
2023-12-07 19:11:27 -03:00
|
|
|
#include <node/types.h>
|
2022-07-12 10:37:27 -04:00
|
|
|
#include <policy/policy.h>
|
2023-08-08 10:45:06 -04:00
|
|
|
#include <script/signingprovider.h>
|
2020-12-16 07:28:22 -03:00
|
|
|
#include <util/check.h>
|
2019-01-09 07:06:29 -03:00
|
|
|
#include <util/strencodings.h>
|
|
|
|
|
|
|
|
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
|
|
|
|
{
|
|
|
|
inputs.resize(tx.vin.size());
|
|
|
|
outputs.resize(tx.vout.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartiallySignedTransaction::IsNull() const
|
|
|
|
{
|
|
|
|
return !tx && inputs.empty() && outputs.empty() && unknown.empty();
|
|
|
|
}
|
|
|
|
|
2019-01-30 03:51:56 -03:00
|
|
|
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
|
2019-01-09 07:06:29 -03:00
|
|
|
{
|
2019-01-30 03:51:56 -03:00
|
|
|
// Prohibited to merge two PSBTs over different transactions
|
|
|
|
if (tx->GetHash() != psbt.tx->GetHash()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-09 07:06:29 -03:00
|
|
|
for (unsigned int i = 0; i < inputs.size(); ++i) {
|
|
|
|
inputs[i].Merge(psbt.inputs[i]);
|
|
|
|
}
|
|
|
|
for (unsigned int i = 0; i < outputs.size(); ++i) {
|
|
|
|
outputs[i].Merge(psbt.outputs[i]);
|
|
|
|
}
|
2019-07-31 17:24:44 -04:00
|
|
|
for (auto& xpub_pair : psbt.m_xpubs) {
|
|
|
|
if (m_xpubs.count(xpub_pair.first) == 0) {
|
|
|
|
m_xpubs[xpub_pair.first] = xpub_pair.second;
|
|
|
|
} else {
|
|
|
|
m_xpubs[xpub_pair.first].insert(xpub_pair.second.begin(), xpub_pair.second.end());
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
|
2019-01-30 03:51:56 -03:00
|
|
|
|
|
|
|
return true;
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
2018-07-20 21:24:16 -04:00
|
|
|
bool PartiallySignedTransaction::AddInput(const CTxIn& txin, PSBTInput& psbtin)
|
|
|
|
{
|
|
|
|
if (std::find(tx->vin.begin(), tx->vin.end(), txin) != tx->vin.end()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
tx->vin.push_back(txin);
|
|
|
|
psbtin.partial_sigs.clear();
|
|
|
|
psbtin.final_script_sig.clear();
|
|
|
|
psbtin.final_script_witness.SetNull();
|
|
|
|
inputs.push_back(psbtin);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartiallySignedTransaction::AddOutput(const CTxOut& txout, const PSBTOutput& psbtout)
|
|
|
|
{
|
|
|
|
tx->vout.push_back(txout);
|
|
|
|
outputs.push_back(psbtout);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:56:47 -04:00
|
|
|
bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const
|
|
|
|
{
|
2021-06-07 19:47:33 -04:00
|
|
|
const PSBTInput& input = inputs[input_index];
|
2019-10-15 18:26:46 -03:00
|
|
|
uint32_t prevout_index = tx->vin[input_index].prevout.n;
|
2018-07-31 20:56:47 -04:00
|
|
|
if (input.non_witness_utxo) {
|
2019-10-15 18:26:46 -03:00
|
|
|
if (prevout_index >= input.non_witness_utxo->vout.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-07 19:47:33 -04:00
|
|
|
if (input.non_witness_utxo->GetHash() != tx->vin[input_index].prevout.hash) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-07-31 20:56:47 -04:00
|
|
|
utxo = input.non_witness_utxo->vout[prevout_index];
|
|
|
|
} else if (!input.witness_utxo.IsNull()) {
|
|
|
|
utxo = input.witness_utxo;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-09 07:06:29 -03:00
|
|
|
bool PSBTInput::IsNull() const
|
|
|
|
{
|
|
|
|
return !non_witness_utxo && witness_utxo.IsNull() && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty() && witness_script.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTInput::FillSignatureData(SignatureData& sigdata) const
|
|
|
|
{
|
|
|
|
if (!final_script_sig.empty()) {
|
|
|
|
sigdata.scriptSig = final_script_sig;
|
|
|
|
sigdata.complete = true;
|
|
|
|
}
|
|
|
|
if (!final_script_witness.IsNull()) {
|
|
|
|
sigdata.scriptWitness = final_script_witness;
|
|
|
|
sigdata.complete = true;
|
|
|
|
}
|
|
|
|
if (sigdata.complete) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigdata.signatures.insert(partial_sigs.begin(), partial_sigs.end());
|
|
|
|
if (!redeem_script.empty()) {
|
|
|
|
sigdata.redeem_script = redeem_script;
|
|
|
|
}
|
|
|
|
if (!witness_script.empty()) {
|
|
|
|
sigdata.witness_script = witness_script;
|
|
|
|
}
|
|
|
|
for (const auto& key_pair : hd_keypaths) {
|
|
|
|
sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
|
|
|
|
}
|
2021-07-19 15:29:29 -04:00
|
|
|
if (!m_tap_key_sig.empty()) {
|
|
|
|
sigdata.taproot_key_path_sig = m_tap_key_sig;
|
|
|
|
}
|
|
|
|
for (const auto& [pubkey_leaf, sig] : m_tap_script_sigs) {
|
|
|
|
sigdata.taproot_script_sigs.emplace(pubkey_leaf, sig);
|
|
|
|
}
|
|
|
|
if (!m_tap_internal_key.IsNull()) {
|
|
|
|
sigdata.tr_spenddata.internal_key = m_tap_internal_key;
|
|
|
|
}
|
|
|
|
if (!m_tap_merkle_root.IsNull()) {
|
|
|
|
sigdata.tr_spenddata.merkle_root = m_tap_merkle_root;
|
|
|
|
}
|
|
|
|
for (const auto& [leaf_script, control_block] : m_tap_scripts) {
|
|
|
|
sigdata.tr_spenddata.scripts.emplace(leaf_script, control_block);
|
|
|
|
}
|
|
|
|
for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
|
|
|
|
sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
|
2023-02-24 12:34:12 -03:00
|
|
|
sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
|
2021-07-19 15:29:29 -04:00
|
|
|
}
|
2023-02-11 08:57:52 -03:00
|
|
|
for (const auto& [hash, preimage] : ripemd160_preimages) {
|
|
|
|
sigdata.ripemd160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
|
|
|
|
}
|
|
|
|
for (const auto& [hash, preimage] : sha256_preimages) {
|
|
|
|
sigdata.sha256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
|
|
|
|
}
|
|
|
|
for (const auto& [hash, preimage] : hash160_preimages) {
|
|
|
|
sigdata.hash160_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
|
|
|
|
}
|
|
|
|
for (const auto& [hash, preimage] : hash256_preimages) {
|
|
|
|
sigdata.hash256_preimages.emplace(std::vector<unsigned char>(hash.begin(), hash.end()), preimage);
|
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTInput::FromSignatureData(const SignatureData& sigdata)
|
|
|
|
{
|
|
|
|
if (sigdata.complete) {
|
|
|
|
partial_sigs.clear();
|
|
|
|
hd_keypaths.clear();
|
|
|
|
redeem_script.clear();
|
|
|
|
witness_script.clear();
|
|
|
|
|
|
|
|
if (!sigdata.scriptSig.empty()) {
|
|
|
|
final_script_sig = sigdata.scriptSig;
|
|
|
|
}
|
|
|
|
if (!sigdata.scriptWitness.IsNull()) {
|
|
|
|
final_script_witness = sigdata.scriptWitness;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end());
|
|
|
|
if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
|
|
|
|
redeem_script = sigdata.redeem_script;
|
|
|
|
}
|
|
|
|
if (witness_script.empty() && !sigdata.witness_script.empty()) {
|
|
|
|
witness_script = sigdata.witness_script;
|
|
|
|
}
|
|
|
|
for (const auto& entry : sigdata.misc_pubkeys) {
|
|
|
|
hd_keypaths.emplace(entry.second);
|
|
|
|
}
|
2021-07-19 15:29:29 -04:00
|
|
|
if (!sigdata.taproot_key_path_sig.empty()) {
|
|
|
|
m_tap_key_sig = sigdata.taproot_key_path_sig;
|
|
|
|
}
|
|
|
|
for (const auto& [pubkey_leaf, sig] : sigdata.taproot_script_sigs) {
|
|
|
|
m_tap_script_sigs.emplace(pubkey_leaf, sig);
|
|
|
|
}
|
|
|
|
if (!sigdata.tr_spenddata.internal_key.IsNull()) {
|
|
|
|
m_tap_internal_key = sigdata.tr_spenddata.internal_key;
|
|
|
|
}
|
|
|
|
if (!sigdata.tr_spenddata.merkle_root.IsNull()) {
|
|
|
|
m_tap_merkle_root = sigdata.tr_spenddata.merkle_root;
|
|
|
|
}
|
|
|
|
for (const auto& [leaf_script, control_block] : sigdata.tr_spenddata.scripts) {
|
|
|
|
m_tap_scripts.emplace(leaf_script, control_block);
|
|
|
|
}
|
|
|
|
for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
|
|
|
|
m_tap_bip32_paths.emplace(pubkey, leaf_origin);
|
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTInput::Merge(const PSBTInput& input)
|
|
|
|
{
|
|
|
|
if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
|
|
|
|
if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
|
|
|
|
witness_utxo = input.witness_utxo;
|
|
|
|
}
|
|
|
|
|
|
|
|
partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
|
2021-12-05 09:07:54 -03:00
|
|
|
ripemd160_preimages.insert(input.ripemd160_preimages.begin(), input.ripemd160_preimages.end());
|
|
|
|
sha256_preimages.insert(input.sha256_preimages.begin(), input.sha256_preimages.end());
|
|
|
|
hash160_preimages.insert(input.hash160_preimages.begin(), input.hash160_preimages.end());
|
|
|
|
hash256_preimages.insert(input.hash256_preimages.begin(), input.hash256_preimages.end());
|
2019-01-09 07:06:29 -03:00
|
|
|
hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
|
|
|
|
unknown.insert(input.unknown.begin(), input.unknown.end());
|
2021-11-25 00:50:10 -03:00
|
|
|
m_tap_script_sigs.insert(input.m_tap_script_sigs.begin(), input.m_tap_script_sigs.end());
|
|
|
|
m_tap_scripts.insert(input.m_tap_scripts.begin(), input.m_tap_scripts.end());
|
|
|
|
m_tap_bip32_paths.insert(input.m_tap_bip32_paths.begin(), input.m_tap_bip32_paths.end());
|
2019-01-09 07:06:29 -03:00
|
|
|
|
|
|
|
if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script;
|
|
|
|
if (witness_script.empty() && !input.witness_script.empty()) witness_script = input.witness_script;
|
|
|
|
if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig;
|
|
|
|
if (final_script_witness.IsNull() && !input.final_script_witness.IsNull()) final_script_witness = input.final_script_witness;
|
2021-11-25 00:50:10 -03:00
|
|
|
if (m_tap_key_sig.empty() && !input.m_tap_key_sig.empty()) m_tap_key_sig = input.m_tap_key_sig;
|
|
|
|
if (m_tap_internal_key.IsNull() && !input.m_tap_internal_key.IsNull()) m_tap_internal_key = input.m_tap_internal_key;
|
|
|
|
if (m_tap_merkle_root.IsNull() && !input.m_tap_merkle_root.IsNull()) m_tap_merkle_root = input.m_tap_merkle_root;
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTOutput::FillSignatureData(SignatureData& sigdata) const
|
|
|
|
{
|
|
|
|
if (!redeem_script.empty()) {
|
|
|
|
sigdata.redeem_script = redeem_script;
|
|
|
|
}
|
|
|
|
if (!witness_script.empty()) {
|
|
|
|
sigdata.witness_script = witness_script;
|
|
|
|
}
|
|
|
|
for (const auto& key_pair : hd_keypaths) {
|
|
|
|
sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
|
|
|
|
}
|
2022-08-16 21:17:43 -04:00
|
|
|
if (!m_tap_tree.empty() && m_tap_internal_key.IsFullyValid()) {
|
|
|
|
TaprootBuilder builder;
|
|
|
|
for (const auto& [depth, leaf_ver, script] : m_tap_tree) {
|
|
|
|
builder.Add((int)depth, script, (int)leaf_ver, /*track=*/true);
|
|
|
|
}
|
|
|
|
assert(builder.IsComplete());
|
|
|
|
builder.Finalize(m_tap_internal_key);
|
|
|
|
TaprootSpendData spenddata = builder.GetSpendData();
|
2021-07-19 16:02:36 -04:00
|
|
|
|
|
|
|
sigdata.tr_spenddata.internal_key = m_tap_internal_key;
|
|
|
|
sigdata.tr_spenddata.Merge(spenddata);
|
|
|
|
}
|
|
|
|
for (const auto& [pubkey, leaf_origin] : m_tap_bip32_paths) {
|
|
|
|
sigdata.taproot_misc_pubkeys.emplace(pubkey, leaf_origin);
|
2023-02-24 12:34:12 -03:00
|
|
|
sigdata.tap_pubkeys.emplace(Hash160(pubkey), pubkey);
|
2021-07-19 16:02:36 -04:00
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTOutput::FromSignatureData(const SignatureData& sigdata)
|
|
|
|
{
|
|
|
|
if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
|
|
|
|
redeem_script = sigdata.redeem_script;
|
|
|
|
}
|
|
|
|
if (witness_script.empty() && !sigdata.witness_script.empty()) {
|
|
|
|
witness_script = sigdata.witness_script;
|
|
|
|
}
|
|
|
|
for (const auto& entry : sigdata.misc_pubkeys) {
|
|
|
|
hd_keypaths.emplace(entry.second);
|
|
|
|
}
|
2021-07-19 16:02:36 -04:00
|
|
|
if (!sigdata.tr_spenddata.internal_key.IsNull()) {
|
|
|
|
m_tap_internal_key = sigdata.tr_spenddata.internal_key;
|
|
|
|
}
|
2022-08-16 15:26:19 -04:00
|
|
|
if (sigdata.tr_builder.has_value() && sigdata.tr_builder->HasScripts()) {
|
2022-08-16 21:17:43 -04:00
|
|
|
m_tap_tree = sigdata.tr_builder->GetTreeTuples();
|
2021-07-19 16:02:36 -04:00
|
|
|
}
|
|
|
|
for (const auto& [pubkey, leaf_origin] : sigdata.taproot_misc_pubkeys) {
|
|
|
|
m_tap_bip32_paths.emplace(pubkey, leaf_origin);
|
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PSBTOutput::IsNull() const
|
|
|
|
{
|
|
|
|
return redeem_script.empty() && witness_script.empty() && hd_keypaths.empty() && unknown.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTOutput::Merge(const PSBTOutput& output)
|
|
|
|
{
|
|
|
|
hd_keypaths.insert(output.hd_keypaths.begin(), output.hd_keypaths.end());
|
|
|
|
unknown.insert(output.unknown.begin(), output.unknown.end());
|
2021-11-25 00:50:10 -03:00
|
|
|
m_tap_bip32_paths.insert(output.m_tap_bip32_paths.begin(), output.m_tap_bip32_paths.end());
|
2019-01-09 07:06:29 -03:00
|
|
|
|
|
|
|
if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
|
|
|
|
if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script;
|
2021-11-25 00:50:10 -03:00
|
|
|
if (m_tap_internal_key.IsNull() && !output.m_tap_internal_key.IsNull()) m_tap_internal_key = output.m_tap_internal_key;
|
2022-08-16 21:17:43 -04:00
|
|
|
if (m_tap_tree.empty() && !output.m_tap_tree.empty()) m_tap_tree = output.m_tap_tree;
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
2022-07-12 10:37:27 -04:00
|
|
|
|
2019-03-01 05:25:10 -03:00
|
|
|
bool PSBTInputSigned(const PSBTInput& input)
|
2019-01-09 07:06:29 -03:00
|
|
|
{
|
|
|
|
return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
|
|
|
|
}
|
|
|
|
|
2022-07-12 10:37:27 -04:00
|
|
|
bool PSBTInputSignedAndVerified(const PartiallySignedTransaction psbt, unsigned int input_index, const PrecomputedTransactionData* txdata)
|
|
|
|
{
|
|
|
|
CTxOut utxo;
|
|
|
|
assert(psbt.inputs.size() >= input_index);
|
|
|
|
const PSBTInput& input = psbt.inputs[input_index];
|
|
|
|
|
|
|
|
if (input.non_witness_utxo) {
|
|
|
|
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
|
|
|
|
COutPoint prevout = psbt.tx->vin[input_index].prevout;
|
|
|
|
if (prevout.n >= input.non_witness_utxo->vout.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (input.non_witness_utxo->GetHash() != prevout.hash) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
utxo = input.non_witness_utxo->vout[prevout.n];
|
|
|
|
} else if (!input.witness_utxo.IsNull()) {
|
|
|
|
utxo = input.witness_utxo;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (txdata) {
|
|
|
|
return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, *txdata, MissingDataBehavior::FAIL});
|
|
|
|
} else {
|
|
|
|
return VerifyScript(input.final_script_sig, utxo.scriptPubKey, &input.final_script_witness, STANDARD_SCRIPT_VERIFY_FLAGS, MutableTransactionSignatureChecker{&(*psbt.tx), input_index, utxo.nValue, MissingDataBehavior::FAIL});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-29 04:25:04 -04:00
|
|
|
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
|
|
|
|
size_t count = 0;
|
|
|
|
for (const auto& input : psbt.inputs) {
|
|
|
|
if (!PSBTInputSigned(input)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2019-02-16 20:49:36 -03:00
|
|
|
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
|
|
|
|
{
|
2020-12-16 07:28:22 -03:00
|
|
|
CMutableTransaction& tx = *Assert(psbt.tx);
|
|
|
|
const CTxOut& out = tx.vout.at(index);
|
2019-02-16 20:49:36 -03:00
|
|
|
PSBTOutput& psbt_out = psbt.outputs.at(index);
|
|
|
|
|
|
|
|
// Fill a SignatureData with output info
|
|
|
|
SignatureData sigdata;
|
|
|
|
psbt_out.FillSignatureData(sigdata);
|
|
|
|
|
|
|
|
// Construct a would-be spend of this output, to update sigdata with.
|
|
|
|
// Note that ProduceSignature is used to fill in metadata (not actual signatures),
|
|
|
|
// so provider does not need to provide any private keys (it can be a HidingSigningProvider).
|
2020-07-01 13:31:53 -04:00
|
|
|
MutableTransactionSignatureCreator creator(tx, /*input_idx=*/0, out.nValue, SIGHASH_ALL);
|
2019-02-16 20:49:36 -03:00
|
|
|
ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
|
|
|
|
|
|
|
|
// Put redeem_script, witness_script, key paths, into PSBTOutput.
|
|
|
|
psbt_out.FromSignatureData(sigdata);
|
|
|
|
}
|
|
|
|
|
2021-03-03 21:47:44 -03:00
|
|
|
PrecomputedTransactionData PrecomputePSBTData(const PartiallySignedTransaction& psbt)
|
|
|
|
{
|
|
|
|
const CMutableTransaction& tx = *psbt.tx;
|
|
|
|
bool have_all_spent_outputs = true;
|
|
|
|
std::vector<CTxOut> utxos(tx.vin.size());
|
|
|
|
for (size_t idx = 0; idx < tx.vin.size(); ++idx) {
|
|
|
|
if (!psbt.GetInputUTXO(utxos[idx], idx)) have_all_spent_outputs = false;
|
|
|
|
}
|
|
|
|
PrecomputedTransactionData txdata;
|
|
|
|
if (have_all_spent_outputs) {
|
|
|
|
txdata.Init(tx, std::move(utxos), true);
|
|
|
|
} else {
|
|
|
|
txdata.Init(tx, {}, true);
|
|
|
|
}
|
|
|
|
return txdata;
|
|
|
|
}
|
|
|
|
|
2021-07-20 21:24:56 -04:00
|
|
|
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, const PrecomputedTransactionData* txdata, int sighash, SignatureData* out_sigdata, bool finalize)
|
2019-01-09 07:06:29 -03:00
|
|
|
{
|
|
|
|
PSBTInput& input = psbt.inputs.at(index);
|
|
|
|
const CMutableTransaction& tx = *psbt.tx;
|
|
|
|
|
2022-07-12 10:37:27 -04:00
|
|
|
if (PSBTInputSignedAndVerified(psbt, index, txdata)) {
|
2019-01-09 07:06:29 -03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill SignatureData with input info
|
|
|
|
SignatureData sigdata;
|
|
|
|
input.FillSignatureData(sigdata);
|
|
|
|
|
|
|
|
// Get UTXO
|
|
|
|
bool require_witness_sig = false;
|
|
|
|
CTxOut utxo;
|
|
|
|
|
|
|
|
if (input.non_witness_utxo) {
|
|
|
|
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
|
|
|
|
COutPoint prevout = tx.vin[index].prevout;
|
2019-10-15 18:26:46 -03:00
|
|
|
if (prevout.n >= input.non_witness_utxo->vout.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
if (input.non_witness_utxo->GetHash() != prevout.hash) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
utxo = input.non_witness_utxo->vout[prevout.n];
|
|
|
|
} else if (!input.witness_utxo.IsNull()) {
|
|
|
|
utxo = input.witness_utxo;
|
|
|
|
// When we're taking our information from a witness UTXO, we can't verify it is actually data from
|
|
|
|
// the output being spent. This is safe in case a witness signature is produced (which includes this
|
|
|
|
// information directly in the hash), but not for non-witness signatures. Remember that we require
|
|
|
|
// a witness signature in this situation.
|
|
|
|
require_witness_sig = true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sigdata.witness = false;
|
2018-07-31 20:57:15 -04:00
|
|
|
bool sig_complete;
|
2021-03-03 21:47:44 -03:00
|
|
|
if (txdata == nullptr) {
|
2018-07-31 20:57:15 -04:00
|
|
|
sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
|
|
|
|
} else {
|
2020-07-01 13:31:53 -04:00
|
|
|
MutableTransactionSignatureCreator creator(tx, index, utxo.nValue, txdata, sighash);
|
2018-07-31 20:57:15 -04:00
|
|
|
sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
|
|
|
|
}
|
2019-01-09 07:06:29 -03:00
|
|
|
// Verify that a witness signature was produced in case one was required.
|
|
|
|
if (require_witness_sig && !sigdata.witness) return false;
|
2021-07-20 21:24:56 -04:00
|
|
|
|
|
|
|
// If we are not finalizing, set sigdata.complete to false to not set the scriptWitness
|
|
|
|
if (!finalize && sigdata.complete) sigdata.complete = false;
|
|
|
|
|
2019-01-09 07:06:29 -03:00
|
|
|
input.FromSignatureData(sigdata);
|
|
|
|
|
2020-06-04 23:43:43 -04:00
|
|
|
// If we have a witness signature, put a witness UTXO.
|
2019-01-09 07:06:29 -03:00
|
|
|
if (sigdata.witness) {
|
|
|
|
input.witness_utxo = utxo;
|
2021-07-20 20:04:33 -04:00
|
|
|
// We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0
|
|
|
|
// inputs in this transaction. Since this requires inspecting the entire transaction, this
|
|
|
|
// is something for the caller to deal with (i.e. FillPSBT).
|
2019-01-09 07:06:29 -03:00
|
|
|
}
|
|
|
|
|
2018-07-31 20:57:15 -04:00
|
|
|
// Fill in the missing info
|
|
|
|
if (out_sigdata) {
|
|
|
|
out_sigdata->missing_pubkeys = sigdata.missing_pubkeys;
|
|
|
|
out_sigdata->missing_sigs = sigdata.missing_sigs;
|
|
|
|
out_sigdata->missing_redeem_script = sigdata.missing_redeem_script;
|
|
|
|
out_sigdata->missing_witness_script = sigdata.missing_witness_script;
|
|
|
|
}
|
|
|
|
|
2019-01-09 07:06:29 -03:00
|
|
|
return sig_complete;
|
|
|
|
}
|
2019-01-09 08:08:32 -03:00
|
|
|
|
2022-08-23 15:24:00 -04:00
|
|
|
void RemoveUnnecessaryTransactions(PartiallySignedTransaction& psbtx, const int& sighash_type)
|
|
|
|
{
|
|
|
|
// Only drop non_witness_utxos if sighash_type != SIGHASH_ANYONECANPAY
|
|
|
|
if ((sighash_type & 0x80) != SIGHASH_ANYONECANPAY) {
|
|
|
|
// Figure out if any non_witness_utxos should be dropped
|
|
|
|
std::vector<unsigned int> to_drop;
|
|
|
|
for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
|
|
|
|
const auto& input = psbtx.inputs.at(i);
|
|
|
|
int wit_ver;
|
|
|
|
std::vector<unsigned char> wit_prog;
|
|
|
|
if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
|
|
|
|
// There's a non-segwit input or Segwit v0, so we cannot drop any witness_utxos
|
|
|
|
to_drop.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (wit_ver == 0) {
|
|
|
|
// Segwit v0, so we cannot drop any non_witness_utxos
|
|
|
|
to_drop.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (input.non_witness_utxo) {
|
|
|
|
to_drop.push_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop the non_witness_utxos that we can drop
|
|
|
|
for (unsigned int i : to_drop) {
|
|
|
|
psbtx.inputs.at(i).non_witness_utxo = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 08:08:32 -03:00
|
|
|
bool FinalizePSBT(PartiallySignedTransaction& psbtx)
|
|
|
|
{
|
|
|
|
// Finalize input signatures -- in case we have partial signatures that add up to a complete
|
|
|
|
// signature, but have not combined them yet (e.g. because the combiner that created this
|
|
|
|
// PartiallySignedTransaction did not understand them), this will combine them into a final
|
|
|
|
// script.
|
|
|
|
bool complete = true;
|
2021-03-03 21:47:44 -03:00
|
|
|
const PrecomputedTransactionData txdata = PrecomputePSBTData(psbtx);
|
2019-01-09 08:08:32 -03:00
|
|
|
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
|
2021-07-20 21:24:56 -04:00
|
|
|
complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, &txdata, SIGHASH_ALL, nullptr, true);
|
2019-01-09 08:08:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return complete;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FinalizeAndExtractPSBT(PartiallySignedTransaction& psbtx, CMutableTransaction& result)
|
|
|
|
{
|
|
|
|
// It's not safe to extract a PSBT that isn't finalized, and there's no easy way to check
|
|
|
|
// whether a PSBT is finalized without finalizing it, so we just do this.
|
|
|
|
if (!FinalizePSBT(psbtx)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = *psbtx.tx;
|
|
|
|
for (unsigned int i = 0; i < result.vin.size(); ++i) {
|
|
|
|
result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig;
|
|
|
|
result.vin[i].scriptWitness = psbtx.inputs[i].final_script_witness;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-13 13:43:16 -03:00
|
|
|
bool CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
|
2019-01-09 08:08:32 -03:00
|
|
|
{
|
|
|
|
out = psbtxs[0]; // Copy the first one
|
|
|
|
|
|
|
|
// Merge
|
|
|
|
for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
|
|
|
|
if (!out.Merge(*it)) {
|
2023-12-13 13:43:16 -03:00
|
|
|
return false;
|
2019-01-09 08:08:32 -03:00
|
|
|
}
|
|
|
|
}
|
2023-12-13 13:43:16 -03:00
|
|
|
return true;
|
2019-01-09 08:08:32 -03:00
|
|
|
}
|
2019-03-05 23:55:40 -03:00
|
|
|
|
2019-03-01 05:25:10 -03:00
|
|
|
std::string PSBTRoleName(PSBTRole role) {
|
|
|
|
switch (role) {
|
2019-11-19 16:35:14 -03:00
|
|
|
case PSBTRole::CREATOR: return "creator";
|
2019-03-01 05:25:10 -03:00
|
|
|
case PSBTRole::UPDATER: return "updater";
|
|
|
|
case PSBTRole::SIGNER: return "signer";
|
|
|
|
case PSBTRole::FINALIZER: return "finalizer";
|
|
|
|
case PSBTRole::EXTRACTOR: return "extractor";
|
2019-05-02 02:24:05 -04:00
|
|
|
// no default case, so the compiler can warn about missing cases
|
2019-03-01 05:25:10 -03:00
|
|
|
}
|
2019-05-02 02:24:05 -04:00
|
|
|
assert(false);
|
2019-03-01 05:25:10 -03:00
|
|
|
}
|
|
|
|
|
2019-03-05 23:55:40 -03:00
|
|
|
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
|
|
|
|
{
|
2022-04-04 13:52:06 -04:00
|
|
|
auto tx_data = DecodeBase64(base64_tx);
|
|
|
|
if (!tx_data) {
|
2019-03-05 23:55:40 -03:00
|
|
|
error = "invalid base64";
|
|
|
|
return false;
|
|
|
|
}
|
2022-04-04 13:52:06 -04:00
|
|
|
return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error);
|
2019-03-05 23:55:40 -03:00
|
|
|
}
|
|
|
|
|
2022-04-04 13:19:49 -04:00
|
|
|
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)
|
2019-03-05 23:55:40 -03:00
|
|
|
{
|
2023-09-11 13:06:51 -03:00
|
|
|
DataStream ss_data{tx_data};
|
2019-03-05 23:55:40 -03:00
|
|
|
try {
|
|
|
|
ss_data >> psbt;
|
|
|
|
if (!ss_data.empty()) {
|
|
|
|
error = "extra data after PSBT";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} catch (const std::exception& e) {
|
|
|
|
error = e.what();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2021-01-04 19:21:29 -03:00
|
|
|
|
|
|
|
uint32_t PartiallySignedTransaction::GetVersion() const
|
|
|
|
{
|
|
|
|
if (m_version != std::nullopt) {
|
|
|
|
return *m_version;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|