Skip to content

Commit 2602198

Browse files
committed
Coalesce all shutdown script checks into a single function
1 parent 91d36cf commit 2602198

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -694,19 +694,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
694694
let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
695695
match &msg.shutdown_scriptpubkey {
696696
&OptionalField::Present(ref script) => {
697-
if unsupported_witness_shutdown_script(&their_features, script) {
698-
return Err(ChannelError::Close("Peer is signaling upfront_shutdown but not support for any segwit, and the script is a witness program".to_owned()));
697+
if is_unsupported_shutdown_script(&their_features, script) {
698+
return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: ({})", script.to_bytes().to_hex())));
699699
}
700700

701-
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
702-
if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
703-
Some(script.clone())
704-
// Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
705-
} else if script.len() == 0 {
701+
if script.len() == 0 {
706702
None
707-
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
708703
} else {
709-
return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: ({})", script.to_bytes().to_hex())));
704+
Some(script.clone())
710705
}
711706
},
712707
// Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
@@ -1395,19 +1390,14 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
13951390
let counterparty_shutdown_scriptpubkey = if their_features.supports_upfront_shutdown_script() {
13961391
match &msg.shutdown_scriptpubkey {
13971392
&OptionalField::Present(ref script) => {
1398-
if unsupported_witness_shutdown_script(&their_features, script) {
1399-
return Err(ChannelError::Close("Peer is signaling upfront_shutdown but not support for any segwit, and the script is a witness program".to_owned()));
1393+
if is_unsupported_shutdown_script(&their_features, script) {
1394+
return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: ({})", script.to_bytes().to_hex())));
14001395
}
14011396

1402-
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. We enforce it while receiving shutdown msg
1403-
if script.is_p2pkh() || script.is_p2sh() || script.is_v0_p2wsh() || script.is_v0_p2wpkh() {
1404-
Some(script.clone())
1405-
// Peer is signaling upfront_shutdown and has opt-out with a 0-length script. We don't enforce anything
1406-
} else if script.len() == 0 {
1397+
if script.len() == 0 {
14071398
None
1408-
// Peer is signaling upfront_shutdown and has provided a non-accepted scriptpubkey format. Fail the channel
14091399
} else {
1410-
return Err(ChannelError::Close(format!("Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. scriptpubkey: ({})", script.to_bytes().to_hex())));
1400+
Some(script.clone())
14111401
}
14121402
},
14131403
// Peer is signaling upfront shutdown but don't opt-out with correct mechanism (a.k.a 0-length script). Peer looks buggy, we fail the channel
@@ -2921,7 +2911,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
29212911
}
29222912

29232913
//Check counterparty_shutdown_scriptpubkey form as BOLT says we must
2924-
if unsupported_witness_shutdown_script(&their_features, &msg.scriptpubkey) && !msg.scriptpubkey.is_p2pkh() && !msg.scriptpubkey.is_p2sh() && !msg.scriptpubkey.is_v0_p2wpkh() && !msg.scriptpubkey.is_v0_p2wsh() {
2914+
if is_unsupported_shutdown_script(&their_features, &msg.scriptpubkey) {
29252915
return Err(ChannelError::Close(format!("Got a nonstandard scriptpubkey ({}) from remote peer", msg.scriptpubkey.to_bytes().to_hex())));
29262916
}
29272917

@@ -4029,7 +4019,11 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
40294019
}
40304020
}
40314021

4032-
fn unsupported_witness_shutdown_script(their_features: &InitFeatures, scriptpubkey: &Script) -> bool {
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()
4024+
}
4025+
4026+
fn is_unsupported_witness_shutdown_script(their_features: &InitFeatures, scriptpubkey: &Script) -> bool {
40334027
return !their_features.supports_shutdown_anysegwit()
40344028
&& scriptpubkey.is_witness_program()
40354029
}

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7339,7 +7339,7 @@ fn test_upfront_shutdown_script_unsupport_segwit() {
73397339
match events[0] {
73407340
MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg }, node_id } => {
73417341
assert_eq!(node_id, nodes[0].node.get_our_node_id());
7342-
assert_eq!(msg.data, "Peer is signaling upfront_shutdown but not support for any segwit, and the script is a witness program".to_owned())
7342+
assert!(regex::Regex::new(r"Peer is signaling upfront_shutdown but has provided a non-accepted scriptpubkey format. script: (\([A-Fa-f0-9]+\))").unwrap().is_match(&*msg.data));
73437343
},
73447344
_ => panic!("Unexpected event"),
73457345
}

0 commit comments

Comments
 (0)