From: Jeffrey Czyz Date: Fri, 30 Jul 2021 02:55:22 +0000 (-0500) Subject: f - Add ShutdownScript::is_compatible X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=99ee676a7644e6b4bb02591a1154e7a803730a0c;p=rust-lightning f - Add ShutdownScript::is_compatible --- diff --git a/lightning/src/ln/script.rs b/lightning/src/ln/script.rs index a01866de6..4ad0af02b 100644 --- a/lightning/src/ln/script.rs +++ b/lightning/src/ln/script.rs @@ -105,6 +105,26 @@ impl ShutdownScript { ShutdownScriptImpl::Bolt2(_) => None, } } + + /// Returns whether the shutdown script is compatible with the features as defined by BOLT #2. + /// + /// Specifically, checks for compliance with feature `option_shutdown_anysegwit`. + pub fn is_compatible(&self, features: &InitFeatures) -> bool { + match &self.0 { + ShutdownScriptImpl::Legacy(_) => true, + ShutdownScriptImpl::Bolt2(script) => is_bolt2_compliant(script, features), + } + } +} + +fn is_bolt2_compliant(script: &Script, features: &InitFeatures) -> bool { + if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wpkh() || script.is_v0_p2wsh() { + true + } else if features.supports_shutdown_anysegwit() { + script.is_witness_program() && script.as_bytes()[0] != SEGWIT_V0.into_u8() + } else { + false + } } impl TryFrom