@@ -1489,6 +1489,17 @@ static u8 *make_revocation_msg(const struct peer *peer, u64 revoke_index,
14891489 point );
14901490}
14911491
1492+ static u8 * make_revocation_msg_from_secret (const struct peer * peer ,
1493+ u64 revoke_index ,
1494+ struct pubkey * point ,
1495+ const struct secret * old_commit_secret ,
1496+ const struct pubkey * next_point )
1497+ {
1498+ * point = * next_point ;
1499+ return towire_revoke_and_ack (peer , & peer -> channel_id ,
1500+ old_commit_secret , next_point );
1501+ }
1502+
14921503/* Convert changed htlcs into parts which lightningd expects. */
14931504static void marshall_htlc_info (const tal_t * ctx ,
14941505 const struct htlc * * changed_htlcs ,
@@ -1547,7 +1558,9 @@ static void send_revocation(struct peer *peer,
15471558 const struct bitcoin_signature * commit_sig ,
15481559 const struct bitcoin_signature * htlc_sigs ,
15491560 const struct htlc * * changed_htlcs ,
1550- const struct bitcoin_tx * committx )
1561+ const struct bitcoin_tx * committx ,
1562+ const struct secret * old_secret ,
1563+ const struct pubkey * next_point )
15511564{
15521565 struct changed_htlc * changed ;
15531566 struct fulfilled_htlc * fulfilled ;
@@ -1565,8 +1578,9 @@ static void send_revocation(struct peer *peer,
15651578 & added );
15661579
15671580 /* Revoke previous commit, get new point. */
1568- u8 * msg = make_revocation_msg (peer , peer -> next_index [LOCAL ]- 1 ,
1569- & peer -> next_local_per_commit );
1581+ u8 * msg = make_revocation_msg_from_secret (peer , peer -> next_index [LOCAL ]- 1 ,
1582+ & peer -> next_local_per_commit ,
1583+ old_secret , next_point );
15701584
15711585 /* From now on we apply changes to the next commitment */
15721586 peer -> next_index [LOCAL ]++ ;
@@ -1731,8 +1745,55 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
17311745 status_debug ("Received commit_sig with %zu htlc sigs" ,
17321746 tal_count (htlc_sigs ));
17331747
1734- send_revocation (peer ,
1735- & commit_sig , htlc_sigs , changed_htlcs , txs [0 ]);
1748+ // Collect the htlcs for call to hsmd validate.
1749+ //
1750+ // We use the existing_htlc to_wire routines, it's unfortunate that
1751+ // we have to send a dummy onion_routing_packet ...
1752+ //
1753+ struct existing_htlc * * htlcs = tal_arr (NULL , struct existing_htlc * , 0 );
1754+ u8 dummy_onion_routing_packet [TOTAL_PACKET_SIZE (ROUTING_INFO_SIZE )];
1755+ memset (dummy_onion_routing_packet , 0 , sizeof (dummy_onion_routing_packet ));
1756+ size_t num_entries = tal_count (htlc_map );
1757+ for (size_t ndx = 0 ; ndx < num_entries ; ++ ndx ) {
1758+ struct htlc const * hh = htlc_map [ndx ];
1759+ if (hh ) {
1760+ status_debug ("HTLC[%lu]=%" PRIu64 ", %s" ,
1761+ ndx , hh -> id , htlc_state_name (hh -> state ));
1762+ struct existing_htlc * existing =
1763+ new_existing_htlc (NULL ,
1764+ hh -> id ,
1765+ hh -> state ,
1766+ hh -> amount ,
1767+ & hh -> rhash ,
1768+ hh -> expiry .locktime ,
1769+ dummy_onion_routing_packet ,
1770+ NULL ,
1771+ NULL ,
1772+ NULL );
1773+ tal_arr_expand (& htlcs , tal_steal (htlcs , existing ));
1774+ }
1775+ }
1776+
1777+ // Validate the counterparty's signatures, returns old_secret.
1778+ const u8 * msg2 =
1779+ towire_hsmd_validate_commitment_tx (NULL ,
1780+ txs [0 ],
1781+ (const struct existing_htlc * * ) htlcs ,
1782+ peer -> next_index [LOCAL ],
1783+ channel_feerate (peer -> channel , LOCAL ),
1784+ & commit_sig ,
1785+ htlc_sigs );
1786+ tal_free (htlcs );
1787+ msg2 = hsm_req (tmpctx , take (msg2 ));
1788+ struct secret * old_secret ;
1789+ struct pubkey next_point ;
1790+ if (!fromwire_hsmd_validate_commitment_tx_reply (tmpctx , msg2 , & old_secret , & next_point ))
1791+ status_failed (STATUS_FAIL_HSM_IO ,
1792+ "Reading validate_commitment_tx reply: %s" ,
1793+ tal_hex (tmpctx , msg2 ));
1794+
1795+ send_revocation (peer , & commit_sig , htlc_sigs , changed_htlcs , txs [0 ],
1796+ old_secret , & next_point );
17361797
17371798 /* We may now be quiescent on our side. */
17381799 maybe_send_stfu (peer );
0 commit comments