forked from neocogent/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
close_tx.c
53 lines (45 loc) · 1.4 KB
/
close_tx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "bitcoin/script.h"
#include "bitcoin/tx.h"
#include "close_tx.h"
#include "permute_tx.h"
#include "protobuf_convert.h"
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *our_script,
const u8 *their_script,
const struct sha256_double *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them,
uint64_t dust_limit)
{
struct bitcoin_tx *tx;
size_t num_outputs = 0;
assert(to_us + to_them <= anchor_satoshis);
/* Now create close tx: one input, two outputs. */
tx = bitcoin_tx(ctx, 1, 2);
/* Our input spends the anchor tx output. */
tx->input[0].txid = *anchor_txid;
tx->input[0].index = anchor_index;
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
if (to_us >= dust_limit) {
/* One output is to us. */
tx->output[num_outputs].amount = to_us;
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
our_script, tal_count(our_script), 0);
num_outputs++;
}
if (to_them >= dust_limit) {
/* Other output is to them. */
tx->output[num_outputs].amount = to_them;
tx->output[num_outputs].script = tal_dup_arr(tx, u8,
their_script, tal_count(their_script),
0);
num_outputs++;
}
/* Can't have no outputs at all! */
if (num_outputs == 0)
return tal_free(tx);
tal_resize(&tx->output, num_outputs);
permute_outputs(tx->output, num_outputs, NULL);
return tx;
}