From 66dda32da2ff27bcadfc323e30d40f635cdf9d88 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 19 Feb 2021 14:52:01 +1030 Subject: [PATCH] common/shutdown_scriptpubkey: extract shutdown scriptpubkey test. Signed-off-by: Rusty Russell --- common/Makefile | 1 + common/shutdown_scriptpubkey.c | 10 ++++++++++ common/shutdown_scriptpubkey.h | 21 +++++++++++++++++++++ lightningd/Makefile | 1 + lightningd/channel_control.c | 17 ++--------------- 5 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 common/shutdown_scriptpubkey.c create mode 100644 common/shutdown_scriptpubkey.h diff --git a/common/Makefile b/common/Makefile index b72c6695c..dfc7ab9cc 100644 --- a/common/Makefile +++ b/common/Makefile @@ -71,6 +71,7 @@ COMMON_SRC_NOGEN := \ common/read_peer_msg.c \ common/route.c \ common/setup.c \ + common/shutdown_scriptpubkey.c \ common/socket_close.c \ common/sphinx.c \ common/status.c \ diff --git a/common/shutdown_scriptpubkey.c b/common/shutdown_scriptpubkey.c new file mode 100644 index 000000000..8c7bcee24 --- /dev/null +++ b/common/shutdown_scriptpubkey.c @@ -0,0 +1,10 @@ +#include +#include + +bool valid_shutdown_scriptpubkey(const u8 *scriptpubkey) +{ + return is_p2pkh(scriptpubkey, NULL) + || is_p2sh(scriptpubkey, NULL) + || is_p2wpkh(scriptpubkey, NULL) + || is_p2wsh(scriptpubkey, NULL); +} diff --git a/common/shutdown_scriptpubkey.h b/common/shutdown_scriptpubkey.h new file mode 100644 index 000000000..1e70e91fd --- /dev/null +++ b/common/shutdown_scriptpubkey.h @@ -0,0 +1,21 @@ +#ifndef LIGHTNING_COMMON_SHUTDOWN_SCRIPTPUBKEY_H +#define LIGHTNING_COMMON_SHUTDOWN_SCRIPTPUBKEY_H +#include "config.h" +#include + +/* BOLT #2: + * + * 1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG` + * (pay to pubkey hash), OR + * 2. `OP_HASH160` `20` 20-bytes `OP_EQUAL` (pay to script hash), OR + * 3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey), OR + * 4. `OP_0` `32` 32-bytes (version 0 pay to witness script hash) + * + * A receiving node: + *... + * - if the `scriptpubkey` is not in one of the above forms: + * - SHOULD fail the connection. + */ +bool valid_shutdown_scriptpubkey(const u8 *scriptpubkey); + +#endif /* LIGHTNING_COMMON_SHUTDOWN_SCRIPTPUBKEY_H */ diff --git a/lightningd/Makefile b/lightningd/Makefile index 497a01801..287ffb873 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -120,6 +120,7 @@ LIGHTNINGD_COMMON_OBJS := \ common/pseudorand.o \ common/random_select.o \ common/setup.o \ + common/shutdown_scriptpubkey.o \ common/sphinx.o \ common/status_wire.o \ common/timeout.o \ diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 6775b645f..de2109baf 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -237,21 +238,7 @@ static void peer_got_shutdown(struct channel *channel, const u8 *msg) tal_free(channel->shutdown_scriptpubkey[REMOTE]); channel->shutdown_scriptpubkey[REMOTE] = scriptpubkey; - /* BOLT #2: - * - * 1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG` - * (pay to pubkey hash), OR - * 2. `OP_HASH160` `20` 20-bytes `OP_EQUAL` (pay to script hash), OR - * 3. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey), OR - * 4. `OP_0` `32` 32-bytes (version 0 pay to witness script hash) - * - * A receiving node: - *... - * - if the `scriptpubkey` is not in one of the above forms: - * - SHOULD fail the connection. - */ - if (!is_p2pkh(scriptpubkey, NULL) && !is_p2sh(scriptpubkey, NULL) - && !is_p2wpkh(scriptpubkey, NULL) && !is_p2wsh(scriptpubkey, NULL)) { + if (!valid_shutdown_scriptpubkey(scriptpubkey)) { channel_fail_permanent(channel, REASON_PROTOCOL, "Bad shutdown scriptpubkey %s",