Skip to content

Commit 7b5ee1d

Browse files
committed
rebased remote-hsmd onto c-lightning master
1 parent 5ef4a2b commit 7b5ee1d

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

hsmd/libhsmd.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,70 @@ static void get_channel_seed(const struct node_id *peer_id, u64 dbid,
275275
info, strlen(info));
276276
}
277277

278+
/*~ This is used to declare a new channel. */
279+
static u8 *handle_new_channel(struct hsmd_client *c, const u8 *msg_in)
280+
{
281+
struct node_id peer_id;
282+
u64 dbid;
283+
284+
if (!fromwire_hsmd_new_channel(msg_in, &peer_id, &dbid))
285+
return hsmd_status_malformed_request(c, msg_in);
286+
287+
return towire_hsmd_new_channel_reply(NULL);
288+
}
289+
290+
static bool mem_is_zero(const void *mem, size_t len)
291+
{
292+
size_t i;
293+
for (i = 0; i < len; ++i)
294+
if (((const unsigned char *)mem)[i])
295+
return false;
296+
return true;
297+
}
298+
299+
/*~ This is used to provide all unchanging public channel parameters. */
300+
static u8 *handle_ready_channel(struct hsmd_client *c, const u8 *msg_in)
301+
{
302+
bool is_outbound;
303+
struct amount_sat channel_value;
304+
struct amount_msat push_value;
305+
struct bitcoin_txid funding_txid;
306+
u16 funding_txout;
307+
u16 local_to_self_delay;
308+
u8 *local_shutdown_script;
309+
struct basepoints remote_basepoints;
310+
struct pubkey remote_funding_pubkey;
311+
u16 remote_to_self_delay;
312+
u8 *remote_shutdown_script;
313+
bool option_static_remotekey;
314+
bool option_anchor_outputs;
315+
struct amount_msat value_msat;
316+
317+
if (!fromwire_hsmd_ready_channel(tmpctx, msg_in, &is_outbound,
318+
&channel_value, &push_value, &funding_txid,
319+
&funding_txout, &local_to_self_delay,
320+
&local_shutdown_script,
321+
&remote_basepoints,
322+
&remote_funding_pubkey,
323+
&remote_to_self_delay,
324+
&remote_shutdown_script,
325+
&option_static_remotekey,
326+
&option_anchor_outputs))
327+
return hsmd_status_malformed_request(c, msg_in);
328+
329+
/* Fail fast if any values are obviously uninitialized. */
330+
assert(amount_sat_greater(channel_value, AMOUNT_SAT(0)));
331+
assert(amount_sat_to_msat(&value_msat, channel_value));
332+
assert(amount_msat_less_eq(push_value, value_msat));
333+
assert(!mem_is_zero(&funding_txid, sizeof(funding_txid)));
334+
assert(local_to_self_delay > 0);
335+
assert(!mem_is_zero(&remote_basepoints, sizeof(remote_basepoints)));
336+
assert(!mem_is_zero(&remote_funding_pubkey, sizeof(remote_funding_pubkey)));
337+
assert(remote_to_self_delay > 0);
338+
339+
return towire_hsmd_ready_channel_reply(NULL);
340+
}
341+
278342
/*~ For almost every wallet tx we use the BIP32 seed, but not for onchain
279343
* unilateral closes from a peer: they (may) have an output to us using a
280344
* public key based on the channel basepoints. It's a bit spammy to spend
@@ -1222,13 +1286,16 @@ static u8 *handle_sign_commitment_tx(struct hsmd_client *c, const u8 *msg_in)
12221286
struct secret channel_seed;
12231287
struct bitcoin_tx *tx;
12241288
struct bitcoin_signature sig;
1289+
struct sha256 *rhashes;
1290+
u64 commit_num;
12251291
struct secrets secrets;
12261292
const u8 *funding_wscript;
12271293

12281294
if (!fromwire_hsmd_sign_commitment_tx(tmpctx, msg_in,
12291295
&peer_id, &dbid,
12301296
&tx,
1231-
&remote_funding_pubkey))
1297+
&remote_funding_pubkey,
1298+
&rhashes, &commit_num))
12321299
return hsmd_status_malformed_request(c, msg_in);
12331300

12341301
tx->chainparams = c->chainparams;
@@ -1396,6 +1463,10 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
13961463
"libhsmd",
13971464
hsmd_wire_name(t));
13981465

1466+
case WIRE_HSMD_NEW_CHANNEL:
1467+
return handle_new_channel(client, msg);
1468+
case WIRE_HSMD_READY_CHANNEL:
1469+
return handle_ready_channel(client, msg);
13991470
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
14001471
return handle_get_output_scriptpubkey(client, msg);
14011472
case WIRE_HSMD_CHECK_FUTURE_SECRET:
@@ -1444,6 +1515,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
14441515
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
14451516
case WIRE_HSMD_CUPDATE_SIG_REPLY:
14461517
case WIRE_HSMD_CLIENT_HSMFD_REPLY:
1518+
case WIRE_HSMD_NEW_CHANNEL_REPLY:
1519+
case WIRE_HSMD_READY_CHANNEL_REPLY:
14471520
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
14481521
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
14491522
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
@@ -1181,6 +1181,7 @@ def test_funder_options(node_factory, bitcoind):
11811181

11821182

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

0 commit comments

Comments
 (0)