From 6622de4041e676103752de7c835812369150dba3 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 5 Feb 2021 14:47:47 -0500 Subject: [PATCH] Add Script:dust_value() to get minimum output value for a spk --- src/blockdata/script.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index bbcf732b5e..533eb6b773 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -325,6 +325,26 @@ impl Script { opcodes::All::from(self.0[0]).classify() == opcodes::Class::IllegalOp) } + /// The minimum value an output to a witness script must have in order to be + /// broadcastable on today's bitcoin network. + pub const WITNESS_OUTPUT_DUST_THRESHOLD: u64 = 294; + + /// The minimum value an output to a non-witness script must have in order to be + /// broadcastable on today's bitcoin network. + pub const LEGACY_OUTPUT_DUST_THRESHOLD: u64 = 546; + + /// Gets the minimum value an output with this script should have in order to be + /// broadcastable on today's bitcoin network. + pub fn dust_value(&self) -> u64 { + if self.is_op_return() { + 0 + } else if self.is_witness_program() { + Self::WITNESS_OUTPUT_DUST_THRESHOLD + } else { + Self::LEGACY_OUTPUT_DUST_THRESHOLD + } + } + /// Iterate over the script in the form of `Instruction`s, which are an enum covering /// opcodes, datapushes and errors. At most one error will be returned and then the /// iterator will end. To instead iterate over the script as sequence of bytes, treat