Skip to content

Commit

Permalink
fix(wallet)!: enforce OP_RETURN standardness
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Nov 19, 2024
1 parent 3bc45b5 commit ef28e9a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions crates/wallet/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ pub enum CreateTxError {
MissingNonWitnessUtxo(OutPoint),
/// Miniscript PSBT error
MiniscriptPsbt(MiniscriptPsbtError),
/// Multiple recipients are OP_RETURN outputs
MultipleOpReturn,
/// The OP_RETURN data contains too many bytes
MaxDataCarrierSize,
}

impl fmt::Display for CreateTxError {
Expand Down Expand Up @@ -171,6 +175,12 @@ impl fmt::Display for CreateTxError {
CreateTxError::MiniscriptPsbt(err) => {
write!(f, "Miniscript PSBT error: {}", err)
}
CreateTxError::MultipleOpReturn => {
write!(f, "Multiple recipients are OP_RETURN outputs")
}
CreateTxError::MaxDataCarrierSize => {
write!(f, "The OP_RETURN data contains too many bytes")
}
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub use persisted::*;
pub use utils::IsDust;

const COINBASE_MATURITY: u32 = 100;
const MAX_OP_RETURN_BYTES: usize = 83;

/// A Bitcoin wallet
///
Expand Down Expand Up @@ -1388,12 +1389,23 @@ impl Wallet {
let mut received = Amount::ZERO;

let recipients = params.recipients.iter().map(|(r, v)| (r, *v));
let mut contains_op_return = false;

for (index, (script_pubkey, value)) in recipients.enumerate() {
if !params.allow_dust && value.is_dust(script_pubkey) && !script_pubkey.is_op_return() {
return Err(CreateTxError::OutputBelowDustLimit(index));
}

if script_pubkey.is_op_return() {
if contains_op_return {
return Err(CreateTxError::MultipleOpReturn);
}
if script_pubkey.len() > MAX_OP_RETURN_BYTES {
return Err(CreateTxError::MaxDataCarrierSize);
}
contains_op_return = true;
}

if self.is_mine(script_pubkey.clone()) {
received += value;
}
Expand Down

0 comments on commit ef28e9a

Please sign in to comment.