X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fscript.rs;h=6c1d2102400786d41a124eafd26db70903bb82d2;hb=b7d0357a8ad42415eb38e83aa4595b51e86d1a98;hp=4e81d76ad670d18b2c8b172d4acd94ea62c64607;hpb=35d0b7aae73760df48f814db6ce4b72e8ee5523a;p=rust-lightning diff --git a/lightning/src/ln/script.rs b/lightning/src/ln/script.rs index 4e81d76a..6c1d2102 100644 --- a/lightning/src/ln/script.rs +++ b/lightning/src/ln/script.rs @@ -3,33 +3,34 @@ 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::secp256k1::key::PublicKey; +use bitcoin::hash_types::{WPubkeyHash, WScriptHash}; +use bitcoin::secp256k1::PublicKey; +use bitcoin::util::address::WitnessVersion; -use ln::features::InitFeatures; -use ln::msgs::DecodeError; -use util::ser::{Readable, Writeable, Writer}; +use crate::ln::channelmanager; +use crate::ln::features::InitFeatures; +use crate::ln::msgs::DecodeError; +use crate::util::ser::{Readable, Writeable, Writer}; use core::convert::TryFrom; -use core::num::NonZeroU8; -use io; +use crate::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)] +/// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md +#[derive(Clone, PartialEq, Eq)] 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]. /// - /// [BOLT #2]: https://github.com/lightningnetwork/lightning-rfc/blob/master/02-peer-protocol.md + /// [BOLT #2]: https://github.com/lightning/bolts/blob/master/02-peer-protocol.md pub script: Script } -#[derive(Clone)] +#[derive(Clone, PartialEq, Eq)] enum ShutdownScriptImpl { /// [`PublicKey`] used to form a P2WPKH script pubkey. Used to support backward-compatible /// serialization. @@ -66,24 +67,14 @@ 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))) + Self(ShutdownScriptImpl::Bolt2(Script::new_v0_p2wpkh(pubkey_hash))) } /// Generates a P2WSH script pubkey from the given [`WScriptHash`]. pub fn new_p2wsh(script_hash: &WScriptHash) -> Self { - Self(ShutdownScriptImpl::Bolt2(Script::new_v0_wsh(script_hash))) + Self(ShutdownScriptImpl::Bolt2(Script::new_v0_p2wsh(script_hash))) } /// Generates a witness script pubkey from the given segwit version and program. @@ -94,9 +85,9 @@ impl ShutdownScript { /// # Errors /// /// This function may return an error if `program` is invalid for the segwit `version`. - pub fn new_witness_program(version: NonZeroU8, program: &[u8]) -> Result { + pub fn new_witness_program(version: WitnessVersion, program: &[u8]) -> Result { let script = Builder::new() - .push_int(version.get().into()) + .push_int(version as i64) .push_slice(&program) .into_script(); Self::try_from(script) @@ -126,29 +117,35 @@ 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() { - script.is_witness_program() && script.as_bytes()[0] != SEGWIT_V0.into_u8() + script.is_witness_program() && script.as_bytes()[0] != SEGWIT_V0.to_u8() } else { false } } +// 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