@@ -1341,6 +1341,17 @@ static u8 *make_revocation_msg(const struct peer *peer, u64 revoke_index,
13411341 point );
13421342}
13431343
1344+ static u8 * make_revocation_msg_from_secret (const struct peer * peer ,
1345+ u64 revoke_index ,
1346+ struct pubkey * point ,
1347+ const struct secret * old_commit_secret ,
1348+ const struct pubkey * next_point )
1349+ {
1350+ * point = * next_point ;
1351+ return towire_revoke_and_ack (peer , & peer -> channel_id ,
1352+ old_commit_secret , next_point );
1353+ }
1354+
13441355/* Convert changed htlcs into parts which lightningd expects. */
13451356static void marshall_htlc_info (const tal_t * ctx ,
13461357 const struct htlc * * changed_htlcs ,
@@ -1399,7 +1410,9 @@ static void send_revocation(struct peer *peer,
13991410 const struct bitcoin_signature * commit_sig ,
14001411 const struct bitcoin_signature * htlc_sigs ,
14011412 const struct htlc * * changed_htlcs ,
1402- const struct bitcoin_tx * committx )
1413+ const struct bitcoin_tx * committx ,
1414+ const struct secret * old_secret ,
1415+ const struct pubkey * next_point )
14031416{
14041417 struct changed_htlc * changed ;
14051418 struct fulfilled_htlc * fulfilled ;
@@ -1417,8 +1430,9 @@ static void send_revocation(struct peer *peer,
14171430 & added );
14181431
14191432 /* Revoke previous commit, get new point. */
1420- u8 * msg = make_revocation_msg (peer , peer -> next_index [LOCAL ]- 1 ,
1421- & peer -> next_local_per_commit );
1433+ u8 * msg = make_revocation_msg_from_secret (peer , peer -> next_index [LOCAL ]- 1 ,
1434+ & peer -> next_local_per_commit ,
1435+ old_secret , next_point );
14221436
14231437 /* From now on we apply changes to the next commitment */
14241438 peer -> next_index [LOCAL ]++ ;
@@ -1582,8 +1596,55 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
15821596 status_debug ("Received commit_sig with %zu htlc sigs" ,
15831597 tal_count (htlc_sigs ));
15841598
1585- send_revocation (peer ,
1586- & commit_sig , htlc_sigs , changed_htlcs , txs [0 ]);
1599+ // Collect the htlcs for call to hsmd validate.
1600+ //
1601+ // We use the existing_htlc to_wire routines, it's unfortunate that
1602+ // we have to send a dummy onion_routing_packet ...
1603+ //
1604+ struct existing_htlc * * htlcs = tal_arr (NULL , struct existing_htlc * , 0 );
1605+ u8 dummy_onion_routing_packet [TOTAL_PACKET_SIZE (ROUTING_INFO_SIZE )];
1606+ memset (dummy_onion_routing_packet , 0 , sizeof (dummy_onion_routing_packet ));
1607+ size_t num_entries = tal_count (htlc_map );
1608+ for (size_t ndx = 0 ; ndx < num_entries ; ++ ndx ) {
1609+ struct htlc const * hh = htlc_map [ndx ];
1610+ if (hh ) {
1611+ status_debug ("HTLC[%lu]=%" PRIu64 ", %s" ,
1612+ ndx , hh -> id , htlc_state_name (hh -> state ));
1613+ struct existing_htlc * existing =
1614+ new_existing_htlc (NULL ,
1615+ hh -> id ,
1616+ hh -> state ,
1617+ hh -> amount ,
1618+ & hh -> rhash ,
1619+ hh -> expiry .locktime ,
1620+ dummy_onion_routing_packet ,
1621+ NULL ,
1622+ NULL ,
1623+ NULL );
1624+ tal_arr_expand (& htlcs , tal_steal (htlcs , existing ));
1625+ }
1626+ }
1627+
1628+ // Validate the counterparty's signatures, returns old_secret.
1629+ const u8 * msg2 =
1630+ towire_hsmd_validate_commitment_tx (NULL ,
1631+ txs [0 ],
1632+ (const struct existing_htlc * * ) htlcs ,
1633+ peer -> next_index [LOCAL ],
1634+ channel_feerate (peer -> channel , LOCAL ),
1635+ & commit_sig ,
1636+ htlc_sigs );
1637+ tal_free (htlcs );
1638+ msg2 = hsm_req (tmpctx , take (msg2 ));
1639+ struct secret * old_secret ;
1640+ struct pubkey next_point ;
1641+ if (!fromwire_hsmd_validate_commitment_tx_reply (tmpctx , msg2 , & old_secret , & next_point ))
1642+ status_failed (STATUS_FAIL_HSM_IO ,
1643+ "Reading validate_commitment_tx reply: %s" ,
1644+ tal_hex (tmpctx , msg2 ));
1645+
1646+ send_revocation (peer , & commit_sig , htlc_sigs , changed_htlcs , txs [0 ],
1647+ old_secret , & next_point );
15871648
15881649 /* We may now be quiescent on our side. */
15891650 maybe_send_stfu (peer );
0 commit comments