Skip to content

Commit

Permalink
onchaind: recognize the to-remote output if option_anchor_outputs.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Aug 14, 2020
1 parent a730cbd commit c753561
Showing 1 changed file with 32 additions and 14 deletions.
46 changes: 32 additions & 14 deletions onchaind/onchaind.c
Original file line number Diff line number Diff line change
Expand Up @@ -2172,6 +2172,30 @@ static void get_anchor_scriptpubkeys(const tal_t *ctx, u8 **anchor)
}
}

static u8 *scriptpubkey_to_remote(const tal_t *ctx,
const struct pubkey *remotekey)
{
/* BOLT-a12da24dd0102c170365124782b46d9710950ac1 #3:
*
* #### `to_remote` Output
*
* If `option_anchor_outputs` applies to the commitment
* transaction, the `to_remote` output is encumbered by a one
* block csv lock.
* <remote_pubkey> OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY
*
*...
* Otherwise, this output is a simple P2WPKH to `remotepubkey`.
*/
if (option_anchor_outputs) {
return scriptpubkey_p2wsh(ctx,
anchor_to_remote_redeem(tmpctx,
remotekey));
} else {
return scriptpubkey_p2wpkh(ctx, remotekey);
}
}

static void handle_our_unilateral(const struct tx_parts *tx,
u32 tx_blockheight,
const struct basepoints basepoints[NUM_SIDES],
Expand Down Expand Up @@ -2240,7 +2264,8 @@ static void handle_our_unilateral(const struct tx_parts *tx,
script[LOCAL] = scriptpubkey_p2wsh(tmpctx, local_wscript);

/* Figure out what direct to-them output looks like. */
script[REMOTE] = scriptpubkey_p2wpkh(tmpctx, &keyset->other_payment_key);
script[REMOTE] = scriptpubkey_to_remote(tmpctx,
&keyset->other_payment_key);

/* Calculate all the HTLC scripts so we can match them */
htlc_scripts = derive_htlc_scripts(htlcs, LOCAL);
Expand Down Expand Up @@ -2675,7 +2700,8 @@ static void handle_their_cheat(const struct tx_parts *tx,
script[REMOTE] = scriptpubkey_p2wsh(tmpctx, remote_wscript);

/* Figure out what direct to-us output looks like. */
script[LOCAL] = scriptpubkey_p2wpkh(tmpctx, &keyset->other_payment_key);
script[LOCAL] = scriptpubkey_to_remote(tmpctx,
&keyset->other_payment_key);

/* Calculate all the HTLC scripts so we can match them */
htlc_scripts = derive_htlc_scripts(htlcs, REMOTE);
Expand Down Expand Up @@ -2956,7 +2982,8 @@ static void handle_their_unilateral(const struct tx_parts *tx,
script[REMOTE] = scriptpubkey_p2wsh(tmpctx, remote_wscript);

/* Figure out what direct to-us output looks like. */
script[LOCAL] = scriptpubkey_p2wpkh(tmpctx, &keyset->other_payment_key);
script[LOCAL] = scriptpubkey_to_remote(tmpctx,
&keyset->other_payment_key);

/* Calculate all the HTLC scripts so we can match them */
htlc_scripts = derive_htlc_scripts(htlcs, REMOTE);
Expand Down Expand Up @@ -3211,17 +3238,8 @@ static void handle_unknown_commitment(const struct tx_parts *tx,
local_script = scriptpubkey_p2wpkh(tmpctx,
&ks->other_payment_key);
} else {
/* BOLT #3:
*
* ### `remotepubkey` Derivation
*
* If `option_static_remotekey` is negotiated the
* `remotepubkey` is simply the remote node's
* `payment_basepoint`, otherwise it is calculated as above
* using the remote node's `payment_basepoint`.
*/
local_script = scriptpubkey_p2wpkh(tmpctx,
&basepoints[LOCAL].payment);
local_script = scriptpubkey_to_remote(tmpctx,
&basepoints[LOCAL].payment);
}

for (size_t i = 0; i < tal_count(tx->outputs); i++) {
Expand Down

0 comments on commit c753561

Please sign in to comment.