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 "commit_tx.h"
|
2015-08-07 05:15:30 +02:00
|
|
|
#include "funding.h"
|
2015-06-25 06:20:16 +02:00
|
|
|
#include "overflows.h"
|
2015-05-30 13:14:00 +02:00
|
|
|
#include "permute_tx.h"
|
2015-06-02 06:08:38 +02:00
|
|
|
#include "pkt.h"
|
2015-06-12 04:39:05 +02:00
|
|
|
#include "protobuf_convert.h"
|
2015-05-30 13:14:00 +02:00
|
|
|
|
|
|
|
struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
|
|
|
|
OpenChannel *ours,
|
|
|
|
OpenChannel *theirs,
|
2015-07-29 08:44:28 +02:00
|
|
|
OpenAnchor *anchor,
|
2015-06-08 22:29:06 +02:00
|
|
|
const struct sha256 *rhash,
|
2015-08-07 05:15:30 +02:00
|
|
|
const struct channel_state *cstate)
|
2015-05-30 13:14:00 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
const u8 *redeemscript;
|
2015-08-07 05:15:20 +02:00
|
|
|
struct pubkey ourkey, theirkey;
|
2015-06-12 05:23:49 +02:00
|
|
|
u32 locktime;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
|
|
|
/* Now create commitment 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-05-30 13:14:00 +02:00
|
|
|
|
2015-06-07 22:59:15 +02:00
|
|
|
/* Output goes to our final pubkeys */
|
2015-07-29 04:30:50 +02:00
|
|
|
if (!proto_to_pubkey(ours->final_key, &ourkey))
|
2015-06-02 06:03:21 +02:00
|
|
|
return tal_free(tx);
|
2015-07-29 04:30:50 +02:00
|
|
|
if (!proto_to_pubkey(theirs->final_key, &theirkey))
|
2015-06-02 06:03:21 +02:00
|
|
|
return tal_free(tx);
|
2015-06-07 22:59:15 +02:00
|
|
|
|
2015-08-07 05:15:30 +02:00
|
|
|
if (!proto_to_rel_locktime(theirs->delay, &locktime))
|
2015-06-12 05:23:49 +02:00
|
|
|
return tal_free(tx);
|
|
|
|
|
2015-05-30 13:14:00 +02:00
|
|
|
/* First output is a P2SH to a complex redeem script (usu. for me) */
|
2015-07-24 08:30:10 +02:00
|
|
|
redeemscript = bitcoin_redeem_secret_or_delay(tx, &ourkey,
|
|
|
|
locktime,
|
|
|
|
&theirkey,
|
|
|
|
rhash);
|
2015-05-30 13:14:00 +02:00
|
|
|
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);
|
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
2015-08-07 05:15:30 +02:00
|
|
|
tx->output[0].amount = cstate->a.pay;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2015-06-05 04:07:27 +02:00
|
|
|
/* Second output is a P2SH payment to them. */
|
|
|
|
tx->output[1].script = scriptpubkey_p2sh(ctx,
|
|
|
|
bitcoin_redeem_single(ctx,
|
2015-08-07 05:15:20 +02:00
|
|
|
&theirkey));
|
2015-06-05 04:07:27 +02:00
|
|
|
tx->output[1].script_length = tal_count(tx->output[1].script);
|
2015-08-07 05:15:30 +02:00
|
|
|
tx->output[1].amount = cstate->b.pay;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2015-06-25 06:20:16 +02:00
|
|
|
/* Calculate fee; difference of inputs and outputs. */
|
2015-07-29 08:44:28 +02:00
|
|
|
assert(tx->output[0].amount + tx->output[1].amount
|
|
|
|
<= tx->input[0].input_amount);
|
2015-06-25 06:20:16 +02:00
|
|
|
tx->fee = tx->input[0].input_amount
|
|
|
|
- (tx->output[0].amount + tx->output[1].amount);
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2015-07-20 02:26:15 +02:00
|
|
|
permute_outputs(tx->output, 2, NULL);
|
2015-05-30 13:14:00 +02:00
|
|
|
return tx;
|
|
|
|
}
|