2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2019-01-30 06:32:38 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2022-09-18 23:11:49 +02:00
|
|
|
#include <coins.h>
|
|
|
|
#include <consensus/tx_verify.h>
|
|
|
|
#include <policy/policy.h>
|
|
|
|
#include <policy/settings.h>
|
2019-01-30 06:32:38 +01:00
|
|
|
#include <psbt.h>
|
2019-12-10 18:11:28 +01:00
|
|
|
#include <tinyformat.h>
|
2022-07-02 20:44:47 +02:00
|
|
|
#include <util/check.h>
|
2019-01-30 06:32:38 +01:00
|
|
|
#include <util/strencodings.h>
|
|
|
|
|
2022-09-18 23:11:49 +02:00
|
|
|
#include <numeric>
|
|
|
|
|
2022-09-24 05:20:39 +02:00
|
|
|
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx) : tx(tx)
|
|
|
|
{
|
|
|
|
inputs.resize(tx.vin.size());
|
|
|
|
outputs.resize(tx.vout.size());
|
|
|
|
}
|
|
|
|
|
2019-01-30 06:32:38 +01:00
|
|
|
bool PartiallySignedTransaction::IsNull() const
|
|
|
|
{
|
|
|
|
return !tx && inputs.empty() && outputs.empty() && unknown.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
|
|
|
|
{
|
|
|
|
// Prohibited to merge two PSBTs over different transactions
|
|
|
|
if (tx->GetHash() != psbt.tx->GetHash()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartiallySignedTransaction::GetInputUTXO(CTxOut& utxo, int input_index) const
|
|
|
|
{
|
|
|
|
PSBTInput input = inputs[input_index];
|
2020-01-29 12:26:17 +01:00
|
|
|
uint32_t prevout_index = tx->vin[input_index].prevout.n;
|
2019-01-30 06:32:38 +01:00
|
|
|
if (input.non_witness_utxo) {
|
2020-01-29 12:26:17 +01:00
|
|
|
if (prevout_index >= input.non_witness_utxo->vout.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-01-30 06:32:38 +01:00
|
|
|
utxo = input.non_witness_utxo->vout[prevout_index];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PSBTInput::IsNull() const
|
|
|
|
{
|
|
|
|
return !non_witness_utxo && partial_sigs.empty() && unknown.empty() && hd_keypaths.empty() && redeem_script.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTInput::FillSignatureData(SignatureData& sigdata) const
|
|
|
|
{
|
|
|
|
if (!final_script_sig.empty()) {
|
|
|
|
sigdata.scriptSig = final_script_sig;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
for (const auto& key_pair : hd_keypaths) {
|
|
|
|
sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTInput::FromSignatureData(const SignatureData& sigdata)
|
|
|
|
{
|
|
|
|
if (sigdata.complete) {
|
|
|
|
partial_sigs.clear();
|
|
|
|
hd_keypaths.clear();
|
|
|
|
redeem_script.clear();
|
|
|
|
|
|
|
|
if (!sigdata.scriptSig.empty()) {
|
|
|
|
final_script_sig = sigdata.scriptSig;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
partial_sigs.insert(sigdata.signatures.begin(), sigdata.signatures.end());
|
|
|
|
if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
|
|
|
|
redeem_script = sigdata.redeem_script;
|
|
|
|
}
|
|
|
|
for (const auto& entry : sigdata.misc_pubkeys) {
|
|
|
|
hd_keypaths.emplace(entry.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTInput::Merge(const PSBTInput& input)
|
|
|
|
{
|
|
|
|
if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
|
|
|
|
|
|
|
|
partial_sigs.insert(input.partial_sigs.begin(), input.partial_sigs.end());
|
|
|
|
hd_keypaths.insert(input.hd_keypaths.begin(), input.hd_keypaths.end());
|
|
|
|
unknown.insert(input.unknown.begin(), input.unknown.end());
|
|
|
|
|
|
|
|
if (redeem_script.empty() && !input.redeem_script.empty()) redeem_script = input.redeem_script;
|
|
|
|
if (final_script_sig.empty() && !input.final_script_sig.empty()) final_script_sig = input.final_script_sig;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTOutput::FillSignatureData(SignatureData& sigdata) const
|
|
|
|
{
|
|
|
|
if (!redeem_script.empty()) {
|
|
|
|
sigdata.redeem_script = redeem_script;
|
|
|
|
}
|
|
|
|
for (const auto& key_pair : hd_keypaths) {
|
|
|
|
sigdata.misc_pubkeys.emplace(key_pair.first.GetID(), key_pair);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PSBTOutput::FromSignatureData(const SignatureData& sigdata)
|
|
|
|
{
|
|
|
|
if (redeem_script.empty() && !sigdata.redeem_script.empty()) {
|
|
|
|
redeem_script = sigdata.redeem_script;
|
|
|
|
}
|
|
|
|
for (const auto& entry : sigdata.misc_pubkeys) {
|
|
|
|
hd_keypaths.emplace(entry.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PSBTOutput::IsNull() const
|
|
|
|
{
|
|
|
|
return redeem_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());
|
|
|
|
|
|
|
|
if (redeem_script.empty() && !output.redeem_script.empty()) redeem_script = output.redeem_script;
|
|
|
|
}
|
|
|
|
|
partial Merge #18027: "PSBT Operations" dialog
BACKPORT NOTICE
fixup psbt. all missing changes belongs to src/wallet/scriptpubkeyman.h/cpp ----- they are related to descriptor wallet!
-------------------
931dd4760855e036c176a23ec2de367c460e4243 Make lint-spelling.py happy (Glenn Willen)
11a0ffb29d1b4dcc55c8826873f340ab4196af21 [gui] Load PSBT from clipboard (Glenn Willen)
a6cb0b0c29d327d01aebb98b0504f317eb19c3dc [gui] PSBT Operations Dialog (sign & broadcast) (Glenn Willen)
5dd0c03ffa3aeaa69d8a3a716f902f450d5eaaec FillPSBT: report number of inputs signed (or would sign) (Glenn Willen)
9e7b23b73387600d175aff8bd5e6624dd51f86e7 Improve TransactionErrorString messages. (Glenn Willen)
Pull request description:
Add a "PSBT Operations" dialog, reached from the "Load PSBT..." menu item, giving options to sign or broadcast the loaded PSBT as appropriate, as well as copying the result to the clipboard or saving it to a file.
This is based on Sjors' #17509, and depends on that PR going in first. (It effectively replaces the small "load PSBT" dialog from that PR with a more feature-rich one.)
Some notes:
* The way I display status information is maybe unusual (a status bar, rather than messageboxes.) I think it's helpful to have the information in it be persistent rather than transitory. But if people dislike it, I would probably move the "current state of the transaction" info to the top line of the main label, and the "what action just happened, and did it succeed" info into a messagebox.
* I don't really know much about the translation/localization stuff. I put tr() in all the places it seemed like it ought to go. I did not attempt to translate the result of TransactionErrorString (which is shared by GUI and non-GUI code); I don't know if that's correct, but it matches the "error messages in logs should be googleable in English" heuristic. I don't know whether there are things I should be doing to reduce translator effort (like minimizing the total number of distinct message strings I use, or something.)
* I don't really know how (if?) automated testing is applied to GUI code. I can make a list of PSBTs exercising all the codepaths for manual testing, if that's the right approach. Input appreciated.
ACKs for top commit:
instagibbs:
tested ACK https://github.com/bitcoin/bitcoin/pull/18027/commits/931dd4760855e036c176a23ec2de367c460e4243
Sjors:
re-tACK 931dd4760855e036c176a23ec2de367c460e4243
jb55:
ACK 931dd4760855e036c176a23ec2de367c460e4243
achow101:
ACK 931dd4760855e036c176a23ec2de367c460e4243
Tree-SHA512: ade52471a2242f839a8bd6a1fd231443cc4b43bb9c1de3fb5ace7c5eb59eca99b1f2e9f17dfdb4b08d84d91f5fd65677db1433dd03eef51c7774963ef4e2e74f
2020-06-21 12:56:58 +02:00
|
|
|
size_t CountPSBTUnsignedInputs(const PartiallySignedTransaction& psbt) {
|
|
|
|
size_t count = 0;
|
|
|
|
for (const auto& input : psbt.inputs) {
|
|
|
|
if (!PSBTInputSigned(input)) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2022-06-23 01:42:19 +02:00
|
|
|
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
|
|
|
|
{
|
2022-07-02 20:44:47 +02:00
|
|
|
CMutableTransaction& tx = *Assert(psbt.tx);
|
|
|
|
const CTxOut& out = tx.vout.at(index);
|
2022-06-23 01:42:19 +02: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).
|
2022-07-02 20:44:47 +02:00
|
|
|
MutableTransactionSignatureCreator creator(&tx, /* index */ 0, out.nValue, SIGHASH_ALL);
|
2022-06-23 01:42:19 +02:00
|
|
|
ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
|
|
|
|
|
|
|
|
// Put redeem_script, key paths, into PSBTOutput.
|
|
|
|
psbt_out.FromSignatureData(sigdata);
|
|
|
|
}
|
2022-09-18 23:11:49 +02:00
|
|
|
bool PSBTInputSigned(const PSBTInput& input)
|
2022-09-24 05:20:39 +02:00
|
|
|
{
|
|
|
|
return !input.final_script_sig.empty();
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:26:17 +02:00
|
|
|
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, int sighash, SignatureData* out_sigdata, bool use_dummy)
|
2019-01-30 06:32:38 +01:00
|
|
|
{
|
2022-09-24 05:20:39 +02:00
|
|
|
PSBTInput& input = psbt.inputs.at(index);
|
|
|
|
const CMutableTransaction& tx = *psbt.tx;
|
|
|
|
|
|
|
|
if (PSBTInputSigned(input)) {
|
2019-01-30 06:32:38 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill SignatureData with input info
|
|
|
|
SignatureData sigdata;
|
|
|
|
input.FillSignatureData(sigdata);
|
|
|
|
|
|
|
|
// Get UTXO
|
|
|
|
CTxOut utxo;
|
2022-09-24 05:20:39 +02:00
|
|
|
|
2019-01-30 06:32:38 +01:00
|
|
|
if (input.non_witness_utxo) {
|
|
|
|
// If we're taking our information from a non-witness UTXO, verify that it matches the prevout.
|
2022-09-24 05:20:39 +02:00
|
|
|
COutPoint prevout = tx.vin[index].prevout;
|
2020-01-29 12:26:17 +01:00
|
|
|
if (prevout.n >= input.non_witness_utxo->vout.size()) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-24 05:20:39 +02:00
|
|
|
if (input.non_witness_utxo->GetHash() != prevout.hash) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
utxo = input.non_witness_utxo->vout[prevout.n];
|
2019-01-30 06:32:38 +01:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-09-20 10:26:17 +02:00
|
|
|
bool sig_complete;
|
|
|
|
if (use_dummy) {
|
|
|
|
sig_complete = ProduceSignature(provider, DUMMY_SIGNATURE_CREATOR, utxo.scriptPubKey, sigdata);
|
|
|
|
} else {
|
|
|
|
MutableTransactionSignatureCreator creator(&tx, index, utxo.nValue, sighash);
|
|
|
|
sig_complete = ProduceSignature(provider, creator, utxo.scriptPubKey, sigdata);
|
|
|
|
}
|
2019-01-30 06:32:38 +01:00
|
|
|
input.FromSignatureData(sigdata);
|
2022-09-20 10:26:17 +02: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;
|
|
|
|
}
|
|
|
|
|
2019-01-30 06:32:38 +01:00
|
|
|
return sig_complete;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
for (unsigned int i = 0; i < psbtx.tx->vin.size(); ++i) {
|
2022-09-24 05:20:39 +02:00
|
|
|
complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, SIGHASH_ALL);
|
2019-01-30 06:32:38 +01: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;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-22 17:09:44 +01:00
|
|
|
TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
|
2019-01-30 06:32:38 +01:00
|
|
|
{
|
|
|
|
out = psbtxs[0]; // Copy the first one
|
|
|
|
|
|
|
|
// Merge
|
|
|
|
for (auto it = std::next(psbtxs.begin()); it != psbtxs.end(); ++it) {
|
|
|
|
if (!out.Merge(*it)) {
|
2019-02-22 17:09:44 +01:00
|
|
|
return TransactionError::PSBT_MISMATCH;
|
2019-01-30 06:32:38 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 17:09:44 +01:00
|
|
|
return TransactionError::OK;
|
2019-01-30 06:32:38 +01:00
|
|
|
}
|
2022-09-18 23:11:49 +02:00
|
|
|
|
|
|
|
std::string PSBTRoleName(PSBTRole role) {
|
|
|
|
switch (role) {
|
2019-12-10 18:11:28 +01:00
|
|
|
case PSBTRole::CREATOR: return "creator";
|
2022-09-18 23:11:49 +02:00
|
|
|
case PSBTRole::UPDATER: return "updater";
|
|
|
|
case PSBTRole::SIGNER: return "signer";
|
|
|
|
case PSBTRole::FINALIZER: return "finalizer";
|
|
|
|
case PSBTRole::EXTRACTOR: return "extractor";
|
2019-05-02 08:24:05 +02:00
|
|
|
// no default case, so the compiler can warn about missing cases
|
2022-09-18 23:11:49 +02:00
|
|
|
}
|
2019-05-02 08:24:05 +02:00
|
|
|
assert(false);
|
2022-09-18 23:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
|
|
|
|
{
|
|
|
|
bool invalid;
|
|
|
|
std::string tx_data = DecodeBase64(base64_tx, &invalid);
|
|
|
|
if (invalid) {
|
|
|
|
error = "invalid base64";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return DecodeRawPSBT(psbt, tx_data, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, const std::string& tx_data, std::string& error)
|
|
|
|
{
|
2024-02-24 08:36:25 +01:00
|
|
|
CDataStream ss_data(MakeByteSpan(tx_data), SER_NETWORK, PROTOCOL_VERSION);
|
2022-09-18 23:11:49 +02: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;
|
|
|
|
}
|