2015-06-12 04:56:59 +02:00
|
|
|
#include "bitcoin/pubkey.h"
|
|
|
|
#include "bitcoin/script.h"
|
2015-06-12 04:23:27 +02:00
|
|
|
#include "bitcoin/shadouble.h"
|
|
|
|
#include "bitcoin/tx.h"
|
2015-06-12 04:56:59 +02:00
|
|
|
#include "close_tx.h"
|
2015-06-08 07:14:47 +02:00
|
|
|
#include "permute_tx.h"
|
|
|
|
#include "pkt.h"
|
2015-06-12 04:39:05 +02:00
|
|
|
#include "protobuf_convert.h"
|
2015-06-08 07:14:47 +02:00
|
|
|
|
|
|
|
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
|
|
|
|
OpenChannel *ours,
|
|
|
|
OpenChannel *theirs,
|
2015-07-29 08:44:28 +02:00
|
|
|
OpenAnchor *anchor,
|
|
|
|
uint64_t to_us, uint64_t to_them)
|
2015-06-08 07:14:47 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
const u8 *redeemscript;
|
|
|
|
struct pubkey ourkey, theirkey;
|
|
|
|
struct sha256 redeem;
|
|
|
|
|
|
|
|
/* Now create close tx: one input, two outputs. */
|
|
|
|
tx = bitcoin_tx(ctx, 1, 2);
|
|
|
|
|
|
|
|
/* Our input spends the anchor tx output. */
|
2015-07-29 08:44:28 +02:00
|
|
|
proto_to_sha256(anchor->txid, &tx->input[0].txid.sha);
|
|
|
|
tx->input[0].index = anchor->output_index;
|
|
|
|
tx->input[0].input_amount = anchor->amount;
|
2015-06-08 07:14:47 +02:00
|
|
|
|
|
|
|
/* Outputs goes to final pubkey */
|
2015-07-29 04:30:50 +02:00
|
|
|
if (!proto_to_pubkey(ours->final_key, &ourkey))
|
2015-06-08 07:14:47 +02:00
|
|
|
return tal_free(tx);
|
2015-07-29 04:30:50 +02:00
|
|
|
if (!proto_to_pubkey(theirs->final_key, &theirkey))
|
2015-06-08 07:14:47 +02:00
|
|
|
return tal_free(tx);
|
|
|
|
|
2015-06-09 06:59:04 +02:00
|
|
|
|
2015-06-08 07:14:47 +02:00
|
|
|
proto_to_sha256(ours->revocation_hash, &redeem);
|
|
|
|
|
|
|
|
/* One output is to us. */
|
2015-07-29 08:44:28 +02:00
|
|
|
tx->output[0].amount = to_us;
|
2015-06-08 07:14:47 +02:00
|
|
|
redeemscript = bitcoin_redeem_single(tx, &ourkey);
|
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);
|
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
|
|
|
|
|
|
|
/* Other output is to them. */
|
2015-07-29 08:44:28 +02:00
|
|
|
tx->output[1].amount = to_them;
|
2015-06-08 07:14:47 +02:00
|
|
|
redeemscript = bitcoin_redeem_single(tx, &theirkey);
|
|
|
|
tx->output[1].script = scriptpubkey_p2sh(tx, redeemscript);
|
|
|
|
tx->output[1].script_length = tal_count(tx->output[1].script);
|
|
|
|
|
2015-07-29 08:44:28 +02:00
|
|
|
assert(tx->output[0].amount + tx->output[1].amount
|
|
|
|
<= tx->input[0].input_amount);
|
|
|
|
tx->fee = tx->input[0].input_amount
|
|
|
|
- (tx->output[0].amount + tx->output[1].amount);
|
|
|
|
|
2015-07-20 02:26:15 +02:00
|
|
|
permute_outputs(tx->output, 2, NULL);
|
2015-06-08 07:14:47 +02:00
|
|
|
return tx;
|
|
|
|
}
|