@@ -1442,6 +1442,17 @@ static u8 *make_revocation_msg(const struct peer *peer, u64 revoke_index,
14421442 point );
14431443}
14441444
1445+ static u8 * make_revocation_msg_from_secret (const struct peer * peer ,
1446+ u64 revoke_index ,
1447+ struct pubkey * point ,
1448+ const struct secret * old_commit_secret ,
1449+ const struct pubkey * next_point )
1450+ {
1451+ * point = * next_point ;
1452+ return towire_revoke_and_ack (peer , & peer -> channel_id ,
1453+ old_commit_secret , next_point );
1454+ }
1455+
14451456/* Convert changed htlcs into parts which lightningd expects. */
14461457static void marshall_htlc_info (const tal_t * ctx ,
14471458 const struct htlc * * changed_htlcs ,
@@ -1501,7 +1512,9 @@ static void send_revocation(struct peer *peer,
15011512 const struct bitcoin_signature * commit_sig ,
15021513 const struct bitcoin_signature * htlc_sigs ,
15031514 const struct htlc * * changed_htlcs ,
1504- const struct bitcoin_tx * committx )
1515+ const struct bitcoin_tx * committx ,
1516+ const struct secret * old_secret ,
1517+ const struct pubkey * next_point )
15051518{
15061519 struct changed_htlc * changed ;
15071520 struct fulfilled_htlc * fulfilled ;
@@ -1519,8 +1532,9 @@ static void send_revocation(struct peer *peer,
15191532 & added );
15201533
15211534 /* Revoke previous commit, get new point. */
1522- u8 * msg = make_revocation_msg (peer , peer -> next_index [LOCAL ]- 1 ,
1523- & peer -> next_local_per_commit );
1535+ u8 * msg = make_revocation_msg_from_secret (peer , peer -> next_index [LOCAL ]- 1 ,
1536+ & peer -> next_local_per_commit ,
1537+ old_secret , next_point );
15241538
15251539 /* From now on we apply changes to the next commitment */
15261540 peer -> next_index [LOCAL ]++ ;
@@ -1685,8 +1699,44 @@ static void handle_peer_commit_sig(struct peer *peer, const u8 *msg)
16851699 status_debug ("Received commit_sig with %zu htlc sigs" ,
16861700 tal_count (htlc_sigs ));
16871701
1688- send_revocation (peer ,
1689- & commit_sig , htlc_sigs , changed_htlcs , txs [0 ]);
1702+ /* Collect the htlcs for call to hsmd validate. */
1703+ struct simple_htlc * * htlcs = tal_arr (NULL , struct simple_htlc * , 0 );
1704+ size_t num_entries = tal_count (htlc_map );
1705+ for (size_t ndx = 0 ; ndx < num_entries ; ++ ndx ) {
1706+ struct htlc const * hh = htlc_map [ndx ];
1707+ if (hh ) {
1708+ status_debug ("HTLC[%lu]=%" PRIu64 ", %s" ,
1709+ ndx , hh -> id , htlc_state_name (hh -> state ));
1710+ struct simple_htlc * simple =
1711+ new_simple_htlc (NULL ,
1712+ htlc_state_owner (hh -> state ),
1713+ hh -> amount ,
1714+ & hh -> rhash ,
1715+ hh -> expiry .locktime );
1716+ tal_arr_expand (& htlcs , tal_steal (htlcs , simple ));
1717+ }
1718+ }
1719+
1720+ /* Validate the counterparty's signatures, returns old_secret. */
1721+ const u8 * msg2 =
1722+ towire_hsmd_validate_commitment_tx (NULL ,
1723+ txs [0 ],
1724+ (const struct simple_htlc * * ) htlcs ,
1725+ peer -> next_index [LOCAL ],
1726+ channel_feerate (peer -> channel , LOCAL ),
1727+ & commit_sig ,
1728+ htlc_sigs );
1729+ tal_free (htlcs );
1730+ msg2 = hsm_req (tmpctx , take (msg2 ));
1731+ struct secret * old_secret ;
1732+ struct pubkey next_point ;
1733+ if (!fromwire_hsmd_validate_commitment_tx_reply (tmpctx , msg2 , & old_secret , & next_point ))
1734+ status_failed (STATUS_FAIL_HSM_IO ,
1735+ "Reading validate_commitment_tx reply: %s" ,
1736+ tal_hex (tmpctx , msg2 ));
1737+
1738+ send_revocation (peer , & commit_sig , htlc_sigs , changed_htlcs , txs [0 ],
1739+ old_secret , & next_point );
16901740
16911741 /* We may now be quiescent on our side. */
16921742 maybe_send_stfu (peer );
0 commit comments