From 99ee676a7644e6b4bb02591a1154e7a803730a0c Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 29 Jul 2021 21:55:22 -0500 Subject: [PATCH] f - Add ShutdownScript::is_compatible --- lightning/src/ln/script.rs | 56 +++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 4 deletions(-) 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