diff --git a/common/htlc_tx.c b/common/htlc_tx.c index 5be4b47f2..7427b2e15 100644 --- a/common/htlc_tx.c +++ b/common/htlc_tx.c @@ -4,24 +4,21 @@ #include #include -static struct bitcoin_tx *htlc_tx(const tal_t *ctx, - const struct chainparams *chainparams, - const struct bitcoin_outpoint *commit, - const u8 *commit_wscript, - struct amount_msat msat, - u16 to_self_delay, - const struct pubkey *revocation_pubkey, - const struct pubkey *local_delayedkey, - struct amount_sat htlc_fee, - u32 locktime, - bool option_anchor_outputs) +/* Low-level tx creator: used when onchaind has done most of the work! */ +struct bitcoin_tx *htlc_tx(const tal_t *ctx, + const struct chainparams *chainparams, + const struct bitcoin_outpoint *commit, + const u8 *commit_wscript, + struct amount_sat amount, + const u8 *htlc_tx_wscript, + struct amount_sat htlc_fee, + u32 locktime, + bool option_anchor_outputs) { /* BOLT #3: * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, 1, 1, locktime); - u8 *wscript; - struct amount_sat amount; /* BOLT #3: * @@ -45,7 +42,6 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, * transaction * * `txin[0]` sequence: `0` (set to `1` for `option_anchors`) */ - amount = amount_msat_to_sat_round_down(msat); bitcoin_tx_add_input(tx, commit, option_anchor_outputs ? 1 : 0, NULL, amount, NULL, commit_wscript); @@ -59,18 +55,14 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, * below */ if (!amount_sat_sub(&amount, amount, htlc_fee)) - abort(); + return tal_free(tx); - wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay, revocation_pubkey, - local_delayedkey); - bitcoin_tx_add_output(tx, scriptpubkey_p2wsh(tmpctx, wscript), - wscript, amount); + bitcoin_tx_add_output(tx, scriptpubkey_p2wsh(tmpctx, htlc_tx_wscript), + htlc_tx_wscript, amount); bitcoin_tx_finalize(tx); assert(bitcoin_tx_check(tx)); - tal_free(wscript); - return tx; } @@ -84,14 +76,19 @@ struct bitcoin_tx *htlc_success_tx(const tal_t *ctx, const struct keyset *keyset, bool option_anchor_outputs) { + const u8 *htlc_wscript; + + htlc_wscript = bitcoin_wscript_htlc_tx(tmpctx, + to_self_delay, + &keyset->self_revocation_key, + &keyset->self_delayed_payment_key); /* BOLT #3: * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ return htlc_tx(ctx, chainparams, commit, - commit_wscript, htlc_msatoshi, - to_self_delay, - &keyset->self_revocation_key, - &keyset->self_delayed_payment_key, + commit_wscript, + amount_msat_to_sat_round_down(htlc_msatoshi), + htlc_wscript, htlc_success_fee(feerate_per_kw, option_anchor_outputs), 0, @@ -137,13 +134,19 @@ struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx, const struct keyset *keyset, bool option_anchor_outputs) { + const u8 *htlc_wscript; + + htlc_wscript = bitcoin_wscript_htlc_tx(tmpctx, + to_self_delay, + &keyset->self_revocation_key, + &keyset->self_delayed_payment_key); /* BOLT #3: * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ return htlc_tx(ctx, chainparams, commit, - commit_wscript, htlc_msatoshi, to_self_delay, - &keyset->self_revocation_key, - &keyset->self_delayed_payment_key, + commit_wscript, + amount_msat_to_sat_round_down(htlc_msatoshi), + htlc_wscript, htlc_timeout_fee(feerate_per_kw, option_anchor_outputs), cltv_expiry, diff --git a/common/htlc_tx.h b/common/htlc_tx.h index 188b6d34d..f58608376 100644 --- a/common/htlc_tx.h +++ b/common/htlc_tx.h @@ -115,4 +115,14 @@ u8 *htlc_offered_wscript(const tal_t *ctx, const struct keyset *keyset, bool option_anchor_outputs); +/* Low-level HTLC tx creator */ +struct bitcoin_tx *htlc_tx(const tal_t *ctx, + const struct chainparams *chainparams, + const struct bitcoin_outpoint *commit, + const u8 *commit_wscript, + struct amount_sat amount, + const u8 *htlc_tx_wscript, + struct amount_sat htlc_fee, + u32 locktime, + bool option_anchor_outputs); #endif /* LIGHTNING_COMMON_HTLC_TX_H */