openingd: pull out validation for shutdown script

We're gonna reuse it in dualopend.
This commit is contained in:
niftynei 2022-10-19 11:58:49 -05:00 committed by Rusty Russell
parent 976f6ef51a
commit 46dc37dff1
3 changed files with 51 additions and 28 deletions

View File

@ -3,6 +3,7 @@
#include <common/channel_config.h>
#include <common/features.h>
#include <common/initial_commit_tx.h>
#include <common/shutdown_scriptpubkey.h>
#include <common/status.h>
#include <common/type_to_string.h>
#include <hsmd/hsmd_wiregen.h>
@ -210,6 +211,43 @@ u8 *no_upfront_shutdown_script(const tal_t *ctx,
return NULL;
}
char *validate_remote_upfront_shutdown(const tal_t *ctx,
struct feature_set *our_features,
const u8 *their_features,
u8 *shutdown_scriptpubkey STEALS,
u8 **state_script)
{
bool anysegwit = feature_negotiated(our_features,
their_features,
OPT_SHUTDOWN_ANYSEGWIT);
bool anchors = feature_negotiated(our_features,
their_features,
OPT_ANCHOR_OUTPUTS)
|| feature_negotiated(our_features,
their_features,
OPT_ANCHORS_ZERO_FEE_HTLC_TX);
/* BOLT #2:
*
* - MUST include `upfront_shutdown_script` with either a valid
* `shutdown_scriptpubkey` as required by `shutdown` `scriptpubkey`,
* or a zero-length `shutdown_scriptpubkey` (ie. `0x0000`).
*/
/* We turn empty into NULL. */
if (tal_bytelen(shutdown_scriptpubkey) == 0)
shutdown_scriptpubkey = tal_free(shutdown_scriptpubkey);
*state_script = tal_steal(ctx, shutdown_scriptpubkey);
if (shutdown_scriptpubkey
&& !valid_shutdown_scriptpubkey(shutdown_scriptpubkey, anysegwit, !anchors))
return tal_fmt(tmpctx,
"Unacceptable upfront_shutdown_script %s",
tal_hex(tmpctx, shutdown_scriptpubkey));
return NULL;
}
void validate_initial_commitment_signature(int hsm_fd,
struct bitcoin_tx *tx,
struct bitcoin_signature *sig)

View File

@ -26,4 +26,10 @@ u8 *no_upfront_shutdown_script(const tal_t *ctx,
void validate_initial_commitment_signature(int hsm_fd,
struct bitcoin_tx *tx,
struct bitcoin_signature *sig);
char *validate_remote_upfront_shutdown(const tal_t *ctx,
struct feature_set *our_features,
const u8 *their_features,
u8 *shutdown_scriptpubkey STEALS,
u8 **state_script);
#endif /* LIGHTNING_OPENINGD_COMMON_H */

View File

@ -22,7 +22,6 @@
#include <common/peer_io.h>
#include <common/per_peer_state.h>
#include <common/read_peer_msg.h>
#include <common/shutdown_scriptpubkey.h>
#include <common/status.h>
#include <common/subdaemon.h>
#include <common/type_to_string.h>
@ -287,35 +286,15 @@ static bool setup_channel_funder(struct state *state)
static void set_remote_upfront_shutdown(struct state *state,
u8 *shutdown_scriptpubkey STEALS)
{
bool anysegwit = feature_negotiated(state->our_features,
state->their_features,
OPT_SHUTDOWN_ANYSEGWIT);
bool anchors = feature_negotiated(state->our_features,
state->their_features,
OPT_ANCHOR_OUTPUTS)
|| feature_negotiated(state->our_features,
state->their_features,
OPT_ANCHORS_ZERO_FEE_HTLC_TX);
char *err;
/* BOLT #2:
*
* - MUST include `upfront_shutdown_script` with either a valid
* `shutdown_scriptpubkey` as required by `shutdown` `scriptpubkey`,
* or a zero-length `shutdown_scriptpubkey` (ie. `0x0000`).
*/
/* We turn empty into NULL. */
if (tal_bytelen(shutdown_scriptpubkey) == 0)
shutdown_scriptpubkey = tal_free(shutdown_scriptpubkey);
err = validate_remote_upfront_shutdown(state, state->our_features,
state->their_features,
shutdown_scriptpubkey,
&state->upfront_shutdown_script[REMOTE]);
state->upfront_shutdown_script[REMOTE]
= tal_steal(state, shutdown_scriptpubkey);
if (shutdown_scriptpubkey
&& !valid_shutdown_scriptpubkey(shutdown_scriptpubkey, anysegwit, !anchors))
peer_failed_err(state->pps,
&state->channel_id,
"Unacceptable upfront_shutdown_script %s",
tal_hex(tmpctx, shutdown_scriptpubkey));
if (err)
peer_failed_err(state->pps, &state->channel_id, "%s", err);
}
/* We start the 'open a channel' negotation with the supplied peer, but