2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2019-01-09 02:06:29 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
|
|
#include <psbt.h>
|
2021-03-03 17:00:15 -05:00
|
|
|
|
2021-01-11 16:21:42 -05:00
|
|
|
#include <primitives/transaction.h>
|
2021-03-03 17:00:15 -05:00
|
|
|
#include <util/check.h>
|
2019-01-09 02:06:29 -08:00
|
|
|
#include <util/strencodings.h>
|
|
|
|
|
|
2019-03-01 00:25:10 -08:00
|
|
|
|
2021-01-18 16:55:20 -05:00
|
|
|
PartiallySignedTransaction::PartiallySignedTransaction(const CMutableTransaction& tx, uint32_t version) : m_version(version)
|
2019-01-09 02:06:29 -08:00
|
|
|
{
|
2021-01-18 16:55:20 -05:00
|
|
|
if (version == 0) {
|
|
|
|
|
this->tx = tx;
|
|
|
|
|
}
|
2021-01-04 17:23:10 -05:00
|
|
|
inputs.resize(tx.vin.size(), PSBTInput(GetVersion()));
|
|
|
|
|
outputs.resize(tx.vout.size(), PSBTOutput(GetVersion()));
|
2021-01-18 16:55:20 -05:00
|
|
|
SetupFromTx(tx);
|
2019-01-09 02:06:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PartiallySignedTransaction::IsNull() const
|
|
|
|
|
{
|
|
|
|
|
return !tx && inputs.empty() && outputs.empty() && unknown.empty();
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 22:51:56 -08:00
|
|
|
bool PartiallySignedTransaction::Merge(const PartiallySignedTransaction& psbt)
|
2019-01-09 02:06:29 -08:00
|
|
|
{
|
2019-01-29 22:51:56 -08:00
|
|
|
// Prohibited to merge two PSBTs over different transactions
|
2021-01-11 15:51:44 -05:00
|
|
|
if (GetUniqueID() != psbt.GetUniqueID()) {
|
2019-01-29 22:51:56 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 17:18:31 -05:00
|
|
|
assert(*tx_version == psbt.tx_version);
|
2019-01-09 02:06:29 -08: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());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-11 17:18:31 -05:00
|
|
|
if (fallback_locktime == nullopt && psbt.fallback_locktime != nullopt) fallback_locktime = psbt.fallback_locktime;
|
|
|
|
|
if (m_tx_modifiable == nullopt && psbt.m_tx_modifiable != nullopt) m_tx_modifiable = psbt.m_tx_modifiable;
|
2019-01-09 02:06:29 -08:00
|
|
|
unknown.insert(psbt.unknown.begin(), psbt.unknown.end());
|
2019-01-29 22:51:56 -08:00
|
|
|
|
|
|
|
|
return true;
|
2019-01-09 02:06:29 -08:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 16:15:04 -05:00
|
|
|
bool PartiallySignedTransaction::ComputeTimeLock(uint32_t& locktime) const
|
|
|
|
|
{
|
|
|
|
|
Optional<uint32_t> time_lock{0};
|
|
|
|
|
Optional<uint32_t> height_lock{0};
|
|
|
|
|
for (const PSBTInput& input : inputs) {
|
|
|
|
|
if (input.time_locktime != nullopt && input.height_locktime == nullopt) {
|
|
|
|
|
height_lock.reset(); // Transaction can no longer have a height locktime
|
|
|
|
|
if (time_lock == nullopt) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else if (input.time_locktime == nullopt && input.height_locktime != nullopt) {
|
|
|
|
|
time_lock.reset(); // Transaction can no longer have a time locktime
|
|
|
|
|
if (height_lock == nullopt) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (input.time_locktime && time_lock != nullopt) {
|
|
|
|
|
time_lock = std::max(time_lock, input.time_locktime);
|
|
|
|
|
}
|
|
|
|
|
if (input.height_locktime && height_lock != nullopt) {
|
|
|
|
|
height_lock = std::max(height_lock, input.height_locktime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (height_lock != nullopt && *height_lock > 0) {
|
|
|
|
|
locktime = *height_lock;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (time_lock != nullopt && *time_lock > 0) {
|
|
|
|
|
locktime = *time_lock;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
locktime = fallback_locktime.value_or(0);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 16:21:42 -05:00
|
|
|
CMutableTransaction PartiallySignedTransaction::GetUnsignedTx() const
|
|
|
|
|
{
|
|
|
|
|
if (tx != nullopt) {
|
|
|
|
|
return *tx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CMutableTransaction mtx;
|
|
|
|
|
mtx.nVersion = *tx_version;
|
|
|
|
|
bool locktime_success = ComputeTimeLock(mtx.nLockTime);
|
|
|
|
|
assert(locktime_success);
|
|
|
|
|
uint32_t max_sequence = CTxIn::SEQUENCE_FINAL;
|
|
|
|
|
for (const PSBTInput& input : inputs) {
|
|
|
|
|
CTxIn txin;
|
|
|
|
|
txin.prevout.hash = input.prev_txid;
|
|
|
|
|
txin.prevout.n = *input.prev_out;
|
|
|
|
|
txin.nSequence = input.sequence.value_or(max_sequence);
|
|
|
|
|
mtx.vin.push_back(txin);
|
|
|
|
|
}
|
|
|
|
|
for (const PSBTOutput& output : outputs) {
|
|
|
|
|
CTxOut txout;
|
|
|
|
|
txout.nValue = *output.amount;
|
|
|
|
|
txout.scriptPubKey = output.script;
|
|
|
|
|
mtx.vout.push_back(txout);
|
|
|
|
|
}
|
|
|
|
|
return mtx;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 15:51:44 -05:00
|
|
|
uint256 PartiallySignedTransaction::GetUniqueID() const
|
|
|
|
|
{
|
|
|
|
|
if (tx != nullopt) {
|
|
|
|
|
return tx->GetHash();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the unsigned transaction
|
|
|
|
|
CMutableTransaction mtx = GetUnsignedTx();
|
|
|
|
|
// Set the locktime to 0
|
|
|
|
|
mtx.nLockTime = 0;
|
|
|
|
|
// Set the sequence numbers to 0
|
|
|
|
|
for (CTxIn& txin : mtx.vin) {
|
|
|
|
|
txin.nSequence = 0;
|
|
|
|
|
}
|
|
|
|
|
return mtx.GetHash();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 16:50:58 -05:00
|
|
|
bool PartiallySignedTransaction::AddInput(PSBTInput& psbtin)
|
2018-07-20 18:24:16 -07:00
|
|
|
{
|
2021-01-18 15:27:05 -05:00
|
|
|
// Check required fields are present and this input is not a duplicate
|
|
|
|
|
if (psbtin.prev_txid.IsNull() ||
|
|
|
|
|
psbtin.prev_out == nullopt ||
|
|
|
|
|
std::find_if(inputs.begin(), inputs.end(),
|
2021-01-11 16:50:58 -05:00
|
|
|
[psbtin](const PSBTInput& psbt) {
|
|
|
|
|
return psbt.prev_txid == psbtin.prev_txid && psbt.prev_out == psbtin.prev_out;
|
|
|
|
|
}
|
|
|
|
|
) != inputs.end()) {
|
2018-07-20 18:24:16 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-01-11 16:50:58 -05:00
|
|
|
|
|
|
|
|
if (tx != nullopt) {
|
|
|
|
|
// This is a v0 psbt, so do the v0 AddInput
|
|
|
|
|
CTxIn txin(COutPoint(psbtin.prev_txid, *psbtin.prev_out));
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 15:27:05 -05:00
|
|
|
// No global tx, must be PSBTv2.
|
|
|
|
|
// Check inputs modifiable flag
|
|
|
|
|
if (m_tx_modifiable == nullopt || !m_tx_modifiable->test(0)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine if we need to iterate the inputs.
|
|
|
|
|
// For now, we only do this if the new input has a required time lock.
|
|
|
|
|
// The BIP states that we should also do this if m_tx_modifiable's bit 2 is set
|
|
|
|
|
// (Has SIGHASH_SINGLE flag) but since we are only adding inputs at the end of the vector,
|
|
|
|
|
// we don't care about that.
|
|
|
|
|
bool iterate_inputs = psbtin.time_locktime != nullopt || psbtin.height_locktime != nullopt;
|
|
|
|
|
if (iterate_inputs) {
|
|
|
|
|
uint32_t old_timelock;
|
|
|
|
|
if (!ComputeTimeLock(old_timelock)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<uint32_t> time_lock = psbtin.time_locktime;
|
|
|
|
|
Optional<uint32_t> height_lock = psbtin.height_locktime;
|
|
|
|
|
bool has_sigs = false;
|
|
|
|
|
for (const PSBTInput& input : inputs) {
|
|
|
|
|
if (input.time_locktime != nullopt && input.height_locktime == nullopt) {
|
|
|
|
|
height_lock.reset(); // Transaction can no longer have a height locktime
|
|
|
|
|
if (time_lock == nullopt) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else if (input.time_locktime == nullopt && input.height_locktime != nullopt) {
|
|
|
|
|
time_lock.reset(); // Transaction can no longer have a time locktime
|
|
|
|
|
if (height_lock == nullopt) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (input.time_locktime && time_lock != nullopt) {
|
|
|
|
|
time_lock = std::max(time_lock, input.time_locktime);
|
|
|
|
|
}
|
|
|
|
|
if (input.height_locktime && height_lock != nullopt) {
|
|
|
|
|
height_lock = std::max(height_lock, input.height_locktime);
|
|
|
|
|
}
|
|
|
|
|
if (!input.partial_sigs.empty()) {
|
|
|
|
|
has_sigs = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint32_t new_timelock = fallback_locktime.value_or(0);
|
|
|
|
|
if (height_lock != nullopt && *height_lock > 0) {
|
|
|
|
|
new_timelock = *height_lock;
|
|
|
|
|
} else if (time_lock != nullopt && *time_lock > 0) {
|
|
|
|
|
new_timelock = *time_lock;
|
|
|
|
|
}
|
|
|
|
|
if (has_sigs && old_timelock != new_timelock) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add the input to the end
|
|
|
|
|
inputs.push_back(psbtin);
|
|
|
|
|
return true;
|
2018-07-20 18:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 16:59:29 -05:00
|
|
|
bool PartiallySignedTransaction::AddOutput(const PSBTOutput& psbtout)
|
2018-07-20 18:24:16 -07:00
|
|
|
{
|
2021-01-18 15:27:05 -05:00
|
|
|
if (psbtout.amount == nullopt || psbtout.script.empty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 16:59:29 -05:00
|
|
|
if (tx != nullopt) {
|
|
|
|
|
// This is a v0 psbt, do the v0 AddOutput
|
|
|
|
|
CTxOut txout(CAsset(), *psbtout.amount, psbtout.script);
|
|
|
|
|
tx->vout.push_back(txout);
|
|
|
|
|
outputs.push_back(psbtout);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-18 15:27:05 -05:00
|
|
|
// No global tx, must be PSBTv2
|
|
|
|
|
// Check outputs are modifiable
|
|
|
|
|
if (m_tx_modifiable == nullopt || !m_tx_modifiable->test(1)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
outputs.push_back(psbtout);
|
|
|
|
|
|
|
|
|
|
return true;
|
2018-07-20 18:24:16 -07:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 15:50:27 -05:00
|
|
|
bool PSBTInput::GetUTXO(CTxOut& utxo) const
|
2018-07-31 17:56:47 -07:00
|
|
|
{
|
2021-01-11 15:50:27 -05:00
|
|
|
if (non_witness_utxo) {
|
|
|
|
|
if (*prev_out >= non_witness_utxo->vout.size()) {
|
2019-10-15 17:26:46 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
2021-01-11 15:50:27 -05:00
|
|
|
utxo = non_witness_utxo->vout[*prev_out];
|
|
|
|
|
} else if (!witness_utxo.IsNull()) {
|
|
|
|
|
utxo = witness_utxo;
|
2018-07-31 17:56:47 -07:00
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 17:48:30 -05:00
|
|
|
COutPoint PSBTInput::GetOutPoint() const
|
|
|
|
|
{
|
|
|
|
|
return COutPoint(prev_txid, *prev_out);
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-09 02:06:29 -08: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PSBTInput::Merge(const PSBTInput& input)
|
|
|
|
|
{
|
2021-01-11 17:18:31 -05:00
|
|
|
assert(prev_txid == input.prev_txid);
|
|
|
|
|
assert(*prev_out == *input.prev_out);
|
|
|
|
|
|
2019-01-09 02:06:29 -08:00
|
|
|
if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
|
|
|
|
|
if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
|
2020-06-04 23:43:43 -04:00
|
|
|
// TODO: For segwit v1, we will want to clear out the non-witness utxo when setting a witness one. For v0 and non-segwit, this is not safe
|
2019-01-09 02:06:29 -08:00
|
|
|
witness_utxo = input.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 (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-01-11 17:18:31 -05:00
|
|
|
if (sequence == nullopt && input.sequence != nullopt) sequence = input.sequence;
|
|
|
|
|
if (time_locktime == nullopt && input.time_locktime != nullopt) time_locktime = input.time_locktime;
|
|
|
|
|
if (height_locktime == nullopt && input.height_locktime != nullopt) height_locktime = input.height_locktime;
|
2019-01-09 02:06:29 -08: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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PSBTOutput::IsNull() const
|
|
|
|
|
{
|
|
|
|
|
return redeem_script.empty() && witness_script.empty() && hd_keypaths.empty() && unknown.empty();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PSBTOutput::Merge(const PSBTOutput& output)
|
|
|
|
|
{
|
2021-01-11 17:18:31 -05:00
|
|
|
assert(*amount == *output.amount);
|
|
|
|
|
assert(script == output.script);
|
|
|
|
|
|
2019-01-09 02:06:29 -08:00
|
|
|
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;
|
|
|
|
|
if (witness_script.empty() && !output.witness_script.empty()) witness_script = output.witness_script;
|
|
|
|
|
}
|
2019-03-01 00:25:10 -08:00
|
|
|
bool PSBTInputSigned(const PSBTInput& input)
|
2019-01-09 02:06:29 -08:00
|
|
|
{
|
|
|
|
|
return !input.final_script_sig.empty() || !input.final_script_witness.IsNull();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 01:25:04 -07: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 15:49:36 -08:00
|
|
|
void UpdatePSBTOutput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index)
|
|
|
|
|
{
|
2021-01-18 17:12:48 -05:00
|
|
|
CMutableTransaction tx = psbt.GetUnsignedTx();
|
2021-03-03 17:00:15 -05:00
|
|
|
const CTxOut& out = tx.vout.at(index);
|
2019-02-16 15:49:36 -08: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).
|
2021-03-03 17:00:15 -05:00
|
|
|
MutableTransactionSignatureCreator creator(&tx, /* index */ 0, out.nValue, SIGHASH_ALL);
|
2019-02-16 15:49:36 -08:00
|
|
|
ProduceSignature(provider, creator, out.scriptPubKey, sigdata);
|
|
|
|
|
|
|
|
|
|
// Put redeem_script, witness_script, key paths, into PSBTOutput.
|
|
|
|
|
psbt_out.FromSignatureData(sigdata);
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-31 17:57:15 -07:00
|
|
|
bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction& psbt, int index, int sighash, SignatureData* out_sigdata, bool use_dummy)
|
2019-01-09 02:06:29 -08:00
|
|
|
{
|
|
|
|
|
PSBTInput& input = psbt.inputs.at(index);
|
2021-01-11 17:18:54 -05:00
|
|
|
const CMutableTransaction& tx = psbt.GetUnsignedTx();
|
2019-01-09 02:06:29 -08:00
|
|
|
|
|
|
|
|
if (PSBTInputSigned(input)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fill SignatureData with input info
|
|
|
|
|
SignatureData sigdata;
|
|
|
|
|
input.FillSignatureData(sigdata);
|
|
|
|
|
|
2021-03-03 17:00:15 -05:00
|
|
|
// Get UTXO
|
2019-01-09 02:06:29 -08:00
|
|
|
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.
|
2021-01-11 17:18:54 -05:00
|
|
|
COutPoint prevout = input.GetOutPoint();
|
2019-10-15 17:26:46 -04:00
|
|
|
if (prevout.n >= input.non_witness_utxo->vout.size()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-01-09 02:06:29 -08: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 17:57:15 -07: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-09 02:06:29 -08:00
|
|
|
// Verify that a witness signature was produced in case one was required.
|
|
|
|
|
if (require_witness_sig && !sigdata.witness) return false;
|
|
|
|
|
input.FromSignatureData(sigdata);
|
|
|
|
|
|
2020-06-04 23:43:43 -04:00
|
|
|
// If we have a witness signature, put a witness UTXO.
|
|
|
|
|
// TODO: For segwit v1, we should remove the non_witness_utxo
|
2019-01-09 02:06:29 -08:00
|
|
|
if (sigdata.witness) {
|
|
|
|
|
input.witness_utxo = utxo;
|
2020-06-04 23:43:43 -04:00
|
|
|
// input.non_witness_utxo = nullptr;
|
2019-01-09 02:06:29 -08:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 17:57:15 -07: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 02:06:29 -08:00
|
|
|
return sig_complete;
|
|
|
|
|
}
|
2019-01-09 03:08:32 -08: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-01-11 17:19:06 -05:00
|
|
|
for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
|
2019-01-09 03:08:32 -08:00
|
|
|
complete &= SignPSBTInput(DUMMY_SIGNING_PROVIDER, psbtx, i, SIGHASH_ALL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-11 17:19:06 -05:00
|
|
|
result = psbtx.GetUnsignedTx();
|
2019-01-09 03:08:32 -08:00
|
|
|
for (unsigned int i = 0; i < result.vin.size(); ++i) {
|
|
|
|
|
result.vin[i].scriptSig = psbtx.inputs[i].final_script_sig;
|
2019-05-17 16:43:38 +01:00
|
|
|
result.witness.vtxinwit[i].scriptWitness = psbtx.inputs[i].final_script_witness;
|
2019-01-09 03:08:32 -08:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-14 10:01:06 -05:00
|
|
|
TransactionError CombinePSBTs(PartiallySignedTransaction& out, const std::vector<PartiallySignedTransaction>& psbtxs)
|
2019-01-09 03:08:32 -08: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-14 10:01:06 -05:00
|
|
|
return TransactionError::PSBT_MISMATCH;
|
2019-01-09 03:08:32 -08:00
|
|
|
}
|
|
|
|
|
}
|
2019-02-14 10:01:06 -05:00
|
|
|
return TransactionError::OK;
|
2019-01-09 03:08:32 -08:00
|
|
|
}
|
2019-03-05 18:55:40 -08:00
|
|
|
|
2019-03-01 00:25:10 -08:00
|
|
|
std::string PSBTRoleName(PSBTRole role) {
|
|
|
|
|
switch (role) {
|
2019-11-19 14:35:14 -05:00
|
|
|
case PSBTRole::CREATOR: return "creator";
|
2019-03-01 00:25:10 -08:00
|
|
|
case PSBTRole::UPDATER: return "updater";
|
|
|
|
|
case PSBTRole::SIGNER: return "signer";
|
|
|
|
|
case PSBTRole::FINALIZER: return "finalizer";
|
|
|
|
|
case PSBTRole::EXTRACTOR: return "extractor";
|
2020-10-28 00:54:12 +00:00
|
|
|
// no default case, so the compiler can warn about missing cases
|
2019-03-01 00:25:10 -08:00
|
|
|
}
|
2020-10-28 00:54:12 +00:00
|
|
|
assert(false);
|
2019-03-01 00:25:10 -08:00
|
|
|
}
|
|
|
|
|
|
2019-05-01 18:28:10 -07:00
|
|
|
std::string EncodePSBT(const PartiallySignedTransaction& psbt)
|
|
|
|
|
{
|
|
|
|
|
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
ssTx << psbt;
|
2020-11-28 13:35:24 +00:00
|
|
|
return EncodeBase64(MakeUCharSpan(ssTx));
|
2019-05-01 18:28:10 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-05 18:55:40 -08: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)
|
|
|
|
|
{
|
|
|
|
|
CDataStream ss_data(tx_data.data(), tx_data.data() + tx_data.size(), SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
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 17:21:29 -05:00
|
|
|
|
|
|
|
|
uint32_t PartiallySignedTransaction::GetVersion() const
|
|
|
|
|
{
|
|
|
|
|
if (m_version != nullopt) {
|
|
|
|
|
return *m_version;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2021-01-11 18:34:38 -05:00
|
|
|
|
|
|
|
|
void PartiallySignedTransaction::SetupFromTx(const CMutableTransaction& tx)
|
|
|
|
|
{
|
|
|
|
|
tx_version = tx.nVersion;
|
|
|
|
|
fallback_locktime = tx.nLockTime;
|
|
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
|
for (i = 0; i < tx.vin.size(); ++i) {
|
|
|
|
|
PSBTInput& input = inputs.at(i);
|
|
|
|
|
const CTxIn& txin = tx.vin.at(i);
|
|
|
|
|
|
|
|
|
|
input.prev_txid = txin.prevout.hash;
|
|
|
|
|
input.prev_out = txin.prevout.n;
|
|
|
|
|
input.sequence = txin.nSequence;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < tx.vout.size(); ++i) {
|
|
|
|
|
PSBTOutput& output = outputs.at(i);
|
|
|
|
|
const CTxOut& txout = tx.vout.at(i);
|
|
|
|
|
|
|
|
|
|
output.amount = txout.nValue.GetAmount();
|
|
|
|
|
output.script = txout.scriptPubKey;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PartiallySignedTransaction::CacheUnsignedTxPieces()
|
|
|
|
|
{
|
|
|
|
|
// To make things easier, we split up the global unsigned transaction
|
|
|
|
|
// and use the PSBTv2 fields for PSBTv0.
|
|
|
|
|
if (tx != nullopt) {
|
|
|
|
|
SetupFromTx(*tx);
|
|
|
|
|
}
|
|
|
|
|
}
|