-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check foreign utxos are not local before inclusion #1823
Open
nymius
wants to merge
2
commits into
bitcoindevkit:master
Choose a base branch
from
nymius:fix/check-foreign-utxos-are-foreign
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -342,8 +342,9 @@ impl<'a, Cs> TxBuilder<'a, Cs> { | |
/// | ||
/// This method returns errors in the following circumstances: | ||
/// | ||
/// 1. The `psbt_input` does not contain a `witness_utxo` or `non_witness_utxo`. | ||
/// 2. The data in `non_witness_utxo` does not match what is in `outpoint`. | ||
/// 1. The provided outpoint is associated to a [`LocalOutput`]. | ||
/// 2. The `psbt_input` does not contain a `witness_utxo` or `non_witness_utxo`. | ||
/// 3. The data in `non_witness_utxo` does not match what is in `outpoint`. | ||
/// | ||
/// Note unless you set [`only_witness_utxo`] any non-taproot `psbt_input` you pass to this | ||
/// method must have `non_witness_utxo` set otherwise you will get an error when [`finish`] | ||
|
@@ -374,24 +375,31 @@ impl<'a, Cs> TxBuilder<'a, Cs> { | |
satisfaction_weight: Weight, | ||
sequence: Sequence, | ||
) -> Result<&mut Self, AddForeignUtxoError> { | ||
if psbt_input.witness_utxo.is_none() { | ||
match psbt_input.non_witness_utxo.as_ref() { | ||
Some(tx) => { | ||
if tx.compute_txid() != outpoint.txid { | ||
return Err(AddForeignUtxoError::InvalidTxid { | ||
input_txid: tx.compute_txid(), | ||
foreign_utxo: outpoint, | ||
}); | ||
} | ||
if tx.output.len() <= outpoint.vout as usize { | ||
return Err(AddForeignUtxoError::InvalidOutpoint(outpoint)); | ||
} | ||
} | ||
None => { | ||
return Err(AddForeignUtxoError::MissingUtxo); | ||
} | ||
let script_pubkey = if let Some(ref txout) = psbt_input.witness_utxo { | ||
txout.script_pubkey.clone() | ||
} else if let Some(tx) = psbt_input.non_witness_utxo.as_ref() { | ||
if tx.compute_txid() != outpoint.txid { | ||
return Err(AddForeignUtxoError::InvalidTxid { | ||
input_txid: tx.compute_txid(), | ||
foreign_utxo: outpoint, | ||
}); | ||
} | ||
} | ||
|
||
if let Ok(txout) = tx.tx_out(outpoint.vout as usize) { | ||
txout.script_pubkey.clone() | ||
} else { | ||
return Err(AddForeignUtxoError::InvalidOutpoint(outpoint)); | ||
} | ||
} else { | ||
return Err(AddForeignUtxoError::MissingUtxo); | ||
}; | ||
|
||
// Avoid the inclusion of local utxos as foreign utxos | ||
if self.wallet.is_mine(script_pubkey) { | ||
// TODO(2.0.0): return Err(AddForeignUtxoError::NotForeignUtxo); | ||
// No-op to avoid breaking changes until next major release | ||
return Ok(self); | ||
}; | ||
|
||
self.params.utxos.push(WeightedUtxo { | ||
satisfaction_weight, | ||
|
@@ -711,6 +719,8 @@ pub enum AddForeignUtxoError { | |
InvalidOutpoint(OutPoint), | ||
/// Foreign utxo missing witness_utxo or non_witness_utxo | ||
MissingUtxo, | ||
// Avoid breaking changes until next major release | ||
// TODO(2.0.0): NotForeignUtxo, | ||
} | ||
|
||
impl fmt::Display for AddForeignUtxoError { | ||
|
@@ -730,6 +740,8 @@ impl fmt::Display for AddForeignUtxoError { | |
outpoint.txid, outpoint.vout, | ||
), | ||
Self::MissingUtxo => write!(f, "Foreign utxo missing witness_utxo or non_witness_utxo"), | ||
// Avoid breaking changes until next major release | ||
// TODO(2.0.0): Self::NotForeignUtxo => write!(f, "UTxO is owned by wallet"), | ||
} | ||
} | ||
} | ||
|
@@ -1098,4 +1110,21 @@ mod test { | |
builder.fee_rate(FeeRate::from_sat_per_kwu(feerate + 250)); | ||
let _ = builder.finish().unwrap(); | ||
} | ||
|
||
#[test] | ||
fn test_add_foreign_utxo_fails_when_utxo_is_owned_by_wallet() { | ||
use crate::test_utils::get_funded_wallet_wpkh; | ||
let (mut wallet, _) = get_funded_wallet_wpkh(); | ||
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint; | ||
let foreign_utxo_satisfaction = wallet | ||
.public_descriptor(KeychainKind::External) | ||
.max_weight_to_satisfy() | ||
.unwrap(); | ||
|
||
let mut builder = wallet.build_tx(); | ||
let _ = | ||
builder.add_foreign_utxo(outpoint, psbt::Input::default(), foreign_utxo_satisfaction); | ||
// TODO(2.0.0): assert!(matches!(result, Err(AddForeignUtxoError::NotForeignUtxo))); | ||
assert!(builder.params.utxos.is_empty()); | ||
Comment on lines
+1126
to
+1128
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you still need to provide the witness utxo if you want this to reach the |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit, but I'm inclined to write this as a match statement with the primary goal of locating the txout.