2017-08-28 18:02:01 +02:00
|
|
|
#include <assert.h>
|
2018-09-06 18:44:47 +02:00
|
|
|
#include <bitcoin/chainparams.h>
|
2020-05-28 01:09:39 +02:00
|
|
|
#include <bitcoin/psbt.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <bitcoin/script.h>
|
2019-12-12 15:07:53 +01:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2020-05-21 21:46:19 +02:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2019-12-12 15:07:53 +01:00
|
|
|
#include <common/fee_states.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/initial_channel.h>
|
|
|
|
#include <common/initial_commit_tx.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/keyset.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
|
|
struct channel *new_initial_channel(const tal_t *ctx,
|
2020-09-09 09:20:53 +02:00
|
|
|
const struct channel_id *cid,
|
2017-12-18 07:41:52 +01:00
|
|
|
const struct bitcoin_txid *funding_txid,
|
2017-08-28 18:02:01 +02:00
|
|
|
unsigned int funding_txout,
|
2019-02-26 17:57:19 +01:00
|
|
|
u32 minimum_depth,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat funding,
|
|
|
|
struct amount_msat local_msatoshi,
|
2019-12-12 15:07:53 +01:00
|
|
|
const struct fee_states *fee_states TAKES,
|
2017-08-28 18:02:01 +02:00
|
|
|
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,
|
2019-09-10 04:23:27 +02:00
|
|
|
bool option_static_remotekey,
|
2020-08-14 03:30:41 +02:00
|
|
|
bool option_anchor_outputs,
|
2019-09-09 18:11:24 +02:00
|
|
|
enum side opener)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
|
|
|
struct channel *channel = tal(ctx, struct channel);
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat remote_msatoshi;
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2020-09-09 09:20:53 +02:00
|
|
|
channel->cid = *cid;
|
2017-08-28 18:02:01 +02:00
|
|
|
channel->funding_txid = *funding_txid;
|
|
|
|
channel->funding_txout = funding_txout;
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->funding = funding;
|
2019-02-26 17:57:19 +01:00
|
|
|
channel->minimum_depth = minimum_depth;
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_sat_sub_msat(&remote_msatoshi,
|
|
|
|
channel->funding, local_msatoshi))
|
2017-08-28 18:02:01 +02:00
|
|
|
return tal_free(channel);
|
|
|
|
|
2019-09-09 18:11:24 +02:00
|
|
|
channel->opener = opener;
|
2018-11-21 04:12:03 +01:00
|
|
|
channel->config[LOCAL] = *local;
|
|
|
|
channel->config[REMOTE] = *remote;
|
2017-08-28 18:02:01 +02:00
|
|
|
channel->funding_pubkey[LOCAL] = *local_funding_pubkey;
|
|
|
|
channel->funding_pubkey[REMOTE] = *remote_funding_pubkey;
|
|
|
|
channel->htlcs = NULL;
|
|
|
|
|
2019-12-12 15:07:53 +01:00
|
|
|
/* takes() if necessary */
|
|
|
|
channel->fee_states = dup_fee_states(channel, fee_states);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->view[LOCAL].owed[LOCAL]
|
|
|
|
= channel->view[REMOTE].owed[LOCAL]
|
2017-08-28 18:02:01 +02:00
|
|
|
= local_msatoshi;
|
2019-02-21 04:45:55 +01:00
|
|
|
channel->view[REMOTE].owed[REMOTE]
|
|
|
|
= channel->view[LOCAL].owed[REMOTE]
|
|
|
|
= remote_msatoshi;
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
channel->basepoints[LOCAL] = *local_basepoints;
|
|
|
|
channel->basepoints[REMOTE] = *remote_basepoints;
|
|
|
|
|
|
|
|
channel->commitment_number_obscurer
|
2019-09-09 18:11:24 +02:00
|
|
|
= commit_number_obscurer(&channel->basepoints[opener].payment,
|
|
|
|
&channel->basepoints[!opener].payment);
|
2017-08-28 18:02:01 +02:00
|
|
|
|
2019-09-10 04:23:27 +02:00
|
|
|
channel->option_static_remotekey = option_static_remotekey;
|
2020-08-14 03:30:41 +02:00
|
|
|
channel->option_anchor_outputs = option_anchor_outputs;
|
|
|
|
if (option_anchor_outputs)
|
|
|
|
assert(option_static_remotekey);
|
2017-08-28 18:02:01 +02:00
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: We could cache this. */
|
|
|
|
struct bitcoin_tx *initial_channel_tx(const tal_t *ctx,
|
|
|
|
const u8 **wscript,
|
|
|
|
const struct channel *channel,
|
|
|
|
const struct pubkey *per_commitment_point,
|
2019-04-03 10:20:36 +02:00
|
|
|
enum side side,
|
2020-05-07 02:43:34 +02:00
|
|
|
struct wally_tx_output *direct_outputs[NUM_SIDES],
|
2019-04-03 10:20:36 +02:00
|
|
|
char** err_reason)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
|
|
|
struct keyset keyset;
|
2020-05-28 01:09:39 +02:00
|
|
|
struct bitcoin_tx *init_tx;
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
/* This assumes no HTLCs! */
|
|
|
|
assert(!channel->htlcs);
|
|
|
|
|
|
|
|
if (!derive_keyset(per_commitment_point,
|
2018-07-23 04:23:02 +02:00
|
|
|
&channel->basepoints[side],
|
|
|
|
&channel->basepoints[!side],
|
2019-09-10 04:25:27 +02:00
|
|
|
channel->option_static_remotekey,
|
|
|
|
&keyset)) {
|
2019-04-03 10:20:36 +02:00
|
|
|
*err_reason = "Cannot derive keyset";
|
2017-08-28 18:02:01 +02:00
|
|
|
return NULL;
|
2019-04-03 10:20:36 +02:00
|
|
|
}
|
2017-08-28 18:02:01 +02:00
|
|
|
|
|
|
|
*wscript = bitcoin_redeem_2of2(ctx,
|
|
|
|
&channel->funding_pubkey[side],
|
|
|
|
&channel->funding_pubkey[!side]);
|
|
|
|
|
2020-05-28 01:09:39 +02:00
|
|
|
init_tx = initial_commit_tx(ctx, &channel->funding_txid,
|
|
|
|
channel->funding_txout,
|
|
|
|
channel->funding,
|
2020-08-13 19:46:02 +02:00
|
|
|
channel->funding_pubkey,
|
2020-05-28 01:09:39 +02:00
|
|
|
channel->opener,
|
|
|
|
/* They specify our to_self_delay and v.v. */
|
|
|
|
channel->config[!side].to_self_delay,
|
|
|
|
&keyset,
|
|
|
|
channel_feerate(channel, side),
|
|
|
|
channel->config[side].dust_limit,
|
|
|
|
channel->view[side].owed[side],
|
|
|
|
channel->view[side].owed[!side],
|
|
|
|
channel->config[!side].channel_reserve,
|
|
|
|
0 ^ channel->commitment_number_obscurer,
|
|
|
|
direct_outputs,
|
|
|
|
side,
|
2020-08-14 03:30:41 +02:00
|
|
|
channel->option_anchor_outputs,
|
2020-05-28 01:09:39 +02:00
|
|
|
err_reason);
|
|
|
|
|
|
|
|
if (init_tx) {
|
|
|
|
psbt_input_add_pubkey(init_tx->psbt, 0,
|
|
|
|
&channel->funding_pubkey[side]);
|
|
|
|
psbt_input_add_pubkey(init_tx->psbt, 0,
|
|
|
|
&channel->funding_pubkey[!side]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return init_tx;
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:05:53 +01:00
|
|
|
u32 channel_feerate(const struct channel *channel, enum side side)
|
|
|
|
{
|
2019-09-09 18:11:24 +02:00
|
|
|
return get_feerate(channel->fee_states, channel->opener, side);
|
2019-12-12 15:05:53 +01:00
|
|
|
}
|
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
static char *fmt_channel_view(const tal_t *ctx, const struct channel_view *view)
|
|
|
|
{
|
2019-12-12 15:07:53 +01:00
|
|
|
return tal_fmt(ctx, "{ owed_local=%s,"
|
2019-02-21 04:45:55 +01:00
|
|
|
" owed_remote=%s }",
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&view->owed[LOCAL]),
|
|
|
|
type_to_string(tmpctx, struct amount_msat,
|
|
|
|
&view->owed[REMOTE]));
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
2019-12-12 15:07:53 +01:00
|
|
|
/* FIXME: This should reference HTLCs somehow, and feerates! */
|
2017-08-28 18:02:01 +02:00
|
|
|
static char *fmt_channel(const tal_t *ctx, const struct channel *channel)
|
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
return tal_fmt(ctx, "{ funding=%s,"
|
2019-09-09 18:11:24 +02:00
|
|
|
" opener=%s,"
|
2017-08-28 18:02:01 +02:00
|
|
|
" local=%s,"
|
|
|
|
" remote=%s }",
|
2019-02-21 04:45:55 +01:00
|
|
|
type_to_string(tmpctx, struct amount_sat,
|
|
|
|
&channel->funding),
|
2019-09-09 18:11:24 +02:00
|
|
|
side_to_str(channel->opener),
|
2017-08-28 18:02:01 +02:00
|
|
|
fmt_channel_view(ctx, &channel->view[LOCAL]),
|
|
|
|
fmt_channel_view(ctx, &channel->view[REMOTE]));
|
|
|
|
}
|
2020-08-13 19:46:02 +02:00
|
|
|
/* Magic comment. */
|
2017-08-28 18:02:01 +02:00
|
|
|
REGISTER_TYPE_TO_STRING(channel, fmt_channel);
|