1 #include "config.h"
2 #include <bitcoin/psbt.h>
3 #include <bitcoin/script.h>
4 #include <ccan/tal/str/str.h>
5 #include <common/blockheight_states.h>
6 #include <common/channel_type.h>
7 #include <common/fee_states.h>
8 #include <common/initial_channel.h>
9 #include <common/initial_commit_tx.h>
10 #include <common/keyset.h>
11 #include <common/type_to_string.h>
12 
new_initial_channel(const tal_t * ctx,const struct channel_id * cid,const struct bitcoin_outpoint * funding,u32 minimum_depth,const struct height_states * height_states TAKES,u32 lease_expiry,struct amount_sat funding_sats,struct amount_msat local_msatoshi,const struct fee_states * fee_states TAKES,const struct channel_config * local,const struct channel_config * remote,const struct basepoints * local_basepoints,const struct basepoints * remote_basepoints,const struct pubkey * local_funding_pubkey,const struct pubkey * remote_funding_pubkey,const struct channel_type * type TAKES,bool option_wumbo,enum side opener)13 struct channel *new_initial_channel(const tal_t *ctx,
14 				    const struct channel_id *cid,
15 				    const struct bitcoin_outpoint *funding,
16 				    u32 minimum_depth,
17 				    const struct height_states *height_states TAKES,
18 				    u32 lease_expiry,
19 				    struct amount_sat funding_sats,
20 				    struct amount_msat local_msatoshi,
21 				    const struct fee_states *fee_states TAKES,
22 				    const struct channel_config *local,
23 				    const struct channel_config *remote,
24 				    const struct basepoints *local_basepoints,
25 				    const struct basepoints *remote_basepoints,
26 				    const struct pubkey *local_funding_pubkey,
27 				    const struct pubkey *remote_funding_pubkey,
28 				    const struct channel_type *type TAKES,
29 				    bool option_wumbo,
30 				    enum side opener)
31 {
32 	struct channel *channel = tal(ctx, struct channel);
33 	struct amount_msat remote_msatoshi;
34 
35 	channel->cid = *cid;
36 	channel->funding = *funding;
37 	channel->funding_sats = funding_sats;
38 	channel->minimum_depth = minimum_depth;
39 	channel->lease_expiry = lease_expiry;
40 	if (!amount_sat_sub_msat(&remote_msatoshi,
41 				 channel->funding_sats, local_msatoshi))
42 		return tal_free(channel);
43 
44 	channel->opener = opener;
45 	channel->config[LOCAL] = *local;
46 	channel->config[REMOTE] = *remote;
47 	channel->funding_pubkey[LOCAL] = *local_funding_pubkey;
48 	channel->funding_pubkey[REMOTE] = *remote_funding_pubkey;
49 	channel->htlcs = NULL;
50 
51 	/* takes() if necessary */
52 	channel->fee_states = dup_fee_states(channel, fee_states);
53 
54 	/* takes() if necessary */
55 	if (!height_states)
56 		channel->blockheight_states = NULL;
57 	else
58 		channel->blockheight_states
59 			= dup_height_states(channel, height_states);
60 
61 	channel->view[LOCAL].owed[LOCAL]
62 		= channel->view[REMOTE].owed[LOCAL]
63 		= local_msatoshi;
64 	channel->view[REMOTE].owed[REMOTE]
65 		= channel->view[LOCAL].owed[REMOTE]
66 		= remote_msatoshi;
67 
68 	channel->basepoints[LOCAL] = *local_basepoints;
69 	channel->basepoints[REMOTE] = *remote_basepoints;
70 
71 	channel->commitment_number_obscurer
72 		= commit_number_obscurer(&channel->basepoints[opener].payment,
73 					 &channel->basepoints[!opener].payment);
74 
75 	channel->option_wumbo = option_wumbo;
76 	/* takes() if necessary */
77 	channel->type = tal_dup(channel, struct channel_type, type);
78 
79 	return channel;
80 }
81 
82 /* FIXME: We could cache this. */
initial_channel_tx(const tal_t * ctx,const u8 ** wscript,const struct channel * channel,const struct pubkey * per_commitment_point,enum side side,struct wally_tx_output * direct_outputs[NUM_SIDES],char ** err_reason)83 struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
84 				      const u8 **wscript,
85 				      const struct channel *channel,
86 				      const struct pubkey *per_commitment_point,
87 				      enum side side,
88 				      struct wally_tx_output *direct_outputs[NUM_SIDES],
89 				      char** err_reason)
90 {
91 	struct keyset keyset;
92 	struct bitcoin_tx *init_tx;
93 	u32 csv_lock;
94 
95 	/* This assumes no HTLCs! */
96 	assert(!channel->htlcs);
97 
98 	if (!derive_keyset(per_commitment_point,
99 			   &channel->basepoints[side],
100 			   &channel->basepoints[!side],
101 			   channel_has(channel, OPT_STATIC_REMOTEKEY),
102 			   &keyset)) {
103 		*err_reason = "Cannot derive keyset";
104 		return NULL;
105 	}
106 
107 	/* Figure out the csv_lock (if there's a lease) */
108 	if (channel->lease_expiry == 0)
109 		csv_lock = 1;
110 	else
111 		/* For the initial commitment, starts max lease */
112 		csv_lock = channel->lease_expiry
113 			- get_blockheight(channel->blockheight_states,
114 					  channel->opener,
115 					  side);
116 
117 	*wscript = bitcoin_redeem_2of2(ctx,
118 				       &channel->funding_pubkey[side],
119 				       &channel->funding_pubkey[!side]);
120 
121 	init_tx = initial_commit_tx(ctx, &channel->funding,
122 				    channel->funding_sats,
123 				    channel->funding_pubkey,
124 				    channel->opener,
125 				    /* They specify our to_self_delay and v.v. */
126 				    channel->config[!side].to_self_delay,
127 				    &keyset,
128 				    channel_feerate(channel, side),
129 				    channel->config[side].dust_limit,
130 				    channel->view[side].owed[side],
131 				    channel->view[side].owed[!side],
132 				    channel->config[!side].channel_reserve,
133 				    0 ^ channel->commitment_number_obscurer,
134 				    direct_outputs,
135 				    side, csv_lock,
136 				    channel_has(channel, OPT_ANCHOR_OUTPUTS),
137 				    err_reason);
138 
139 	if (init_tx) {
140 		psbt_input_add_pubkey(init_tx->psbt, 0,
141 				      &channel->funding_pubkey[side]);
142 		psbt_input_add_pubkey(init_tx->psbt, 0,
143 				      &channel->funding_pubkey[!side]);
144 	}
145 
146 	return init_tx;
147 }
148 
channel_feerate(const struct channel * channel,enum side side)149 u32 channel_feerate(const struct channel *channel, enum side side)
150 {
151 	return get_feerate(channel->fee_states, channel->opener, side);
152 }
153 
channel_blockheight(const struct channel * channel,enum side side)154 u32 channel_blockheight(const struct channel *channel, enum side side)
155 {
156 	return get_blockheight(channel->blockheight_states,
157 			       channel->opener, side);
158 }
159 
channel_upgradable_type(const tal_t * ctx,const struct channel * channel)160 struct channel_type *channel_upgradable_type(const tal_t *ctx,
161 					     const struct channel *channel)
162 {
163 	if (!channel_has(channel, OPT_STATIC_REMOTEKEY))
164 		return channel_type_static_remotekey(ctx);
165 
166 	return NULL;
167 }
168 
channel_desired_type(const tal_t * ctx,const struct channel * channel)169 struct channel_type *channel_desired_type(const tal_t *ctx,
170 					  const struct channel *channel)
171 {
172 	/* We don't actually want to downgrade anchors! */
173 	if (channel_has(channel, OPT_ANCHOR_OUTPUTS))
174 		return channel_type_anchor_outputs(ctx);
175 
176 	/* For now, we just want option_static_remotekey */
177 	return channel_type_static_remotekey(ctx);
178 }
179 
channel_has(const struct channel * channel,int feature)180 bool channel_has(const struct channel *channel, int feature)
181 {
182 	return channel_type_has(channel->type, feature);
183 }
184 
fmt_channel_view(const tal_t * ctx,const struct channel_view * view)185 static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view)
186 {
187 	return tal_fmt(ctx, "{ owed_local=%s,"
188 		       " owed_remote=%s }",
189 		       type_to_string(tmpctx, struct amount_msat,
190 				      &view->owed[LOCAL]),
191 		       type_to_string(tmpctx, struct amount_msat,
192 				      &view->owed[REMOTE]));
193 }
194 
195 /* FIXME: This should reference HTLCs somehow, and feerates! */
fmt_channel(const tal_t * ctx,const struct channel * channel)196 static char *fmt_channel(const tal_t *ctx, const struct channel *channel)
197 {
198 	return tal_fmt(ctx, "{ funding=%s,"
199 		       " opener=%s,"
200 		       " local=%s,"
201 		       " remote=%s }",
202 		       type_to_string(tmpctx, struct amount_sat,
203 				      &channel->funding_sats),
204 		       side_to_str(channel->opener),
205 		       fmt_channel_view(ctx, &channel->view[LOCAL]),
206 		       fmt_channel_view(ctx, &channel->view[REMOTE]));
207 }
208 /* Magic comment. */
209 REGISTER_TYPE_TO_STRING(channel, fmt_channel);
210