2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2017-02-07 02:44:22 +01:00
|
|
|
#include <bitcoin/script.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <channeld/commit_tx.h>
|
2019-05-31 09:30:32 +02:00
|
|
|
#include <common/htlc_trim.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/htlc_tx.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/keyset.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/permute_tx.h>
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
#ifndef SUPERVERBOSE
|
|
|
|
#define SUPERVERBOSE(...)
|
|
|
|
#endif
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
static bool trim(const struct htlc *htlc,
|
2019-02-21 04:45:55 +01:00
|
|
|
u32 feerate_per_kw,
|
|
|
|
struct amount_sat dust_limit,
|
2020-08-13 19:47:02 +02:00
|
|
|
bool option_anchor_outputs,
|
2017-02-21 05:45:28 +01:00
|
|
|
enum side side)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
2019-05-31 09:30:32 +02:00
|
|
|
return htlc_is_trimmed(htlc_owner(htlc), htlc->amount,
|
2020-08-13 19:44:02 +02:00
|
|
|
feerate_per_kw, dust_limit, side,
|
2020-08-13 19:47:02 +02:00
|
|
|
option_anchor_outputs);
|
2017-02-21 05:45:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
|
2019-02-21 04:45:55 +01:00
|
|
|
u32 feerate_per_kw,
|
|
|
|
struct amount_sat dust_limit,
|
2020-08-13 19:47:02 +02:00
|
|
|
bool option_anchor_outputs,
|
2017-02-21 05:45:28 +01:00
|
|
|
enum side side)
|
|
|
|
{
|
|
|
|
size_t i, n;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
for (i = n = 0; i < tal_count(htlcs); i++)
|
2020-08-13 19:47:02 +02:00
|
|
|
n += !trim(htlcs[i], feerate_per_kw, dust_limit,
|
|
|
|
option_anchor_outputs, side);
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
return n;
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
2021-09-28 21:12:01 +02:00
|
|
|
bool commit_tx_amount_trimmed(const struct htlc **htlcs,
|
|
|
|
u32 feerate_per_kw,
|
|
|
|
struct amount_sat dust_limit,
|
|
|
|
bool option_anchor_outputs,
|
|
|
|
enum side side,
|
|
|
|
struct amount_msat *amt)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < tal_count(htlcs); i++) {
|
|
|
|
if (trim(htlcs[i], feerate_per_kw, dust_limit,
|
|
|
|
option_anchor_outputs, side))
|
|
|
|
if (!amount_msat_add(amt, *amt, htlcs[i]->amount))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
static void add_offered_htlc_out(struct bitcoin_tx *tx, size_t n,
|
|
|
|
const struct htlc *htlc,
|
2020-08-13 19:47:02 +02:00
|
|
|
const struct keyset *keyset,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-21 05:45:28 +01:00
|
|
|
{
|
2017-08-18 06:43:53 +02:00
|
|
|
struct ripemd160 ripemd;
|
2019-03-15 21:50:08 +01:00
|
|
|
u8 *wscript, *p2wsh;
|
|
|
|
struct amount_sat amount = amount_msat_to_sat_round_down(htlc->amount);
|
2017-08-18 06:43:53 +02:00
|
|
|
|
|
|
|
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
2020-08-13 19:47:02 +02:00
|
|
|
wscript = htlc_offered_wscript(tx, &ripemd, keyset,
|
|
|
|
option_anchor_outputs);
|
2019-03-15 21:50:08 +01:00
|
|
|
p2wsh = scriptpubkey_p2wsh(tx, wscript);
|
2020-05-21 03:57:00 +02:00
|
|
|
bitcoin_tx_add_output(tx, p2wsh, wscript, amount);
|
2021-04-27 06:45:10 +02:00
|
|
|
SUPERVERBOSE("# HTLC #%" PRIu64 " offered amount %"PRIu64" wscript %s\n", htlc->id,
|
|
|
|
amount.satoshis, /* Raw: BOLT 3 output match */
|
2019-02-21 04:45:55 +01:00
|
|
|
tal_hex(wscript, wscript));
|
2017-02-21 05:45:28 +01:00
|
|
|
tal_free(wscript);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
|
|
|
|
const struct htlc *htlc,
|
2020-08-13 19:47:02 +02:00
|
|
|
const struct keyset *keyset,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-21 05:45:28 +01:00
|
|
|
{
|
2017-08-18 06:43:53 +02:00
|
|
|
struct ripemd160 ripemd;
|
2019-03-15 21:50:08 +01:00
|
|
|
u8 *wscript, *p2wsh;
|
|
|
|
struct amount_sat amount;
|
2017-08-18 06:43:53 +02:00
|
|
|
|
|
|
|
ripemd160(&ripemd, htlc->rhash.u.u8, sizeof(htlc->rhash.u.u8));
|
2020-08-13 19:47:02 +02:00
|
|
|
wscript = htlc_received_wscript(tx, &ripemd, &htlc->expiry, keyset,
|
|
|
|
option_anchor_outputs);
|
2019-03-15 21:50:08 +01:00
|
|
|
p2wsh = scriptpubkey_p2wsh(tx, wscript);
|
|
|
|
amount = amount_msat_to_sat_round_down(htlc->amount);
|
|
|
|
|
2020-05-21 03:57:00 +02:00
|
|
|
bitcoin_tx_add_output(tx, p2wsh, wscript, amount);
|
2019-03-15 21:50:08 +01:00
|
|
|
|
2021-04-27 06:45:10 +02:00
|
|
|
SUPERVERBOSE("# HTLC #%"PRIu64" received amount %"PRIu64" wscript %s\n",
|
2019-02-21 04:45:55 +01:00
|
|
|
htlc->id,
|
2021-04-27 06:45:10 +02:00
|
|
|
amount.satoshis, /* Raw: BOLT 3 output match */
|
2019-02-21 04:45:55 +01:00
|
|
|
tal_hex(wscript, wscript));
|
2017-02-21 05:45:28 +01:00
|
|
|
tal_free(wscript);
|
|
|
|
}
|
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
struct bitcoin_tx *commit_tx(const tal_t *ctx,
|
2021-10-13 05:45:36 +02:00
|
|
|
const struct bitcoin_outpoint *funding,
|
|
|
|
struct amount_sat funding_sats,
|
2020-08-13 19:46:02 +02:00
|
|
|
const struct pubkey *local_funding_key,
|
|
|
|
const struct pubkey *remote_funding_key,
|
2019-09-09 18:11:24 +02:00
|
|
|
enum side opener,
|
2017-02-07 02:44:22 +01:00
|
|
|
u16 to_self_delay,
|
2021-06-22 23:49:04 +02:00
|
|
|
u32 lease_expiry,
|
|
|
|
u32 blockheight,
|
2017-08-18 06:43:53 +02:00
|
|
|
const struct keyset *keyset,
|
2017-11-21 04:33:22 +01:00
|
|
|
u32 feerate_per_kw,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat dust_limit,
|
|
|
|
struct amount_msat self_pay,
|
|
|
|
struct amount_msat other_pay,
|
2017-02-07 02:44:22 +01:00
|
|
|
const struct htlc **htlcs,
|
2017-02-07 02:44:22 +01:00
|
|
|
const struct htlc ***htlcmap,
|
2020-05-07 02:43:34 +02:00
|
|
|
struct wally_tx_output *direct_outputs[NUM_SIDES],
|
2017-02-21 05:45:28 +01:00
|
|
|
u64 obscured_commitment_number,
|
2020-08-13 19:47:02 +02:00
|
|
|
bool option_anchor_outputs,
|
2017-02-21 05:45:28 +01:00
|
|
|
enum side side)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat base_fee;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_msat total_pay;
|
2017-02-07 02:44:22 +01:00
|
|
|
struct bitcoin_tx *tx;
|
2022-07-25 09:00:09 +02:00
|
|
|
size_t n, untrimmed;
|
2021-07-02 21:46:20 +02:00
|
|
|
/* Is this the lessor ? */
|
|
|
|
enum side lessor = !opener;
|
2018-10-22 06:21:05 +02:00
|
|
|
u32 *cltvs;
|
2020-08-13 19:47:02 +02:00
|
|
|
bool to_local, to_remote;
|
2020-05-07 02:43:34 +02:00
|
|
|
struct htlc *dummy_to_local = (struct htlc *)0x01,
|
|
|
|
*dummy_to_remote = (struct htlc *)0x02;
|
2020-08-13 19:46:02 +02:00
|
|
|
const u8 *funding_wscript = bitcoin_redeem_2of2(tmpctx,
|
|
|
|
local_funding_key,
|
|
|
|
remote_funding_key);
|
2021-07-02 21:46:20 +02:00
|
|
|
u32 csv_lock = lease_expiry > blockheight ?
|
|
|
|
lease_expiry - blockheight : 1;
|
2020-08-13 19:46:02 +02:00
|
|
|
|
2019-02-21 04:45:55 +01:00
|
|
|
if (!amount_msat_add(&total_pay, self_pay, other_pay))
|
|
|
|
abort();
|
2021-10-13 05:45:36 +02:00
|
|
|
assert(!amount_msat_greater_sat(total_pay, funding_sats));
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* 1. Calculate which committed HTLCs need to be trimmed (see
|
|
|
|
* [Trimmed Outputs](#trimmed-outputs)).
|
|
|
|
*/
|
2017-02-21 05:45:28 +01:00
|
|
|
untrimmed = commit_tx_num_untrimmed(htlcs,
|
|
|
|
feerate_per_kw,
|
2020-08-13 19:47:02 +02:00
|
|
|
dust_limit,
|
|
|
|
option_anchor_outputs,
|
|
|
|
side);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* 2. Calculate the base [commitment transaction
|
|
|
|
* fee](#fee-calculation).
|
|
|
|
*/
|
2020-08-13 19:44:02 +02:00
|
|
|
base_fee = commit_tx_base_fee(feerate_per_kw, untrimmed,
|
2020-08-13 19:47:02 +02:00
|
|
|
option_anchor_outputs);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2023-03-23 06:48:09 +01:00
|
|
|
SUPERVERBOSE("# base commitment transaction fee = %"PRIu64" for %zu untrimmed\n",
|
|
|
|
base_fee.satoshis /* Raw: spec uses raw numbers */, untrimmed);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
2021-09-08 02:08:14 +02:00
|
|
|
* If `option_anchors` applies to the commitment
|
2020-08-13 19:47:02 +02:00
|
|
|
* transaction, also subtract two times the fixed anchor size
|
|
|
|
* of 330 sats from the funder (either `to_local` or
|
|
|
|
* `to_remote`).
|
|
|
|
*/
|
|
|
|
if (option_anchor_outputs
|
|
|
|
&& !amount_sat_add(&base_fee, base_fee, AMOUNT_SAT(660)))
|
|
|
|
/* Can't overflow: feerate is u32. */
|
|
|
|
abort();
|
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* 3. Subtract this base fee from the funder (either `to_local` or
|
2020-08-20 08:49:47 +02:00
|
|
|
* `to_remote`).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-09-09 18:11:24 +02:00
|
|
|
try_subtract_fee(opener, side, base_fee, &self_pay, &other_pay);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2017-03-07 06:49:11 +01:00
|
|
|
#ifdef PRINT_ACTUAL_FEE
|
|
|
|
{
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat out = AMOUNT_SAT(0);
|
|
|
|
bool ok = true;
|
2022-07-25 09:00:09 +02:00
|
|
|
for (size_t i = 0; i < tal_count(htlcs); i++) {
|
2020-08-13 19:47:02 +02:00
|
|
|
if (!trim(htlcs[i], feerate_per_kw, dust_limit,
|
|
|
|
option_anchor_outputs, side))
|
2019-02-21 04:45:55 +01:00
|
|
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(htlcs[i]->amount));
|
2017-03-07 06:49:11 +01:00
|
|
|
}
|
2021-04-27 06:42:10 +02:00
|
|
|
if (amount_msat_greater_eq_sat(self_pay, dust_limit))
|
2019-02-21 04:45:55 +01:00
|
|
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(self_pay));
|
2021-04-27 06:42:10 +02:00
|
|
|
if (amount_msat_greater_eq_sat(other_pay, dust_limit))
|
2019-02-21 04:45:55 +01:00
|
|
|
ok &= amount_sat_add(&out, out, amount_msat_to_sat_round_down(other_pay));
|
|
|
|
assert(ok);
|
2017-03-07 06:49:11 +01:00
|
|
|
SUPERVERBOSE("# actual commitment transaction fee = %"PRIu64"\n",
|
2021-10-13 05:45:36 +02:00
|
|
|
funding_sats.satoshis - out.satoshis); /* Raw: test output */
|
2017-03-07 06:49:11 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-13 19:47:02 +02:00
|
|
|
/* Worst-case sizing: both to-local and to-remote outputs, and anchors. */
|
|
|
|
tx = bitcoin_tx(ctx, chainparams, 1, untrimmed + 2 + 2, 0);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
/* We keep track of which outputs have which HTLCs */
|
2019-03-15 21:50:08 +01:00
|
|
|
*htlcmap = tal_arr(tx, const struct htlc *, tx->wtx->outputs_allocation_len);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2018-10-22 06:21:05 +02:00
|
|
|
/* We keep cltvs for tie-breaking HTLC outputs; we use the same order
|
|
|
|
* for sending the htlc txs, so it may matter. */
|
2019-03-15 21:50:08 +01:00
|
|
|
cltvs = tal_arr(tmpctx, u32, tx->wtx->outputs_allocation_len);
|
2018-10-22 06:21:05 +02:00
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
/* This could be done in a single loop, but we follow the BOLT
|
|
|
|
* literally to make comments in test vectors clearer. */
|
|
|
|
|
|
|
|
n = 0;
|
2017-02-07 02:44:22 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2020-08-20 08:49:47 +02:00
|
|
|
* 4. For every offered HTLC, if it is not trimmed, add an
|
2017-02-21 05:45:28 +01:00
|
|
|
* [offered HTLC output](#offered-htlc-outputs).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2022-07-25 09:00:09 +02:00
|
|
|
for (size_t i = 0; i < tal_count(htlcs); i++) {
|
2017-02-21 05:45:28 +01:00
|
|
|
if (htlc_owner(htlcs[i]) != side)
|
|
|
|
continue;
|
2020-08-13 19:47:02 +02:00
|
|
|
if (trim(htlcs[i], feerate_per_kw, dust_limit,
|
|
|
|
option_anchor_outputs, side))
|
2017-02-21 05:45:28 +01:00
|
|
|
continue;
|
2020-08-13 19:47:02 +02:00
|
|
|
add_offered_htlc_out(tx, n, htlcs[i], keyset,
|
|
|
|
option_anchor_outputs);
|
2018-10-22 06:17:24 +02:00
|
|
|
(*htlcmap)[n] = htlcs[i];
|
2018-10-22 06:21:05 +02:00
|
|
|
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
|
2018-10-22 06:17:24 +02:00
|
|
|
n++;
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2020-08-20 08:49:47 +02:00
|
|
|
* 5. For every received HTLC, if it is not trimmed, add an
|
2017-02-21 05:45:28 +01:00
|
|
|
* [received HTLC output](#received-htlc-outputs).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2022-07-25 09:00:09 +02:00
|
|
|
for (size_t i = 0; i < tal_count(htlcs); i++) {
|
2017-02-21 05:45:28 +01:00
|
|
|
if (htlc_owner(htlcs[i]) == side)
|
|
|
|
continue;
|
2020-08-13 19:47:02 +02:00
|
|
|
if (trim(htlcs[i], feerate_per_kw, dust_limit,
|
|
|
|
option_anchor_outputs, side))
|
2017-02-21 05:45:28 +01:00
|
|
|
continue;
|
2020-08-13 19:47:02 +02:00
|
|
|
add_received_htlc_out(tx, n, htlcs[i], keyset,
|
|
|
|
option_anchor_outputs);
|
2018-10-22 06:17:24 +02:00
|
|
|
(*htlcmap)[n] = htlcs[i];
|
2018-10-22 06:21:05 +02:00
|
|
|
cltvs[n] = abs_locktime_to_blocks(&htlcs[i]->expiry);
|
2018-10-22 06:17:24 +02:00
|
|
|
n++;
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2020-08-20 08:49:47 +02:00
|
|
|
* 6. If the `to_local` amount is greater or equal to
|
2017-06-06 01:48:10 +02:00
|
|
|
* `dust_limit_satoshis`, add a [`to_local`
|
2018-09-20 22:19:38 +02:00
|
|
|
* output](#to_local-output).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_greater_eq_sat(self_pay, dust_limit)) {
|
2021-06-22 23:49:04 +02:00
|
|
|
/* BOLT- #3:
|
|
|
|
* In a leased channel, the `to_local` output that
|
|
|
|
* pays the `accepter` node is modified so that its
|
|
|
|
* CSV is equal to the greater of the
|
|
|
|
* `to_self_delay` or the `lease_end` - `blockheight`.
|
|
|
|
*/
|
2021-06-16 19:44:51 +02:00
|
|
|
u8 *wscript = to_self_wscript(tmpctx,
|
|
|
|
to_self_delay,
|
2021-07-02 21:46:20 +02:00
|
|
|
side == lessor ? csv_lock : 0,
|
2021-06-16 19:44:51 +02:00
|
|
|
keyset);
|
2019-03-15 21:50:08 +01:00
|
|
|
u8 *p2wsh = scriptpubkey_p2wsh(tx, wscript);
|
|
|
|
struct amount_sat amount = amount_msat_to_sat_round_down(self_pay);
|
|
|
|
|
2020-05-21 03:57:00 +02:00
|
|
|
bitcoin_tx_add_output(tx, p2wsh, wscript, amount);
|
2020-05-07 02:43:34 +02:00
|
|
|
/* Add a dummy entry to the htlcmap so we can recognize it later */
|
|
|
|
(*htlcmap)[n] = direct_outputs ? dummy_to_local : NULL;
|
2018-10-22 06:21:05 +02:00
|
|
|
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
|
|
|
|
* However, valgrind will warn us something wierd is happening */
|
2021-04-27 06:45:10 +02:00
|
|
|
SUPERVERBOSE("# to_local amount %"PRIu64" wscript %s\n",
|
|
|
|
amount.satoshis, /* Raw: BOLT 3 output match */
|
2017-02-07 02:44:22 +01:00
|
|
|
tal_hex(tmpctx, wscript));
|
|
|
|
n++;
|
2020-08-13 19:47:02 +02:00
|
|
|
to_local = true;
|
|
|
|
} else
|
|
|
|
to_local = false;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2020-08-20 08:49:47 +02:00
|
|
|
* 7. If the `to_remote` amount is greater or equal to
|
2017-06-06 01:48:10 +02:00
|
|
|
* `dust_limit_satoshis`, add a [`to_remote`
|
2018-09-20 22:19:38 +02:00
|
|
|
* output](#to_remote-output).
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2021-12-22 05:07:24 +01:00
|
|
|
u8 *redeem;
|
2019-02-21 04:45:55 +01:00
|
|
|
if (amount_msat_greater_eq_sat(other_pay, dust_limit)) {
|
2019-03-15 21:50:08 +01:00
|
|
|
struct amount_sat amount = amount_msat_to_sat_round_down(other_pay);
|
2020-08-13 19:48:02 +02:00
|
|
|
u8 *scriptpubkey;
|
|
|
|
int pos;
|
|
|
|
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
2017-02-07 02:44:22 +01:00
|
|
|
*
|
2017-06-06 01:48:10 +02:00
|
|
|
* #### `to_remote` Output
|
2017-02-07 02:44:22 +01:00
|
|
|
*
|
2021-09-08 02:08:14 +02:00
|
|
|
* If `option_anchors` applies to the commitment
|
2020-08-13 19:48:02 +02:00
|
|
|
* transaction, the `to_remote` output is encumbered by a one
|
|
|
|
* block csv lock.
|
2022-05-18 02:59:04 +02:00
|
|
|
* <remotepubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY
|
2020-08-13 19:48:02 +02:00
|
|
|
*
|
|
|
|
*...
|
|
|
|
* Otherwise, this output is a simple P2WPKH to `remotepubkey`.
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2020-08-13 19:48:02 +02:00
|
|
|
if (option_anchor_outputs) {
|
2021-12-22 05:07:24 +01:00
|
|
|
redeem = anchor_to_remote_redeem(tmpctx,
|
|
|
|
&keyset->other_payment_key,
|
|
|
|
(!side) == lessor ?
|
|
|
|
csv_lock : 1);
|
2021-06-22 23:49:04 +02:00
|
|
|
/* BOLT- #3:
|
|
|
|
* ##### Leased channel (`option_will_fund`)
|
|
|
|
*
|
|
|
|
* If a `lease` applies to the channel, the
|
|
|
|
* `to_remote` output of the `initiator`
|
|
|
|
* ensures the `leasor` funds are not
|
|
|
|
* spendable until the lease expires.
|
|
|
|
*
|
|
|
|
* <remote_pubkey> OP_CHECKSIGVERIFY
|
|
|
|
* MAX(1, lease_end - blockheight)
|
|
|
|
* OP_CHECKSEQUENCEVERIFY
|
|
|
|
*/
|
2021-07-02 21:46:20 +02:00
|
|
|
scriptpubkey = scriptpubkey_p2wsh(tmpctx, redeem);
|
2020-08-13 19:48:02 +02:00
|
|
|
} else {
|
2021-12-22 05:07:24 +01:00
|
|
|
redeem = NULL;
|
2020-08-13 19:48:02 +02:00
|
|
|
scriptpubkey = scriptpubkey_p2wpkh(tmpctx,
|
|
|
|
&keyset->other_payment_key);
|
|
|
|
}
|
2021-12-22 05:07:24 +01:00
|
|
|
pos = bitcoin_tx_add_output(tx, scriptpubkey, redeem, amount);
|
2019-03-15 21:50:08 +01:00
|
|
|
assert(pos == n);
|
2020-05-07 02:43:34 +02:00
|
|
|
(*htlcmap)[n] = direct_outputs ? dummy_to_remote : NULL;
|
2018-10-22 06:21:05 +02:00
|
|
|
/* We don't assign cltvs[n]: if we use it, order doesn't matter.
|
|
|
|
* However, valgrind will warn us something wierd is happening */
|
2021-04-27 06:45:10 +02:00
|
|
|
SUPERVERBOSE("# to_remote amount %"PRIu64" P2WPKH(%s)\n",
|
|
|
|
amount.satoshis, /* Raw: BOLT 3 output match */
|
2017-08-18 06:43:53 +02:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
|
|
|
&keyset->other_payment_key));
|
2017-02-07 02:44:22 +01:00
|
|
|
n++;
|
2020-08-13 19:47:02 +02:00
|
|
|
|
|
|
|
to_remote = true;
|
2021-12-22 05:07:24 +01:00
|
|
|
} else {
|
2020-08-13 19:47:02 +02:00
|
|
|
to_remote = false;
|
2021-12-22 05:07:24 +01:00
|
|
|
redeem = NULL;
|
|
|
|
}
|
2020-08-13 19:47:02 +02:00
|
|
|
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2021-09-08 02:08:14 +02:00
|
|
|
* 8. If `option_anchors` applies to the commitment transaction:
|
2020-08-20 08:49:47 +02:00
|
|
|
* * if `to_local` exists or there are untrimmed HTLCs, add a
|
2021-02-22 02:58:26 +01:00
|
|
|
* [`to_local_anchor` output]...
|
2020-08-20 08:49:47 +02:00
|
|
|
* * if `to_remote` exists or there are untrimmed HTLCs, add a
|
2021-02-22 02:58:26 +01:00
|
|
|
* [`to_remote_anchor` output]
|
2020-08-20 08:49:47 +02:00
|
|
|
*/
|
2020-08-13 19:47:02 +02:00
|
|
|
if (option_anchor_outputs) {
|
|
|
|
if (to_local || untrimmed != 0) {
|
|
|
|
tx_add_anchor_output(tx, local_funding_key);
|
|
|
|
(*htlcmap)[n] = NULL;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (to_remote || untrimmed != 0) {
|
|
|
|
tx_add_anchor_output(tx, remote_funding_key);
|
|
|
|
(*htlcmap)[n] = NULL;
|
|
|
|
n++;
|
|
|
|
}
|
2017-02-07 02:44:22 +01:00
|
|
|
}
|
|
|
|
|
2019-06-27 02:25:17 +02:00
|
|
|
/* BOLT #2:
|
|
|
|
*
|
|
|
|
* - MUST set `channel_reserve_satoshis` greater than or equal to
|
|
|
|
* `dust_limit_satoshis`.
|
|
|
|
*/
|
|
|
|
/* This means there must be at least one output. */
|
|
|
|
assert(n > 0);
|
|
|
|
|
2019-03-15 21:50:08 +01:00
|
|
|
assert(n <= tx->wtx->outputs_allocation_len);
|
2018-10-22 06:17:24 +02:00
|
|
|
tal_resize(htlcmap, n);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2020-08-20 08:49:47 +02:00
|
|
|
* 9. Sort the outputs into [BIP 69+CLTV
|
2022-05-18 02:58:58 +02:00
|
|
|
* order](#transaction-output-ordering)
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-03-14 17:47:13 +01:00
|
|
|
permute_outputs(tx, cltvs, (const void **)*htlcmap);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* ## Commitment Transaction
|
|
|
|
*
|
|
|
|
* * version: 2
|
|
|
|
*/
|
2019-03-05 14:28:29 +01:00
|
|
|
assert(tx->wtx->version == 2);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* * locktime: upper 8 bits are 0x20, lower 24 bits are the lower 24 bits of the obscured commitment number
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2020-05-22 05:16:11 +02:00
|
|
|
bitcoin_tx_set_locktime(tx,
|
|
|
|
(0x20000000 | (obscured_commitment_number & 0xFFFFFF)));
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* * txin count: 1
|
|
|
|
* * `txin[0]` outpoint: `txid` and `output_index` from
|
|
|
|
* `funding_created` message
|
|
|
|
*/
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
2019-01-14 03:22:05 +01:00
|
|
|
* * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-03-15 21:50:08 +01:00
|
|
|
u32 sequence = (0x80000000 | ((obscured_commitment_number>>24) & 0xFFFFFF));
|
2021-10-13 05:45:36 +02:00
|
|
|
bitcoin_tx_add_input(tx, funding,
|
|
|
|
sequence, NULL, funding_sats, NULL, funding_wscript);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2020-05-07 02:43:34 +02:00
|
|
|
/* Identify the direct outputs (to_us, to_them). */
|
|
|
|
if (direct_outputs != NULL) {
|
|
|
|
direct_outputs[LOCAL] = direct_outputs[REMOTE] = NULL;
|
|
|
|
for (size_t i = 0; i < tx->wtx->num_outputs; i++) {
|
|
|
|
if ((*htlcmap)[i] == dummy_to_local) {
|
|
|
|
(*htlcmap)[i] = NULL;
|
|
|
|
direct_outputs[LOCAL] = tx->wtx->outputs + i;
|
|
|
|
} else if ((*htlcmap)[i] == dummy_to_remote) {
|
|
|
|
(*htlcmap)[i] = NULL;
|
|
|
|
direct_outputs[REMOTE] = tx->wtx->outputs + i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:26:55 +01:00
|
|
|
bitcoin_tx_finalize(tx);
|
|
|
|
assert(bitcoin_tx_check(tx));
|
2019-05-09 13:04:24 +02:00
|
|
|
|
2017-02-07 02:44:22 +01:00
|
|
|
return tx;
|
|
|
|
}
|