diff --git a/bitcoin/script.c b/bitcoin/script.c index 2ca0e1da3..cbf45858b 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -327,13 +327,24 @@ u8 *scriptpubkey_witness_raw(const tal_t *ctx, u8 version, * block csv lock. * OP_CHECKSIGVERIFY 1 OP_CHECKSEQUENCEVERIFY */ +/* BOLT- #3 + * ##### Leased channel (`option_will_fund`) + * + * If a `lease` applies to the channel, the `to_remote` output + * of the `initiator` ensures the `leasor` funds are not + * spendable until the lease expires. + * + * OP_CHECKSIGVERIFY MAX(1, lease_end - blockheight) OP_CHECKSEQUENCEVERIFY + */ + u8 *anchor_to_remote_redeem(const tal_t *ctx, - const struct pubkey *remote_key) + const struct pubkey *remote_key, + u32 csv_lock) { u8 *script = tal_arr(ctx, u8, 0); add_push_key(&script, remote_key); add_op(&script, OP_CHECKSIGVERIFY); - add_number(&script, 1); + add_number(&script, csv_lock); add_op(&script, OP_CHECKSEQUENCEVERIFY); assert(is_anchor_witness_script(script, tal_bytelen(script))); diff --git a/bitcoin/script.h b/bitcoin/script.h index 939ecdc9c..351bbcda0 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -69,7 +69,8 @@ u8 *scriptpubkey_witness_raw(const tal_t *ctx, u8 version, /* To-remotekey with csv 1 delay. */ u8 *anchor_to_remote_redeem(const tal_t *ctx, - const struct pubkey *remote_key); + const struct pubkey *remote_key, + u32 csv_lock); /* Create a witness which spends the 2of2. */ u8 **bitcoin_witness_2of2(const tal_t *ctx, diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index 1b4fd94cf..77ad07c0b 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -276,7 +276,7 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, */ if (option_anchor_outputs) { scriptpubkey = scriptpubkey_p2wsh(tmpctx, - anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key)); + anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key, 1)); } else { scriptpubkey = scriptpubkey_p2wpkh(tmpctx, &keyset->other_payment_key); diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index c18201b42..792b32be4 100644 --- a/common/initial_commit_tx.c +++ b/common/initial_commit_tx.c @@ -242,7 +242,7 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, amount = amount_msat_to_sat_round_down(other_pay); if (option_anchor_outputs) { scriptpubkey = scriptpubkey_p2wsh(tmpctx, - anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key)); + anchor_to_remote_redeem(tmpctx, &keyset->other_payment_key, 1)); } else { scriptpubkey = scriptpubkey_p2wpkh(tmpctx, &keyset->other_payment_key); diff --git a/hsmd/libhsmd.c b/hsmd/libhsmd.c index 944982f1d..5d026fbe5 100644 --- a/hsmd/libhsmd.c +++ b/hsmd/libhsmd.c @@ -388,7 +388,7 @@ static void sign_our_inputs(struct utxo **utxos, struct wally_psbt *psbt) /* It's actually a P2WSH in this case. */ if (utxo->close_info && utxo->close_info->option_anchor_outputs) { - const u8 *wscript = anchor_to_remote_redeem(tmpctx, &pubkey); + const u8 *wscript = anchor_to_remote_redeem(tmpctx, &pubkey, 1); psbt_input_set_witscript(psbt, j, wscript); psbt_input_set_wit_utxo(psbt, j, scriptpubkey_p2wsh(psbt, wscript), diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 22b6413ce..2cc8f01f3 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -2557,7 +2557,8 @@ static void get_anchor_scriptpubkeys(const tal_t *ctx, u8 **anchor) } static u8 *scriptpubkey_to_remote(const tal_t *ctx, - const struct pubkey *remotekey) + const struct pubkey *remotekey, + u32 csv_lock) { /* BOLT #3: * @@ -2574,7 +2575,8 @@ static u8 *scriptpubkey_to_remote(const tal_t *ctx, if (option_anchor_outputs) { return scriptpubkey_p2wsh(ctx, anchor_to_remote_redeem(tmpctx, - remotekey)); + remotekey, + csv_lock)); } else { return scriptpubkey_p2wpkh(ctx, remotekey); } @@ -2649,7 +2651,8 @@ static void handle_our_unilateral(const struct tx_parts *tx, /* Figure out what direct to-them output looks like. */ script[REMOTE] = scriptpubkey_to_remote(tmpctx, - &keyset->other_payment_key); + &keyset->other_payment_key, + 1); /* Calculate all the HTLC scripts so we can match them */ htlc_scripts = derive_htlc_scripts(htlcs, LOCAL); @@ -3087,7 +3090,7 @@ static void handle_their_cheat(const struct tx_parts *tx, /* Figure out what direct to-us output looks like. */ script[LOCAL] = scriptpubkey_to_remote(tmpctx, - &keyset->other_payment_key); + &keyset->other_payment_key, 1); /* Calculate all the HTLC scripts so we can match them */ htlc_scripts = derive_htlc_scripts(htlcs, REMOTE); @@ -3369,7 +3372,7 @@ static void handle_their_unilateral(const struct tx_parts *tx, /* Figure out what direct to-us output looks like. */ script[LOCAL] = scriptpubkey_to_remote(tmpctx, - &keyset->other_payment_key); + &keyset->other_payment_key, 1); /* Calculate all the HTLC scripts so we can match them */ htlc_scripts = derive_htlc_scripts(htlcs, REMOTE); @@ -3624,7 +3627,8 @@ static void handle_unknown_commitment(const struct tx_parts *tx, /* Other possible local script is for option_static_remotekey */ local_scripts[1] = scriptpubkey_to_remote(tmpctx, - &basepoints[LOCAL].payment); + &basepoints[LOCAL].payment, + 1); for (size_t i = 0; i < tal_count(tx->outputs); i++) { struct tracked_output *out;