2015-06-12 12:26:59 +09:30
|
|
|
#include "bitcoin/script.h"
|
2015-06-12 11:53:27 +09:30
|
|
|
#include "bitcoin/tx.h"
|
2015-06-12 12:26:59 +09:30
|
|
|
#include "close_tx.h"
|
2015-06-08 14:44:47 +09:30
|
|
|
#include "permute_tx.h"
|
2015-06-12 12:09:05 +09:30
|
|
|
#include "protobuf_convert.h"
|
2015-06-08 14:44:47 +09:30
|
|
|
|
2016-12-02 18:12:58 +10:30
|
|
|
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
|
2016-04-24 20:01:52 +09:30
|
|
|
const u8 *our_script,
|
|
|
|
const u8 *their_script,
|
2016-01-22 06:41:47 +10:30
|
|
|
const struct sha256_double *anchor_txid,
|
|
|
|
unsigned int anchor_index,
|
|
|
|
u64 anchor_satoshis,
|
2015-07-29 16:14:28 +09:30
|
|
|
uint64_t to_us, uint64_t to_them)
|
2015-06-08 14:44:47 +09:30
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
|
|
|
|
/* Now create close tx: one input, two outputs. */
|
|
|
|
tx = bitcoin_tx(ctx, 1, 2);
|
|
|
|
|
|
|
|
/* Our input spends the anchor tx output. */
|
2016-01-22 06:41:47 +10:30
|
|
|
tx->input[0].txid = *anchor_txid;
|
|
|
|
tx->input[0].index = anchor_index;
|
2016-04-11 16:43:53 +09:30
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
|
2015-06-08 14:44:47 +09:30
|
|
|
|
|
|
|
/* One output is to us. */
|
2015-07-29 16:14:28 +09:30
|
|
|
tx->output[0].amount = to_us;
|
2016-04-24 20:01:52 +09:30
|
|
|
tx->output[0].script = tal_dup_arr(tx, u8,
|
|
|
|
our_script, tal_count(our_script), 0);
|
2015-06-08 14:44:47 +09:30
|
|
|
|
|
|
|
/* Other output is to them. */
|
2015-07-29 16:14:28 +09:30
|
|
|
tx->output[1].amount = to_them;
|
2016-04-24 20:01:52 +09:30
|
|
|
tx->output[1].script = tal_dup_arr(tx, u8,
|
|
|
|
their_script, tal_count(their_script),
|
|
|
|
0);
|
2015-06-08 14:44:47 +09:30
|
|
|
|
2016-04-11 16:32:43 +09:30
|
|
|
assert(tx->output[0].amount + tx->output[1].amount <= anchor_satoshis);
|
2015-07-29 16:14:28 +09:30
|
|
|
|
2017-02-07 12:14:22 +10:30
|
|
|
permute_outputs(tx->output, 2, NULL);
|
2015-06-08 14:44:47 +09:30
|
|
|
return tx;
|
|
|
|
}
|