Skip to content

Commit 7a740b7

Browse files
committed
Adds validate holder commitment API (#28)
* added hsm_validate_commitment, channeld call populated, other 2 opening calls not done. * Replaced payment_hashmap in validate_commitment_tx with htlcs and added feerate. * return next_point and old_secret from handle_validate_commitment_tx and use. * added hsmd_validate_commitment_tx calls to openingd initial commitment cases * fixed botched syntax, skipped wallet test which violates policy
1 parent 7b5ee1d commit 7a740b7

File tree

13 files changed

+379
-12
lines changed

13 files changed

+379
-12
lines changed

channeld/channeld.c

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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. */
14931504
static 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);

contrib/remote_hsmd/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,20 @@ RMTHSMD_COMMON_OBJS := \
4040
common/daemon.o \
4141
common/daemon_conn.o \
4242
common/derive_basepoints.o \
43-
common/status_wiregen.o \
4443
common/hash_u5.o \
44+
common/htlc_state.o \
45+
common/htlc_wire.o \
4546
common/key_derive.o \
4647
common/memleak.o \
4748
common/msg_queue.o \
4849
common/node_id.o \
50+
common/onionreply.o \
4951
common/permute_tx.o \
5052
common/pseudorand.o \
5153
common/setup.o \
5254
common/status.o \
5355
common/status_wire.o \
56+
common/status_wiregen.o \
5457
common/subdaemon.o \
5558
common/type_to_string.o \
5659
common/utils.o \

contrib/remote_hsmd/dump.cc

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ extern "C" {
66
#include <bitcoin/signature.h>
77
#include <bitcoin/tx.h>
88
#include <common/derive_basepoints.h>
9+
#include <common/htlc_wire.h>
910
#include <common/node_id.h>
1011
#include <common/status.h>
1112
#include <common/utils.h>
@@ -56,11 +57,24 @@ string dump_bitcoin_signature(const struct bitcoin_signature *sp)
5657
ostrm << "{ "
5758
<< "\"sighash_type\":" << int(sp->sighash_type)
5859
<< ", \"s\":"
59-
<< '"' << dump_secp256k1_ecdsa_signature(&sp->s) << '"'
60+
<< dump_secp256k1_ecdsa_signature(&sp->s)
6061
<< " }";
6162
return ostrm.str();
6263
}
6364

65+
string dump_htlc_signatures(const struct bitcoin_signature *sps)
66+
{
67+
ostringstream ostrm;
68+
ostrm << "[";
69+
for (size_t input_ndx = 0; input_ndx < tal_count(sps); ++input_ndx) {
70+
if (input_ndx != 0)
71+
ostrm << ", ";
72+
ostrm << dump_bitcoin_signature(&sps[input_ndx]);
73+
}
74+
ostrm << "]";
75+
return ostrm.str();
76+
}
77+
6478
string dump_secp256k1_ecdsa_signature(const secp256k1_ecdsa_signature *sp)
6579
{
6680
return dump_hex(sp->data, sizeof(sp->data));
@@ -428,7 +442,6 @@ string dump_wally_psbt_output(const struct wally_psbt_output *out)
428442
ostrm << ", \"unknowns\":" << dump_wally_unknowns_map(&out->unknowns);
429443
ostrm << " }";
430444
return ostrm.str();
431-
432445
}
433446

434447
string dump_wally_psbt_outputs(const struct wally_psbt_output *outputs,
@@ -484,6 +497,32 @@ string dump_rhashes(const struct sha256 *rhashes, size_t num_rhashes)
484497
return ostrm.str();
485498
}
486499

500+
string dump_htlc(const struct existing_htlc *htlc)
501+
{
502+
ostringstream ostrm;
503+
ostrm << "{ "
504+
<< "\"id\":" << htlc->id
505+
<< ", \"state\":" << htlc_state_name(htlc->state)
506+
<< ", \"amount_msat\":" << htlc->amount.millisatoshis
507+
<< ", \"payment_hash\":" << dump_hex(&htlc->payment_hash, sizeof(htlc->payment_hash))
508+
<< ", \"cltv_expiry\":" << htlc->cltv_expiry
509+
<< " }";
510+
return ostrm.str();
511+
}
512+
513+
string dump_htlcs(const struct existing_htlc **htlc, size_t num_htlc)
514+
{
515+
ostringstream ostrm;
516+
ostrm << "[";
517+
for (size_t ii = 0; ii < num_htlc; ii++) {
518+
if (ii != 0)
519+
ostrm << ",";
520+
ostrm << dump_htlc(htlc[ii]);
521+
}
522+
ostrm << "]";
523+
return ostrm.str();
524+
}
525+
487526
/* <sigh>. Bitcoind represents hashes as little-endian for RPC. */
488527
void reverse_bytes(u8 *arr, size_t len)
489528
{

contrib/remote_hsmd/dump.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ std::string dump_hex(const void *vptr, size_t sz);
1111
std::string dump_basepoints(const struct basepoints *bp);
1212
std::string dump_bitcoin_txid(const struct bitcoin_txid *txid);
1313
std::string dump_bitcoin_signature(const struct bitcoin_signature *sp);
14+
std::string dump_htlc_signatures(const struct bitcoin_signature *sps);
1415
std::string dump_secp256k1_ecdsa_signature(const secp256k1_ecdsa_signature *sp);
1516
std::string dump_secp256k1_ecdsa_recoverable_signature(const secp256k1_ecdsa_recoverable_signature *sp);
1617
std::string dump_secret(const struct secret *sp);
@@ -37,6 +38,8 @@ std::string dump_wally_tx(const struct wally_tx *wtx);
3738
std::string dump_wally_psbt(const struct wally_psbt *psbt);
3839
std::string dump_tx(const struct bitcoin_tx *tx);
3940
std::string dump_rhashes(const struct sha256 *rhashes, size_t num_rhashes);
41+
std::string dump_htlc(const struct existing_htlc *htlc);
42+
std::string dump_htlcs(const struct existing_htlc **htlc, size_t num_htlc);
4043

4144
// needed for formatting txid
4245
void reverse_bytes(u8 *arr, size_t len);

contrib/remote_hsmd/hsmd.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,46 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
808808
take(towire_hsmd_sign_commitment_tx_reply(NULL, &sig)));
809809
}
810810

