2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2021-02-19 05:22:01 +01:00
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <common/shutdown_scriptpubkey.h>
|
|
|
|
|
2021-05-26 06:09:06 +02:00
|
|
|
/* BOLT #2:
|
2022-03-31 11:10:50 +02:00
|
|
|
* 3. if (and only if) `option_shutdown_anysegwit` is negotiated:
|
2021-02-24 03:53:12 +01:00
|
|
|
* * `OP_1` through `OP_16` inclusive, followed by a single
|
|
|
|
* push of 2 to 40 bytes
|
|
|
|
* (witness program versions 1 through 16)
|
|
|
|
*/
|
2024-02-21 22:03:07 +01:00
|
|
|
static bool is_valid_witnessprog(const u8 *scriptpubkey, size_t scriptpubkey_len)
|
2021-02-24 03:53:12 +01:00
|
|
|
{
|
|
|
|
size_t pushlen;
|
|
|
|
|
2024-02-21 22:03:07 +01:00
|
|
|
if (scriptpubkey_len < 2)
|
2021-02-24 03:53:12 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
switch (scriptpubkey[0]) {
|
|
|
|
case OP_1:
|
|
|
|
case OP_2:
|
|
|
|
case OP_3:
|
|
|
|
case OP_4:
|
|
|
|
case OP_5:
|
|
|
|
case OP_6:
|
|
|
|
case OP_7:
|
|
|
|
case OP_8:
|
|
|
|
case OP_9:
|
|
|
|
case OP_10:
|
|
|
|
case OP_11:
|
|
|
|
case OP_12:
|
|
|
|
case OP_13:
|
|
|
|
case OP_14:
|
|
|
|
case OP_15:
|
|
|
|
case OP_16:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
pushlen = scriptpubkey[1];
|
|
|
|
/* Must be all of the rest of scriptpubkey */
|
2024-02-21 22:03:07 +01:00
|
|
|
if (2 + pushlen != scriptpubkey_len) {
|
2021-02-24 03:53:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pushlen >= 2 && pushlen <= 40;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valid_shutdown_scriptpubkey(const u8 *scriptpubkey,
|
2022-03-31 11:10:50 +02:00
|
|
|
bool anysegwit,
|
2022-12-12 07:06:04 +01:00
|
|
|
bool allow_oldstyle)
|
2021-02-19 05:22:01 +01:00
|
|
|
{
|
2024-02-21 22:03:07 +01:00
|
|
|
const size_t script_len = tal_bytelen(scriptpubkey);
|
2022-12-12 07:06:04 +01:00
|
|
|
if (allow_oldstyle) {
|
2024-02-21 22:03:07 +01:00
|
|
|
if (is_p2pkh(scriptpubkey, script_len, NULL)
|
|
|
|
|| is_p2sh(scriptpubkey, script_len, NULL))
|
2022-03-31 11:10:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-21 22:03:07 +01:00
|
|
|
return is_p2wpkh(scriptpubkey, script_len, NULL)
|
|
|
|
|| is_p2wsh(scriptpubkey, script_len, NULL)
|
|
|
|
|| (anysegwit && is_valid_witnessprog(scriptpubkey, script_len));
|
2021-02-19 05:22:01 +01:00
|
|
|
}
|