Skip to content

Commit 1b902a6

Browse files
committed
Added upfront_shutdown_wallet_index to support validation of ready_channel
1 parent 5a2d6e6 commit 1b902a6

File tree

9 files changed

+49
-5
lines changed

9 files changed

+49
-5
lines changed

contrib/remote_hsmd/hsmd.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ static struct io_plan *handle_ready_channel(struct io_conn *conn,
13331333
u16 funding_txout;
13341334
u16 local_to_self_delay;
13351335
u8 *local_shutdown_script;
1336+
u32 local_shutdown_wallet_index;
13361337
struct basepoints remote_basepoints;
13371338
struct pubkey remote_funding_pubkey;
13381339
u16 remote_to_self_delay;
@@ -1344,6 +1345,7 @@ static struct io_plan *handle_ready_channel(struct io_conn *conn,
13441345
&channel_value, &push_value, &funding_txid,
13451346
&funding_txout, &local_to_self_delay,
13461347
&local_shutdown_script,
1348+
&local_shutdown_wallet_index,
13471349
&remote_basepoints,
13481350
&remote_funding_pubkey,
13491351
&remote_to_self_delay,
@@ -1361,6 +1363,7 @@ static struct io_plan *handle_ready_channel(struct io_conn *conn,
13611363
funding_txout,
13621364
local_to_self_delay, // locally imposed on counterparty to_self outputs
13631365
local_shutdown_script,
1366+
local_shutdown_wallet_index,
13641367
&remote_basepoints,
13651368
&remote_funding_pubkey,
13661369
remote_to_self_delay, // counterparty imposed on our to_self outputs

contrib/remote_hsmd/proxy.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ proxy_stat proxy_handle_ready_channel(
569569
u16 funding_txout,
570570
u16 holder_to_self_delay,
571571
u8 *holder_shutdown_script,
572+
u32 holder_shutdown_wallet_index,
572573
struct basepoints *counterparty_basepoints,
573574
struct pubkey *counterparty_funding_pubkey,
574575
u16 counterparty_to_self_delay,
@@ -582,7 +583,9 @@ proxy_stat proxy_handle_ready_channel(
582583
"\"is_outbound\":%s, \"channel_value\":%" PRIu64 ", "
583584
"\"push_value\":%" PRIu64 ", "
584585
"\"funding_txid\":%s, \"funding_txout\":%d, "
585-
"\"holder_to_self_delay\":%d, \"holder_shutdown_script\":%s, "
586+
"\"holder_to_self_delay\":%d, "
587+
"\"holder_shutdown_script\":%s, "
588+
"\"holder_shutdown_wallet_index\":%d, "
586589
"\"counterparty_basepoints\":%s, "
587590
"\"counterparty_funding_pubkey\":%s, "
588591
"\"counterparty_to_self_delay\":%d, "
@@ -601,6 +604,7 @@ proxy_stat proxy_handle_ready_channel(
601604
holder_to_self_delay,
602605
dump_hex(holder_shutdown_script,
603606
tal_count(holder_shutdown_script)).c_str(),
607+
holder_shutdown_wallet_index,
604608
dump_basepoints(counterparty_basepoints).c_str(),
605609
dump_pubkey(counterparty_funding_pubkey).c_str(),
606610
counterparty_to_self_delay,
@@ -622,6 +626,8 @@ proxy_stat proxy_handle_ready_channel(
622626
req.set_holder_selected_contest_delay(holder_to_self_delay);
623627
marshal_script(holder_shutdown_script,
624628
req.mutable_holder_shutdown_script());
629+
if (holder_shutdown_wallet_index != UINT32_MAX)
630+
req.add_holder_shutdown_key_path(holder_shutdown_wallet_index);
625631
marshal_basepoints(counterparty_basepoints, counterparty_funding_pubkey,
626632
req.mutable_counterparty_basepoints());
627633
req.set_counterparty_selected_contest_delay(counterparty_to_self_delay);

contrib/remote_hsmd/proxy.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ proxy_stat proxy_handle_ready_channel(
7171
u16 funding_txout,
7272
u16 local_to_self_delay,
7373
u8 *local_shutdown_script,
74+
u32 local_shutdown_wallet_index,
7475
struct basepoints *remote_basepoints,
7576
struct pubkey *remote_funding_pubkey,
7677
u16 remote_to_self_delay,

hsmd/hsmd_wire.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ msgdata,hsmd_ready_channel,funding_txout,u16,
6363
msgdata,hsmd_ready_channel,local_to_self_delay,u16,
6464
msgdata,hsmd_ready_channel,local_shutdown_script_len,u16,
6565
msgdata,hsmd_ready_channel,local_shutdown_script,u8,local_shutdown_script_len
66+
msgdata,hsmd_ready_channel,local_shutdown_wallet_index,u32,
6667
msgdata,hsmd_ready_channel,remote_basepoints,basepoints,
6768
msgdata,hsmd_ready_channel,remote_funding_pubkey,pubkey,
6869
msgdata,hsmd_ready_channel,remote_to_self_delay,u16,

hsmd/libhsmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ static u8 *handle_ready_channel(struct hsmd_client *c, const u8 *msg_in)
310310
u16 funding_txout;
311311
u16 local_to_self_delay;
312312
u8 *local_shutdown_script;
313+
u32 local_shutdown_wallet_index;
313314
struct basepoints remote_basepoints;
314315
struct pubkey remote_funding_pubkey;
315316
u16 remote_to_self_delay;
@@ -322,6 +323,7 @@ static u8 *handle_ready_channel(struct hsmd_client *c, const u8 *msg_in)
322323
&channel_value, &push_value, &funding_txid,
323324
&funding_txout, &local_to_self_delay,
324325
&local_shutdown_script,
326+
&local_shutdown_wallet_index,
325327
&remote_basepoints,
326328
&remote_funding_pubkey,
327329
&remote_to_self_delay,

lightningd/opening_control.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -683,9 +683,19 @@ openchannel_hook_final(struct openchannel_hook_payload *payload STEALS)
683683
uc->got_offer = true;
684684
}
685685

686+
// Determine the wallet index for our upfront_shutdown_script, UINT32_MAX if not found.
687+
u32 upfront_shutdown_script_wallet_index = UINT32_MAX;
688+
bool is_p2sh;
689+
wallet_can_spend(
690+
payload->openingd->ld->wallet,
691+
our_upfront_shutdown_script,
692+
&upfront_shutdown_script_wallet_index,
693+
&is_p2sh);
694+
686695
subd_send_msg(openingd,
687696
take(towire_openingd_got_offer_reply(NULL, errmsg,
688-
our_upfront_shutdown_script)));
697+
our_upfront_shutdown_script,
698+
upfront_shutdown_script_wallet_index)));
689699
}
690700

691701
static bool
@@ -774,8 +784,9 @@ static void opening_got_offer(struct subd *openingd,
774784
/* Tell them they can't open, if we already have open channel. */
775785
if (peer_active_channel(uc->peer)) {
776786
subd_send_msg(openingd,
777-
take(towire_openingd_got_offer_reply(NULL,
778-
"Already have active channel", NULL)));
787+
take(towire_openingd_got_offer_reply(
788+
NULL, "Already have active channel",
789+
NULL, UINT32_MAX)));
779790
return;
780791
}
781792

@@ -1263,10 +1274,20 @@ static struct command_result *json_fundchannel_start(struct command *cmd,
12631274
fc->our_upfront_shutdown_script
12641275
= tal_steal(fc, fc->our_upfront_shutdown_script);
12651276

1277+
// Determine the wallet index for our upfront_shutdown_script, UINT32_MAX if not found.
1278+
u32 upfront_shutdown_script_wallet_index = UINT32_MAX;
1279+
bool is_p2sh;
1280+
wallet_can_spend(
1281+
fc->cmd->ld->wallet,
1282+
fc->our_upfront_shutdown_script,
1283+
&upfront_shutdown_script_wallet_index,
1284+
&is_p2sh);
1285+
12661286
msg = towire_openingd_funder_start(NULL,
12671287
*amount,
12681288
fc->push,
12691289
fc->our_upfront_shutdown_script,
1290+
upfront_shutdown_script_wallet_index,
12701291
*feerate_per_kw,
12711292
fc->channel_flags);
12721293

openingd/openingd.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ struct state {
105105
/* If non-NULL, this is the scriptpubkey we/they *must* close with */
106106
u8 *upfront_shutdown_script[NUM_SIDES];
107107

108+
/* If not UINT32_MAX, the wallet index for the LOCAL script */
109+
u32 local_upfront_shutdown_wallet_index;
110+
108111
/* This is a cluster of fields in open_channel and accept_channel which
109112
* indicate the restrictions each side places on the channel. */
110113
struct channel_config localconf, remoteconf;
@@ -541,6 +544,7 @@ static bool funder_finalize_channel_setup(struct state *state,
541544
state->funding_txout,
542545
state->localconf.to_self_delay,
543546
state->upfront_shutdown_script[LOCAL],
547+
state->local_upfront_shutdown_wallet_index,
544548
&state->their_points,
545549
&state->their_funding_pubkey,
546550
state->remoteconf.to_self_delay,
@@ -991,7 +995,8 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
991995
/* We don't allocate off tmpctx, because that's freed inside
992996
* opening_negotiate_msg */
993997
if (!fromwire_openingd_got_offer_reply(state, msg, &err_reason,
994-
&state->upfront_shutdown_script[LOCAL]))
998+
&state->upfront_shutdown_script[LOCAL],
999+
&state->local_upfront_shutdown_wallet_index))
9951000
master_badmsg(WIRE_OPENINGD_GOT_OFFER_REPLY, msg);
9961001

9971002
/* If they give us a reason to reject, do so. */
@@ -1071,6 +1076,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
10711076
state->funding_txout,
10721077
state->localconf.to_self_delay,
10731078
state->upfront_shutdown_script[LOCAL],
1079+
state->local_upfront_shutdown_wallet_index,
10741080
&theirs,
10751081
&their_funding_pubkey,
10761082
state->remoteconf.to_self_delay,
@@ -1353,6 +1359,7 @@ static u8 *handle_master_in(struct state *state)
13531359
if (!fromwire_openingd_funder_start(state, msg, &state->funding,
13541360
&state->push_msat,
13551361
&state->upfront_shutdown_script[LOCAL],
1362+
&state->local_upfront_shutdown_wallet_index,
13561363
&state->feerate_per_kw,
13571364
&channel_flags))
13581365
master_badmsg(WIRE_OPENINGD_FUNDER_START, msg);

openingd/openingd_wire.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ msgtype,openingd_got_offer_reply,6105
5555
msgdata,openingd_got_offer_reply,rejection,?wirestring,
5656
msgdata,openingd_got_offer_reply,shutdown_len,u16,
5757
msgdata,openingd_got_offer_reply,our_shutdown_scriptpubkey,?u8,shutdown_len
58+
msgdata,openingd_got_offer_reply,our_shutdown_wallet_index,u32,
5859

5960
#include <common/penalty_base.h>
6061
# Openingd->master: we've successfully offered channel.
@@ -85,6 +86,7 @@ msgdata,openingd_funder_start,funding_satoshis,amount_sat,
8586
msgdata,openingd_funder_start,push_msat,amount_msat,
8687
msgdata,openingd_funder_start,len_upfront,u16,
8788
msgdata,openingd_funder_start,upfront_shutdown_script,u8,len_upfront
89+
msgdata,openingd_funder_start,upfront_shutdown_wallet_index,u32,
8890
msgdata,openingd_funder_start,feerate_per_kw,u32,
8991
msgdata,openingd_funder_start,channel_flags,u8,
9092

tests/test_connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1501,6 +1501,7 @@ def test_funding_v2_cancel_race(node_factory, bitcoind, executor):
15011501
@pytest.mark.openchannel('v1')
15021502
@pytest.mark.openchannel('v2')
15031503
@unittest.skipIf(TEST_NETWORK != 'regtest', "External wallet support doesn't work with elements yet.")
1504+
@unittest.skipIf(os.getenv('SUBDAEMON') == 'hsmd:remote_hsmd', "remote_hsmd can't handle random external addresses (allowlist)")
15041505
def test_funding_close_upfront(node_factory, bitcoind):
15051506
opts = {'plugin': os.path.join(os.getcwd(), 'tests/plugins/openchannel_hook_accepter.py')}
15061507

0 commit comments

Comments
 (0)