Skip to content

Commit eb7ebfb

Browse files
committed
rebased remote-hsmd onto c-lightning master
1 parent cca926d commit eb7ebfb

10 files changed

+246
-118
lines changed

hsmd/hsmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,8 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
653653
case WIRE_HSMD_DEV_MEMLEAK:
654654
#endif /* DEVELOPER */
655655

656+
case WIRE_HSMD_NEW_CHANNEL:
657+
case WIRE_HSMD_READY_CHANNEL:
656658
case WIRE_HSMD_SIGN_COMMITMENT_TX:
657659
case WIRE_HSMD_SIGN_PENALTY_TO_US:
658660
case WIRE_HSMD_SIGN_REMOTE_COMMITMENT_TX:

hsmd/hsmd_wiregen.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hsmd/hsmd_wiregen.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hsmd/libhsmd.c

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
8484

8585
case WIRE_HSMD_GET_PER_COMMITMENT_POINT:
8686
case WIRE_HSMD_CHECK_FUTURE_SECRET:
87+
case WIRE_HSMD_READY_CHANNEL:
8788
return (client->capabilities & HSM_CAP_COMMITMENT_POINT) != 0;
8889

8990
case WIRE_HSMD_SIGN_REMOTE_COMMITMENT_TX:
@@ -94,6 +95,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
9495
return (client->capabilities & HSM_CAP_SIGN_CLOSING_TX) != 0;
9596

9697
case WIRE_HSMD_INIT:
98+
case WIRE_HSMD_NEW_CHANNEL:
9799
case WIRE_HSMD_CLIENT_HSMFD:
98100
case WIRE_HSMD_SIGN_WITHDRAWAL:
99101
case WIRE_HSMD_SIGN_INVOICE:
@@ -112,6 +114,8 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
112114
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
113115
case WIRE_HSMD_CUPDATE_SIG_REPLY:
114116
case WIRE_HSMD_CLIENT_HSMFD_REPLY:
117+
case WIRE_HSMD_NEW_CHANNEL_REPLY:
118+
case WIRE_HSMD_READY_CHANNEL_REPLY:
115119
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
116120
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
117121
case WIRE_HSMD_SIGN_INVOICE_REPLY:
@@ -266,6 +270,70 @@ static void get_channel_seed(const struct node_id *peer_id, u64 dbid,
266270
info, strlen(info));
267271
}
268272

