Skip to content

Commit 95e92fd

Browse files
authored
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 eb7ebfb commit 95e92fd

File tree

15 files changed

+480
-14
lines changed

15 files changed

+480
-14
lines changed

channeld/channeld.c

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

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)