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"
|
2015-06-12 04:39:05 +02:00
|
|
|
#include "protobuf_convert.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,
|
|
|
|
const UpdateAddHtlc *h,
|
|
|
|
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 *))
|
|
|
|
{
|
2016-01-21 21:08:08 +01:00
|
|
|
struct abs_locktime htlc_abstime;
|
2015-08-07 05:16:46 +02:00
|
|
|
struct sha256 htlc_rhash;
|
|
|
|
|
|
|
|
assert(!tx->output[n].script);
|
|
|
|
|
|
|
|
/* This shouldn't happen... */
|
|
|
|
if (!proto_to_abs_locktime(h->expiry, &htlc_abstime))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
proto_to_sha256(h->r_hash, &htlc_rhash);
|
|
|
|
tx->output[n].script = scriptpubkey_p2sh(tx,
|
2015-09-24 07:26:01 +02:00
|
|
|
scriptpubkeyfn(tx, ourkey, theirkey,
|
2016-01-21 21:08:08 +01:00
|
|
|
&htlc_abstime, locktime, rhash,
|
2015-08-07 05:16:46 +02:00
|
|
|
&htlc_rhash));
|
|
|
|
tx->output[n].script_length = tal_count(tx->output[n].script);
|
2015-09-24 07:30:47 +02:00
|
|
|
tx->output[n].amount = h->amount_msat / 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,
|
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: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;
|
|
|
|
tx->input[0].input_amount = anchor_satoshis;
|
2015-06-12 05:23:49 +02:00
|
|
|
|
2015-05-30 13:14:00 +02:00
|
|
|
/* First output is a P2SH 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);
|
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-09-24 07:30:47 +02:00
|
|
|
tx->output[0].amount = cstate->a.pay_msat / 1000;
|
2015-05-30 13:14:00 +02:00
|
|
|
|
2015-06-05 04:07:27 +02:00
|
|
|
/* Second output is a P2SH payment to them. */
|
2016-01-21 21:11:47 +01:00
|
|
|
tx->output[1].script = scriptpubkey_p2sh(tx,
|
|
|
|
bitcoin_redeem_single(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],
|
|
|
|
our_final, their_final,
|
|
|
|
rhash, their_locktime, scriptpubkey_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],
|
|
|
|
our_final, their_final,
|
|
|
|
rhash, their_locktime, scriptpubkey_htlc_recv))
|
2015-08-07 05:16:46 +02:00
|
|
|
return tal_free(tx);
|
|
|
|
total += tx->output[num++].amount;
|
|
|
|
}
|
|
|
|
assert(num == tx->output_count);
|
|
|
|
|
2015-06-25 06:20:16 +02:00
|
|
|
/* Calculate fee; difference of inputs and outputs. */
|
2015-08-07 05:16:46 +02:00
|
|
|
assert(total <= tx->input[0].input_amount);
|
|
|
|
tx->fee = tx->input[0].input_amount - total;
|
2015-07-29 08:44:28 +02:00
|
|
|
|
2015-08-07 05:16:46 +02:00
|
|
|
permute_outputs(tx->output, tx->output_count, NULL);
|
2015-05-30 13:14:00 +02:00
|
|
|
return tx;
|
|
|
|
}
|