commit_tx: expose internals to give access to HTLC witness scripts.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2016-08-18 14:23:45 +09:30
parent ca142a0b47
commit ef77cb8349
2 changed files with 58 additions and 30 deletions

View File

@@ -11,31 +11,33 @@
#include "remove_dust.h" #include "remove_dust.h"
#include <assert.h> #include <assert.h>
static bool add_htlc(struct bitcoin_tx *tx, size_t n, u8 *wscript_for_htlc(const tal_t *ctx,
secp256k1_context *secpctx, secp256k1_context *secpctx,
const struct htlc *h, const struct htlc *h,
const struct pubkey *ourkey, const struct pubkey *our_final,
const struct pubkey *theirkey, const struct pubkey *their_final,
const struct rel_locktime *our_locktime,
const struct rel_locktime *their_locktime,
const struct sha256 *rhash, const struct sha256 *rhash,
const struct rel_locktime *locktime, enum htlc_side side)
u8 *(*scriptpubkeyfn)(const tal_t *,
secp256k1_context *,
const struct pubkey *,
const struct pubkey *,
const struct abs_locktime *,
const struct rel_locktime *,
const struct sha256 *,
const struct sha256 *))
{ {
assert(!tx->output[n].script); u8 *(*fn)(const tal_t *, secp256k1_context *,
const struct pubkey *, const struct pubkey *,
const struct abs_locktime *, const struct rel_locktime *,
const struct sha256 *, const struct sha256 *);
tx->output[n].script = scriptpubkey_p2wsh(tx, /* scripts are different for htlcs offered vs accepted */
scriptpubkeyfn(tx, secpctx, ourkey, theirkey, if (side == htlc_owner(h))
&h->expiry, locktime, rhash, fn = bitcoin_redeem_htlc_send;
&h->rhash)); else
tx->output[n].script_length = tal_count(tx->output[n].script); fn = bitcoin_redeem_htlc_recv;
tx->output[n].amount = h->msatoshis / 1000;
return true; if (side == LOCAL)
return fn(ctx, secpctx, our_final, their_final,
&h->expiry, our_locktime, rhash, &h->rhash);
else
return fn(ctx, secpctx, their_final, our_final,
&h->expiry, their_locktime, rhash, &h->rhash);
} }
struct bitcoin_tx *create_commit_tx(const tal_t *ctx, struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
@@ -58,6 +60,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
uint64_t total; uint64_t total;
const struct pubkey *self, *other; const struct pubkey *self, *other;
const struct rel_locktime *locktime; const struct rel_locktime *locktime;
enum htlc_side htlc_side;
/* Now create commitment tx: one input, two outputs (plus htlcs) */ /* Now create commitment tx: one input, two outputs (plus htlcs) */
tx = bitcoin_tx(ctx, 1, 2 + tal_count(cstate->side[OURS].htlcs) tx = bitcoin_tx(ctx, 1, 2 + tal_count(cstate->side[OURS].htlcs)
@@ -70,10 +73,12 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
/* For our commit tx, our payment is delayed by amount they said */ /* For our commit tx, our payment is delayed by amount they said */
if (side == OURS) { if (side == OURS) {
htlc_side = LOCAL;
self = our_final; self = our_final;
other = their_final; other = their_final;
locktime = their_locktime; locktime = their_locktime;
} else { } else {
htlc_side = REMOTE;
self = their_final; self = their_final;
other = our_final; other = our_final;
locktime = our_locktime; locktime = our_locktime;
@@ -98,20 +103,32 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
total = tx->output[0].amount + tx->output[1].amount; total = tx->output[0].amount + tx->output[1].amount;
num = 2; num = 2;
/* HTLCs this side sent. */
for (i = 0; i < tal_count(cstate->side[side].htlcs); i++) { for (i = 0; i < tal_count(cstate->side[side].htlcs); i++) {
if (!add_htlc(tx, num, secpctx, cstate->side[side].htlcs[i], tx->output[num].script
self, other, rhash, locktime, = scriptpubkey_p2wsh(tx,
bitcoin_redeem_htlc_send)) wscript_for_htlc(tx, secpctx,
return tal_free(tx); cstate->side[side].htlcs[i],
our_final, their_final,
our_locktime, their_locktime,
rhash, htlc_side));
tx->output[num].script_length
= tal_count(tx->output[num].script);
tx->output[num].amount
= cstate->side[side].htlcs[i]->msatoshis / 1000;
total += tx->output[num++].amount; total += tx->output[num++].amount;
} }
/* HTLCs this side has received. */
for (i = 0; i < tal_count(cstate->side[!side].htlcs); i++) { for (i = 0; i < tal_count(cstate->side[!side].htlcs); i++) {
if (!add_htlc(tx, num, secpctx, cstate->side[!side].htlcs[i], tx->output[num].script
self, other, rhash, locktime, = scriptpubkey_p2wsh(tx,
bitcoin_redeem_htlc_recv)) wscript_for_htlc(tx, secpctx,
return tal_free(tx); cstate->side[!side].htlcs[i],
our_final, their_final,
our_locktime, their_locktime,
rhash, htlc_side));
tx->output[num].script_length
= tal_count(tx->output[num].script);
tx->output[num].amount
= cstate->side[!side].htlcs[i]->msatoshis / 1000;
total += tx->output[num++].amount; total += tx->output[num++].amount;
} }
assert(num == tx->output_count); assert(num == tx->output_count);

View File

@@ -2,6 +2,7 @@
#define LIGHTNING_COMMIT_TX_H #define LIGHTNING_COMMIT_TX_H
#include "config.h" #include "config.h"
#include "daemon/channel.h" #include "daemon/channel.h"
#include "daemon/htlc.h"
struct channel_state; struct channel_state;
struct sha256_double; struct sha256_double;
@@ -9,6 +10,16 @@ struct sha256;
struct pubkey; struct pubkey;
struct rel_locktime; struct rel_locktime;
u8 *wscript_for_htlc(const tal_t *ctx,
secp256k1_context *secpctx,
const struct htlc *h,
const struct pubkey *our_final,
const struct pubkey *their_final,
const struct rel_locktime *our_locktime,
const struct rel_locktime *their_locktime,
const struct sha256 *rhash,
enum htlc_side side);
/* Create commitment tx to spend the anchor tx output; doesn't fill in /* Create commitment tx to spend the anchor tx output; doesn't fill in
* input scriptsig. */ * input scriptsig. */
struct bitcoin_tx *create_commit_tx(const tal_t *ctx, struct bitcoin_tx *create_commit_tx(const tal_t *ctx,