Skip to content

Commit c6c34af

Browse files
committed
Move script too long check into is_unsupported_shutdown_script
1 parent 2602198 commit c6c34af

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

lightning/src/ln/channel.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
691691
return Err(ChannelError::Close("Insufficient funding amount for initial commitment".to_owned()));
692692
}
693693

694+
let is_outbound = false;
695+
694696
let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
695697
match &msg.shutdown_scriptpubkey {
696698
&OptionalField::Present(ref script) => {
697-
if is_unsupported_shutdown_script(&their_features, script) {
699+
if is_unsupported_shutdown_script(&their_features, script, is_outbound) {
698700
return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: ({})", script.to_bytes().to_hex())));
699701
}
700702

@@ -772,7 +774,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
772774
channel_transaction_parameters: ChannelTransactionParameters {
773775
holder_pubkeys: pubkeys,
774776
holder_selected_contest_delay: config.own_channel_config.our_to_self_delay,
775-
is_outbound_from_holder: false,
777+
is_outbound_from_holder: is_outbound,
776778
counterparty_parameters: Some(CounterpartyChannelTransactionParameters {
777779
selected_contest_delay: msg.to_self_delay,
778780
pubkeys: counterparty_pubkeys,
@@ -1390,7 +1392,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
13901392
let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
13911393
match &msg.shutdown_scriptpubkey {
13921394
&OptionalField::Present(ref script) => {
1393-
if is_unsupported_shutdown_script(&their_features, script) {
1395+
if is_unsupported_shutdown_script(&their_features, script, self.is_outbound()) {
13941396
return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: ({})", script.to_bytes().to_hex())));
13951397
}
13961398

@@ -2903,15 +2905,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29032905
}
29042906
assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
29052907

2906-
// BOLT 2 says we must only send a scriptpubkey of certain standard forms,
2907-
// which for a a BIP-141-compliant witness program is at max 42 bytes in length.
2908-
// So don't let the remote peer feed us some super fee-heavy script.
2909-
if self.is_outbound() && msg.scriptpubkey.len() > 42 {
2910-
return Err(ChannelError::Close(format!("Got counterparty shutdown_scriptpubkey ({}) of absurd length from remote peer", msg.scriptpubkey.to_bytes().to_hex())));
2911-
}
2912-
2913-
//Check counterparty_shutdown_scriptpubkey form as BOLT says we must
2914-
if is_unsupported_shutdown_script(&their_features, &msg.scriptpubkey) {
2908+
if is_unsupported_shutdown_script(&their_features, &msg.scriptpubkey, self.is_outbound()) {
29152909
return Err(ChannelError::Close(format!("Got a nonstandard scriptpubkey ({}) from remote peer", msg.scriptpubkey.to_bytes().to_hex())));
29162910
}
29172911

@@ -4019,8 +4013,12 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
40194013
}
40204014
}
40214015

4022-
fn is_unsupported_shutdown_script(their_features: &InitFeatures, scriptpubkey: &Script) -> bool {
4023-
return is_unsupported_witness_shutdown_script(their_features, scriptpubkey) && !scriptpubkey.is_p2pkh() && !scriptpubkey.is_p2sh() && !scriptpubkey.is_v0_p2wpkh() && !scriptpubkey.is_v0_p2wsh()
4016+
fn is_unsupported_shutdown_script(their_features: &InitFeatures, scriptpubkey: &Script, is_outbound: bool) -> bool {
4017+
// BOLT 2 says we must only send a scriptpubkey of certain standard forms,
4018+
// which for a a BIP-141-compliant witness program is at max 42 bytes in length.
4019+
// So don't let the remote peer feed us some super fee-heavy script.
4020+
let is_script_too_long = is_outbound && scriptpubkey.len() > 42;
4021+
return is_script_too_long || (is_unsupported_witness_shutdown_script(their_features, scriptpubkey) && !scriptpubkey.is_p2pkh() && !scriptpubkey.is_p2sh() && !scriptpubkey.is_v0_p2wpkh() && !scriptpubkey.is_v0_p2wsh())
40244022
}
40254023

40264024
fn is_unsupported_witness_shutdown_script(their_features: &InitFeatures, scriptpubkey: &Script) -> bool {

0 commit comments

Comments
 (0)