1 /* Commit tx without HTLC support; needed for openingd. */
2 #ifndef LIGHTNING_COMMON_INITIAL_COMMIT_TX_H
3 #define LIGHTNING_COMMON_INITIAL_COMMIT_TX_H
4 #include "config.h"
5 #include <bitcoin/chainparams.h>
6 #include <bitcoin/pubkey.h>
7 #include <common/htlc.h>
8 #include <common/utils.h>
9
10 struct bitcoin_outpoint;
11 struct keyset;
12 struct wally_tx_output;
13
14 /* BOLT #3:
15 *
16 * This obscures the number of commitments made on the channel in the
17 * case of unilateral close, yet still provides a useful index for
18 * both nodes (who know the `payment_basepoint`s) to quickly find a
19 * revoked commitment transaction.
20 */
21 u64 commit_number_obscurer(const struct pubkey *opener_payment_basepoint,
22 const struct pubkey *accepter_payment_basepoint);
23
24
25 /* The base weight of a commitment tx */
commit_tx_base_weight(size_t num_untrimmed_htlcs,bool option_anchor_outputs)26 static inline size_t commit_tx_base_weight(size_t num_untrimmed_htlcs,
27 bool option_anchor_outputs)
28 {
29 size_t weight;
30
31 /* BOLT #3:
32 *
33 * The base fee for a commitment transaction:
34 * - MUST be calculated to match:
35 * 1. Start with `weight` = 724 (1124 if `option_anchors` applies).
36 */
37 if (option_anchor_outputs)
38 weight = 1124;
39 else
40 weight = 724;
41
42 /* BOLT #3:
43 *
44 * 2. For each committed HTLC, if that output is not trimmed as
45 * specified in [Trimmed Outputs](#trimmed-outputs), add 172
46 * to `weight`.
47 */
48 weight += 172 * num_untrimmed_htlcs;
49
50 if (chainparams->is_elements) {
51 /* Each transaction has surjection and rangeproof (both empty
52 * for us as long as we use unblinded L-BTC transactions). */
53 weight += 2 * 4;
54
55 /* Inputs have 6 bytes of blank proofs attached. This TX only
56 * has a single input. */
57 weight += 6;
58
59 /* Each direct output has a bit more weight to it */
60 weight += (32 + 1 + 1 + 1) * 4 * 2; /* Elements added fields */
61
62 /* Each HTLC output also carries a bit more weight */
63 weight += (32 + 1 + 1 + 1) * 4 * num_untrimmed_htlcs;
64
65 /* For elements we also need to add the fee output and the
66 * overhead for rangeproofs into the mix. */
67 weight += (8 + 1) * 4; /* Bitcoin style output */
68 weight += (32 + 1 + 1 + 1) * 4; /* Elements added fields */
69 }
70
71 return weight;
72 }
73
74 /* Helper to calculate the base fee if we have this many htlc outputs */
commit_tx_base_fee(u32 feerate_per_kw,size_t num_untrimmed_htlcs,bool option_anchor_outputs)75 static inline struct amount_sat commit_tx_base_fee(u32 feerate_per_kw,
76 size_t num_untrimmed_htlcs,
77 bool option_anchor_outputs)
78 {
79 return amount_tx_fee(feerate_per_kw,
80 commit_tx_base_weight(num_untrimmed_htlcs,
81 option_anchor_outputs));
82 }
83
84 /**
85 * initial_commit_tx: create (unsigned) commitment tx to spend the funding tx output
86 * @ctx: context to allocate transaction and @htlc_map from.
87 * @funding, @funding_sats: funding outpoint and amount
88 * @funding_wscript: scriptPubkey of the funding output
89 * @funding_keys: funding bitcoin keys
90 * @opener: is the LOCAL or REMOTE paying the fee?
91 * @keyset: keys derived for this commit tx.
92 * @feerate_per_kw: feerate to use
93 * @dust_limit: dust limit below which to trim outputs.
94 * @self_pay: amount to pay directly to self
95 * @other_pay: amount to pay directly to the other side
96 * @self_reserve: reserve the other side insisted we have
97 * @obscured_commitment_number: number to encode in commitment transaction
98 * @direct_outputs: If non-NULL, fill with pointers to the direct (non-HTLC) outputs (or NULL if none).
99 * @side: side to generate commitment transaction for.
100 * @option_anchor_outputs: does option_anchor_outputs apply to this channel?
101 * @err_reason: When NULL is returned, this will point to a human readable reason.
102 *
103 * We need to be able to generate the remote side's tx to create signatures,
104 * but the BOLT is expressed in terms of generating our local commitment
105 * transaction, so we carefully use the terms "self" and "other" here.
106 */
107 struct bitcoin_tx *initial_commit_tx(const tal_t *ctx,
108 const struct bitcoin_outpoint *funding,
109 struct amount_sat funding_sats,
110 const struct pubkey funding_key[NUM_SIDES],
111 enum side opener,
112 u16 to_self_delay,
113 const struct keyset *keyset,
114 u32 feerate_per_kw,
115 struct amount_sat dust_limit,
116 struct amount_msat self_pay,
117 struct amount_msat other_pay,
118 struct amount_sat self_reserve,
119 u64 obscured_commitment_number,
120 struct wally_tx_output *direct_outputs[NUM_SIDES],
121 enum side side,
122 u32 csv_lock,
123 bool option_anchor_outputs,
124 char** err_reason);
125
126 /* try_subtract_fee - take away this fee from the opener (and return true), or all if insufficient (and return false). */
127 bool try_subtract_fee(enum side opener, enum side side,
128 struct amount_sat base_fee,
129 struct amount_msat *self,
130 struct amount_msat *other);
131
132 /* Generate the witness script for the to-self output:
133 * scriptpubkey_p2wsh(ctx, wscript) gives the scriptpubkey */
134 u8 *to_self_wscript(const tal_t *ctx,
135 u16 to_self_delay,
136 u32 csv,
137 const struct keyset *keyset);
138
139 /* To-other is simply: scriptpubkey_p2wpkh(tx, keyset->other_payment_key) */
140
141 /* If we determine we need one, append this anchor output */
142 void tx_add_anchor_output(struct bitcoin_tx *tx,
143 const struct pubkey *funding_key);
144
145 #endif /* LIGHTNING_COMMON_INITIAL_COMMIT_TX_H */
146