X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fscript.rs;h=a9f44bae1aec79199230d467f426b50934c225dd;hb=588337fbdd44594531a62a12d4d9979d33071db2;hp=a123de741f043c3459b5b6acd0d3d47f6bf72ae4;hpb=ccd11fc35a56b06b93e8102bb6c76a109b7a3309;p=rust-lightning diff --git a/lightning/src/ln/script.rs b/lightning/src/ln/script.rs index a123de74..a9f44bae 100644 --- a/lightning/src/ln/script.rs +++ b/lightning/src/ln/script.rs @@ -3,7 +3,7 @@ use bitcoin::blockdata::opcodes::all::OP_PUSHBYTES_0 as SEGWIT_V0; use bitcoin::blockdata::script::{Builder, Script}; use bitcoin::hashes::Hash; -use bitcoin::hash_types::{PubkeyHash, ScriptHash, WPubkeyHash, WScriptHash}; +use bitcoin::hash_types::{WPubkeyHash, WScriptHash}; use bitcoin::secp256k1::key::PublicKey; use ln::features::InitFeatures; @@ -17,11 +17,11 @@ use io; /// A script pubkey for shutting down a channel as defined by [BOLT #2]. /// /// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md -#[derive(Clone)] +#[derive(Clone, PartialEq)] pub struct ShutdownScript(ShutdownScriptImpl); /// An error occurring when converting from [`Script`] to [`ShutdownScript`]. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct InvalidShutdownScript { /// The script that did not meet the requirements from [BOLT #2]. /// @@ -29,7 +29,7 @@ pub struct InvalidShutdownScript { pub script: Script } -#[derive(Clone)] +#[derive(Clone, PartialEq)] enum ShutdownScriptImpl { /// [`PublicKey`] used to form a P2WPKH script pubkey. Used to support backward-compatible /// serialization. @@ -66,16 +66,6 @@ impl ShutdownScript { Self(ShutdownScriptImpl::Legacy(pubkey)) } - /// Generates a P2PKH script pubkey from the given [`PubkeyHash`]. - pub fn new_p2pkh(pubkey_hash: &PubkeyHash) -> Self { - Self(ShutdownScriptImpl::Bolt2(Script::new_p2pkh(pubkey_hash))) - } - - /// Generates a P2SH script pubkey from the given [`ScriptHash`]. - pub fn new_p2sh(script_hash: &ScriptHash) -> Self { - Self(ShutdownScriptImpl::Bolt2(Script::new_p2sh(script_hash))) - } - /// Generates a P2WPKH script pubkey from the given [`WPubkeyHash`]. pub fn new_p2wpkh(pubkey_hash: &WPubkeyHash) -> Self { Self(ShutdownScriptImpl::Bolt2(Script::new_v0_wpkh(pubkey_hash))) @@ -86,7 +76,10 @@ impl ShutdownScript { Self(ShutdownScriptImpl::Bolt2(Script::new_v0_wsh(script_hash))) } - /// Generates a P2WSH script pubkey from the given segwit version and program. + /// Generates a witness script pubkey from the given segwit version and program. + /// + /// Note for version-zero witness scripts you must use [`ShutdownScript::new_p2wpkh`] or + /// [`ShutdownScript::new_p2wsh`] instead. /// /// # Errors /// @@ -123,7 +116,9 @@ impl ShutdownScript { } } -fn is_bolt2_compliant(script: &Script, features: &InitFeatures) -> bool { +/// Check if a given script is compliant with BOLT 2's shutdown script requirements for the given +/// counterparty features. +pub(crate) 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() { @@ -133,6 +128,8 @@ fn is_bolt2_compliant(script: &Script, features: &InitFeatures) -> bool { } } +// Note that this is only for our own shutdown scripts. Counterparties are still allowed to send us +// non-witness shutdown scripts which this rejects. impl TryFrom