@@ -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,48 @@ 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+ //
1704+ // We use the simple_htlc to_wire routines, it's unfortunate that
1705+ // we have to send a dummy onion_routing_packet ...
1706+ //
1707+ struct simple_htlc * * htlcs = tal_arr (NULL , struct simple_htlc * , 0 );
1708+ size_t num_entries = tal_count (htlc_map );
1709+ for (size_t ndx = 0 ; ndx < num_entries ; ++ ndx ) {
1710+ struct htlc const * hh = htlc_map [ndx ];
1711+ if (hh ) {
1712+ status_debug ("HTLC[%lu]=%" PRIu64 ", %s" ,
1713+ ndx , hh -> id , htlc_state_name (hh -> state ));
1714+ struct simple_htlc * simple =
1715+ new_simple_htlc (NULL ,
1716+ htlc_state_owner (hh -> state ),
1717+ hh -> amount ,
1718+ & hh -> rhash ,
1719+ hh -> expiry .locktime );
1720+ tal_arr_expand (& htlcs , tal_steal (htlcs , simple ));
1721+ }
1722+ }
1723+
1724+ // Validate the counterparty's signatures, returns old_secret.
1725+ const u8 * msg2 =
1726+ towire_hsmd_validate_commitment_tx (NULL ,
1727+ txs [0 ],
1728+ (const struct simple_htlc * * ) htlcs ,
1729+ peer -> next_index [LOCAL ],
1730+ channel_feerate (peer -> channel , LOCAL ),
1731+ & commit_sig ,
1732+ htlc_sigs );
1733+ tal_free (htlcs );
1734+ msg2 = hsm_req (tmpctx , take (msg2 ));
1735+ struct secret * old_secret ;
1736+ struct pubkey next_point ;
1737+ if (!fromwire_hsmd_validate_commitment_tx_reply (tmpctx , msg2 , & old_secret , & next_point ))
1738+ status_failed (STATUS_FAIL_HSM_IO ,
1739+ "Reading validate_commitment_tx reply: %s" ,
1740+ tal_hex (tmpctx , msg2 ));
1741+
1742+ send_revocation (peer , & commit_sig , htlc_sigs , changed_htlcs , txs [0 ],
1743+ old_secret , & next_point );
16901744
16911745 /* We may now be quiescent on our side. */
16921746 maybe_send_stfu (peer );
0 commit comments