MCPcopy Create free account
hub / github.com/ElementsProject/lightning / create_close_tx

Function create_close_tx

common/close_tx.c:9–75  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7#include <common/utils.h>
8
9struct bitcoin_tx *create_close_tx(const tal_t *ctx,
10 const struct chainparams *chainparams,
11 u32 *local_wallet_index,
12 const struct ext_key *local_wallet_ext_key,
13 const u8 *our_script,
14 const u8 *their_script,
15 const u8 *funding_wscript,
16 const struct bitcoin_outpoint *funding,
17 struct amount_sat funding_sats,
18 struct amount_sat to_us,
19 struct amount_sat to_them,
20 struct amount_sat dust_limit)
21{
22 struct bitcoin_tx *tx;
23 size_t num_outputs = 0;
24 struct amount_sat total_out;
25 u8 *script;
26
27 assert(amount_sat_add(&total_out, to_us, to_them));
28 assert(amount_sat_less_eq(total_out, funding_sats));
29
30 /* BOLT #3:
31 *
32 * ## Closing Transaction
33 *
34 * Note that there are two possible variants for each node.
35 *
36 * * version: 2
37 * * locktime: 0
38 * * txin count: 1
39 */
40 /* Now create close tx: one input, two outputs. */
41 tx = bitcoin_tx(ctx, chainparams, 1, 2, 0);
42
43 /* Our input spends the anchor tx output. */
44 bitcoin_tx_add_input(tx, funding,
45 BITCOIN_TX_DEFAULT_SEQUENCE, NULL,
46 funding_sats, NULL, funding_wscript);
47
48 if (amount_sat_greater_eq(to_us, dust_limit)) {
49 script = tal_dup_talarr(tx, u8, our_script);
50 /* One output is to us. */
51 bitcoin_tx_add_output(tx, script, NULL, to_us);
52 assert((local_wallet_index == NULL) == (local_wallet_ext_key == NULL));
53 if (local_wallet_index)
54 psbt_add_keypath_to_last_output(
55 tx, *local_wallet_index, local_wallet_ext_key);
56 num_outputs++;
57 }
58
59 if (amount_sat_greater_eq(to_them, dust_limit)) {
60 script = tal_dup_talarr(tx, u8, their_script);
61 /* Other output is to them. */
62 bitcoin_tx_add_output(tx, script, NULL, to_them);
63 num_outputs++;
64 }
65
66 /* Can't have no outputs at all! */

Callers 3

close_txFunction · 0.85
runFunction · 0.85

Calls 11

amount_sat_less_eqFunction · 0.85
bitcoin_tx_add_inputFunction · 0.85
bitcoin_tx_add_outputFunction · 0.85
tal_freeFunction · 0.85
permute_outputsFunction · 0.85
bitcoin_tx_finalizeFunction · 0.85
bitcoin_tx_checkFunction · 0.85
amount_sat_addFunction · 0.70
bitcoin_txClass · 0.70
amount_sat_greater_eqFunction · 0.70

Tested by

no test coverage detected