811+
/* Validate the peer's signatures for our commitment and htlc txs. */
812+
static struct io_plan *handle_validate_commitment_tx(struct io_conn *conn,
813+
struct client *c,
814+
const u8 *msg_in)
815+
{
816+
struct bitcoin_tx *tx;
817+
struct existing_htlc **htlc;
818+
u64 commit_num;
819+
u32 feerate;
820+
struct bitcoin_signature commit_sig;
821+
struct bitcoin_signature *htlc_sigs;
822+
struct secret *old_secret;
823+
struct pubkey next_per_commitment_point;
824+
825+
if (!fromwire_hsmd_validate_commitment_tx(tmpctx, msg_in,
826+
&tx, &htlc,
827+
&commit_num, &feerate,
828+
&commit_sig, &htlc_sigs))
829+
bad_req(conn, c, msg_in);
830+
831+
proxy_stat rv = proxy_handle_validate_commitment_tx(
832+
tx,
833+
&c->id, c->dbid,
834+
htlc, commit_num, feerate,
835+
&commit_sig, htlc_sigs,
836+
&old_secret, &next_per_commitment_point);
837+
if (PROXY_PERMANENT(rv))
838+
status_failed(STATUS_FAIL_INTERNAL_ERROR,
839+
"proxy_%s failed: %s", __FUNCTION__,
840+
proxy_last_message());
841+
else if (!PROXY_SUCCESS(rv))
842+
return bad_req_fmt(conn, c, msg_in,
843+
"proxy_%s error: %s", __FUNCTION__,
844+
proxy_last_message());
845+
846+
return req_reply(conn, c,
847+
take(towire_hsmd_validate_commitment_tx_reply(
848+
NULL, old_secret, &next_per_commitment_point)));
849+
}
850+
811851
/*~ This is used by channeld to create signatures for the remote peer's
812852
* commitment transaction. It's functionally identical to signing our own,
813853
* but we expect to do this repeatedly as commitment transactions are
@@ -1595,6 +1635,7 @@ static bool check_client_capabilities(struct client *client,
15951635

15961636
case WIRE_HSMD_SIGN_REMOTE_COMMITMENT_TX:
15971637
case WIRE_HSMD_SIGN_REMOTE_HTLC_TX:
1638+
case WIRE_HSMD_VALIDATE_COMMITMENT_TX:
15981639
return (client->capabilities & HSM_CAP_SIGN_REMOTE_TX) != 0;
15991640

16001641
case WIRE_HSMD_SIGN_MUTUAL_CLOSE_TX:
@@ -1628,6 +1669,7 @@ static bool check_client_capabilities(struct client *client,
16281669
case WIRE_HSMD_INIT_REPLY:
16291670
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
16301671
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
1672+
case WIRE_HSMD_VALIDATE_COMMITMENT_TX_REPLY:
16311673
case WIRE_HSMD_SIGN_TX_REPLY:
16321674
case WIRE_HSMD_GET_PER_COMMITMENT_POINT_REPLY:
16331675
case WIRE_HSMD_CHECK_FUTURE_SECRET_REPLY:
@@ -1704,6 +1746,9 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
17041746
case WIRE_HSMD_SIGN_COMMITMENT_TX:
17051747
return handle_sign_commitment_tx(conn, c, c->msg_in);
17061748

1749+
case WIRE_HSMD_VALIDATE_COMMITMENT_TX:
1750+
return handle_validate_commitment_tx(conn, c, c->msg_in);
1751+
17071752
case WIRE_HSMD_SIGN_DELAYED_PAYMENT_TO_US:
17081753
return handle_sign_delayed_payment_to_us(conn, c, c->msg_in);
17091754

@@ -1754,6 +1799,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
17541799
case WIRE_HSMD_INIT_REPLY:
17551800
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
17561801
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
1802+
case WIRE_HSMD_VALIDATE_COMMITMENT_TX_REPLY:
17571803
case WIRE_HSMD_SIGN_TX_REPLY:
17581804
case WIRE_HSMD_GET_PER_COMMITMENT_POINT_REPLY:
17591805
case WIRE_HSMD_CHECK_FUTURE_SECRET_REPLY:

0 commit comments

Comments
 (0)