Skip to content

Commit 103c6fd

Browse files
committed
psbt: Remove non_witness_utxo for segwit v1+
If all inputs are segwit v1+, the non_witness_utxos can be removed.
1 parent 7dccdd3 commit 103c6fd

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

src/psbt.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ void PSBTInput::Merge(const PSBTInput& input)
184184
{
185185
if (!non_witness_utxo && input.non_witness_utxo) non_witness_utxo = input.non_witness_utxo;
186186
if (witness_utxo.IsNull() && !input.witness_utxo.IsNull()) {
187-
// 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
188187
witness_utxo = input.witness_utxo;
189188
}
190189

@@ -367,10 +366,11 @@ bool SignPSBTInput(const SigningProvider& provider, PartiallySignedTransaction&
367366
input.FromSignatureData(sigdata);
368367

369368
// If we have a witness signature, put a witness UTXO.
370-
// TODO: For segwit v1, we should remove the non_witness_utxo
371369
if (sigdata.witness) {
372370
input.witness_utxo = utxo;
373-
// input.non_witness_utxo = nullptr;
371+
// We can remove the non_witness_utxo if and only if there are no non-segwit or segwit v0
372+
// inputs in this transaction. Since this requires inspecting the entire transaction, this
373+
// is something for the caller to deal with (i.e. FillPSBT).
374374
}
375375

376376
// Fill in the missing info

src/wallet/wallet.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2006,6 +2006,35 @@ TransactionError CWallet::FillPSBT(PartiallySignedTransaction& psbtx, bool& comp
20062006
}
20072007
}
20082008

2009+
// Only drop non_witness_utxos if sighash_type != SIGHASH_ANYONECANPAY
2010+
if ((sighash_type & 0x80) != SIGHASH_ANYONECANPAY) {
2011+
// Figure out if any non_witness_utxos should be dropped
2012+
std::vector<unsigned int> to_drop;
2013+
for (unsigned int i = 0; i < psbtx.inputs.size(); ++i) {
2014+
const auto& input = psbtx.inputs.at(i);
2015+
int wit_ver;
2016+
std::vector<unsigned char> wit_prog;
2017+
if (input.witness_utxo.IsNull() || !input.witness_utxo.scriptPubKey.IsWitnessProgram(wit_ver, wit_prog)) {
2018+
// There's a non-segwit input or Segwit v0, so we cannot drop any witness_utxos
2019+
to_drop.clear();
2020+
break;
2021+
}
2022+
if (wit_ver == 0) {
2023+
// Segwit v0, so we cannot drop any non_witness_utxos
2024+
to_drop.clear();
2025+
break;
2026+
}
2027+
if (input.non_witness_utxo) {
2028+
to_drop.push_back(i);
2029+
}
2030+
}
2031+
2032+
// Drop the non_witness_utxos that we can drop
2033+
for (unsigned int i : to_drop) {
2034+
psbtx.inputs.at(i).non_witness_utxo = nullptr;
2035+
}
2036+
}
2037+
20092038
// Complete if every input is now signed
20102039
complete = true;
20112040
for (const auto& input : psbtx.inputs) {

0 commit comments

Comments
 (0)