273+
/*~ This is used to declare a new channel. */
274+
static u8 *handle_new_channel(struct hsmd_client *c, const u8 *msg_in)
275+
{
276+
struct node_id peer_id;
277+
u64 dbid;
278+
279+
if (!fromwire_hsmd_new_channel(msg_in, &peer_id, &dbid))
280+
return hsmd_status_malformed_request(c, msg_in);
281+
282+
return towire_hsmd_new_channel_reply(NULL);
283+
}
284+
285+
static bool mem_is_zero(const void *mem, size_t len)
286+
{
287+
size_t i;
288+
for (i = 0; i < len; ++i)
289+
if (((const unsigned char *)mem)[i])
290+
return false;
291+
return true;
292+
}
293+
294+
/*~ This is used to provide all unchanging public channel parameters. */
295+
static u8 *handle_ready_channel(struct hsmd_client *c, const u8 *msg_in)
296+
{
297+
bool is_outbound;
298+
struct amount_sat channel_value;
299+
struct amount_msat push_value;
300+
struct bitcoin_txid funding_txid;
301+
u16 funding_txout;
302+
u16 local_to_self_delay;
303+
u8 *local_shutdown_script;
304+
struct basepoints remote_basepoints;
305+
struct pubkey remote_funding_pubkey;
306+
u16 remote_to_self_delay;
307+
u8 *remote_shutdown_script;
308+
bool option_static_remotekey;
309+
bool option_anchor_outputs;
310+
struct amount_msat value_msat;
311+
312+
if (!fromwire_hsmd_ready_channel(tmpctx, msg_in, &is_outbound,
313+
&channel_value, &push_value, &funding_txid,
314+
&funding_txout, &local_to_self_delay,
315+
&local_shutdown_script,
316+
&remote_basepoints,
317+
&remote_funding_pubkey,
318+
&remote_to_self_delay,
319+
&remote_shutdown_script,
320+
&option_static_remotekey,
321+
&option_anchor_outputs))
322+
return hsmd_status_malformed_request(c, msg_in);
323+
324+
/* Fail fast if any values are obviously uninitialized. */
325+
assert(amount_sat_greater(channel_value, AMOUNT_SAT(0)));
326+
assert(amount_sat_to_msat(&value_msat, channel_value));
327+
assert(amount_msat_less_eq(push_value, value_msat));
328+
assert(!mem_is_zero(&funding_txid, sizeof(funding_txid)));
329+
assert(local_to_self_delay > 0);
330+
assert(!mem_is_zero(&remote_basepoints, sizeof(remote_basepoints)));
331+
assert(!mem_is_zero(&remote_funding_pubkey, sizeof(remote_funding_pubkey)));
332+
assert(remote_to_self_delay > 0);
333+
334+
return towire_hsmd_ready_channel_reply(NULL);
335+
}
336+
269337
/*~ For almost every wallet tx we use the BIP32 seed, but not for onchain
270338
* unilateral closes from a peer: they (may) have an output to us using a
271339
* public key based on the channel basepoints. It's a bit spammy to spend
@@ -1080,12 +1148,15 @@ static u8 *handle_sign_remote_commitment_tx(struct hsmd_client *c, const u8 *msg
10801148
const u8 *funding_wscript;
10811149
struct pubkey remote_per_commit;
10821150
bool option_static_remotekey;
1151+
struct sha256 *htlc_rhash;
1152+
u64 commit_num;
10831153

10841154
if (!fromwire_hsmd_sign_remote_commitment_tx(tmpctx, msg_in,
10851155
&tx,
10861156
&remote_funding_pubkey,
10871157
&remote_per_commit,
1088-
&option_static_remotekey))
1158+
&option_static_remotekey,
1159+
&htlc_rhash, &commit_num))
10891160
return hsmd_status_malformed_request(c, msg_in);
10901161
tx->chainparams = c->chainparams;
10911162

@@ -1170,13 +1241,16 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
11701241
struct secret channel_seed;
11711242
struct bitcoin_tx *tx;
11721243
struct bitcoin_signature sig;
1244+
struct sha256 *rhashes;
1245+
u64 commit_num;
11731246
struct secrets secrets;
11741247
const u8 *funding_wscript;
11751248

11761249
if (!fromwire_hsmd_sign_commitment_tx(tmpctx, msg_in,
11771250
&peer_id, &dbid,
11781251
&tx,
1179-
&remote_funding_pubkey))
1252+
&remote_funding_pubkey,
1253+
&rhashes, &commit_num))
11801254
return hsmd_status_malformed_request(c, msg_in);
11811255

11821256
tx->chainparams = c->chainparams;
@@ -1344,6 +1418,10 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
13441418
"libhsmd",
13451419
hsmd_wire_name(t));
13461420

1421+
case WIRE_HSMD_NEW_CHANNEL:
1422+
return handle_new_channel(client, msg);
1423+
case WIRE_HSMD_READY_CHANNEL:
1424+
return handle_ready_channel(client, msg);
13471425
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
13481426
return handle_get_output_scriptpubkey(client, msg);
13491427
case WIRE_HSMD_CHECK_FUTURE_SECRET:
@@ -1390,6 +1468,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
13901468
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
13911469
case WIRE_HSMD_CUPDATE_SIG_REPLY:
13921470
case WIRE_HSMD_CLIENT_HSMFD_REPLY:
1471+
case WIRE_HSMD_NEW_CHANNEL_REPLY:
1472+
case WIRE_HSMD_READY_CHANNEL_REPLY:
13931473
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
13941474
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
13951475
case WIRE_HSMD_SIGN_INVOICE_REPLY:

lightningd/peer_control.c

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,57 @@ static void sign_last_tx(struct channel *channel,
189189
struct bitcoin_signature sig;
190190
u8 *msg, **witness;
191191

192+
struct htlc_in_map *htlcs_in = &channel->peer->ld->htlcs_in;
193+
struct htlc_out_map *htlcs_out = &channel->peer->ld->htlcs_out;
194+
195+
// Count how many payment hashes we will be sending.
196+
size_t num_entries = 0;
197+
struct htlc_in_map_iter ini;
198+
struct htlc_in *hin;
199+
for (hin = htlc_in_map_first(htlcs_in, &ini);
200+
hin;
201+
hin = htlc_in_map_next(htlcs_in, &ini))
202+
if (hin->key.channel == channel)
203+
++num_entries;
204+
struct htlc_out_map_iter outi;
205+
struct htlc_out *hout;
206+
for (hout = htlc_out_map_first(htlcs_out, &outi);
207+
hout;
208+
hout = htlc_out_map_next(htlcs_out, &outi))
209+
if (hout->key.channel == channel)
210+
++num_entries;
211+
212+
// Gather the payment hashes.
213+
struct sha256 *rhashes = tal_arrz(tmpctx, struct sha256, num_entries);
214+
size_t nrhash = 0;
215+
for (hin = htlc_in_map_first(htlcs_in, &ini);
216+
hin;
217+
hin = htlc_in_map_next(htlcs_in, &ini)) {
218+
if (hin->key.channel != channel)
219+
continue;
220+
memcpy(&rhashes[nrhash], &hin->payment_hash, sizeof(rhashes[nrhash]));
221+
++nrhash;
222+
}
223+
for (hout = htlc_out_map_first(htlcs_out, &outi);
224+
hout;
225+
hout = htlc_out_map_next(htlcs_out, &outi)) {
226+
if (hout->key.channel != channel)
227+
continue;
228+
memcpy(&rhashes[nrhash], &hout->payment_hash, sizeof(rhashes[nrhash]));
229+
++nrhash;
230+
}
231+
assert(nrhash == num_entries);
232+
233+
u64 commit_index = channel->next_index[LOCAL] - 1;
234+
192235
assert(!last_tx->wtx->inputs[0].witness);
193236
msg = towire_hsmd_sign_commitment_tx(tmpctx,
194237
&channel->peer->id,
195238
channel->dbid,
196239
last_tx,
197240
&channel->channel_info
198-
.remote_fundingkey);
241+
.remote_fundingkey,
242+
rhashes, commit_index);
199243

200244
if (!wire_sync_write(ld->hsm_fd, take(msg)))
201245
fatal("Could not write to HSM: %s", strerror(errno));

tests/test_db.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def test_scid_upgrade(node_factory, bitcoind):
147147
@unittest.skipIf(not COMPAT, "needs COMPAT to convert obsolete db")
148148
@unittest.skipIf(os.getenv('TEST_DB_PROVIDER', 'sqlite3') != 'sqlite3', "This test is based on a sqlite3 snapshot")
149149
@unittest.skipIf(TEST_NETWORK != 'regtest', "The network must match the DB snapshot")
150+
@unittest.skipIf(os.getenv('SUBDAEMON') == 'hsmd:remote_hsmd', "remote_hsmd doesn't like channel_nonce changing")
150151
def test_last_tx_inflight_psbt_upgrade(node_factory, bitcoind):
151152
bitcoind.generate_block(12)
152153

tests/test_opening.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ def test_funder_options(node_factory, bitcoind):
10321032

10331033

10341034
@unittest.skipIf(TEST_NETWORK != 'regtest', 'elementsd doesnt yet support PSBT features we need')
1035+
@unittest.skipIf(os.getenv('SUBDAEMON') == 'hsmd:remote_hsmd', "remote_hsmd doesn't support dual-funding yet")
10351036
def test_funder_contribution_limits(node_factory, bitcoind):
10361037
opts = {'experimental-dual-fund': None,
10371038
'feerates': (5000, 5000, 5000, 5000)}

wallet/db_postgres_sqlgen.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wallet/db_sqlite3_sqlgen.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)