2016-01-21 21:08:08 +01:00
|
|
|
#include "bitcoin/locktime.h"
|
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"
|
2016-05-02 08:36:56 +02:00
|
|
|
#include "remove_dust.h"
|
2016-01-21 21:11:47 +01:00
|
|
|
#include <assert.h>
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2015-08-07 05:16:46 +02:00
|
|
|
static bool add_htlc(struct bitcoin_tx *tx, size_t n,
|
2016-01-21 21:11:47 +01:00
|
|
|
const struct channel_htlc *h,
|
2015-08-07 05:16:46 +02:00
|
|
|
const struct pubkey *ourkey,
|
|
|
|
const struct pubkey *theirkey,
|
|
|
|
const struct sha256 *rhash,
|
2016-01-21 21:08:08 +01:00
|
|
|
const struct rel_locktime *locktime,
|
2015-08-07 05:16:46 +02:00
|
|
|
u8 *(*scriptpubkeyfn)(const tal_t *,
|
|
|
|
const struct pubkey *,
|
|
|
|
const struct pubkey *,
|
2016-01-21 21:08:08 +01:00
|
|
|
const struct abs_locktime *,
|
|
|
|
const struct rel_locktime *,
|
2015-08-07 05:16:46 +02:00
|
|
|
const struct sha256 *,
|
|
|
|
const struct sha256 *))
|
|
|
|
{
|
|
|
|
assert(!tx->output[n].script);
|
|
|
|
|
2016-04-24 12:31:51 +02:00
|
|
|
tx->output[n].script = scriptpubkey_p2wsh(tx,
|
2015-09-24 07:26:01 +02:00
|
|
|
scriptpubkeyfn(tx, ourkey, theirkey,
|
2016-01-21 21:11:47 +01:00
|
|
|
&h->expiry, locktime, rhash,
|
|
|
|
&h->rhash));
|
2015-08-07 05:16:46 +02:00
|
|
|
tx->output[n].script_length = tal_count(tx->output[n].script);
|
2016-01-21 21:11:47 +01:00
|
|
|
tx->output[n].amount = h->msatoshis / 1000;
|
2015-08-07 05:16:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-05-30 13:14:00 +02:00
|
|
|
struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
|
2016-01-21 21:11:47 +01:00
|
|
|
const struct pubkey *our_final,
|
|
|
|
const struct pubkey *their_final,
|
|
|
|
const struct rel_locktime *their_locktime,
|
|
|
|
const struct sha256_double *anchor_txid,
|
|
|
|
unsigned int anchor_index,
|
|
|
|
u64 anchor_satoshis,
|
2015-06-08 22:29:06 +02:00
|
|
|
const struct sha256 *rhash,
|
2016-05-02 08:35:56 +02:00
|
|
|
const struct channel_state *cstate,
|
|
|
|
int **map)
|
2015-05-30 13:14:00 +02:00
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
const u8 *redeemscript;
|
2015-08-07 05:16:46 +02:00
|
|
|
size_t i, num;
|
|
|
|
uint64_t total;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2015-08-07 05:16:46 +02:00
|
|
|
/* Now create commitment tx: one input, two outputs (plus htlcs) */
|
|
|
|
tx = bitcoin_tx(ctx, 1, 2 + tal_count(cstate->a.htlcs)
|
|
|
|
+ tal_count(cstate->b.htlcs));
|
2015-05-30 13:14:00 +02:00
|
|
|
|
|
|
|
/* Our input spends the anchor tx output. */
|
2016-01-21 21:11:47 +01:00
|
|
|
tx->input[0].txid = *anchor_txid;
|
|
|
|
tx->input[0].index = anchor_index;
|
2016-04-11 09:13:53 +02:00
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis);
|
2015-06-12 05:23:49 +02:00
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
/* First output is a P2WSH to a complex redeem script (usu. for me) */
|
2016-01-21 21:11:47 +01:00
|
|
|
redeemscript = bitcoin_redeem_secret_or_delay(tx, our_final,
|
|
|
|
their_locktime,
|
|
|
|
their_final,
|
2015-07-24 08:30:10 +02:00
|
|
|
rhash);
|
2016-04-24 12:27:35 +02:00
|
|
|
tx->output[0].script = scriptpubkey_p2wsh(tx, redeemscript);
|
2015-05-30 13:14:00 +02:00
|
|
|
tx->output[0].script_length = tal_count(tx->output[0].script);
|
2015-09-24 07:30:47 +02:00
|
|
|
tx->output[0].amount = cstate->a.pay_msat / 1000;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2016-04-24 12:27:35 +02:00
|
|
|
/* Second output is a P2WPKH payment to them. */
|
|
|
|
tx->output[1].script = scriptpubkey_p2wpkh(tx, their_final);
|
2015-06-05 04:07:27 +02:00
|
|
|
tx->output[1].script_length = tal_count(tx->output[1].script);
|
2015-09-24 07:30:47 +02:00
|
|
|
tx->output[1].amount = cstate->b.pay_msat / 1000;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2015-08-07 05:16:46 +02:00
|
|
|
/* First two outputs done, now for the HTLCs. */
|
|
|
|
total = tx->output[0].amount + tx->output[1].amount;
|
|
|
|
num = 2;
|
|
|
|
|
|
|
|
/* HTLCs we've sent. */
|
|
|
|
for (i = 0; i < tal_count(cstate->a.htlcs); i++) {
|
2016-01-21 21:11:47 +01:00
|
|
|
if (!add_htlc(tx, num, &cstate->a.htlcs[i],
|
2016-01-21 21:11:47 +01:00
|
|
|
our_final, their_final,
|
2016-05-03 03:38:20 +02:00
|
|
|
rhash, their_locktime, bitcoin_redeem_htlc_send))
|
2015-08-07 05:16:46 +02:00
|
|
|
return tal_free(tx);
|
|
|
|
total += tx->output[num++].amount;
|
|
|
|
}
|
|
|
|
/* HTLCs we've received. */
|
|
|
|
for (i = 0; i < tal_count(cstate->b.htlcs); i++) {
|
2016-01-21 21:11:47 +01:00
|
|
|
if (!add_htlc(tx, num, &cstate->b.htlcs[i],
|
2016-01-21 21:11:47 +01:00
|
|
|
our_final, their_final,
|
2016-05-03 03:38:20 +02:00
|
|
|
rhash, their_locktime, bitcoin_redeem_htlc_recv))
|
2015-08-07 05:16:46 +02:00
|
|
|
return tal_free(tx);
|
|
|
|
total += tx->output[num++].amount;
|
|
|
|
}
|
|
|
|
assert(num == tx->output_count);
|
2016-04-11 09:02:43 +02:00
|
|
|
assert(total <= anchor_satoshis);
|
2016-05-02 08:35:56 +02:00
|
|
|
|
|
|
|
*map = tal_arr(ctx, int, tx->output_count);
|
|
|
|
permute_outputs(tx->output, tx->output_count, *map);
|
2016-05-02 08:36:56 +02:00
|
|
|
remove_dust(tx, *map);
|
|
|
|
|
2015-05-30 13:14:00 +02:00
|
|
|
return tx;
|
|
|
|
}
|