2017-08-28 18:06:01 +02:00
|
|
|
#ifndef LIGHTNING_COMMON_HTLC_TX_H
|
|
|
|
#define LIGHTNING_COMMON_HTLC_TX_H
|
2017-02-07 02:44:22 +01:00
|
|
|
#include "config.h"
|
2019-07-30 16:14:43 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2022-01-26 03:12:28 +01:00
|
|
|
#include <bitcoin/tx.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/htlc.h>
|
2019-07-02 17:21:26 +02:00
|
|
|
#include <common/utils.h>
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2019-05-31 09:30:32 +02:00
|
|
|
struct bitcoin_signature;
|
2021-10-13 05:45:36 +02:00
|
|
|
struct bitcoin_outpoint;
|
2017-08-28 18:02:01 +02:00
|
|
|
struct keyset;
|
2017-02-07 02:44:22 +01:00
|
|
|
struct preimage;
|
|
|
|
struct pubkey;
|
2019-05-31 09:30:32 +02:00
|
|
|
struct ripemd160;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2020-08-13 19:44:02 +02:00
|
|
|
static inline struct amount_sat htlc_timeout_fee(u32 feerate_per_kw,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
2017-08-28 18:02:01 +02:00
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* The fee for an HTLC-timeout transaction:
|
2023-06-26 01:18:21 +02:00
|
|
|
* - If `option_anchors_zero_fee_htlc_tx` applies:
|
|
|
|
* 1. MUST be 0.
|
2022-03-31 11:10:50 +02:00
|
|
|
* - Otherwise, MUST be calculated to match:
|
2020-08-13 19:44:02 +02:00
|
|
|
* 1. Multiply `feerate_per_kw` by 663 (666 if `option_anchor_outputs`
|
|
|
|
* applies) and divide by 1000 (rounding down).
|
2017-08-28 18:02:01 +02:00
|
|
|
*/
|
2020-08-13 19:44:02 +02:00
|
|
|
u32 base;
|
2023-06-26 01:18:21 +02:00
|
|
|
|
|
|
|
if (option_anchors_zero_fee_htlc_tx)
|
|
|
|
return AMOUNT_SAT(0);
|
|
|
|
|
2020-08-13 19:44:02 +02:00
|
|
|
if (option_anchor_outputs)
|
|
|
|
base = 666;
|
|
|
|
else
|
|
|
|
base = 663;
|
2022-01-26 03:12:28 +01:00
|
|
|
return amount_tx_fee(base + elements_tx_overhead(chainparams, 1, 1),
|
|
|
|
feerate_per_kw);
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 19:44:02 +02:00
|
|
|
static inline struct amount_sat htlc_success_fee(u32 feerate_per_kw,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
2017-08-28 18:02:01 +02:00
|
|
|
*
|
2018-06-17 12:11:53 +02:00
|
|
|
* The fee for an HTLC-success transaction:
|
2023-06-26 01:18:21 +02:00
|
|
|
* - If `option_anchors_zero_fee_htlc_tx` applies:
|
|
|
|
* 1. MUST be 0.
|
2022-03-31 11:10:50 +02:00
|
|
|
* - Otherwise, MUST be calculated to match:
|
2020-08-13 19:44:02 +02:00
|
|
|
* 1. Multiply `feerate_per_kw` by 703 (706 if `option_anchor_outputs`
|
|
|
|
* applies) and divide by 1000 (rounding down).
|
2017-08-28 18:02:01 +02:00
|
|
|
*/
|
2020-08-13 19:44:02 +02:00
|
|
|
u32 base;
|
2023-06-26 01:18:21 +02:00
|
|
|
|
|
|
|
if (option_anchors_zero_fee_htlc_tx)
|
|
|
|
return AMOUNT_SAT(0);
|
|
|
|
|
2020-08-13 19:44:02 +02:00
|
|
|
if (option_anchor_outputs)
|
|
|
|
base = 706;
|
|
|
|
else
|
|
|
|
base = 703;
|
2022-01-26 03:12:28 +01:00
|
|
|
return amount_tx_fee(base + elements_tx_overhead(chainparams, 1, 1),
|
|
|
|
feerate_per_kw);
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
/* Create HTLC-success tx to spend a received HTLC commitment tx
|
|
|
|
* output; doesn't fill in input witness. */
|
|
|
|
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
|
2019-07-30 16:14:43 +02:00
|
|
|
const struct chainparams *chainparams,
|
2021-10-13 05:45:36 +02:00
|
|
|
const struct bitcoin_outpoint *commit,
|
2020-05-21 21:46:19 +02:00
|
|
|
const u8 *commit_wscript,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat htlc_msatoshi,
|
2017-02-07 02:44:22 +01:00
|
|
|
u16 to_self_delay,
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 feerate_per_kw,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct keyset *keyset,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* Fill in the witness for HTLC-success tx produced above. */
|
|
|
|
void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
|
|
|
|
const struct abs_locktime *htlc_abstimeout,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *localsig,
|
|
|
|
const struct bitcoin_signature *remotesig,
|
2017-03-07 06:46:59 +01:00
|
|
|
const struct preimage *payment_preimage,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct pubkey *revocationkey,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* Create HTLC-timeout tx to spend an offered HTLC commitment tx
|
|
|
|
* output; doesn't fill in input witness. */
|
|
|
|
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
|
2019-07-30 16:14:43 +02:00
|
|
|
const struct chainparams *chainparams,
|
2021-10-13 05:45:36 +02:00
|
|
|
const struct bitcoin_outpoint *commit,
|
2020-05-21 21:46:19 +02:00
|
|
|
const u8 *commit_wscript,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat htlc_msatoshi,
|
2017-08-18 06:43:53 +02:00
|
|
|
u32 cltv_expiry,
|
2017-02-07 02:44:22 +01:00
|
|
|
u16 to_self_delay,
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 feerate_per_kw,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct keyset *keyset,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* Fill in the witness for HTLC-timeout tx produced above. */
|
|
|
|
void htlc_timeout_tx_add_witness(struct bitcoin_tx *htlc_timeout,
|
|
|
|
const struct pubkey *localkey,
|
|
|
|
const struct pubkey *remotekey,
|
|
|
|
const struct sha256 *payment_hash,
|
2017-03-07 06:46:59 +01:00
|
|
|
const struct pubkey *revocationkey,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *localsig,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct bitcoin_signature *remotesig,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
/* Generate the witness script for an HTLC the other side offered:
|
|
|
|
* scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */
|
|
|
|
u8 *htlc_received_wscript(const tal_t *ctx,
|
|
|
|
const struct ripemd160 *ripemd,
|
|
|
|
const struct abs_locktime *expiry,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct keyset *keyset,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
/* Generate the witness script for an HTLC this side offered:
|
|
|
|
* scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */
|
|
|
|
u8 *htlc_offered_wscript(const tal_t *ctx,
|
|
|
|
const struct ripemd160 *ripemd,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct keyset *keyset,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2023-04-06 01:33:24 +02:00
|
|
|
/* 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,
|
2023-06-26 01:18:21 +02:00
|
|
|
bool option_anchor_outputs,
|
|
|
|
bool option_anchors_zero_fee_htlc_tx);
|
2017-08-28 18:06:01 +02:00
|
|
|
#endif /* LIGHTNING_COMMON_HTLC_TX_H */
|