2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2017-02-07 02:44:22 +01:00
|
|
|
#include <bitcoin/preimage.h>
|
|
|
|
#include <bitcoin/script.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-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
static struct bitcoin_tx *htlc_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 msat,
|
2017-02-07 02:44:22 +01:00
|
|
|
u16 to_self_delay,
|
|
|
|
const struct pubkey *revocation_pubkey,
|
|
|
|
const struct pubkey *local_delayedkey,
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat htlc_fee,
|
2020-08-13 19:42:02 +02:00
|
|
|
u32 locktime,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
2020-01-29 12:59:06 +01:00
|
|
|
/* BOLT #3:
|
|
|
|
* * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout
|
|
|
|
*/
|
|
|
|
struct bitcoin_tx *tx = bitcoin_tx(ctx, chainparams, 1, 1, locktime);
|
2017-02-07 02:44:22 +01:00
|
|
|
u8 *wscript;
|
2019-02-21 04:45:55 +01:00
|
|
|
struct amount_sat amount;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
*
|
|
|
|
* ## HTLC-Timeout and HTLC-Success Transactions
|
|
|
|
*
|
|
|
|
* These HTLC transactions are almost identical, except the
|
2019-04-01 05:25:45 +02:00
|
|
|
* HTLC-timeout transaction is timelocked. Both
|
|
|
|
* HTLC-timeout/HTLC-success transactions can be spent by a valid
|
2018-06-17 12:11:53 +02:00
|
|
|
* penalty transaction.
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
* * version: 2
|
|
|
|
*/
|
2019-03-05 14:28:29 +01:00
|
|
|
assert(tx->wtx->version == 2);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2020-08-20 08:49:47 +02:00
|
|
|
/* BOLT #3:
|
2017-02-07 02:44:22 +01:00
|
|
|
* * txin count: 1
|
|
|
|
* * `txin[0]` outpoint: `txid` of the commitment transaction and
|
|
|
|
* `output_index` of the matching HTLC output for the HTLC
|
2018-06-17 12:11:53 +02:00
|
|
|
* transaction
|
2021-09-08 02:08:14 +02:00
|
|
|
* * `txin[0]` sequence: `0` (set to `1` for `option_anchors`)
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-03-19 16:31:19 +01:00
|
|
|
amount = amount_msat_to_sat_round_down(msat);
|
2021-10-13 05:45:36 +02:00
|
|
|
bitcoin_tx_add_input(tx, commit,
|
2020-08-13 19:42:02 +02:00
|
|
|
option_anchor_outputs ? 1 : 0,
|
2020-05-21 21:46:19 +02:00
|
|
|
NULL, amount, NULL, commit_wscript);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
/* BOLT #3:
|
|
|
|
* * txout count: 1
|
2022-09-24 01:37:37 +02:00
|
|
|
* * `txout[0]` amount: the HTLC `amount_msat` divided by 1000
|
|
|
|
* (rounding down) minus fees in satoshis (see
|
|
|
|
* [Fee Calculation](#fee-calculation))
|
2018-06-17 12:11:53 +02:00
|
|
|
* * `txout[0]` script: version-0 P2WSH with witness script as shown
|
|
|
|
* below
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2019-03-19 16:31:19 +01:00
|
|
|
if (!amount_sat_sub(&amount, amount, htlc_fee))
|
2019-02-21 04:45:55 +01:00
|
|
|
abort();
|
|
|
|
|
2019-03-19 16:31:19 +01:00
|
|
|
wscript = bitcoin_wscript_htlc_tx(tx, to_self_delay, revocation_pubkey,
|
|
|
|
local_delayedkey);
|
2022-02-26 01:50:33 +01:00
|
|
|
bitcoin_tx_add_output(tx, scriptpubkey_p2wsh(tmpctx, wscript),
|
2020-05-21 03:57:00 +02:00
|
|
|
wscript, amount);
|
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
|
|
|
tal_free(wscript);
|
|
|
|
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
|
|
|
/* BOLT #3:
|
2018-06-17 12:11:53 +02:00
|
|
|
* * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2021-10-13 05:45:36 +02:00
|
|
|
return htlc_tx(ctx, chainparams, commit,
|
2020-05-21 21:46:19 +02:00
|
|
|
commit_wscript, htlc_msatoshi,
|
2017-08-18 06:43:53 +02:00
|
|
|
to_self_delay,
|
|
|
|
&keyset->self_revocation_key,
|
|
|
|
&keyset->self_delayed_payment_key,
|
2020-08-14 03:30:41 +02:00
|
|
|
htlc_success_fee(feerate_per_kw,
|
|
|
|
option_anchor_outputs),
|
2020-08-13 19:42:02 +02:00
|
|
|
0,
|
|
|
|
option_anchor_outputs);
|
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,
|
2017-11-15 07:18:39 +01:00
|
|
|
const struct pubkey *localhtlckey,
|
|
|
|
const struct pubkey *remotehtlckey,
|
2018-12-03 00:15:06 +01:00
|
|
|
const struct bitcoin_signature *localhtlcsig,
|
|
|
|
const struct bitcoin_signature *remotehtlcsig,
|
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,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
|
|
|
struct sha256 hash;
|
2019-03-21 14:24:43 +01:00
|
|
|
u8 *wscript, **witness;
|
2017-02-07 02:44:22 +01:00
|
|
|
|
|
|
|
sha256(&hash, payment_preimage, sizeof(*payment_preimage));
|
|
|
|
wscript = bitcoin_wscript_htlc_receive(htlc_success,
|
|
|
|
htlc_abstimeout,
|
2017-11-15 07:18:39 +01:00
|
|
|
localhtlckey, remotehtlckey,
|
2020-08-13 19:42:02 +02:00
|
|
|
&hash, revocationkey,
|
|
|
|
option_anchor_outputs);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2019-03-25 11:35:56 +01:00
|
|
|
witness = bitcoin_witness_htlc_success_tx(htlc_success,
|
2017-11-15 07:18:39 +01:00
|
|
|
localhtlcsig, remotehtlcsig,
|
2019-03-21 14:24:43 +01:00
|
|
|
payment_preimage, wscript);
|
2019-08-21 05:52:42 +02:00
|
|
|
bitcoin_tx_input_set_witness(htlc_success, 0, take(witness));
|
2017-02-07 02:44:22 +01:00
|
|
|
tal_free(wscript);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
|
|
|
/* BOLT #3:
|
2018-06-17 12:11:53 +02:00
|
|
|
* * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout
|
2017-02-07 02:44:22 +01:00
|
|
|
*/
|
2021-10-13 05:45:36 +02:00
|
|
|
return htlc_tx(ctx, chainparams, commit,
|
2020-05-21 21:46:19 +02:00
|
|
|
commit_wscript, htlc_msatoshi, to_self_delay,
|
2017-08-18 06:43:53 +02:00
|
|
|
&keyset->self_revocation_key,
|
|
|
|
&keyset->self_delayed_payment_key,
|
2020-08-14 03:30:41 +02:00
|
|
|
htlc_timeout_fee(feerate_per_kw,
|
|
|
|
option_anchor_outputs),
|
2020-08-13 19:42:02 +02:00
|
|
|
cltv_expiry,
|
|
|
|
option_anchor_outputs);
|
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,
|
2017-11-15 07:18:39 +01:00
|
|
|
const struct pubkey *localhtlckey,
|
|
|
|
const struct pubkey *remotehtlckey,
|
2017-02-07 02:44:22 +01:00
|
|
|
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 *localhtlcsig,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct bitcoin_signature *remotehtlcsig,
|
|
|
|
bool option_anchor_outputs)
|
2017-02-07 02:44:22 +01:00
|
|
|
{
|
2019-03-21 14:24:43 +01:00
|
|
|
u8 **witness;
|
2017-02-07 02:44:22 +01:00
|
|
|
u8 *wscript = bitcoin_wscript_htlc_offer(htlc_timeout,
|
2017-11-15 07:18:39 +01:00
|
|
|
localhtlckey, remotehtlckey,
|
2020-08-13 19:42:02 +02:00
|
|
|
payment_hash, revocationkey,
|
|
|
|
option_anchor_outputs);
|
2017-02-07 02:44:22 +01:00
|
|
|
|
2019-03-25 11:35:56 +01:00
|
|
|
witness = bitcoin_witness_htlc_timeout_tx(htlc_timeout, localhtlcsig,
|
|
|
|
remotehtlcsig, wscript);
|
2019-08-21 05:52:42 +02:00
|
|
|
bitcoin_tx_input_set_witness(htlc_timeout, 0, take(witness));
|
2017-02-07 02:44:22 +01:00
|
|
|
tal_free(wscript);
|
|
|
|
}
|
|
|
|
|
2017-08-28 18:02:01 +02:00
|
|
|
u8 *htlc_offered_wscript(const tal_t *ctx,
|
|
|
|
const struct ripemd160 *ripemd,
|
2020-08-13 19:42:02 +02:00
|
|
|
const struct keyset *keyset,
|
|
|
|
bool option_anchor_outputs)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
|
|
|
return bitcoin_wscript_htlc_offer_ripemd160(ctx,
|
2017-11-15 07:18:39 +01:00
|
|
|
&keyset->self_htlc_key,
|
|
|
|
&keyset->other_htlc_key,
|
2017-08-28 18:02:01 +02:00
|
|
|
ripemd,
|
2020-08-13 19:42:02 +02:00
|
|
|
&keyset->self_revocation_key,
|
|
|
|
option_anchor_outputs);
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
bool option_anchor_outputs)
|
2017-08-28 18:02:01 +02:00
|
|
|
{
|
|
|
|
return bitcoin_wscript_htlc_receive_ripemd(ctx,
|
|
|
|
expiry,
|
2017-11-15 07:18:39 +01:00
|
|
|
&keyset->self_htlc_key,
|
|
|
|
&keyset->other_htlc_key,
|
2017-08-28 18:02:01 +02:00
|
|
|
ripemd,
|
2020-08-13 19:42:02 +02:00
|
|
|
&keyset->self_revocation_key,
|
|
|
|
option_anchor_outputs);
|
2017-08-28 18:02:01 +02:00
|
|
|
}
|