Skip to content

Commit ba0326a

Browse files
authored
Policy recompose (#21)
* added payment_hashmap, commit_num and feerate_per_kw to SignCounterpartyCommitmentTx * removed feerate from API extension, not needed for commitment only
1 parent 0f5c37e commit ba0326a

File tree

10 files changed

+63
-8
lines changed

10 files changed

+63
-8
lines changed

channeld/channeld.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,10 +840,22 @@ static secp256k1_ecdsa_signature *calc_commitsigs(const tal_t *ctx,
840840
const u8 *msg;
841841
secp256k1_ecdsa_signature *htlc_sigs;
842842

843+
size_t num_entries = tal_count(htlc_map);
844+
struct sha256 *rhashes = tal_arrz(tmpctx, struct sha256, num_entries);
845+
size_t nrhash = 0;
846+
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
847+
if (htlc_map[ndx]) {
848+
memcpy(&rhashes[nrhash], &htlc_map[ndx]->rhash, sizeof(rhashes[nrhash]));
849+
++nrhash;
850+
}
851+
}
852+
tal_resize(&rhashes, nrhash);
853+
843854
msg = towire_hsm_sign_remote_commitment_tx(NULL, txs[0],
844855
&peer->channel->funding_pubkey[REMOTE],
845856
&peer->remote_per_commit,
846-
peer->channel->option_static_remotekey);
857+
peer->channel->option_static_remotekey,
858+
rhashes, commit_index);
847859

848860
msg = hsm_req(tmpctx, take(msg));
849861
if (!fromwire_hsm_sign_tx_reply(msg, commit_sig))

contrib/remote_hsmd/NOTES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,4 @@ Some popular tests:
8686
rust-lightning-signer
8787
----------------------------------------------------------------
8888

89-
cargo run --bin server |& tee log3
89+
cargo run --bin server -- --no-persist --test-mode |& tee log3

contrib/remote_hsmd/dump.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,16 @@ string dump_tx(const struct bitcoin_tx *tx)
467467
ostrm << " }";
468468
return ostrm.str();
469469
}
470+
471+
string dump_rhashes(const struct sha256 *rhashes, size_t num_rhashes)
472+
{
473+
ostringstream ostrm;
474+
ostrm << "[";
475+
for (size_t ii = 0; ii < num_rhashes; ii++) {
476+
if (ii != 0)
477+
ostrm << ",";
478+
ostrm << dump_hex(&rhashes[ii], sizeof(rhashes[ii]));
479+
}
480+
ostrm << "]";
481+
return ostrm.str();
482+
}

contrib/remote_hsmd/dump.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ std::string dump_wally_tx_outputs(const struct wally_tx_output *outputs,
3636
std::string dump_wally_tx(const struct wally_tx *wtx);
3737
std::string dump_wally_psbt(const struct wally_psbt *psbt);
3838
std::string dump_tx(const struct bitcoin_tx *tx);
39+
std::string dump_rhashes(const struct sha256 *rhashes, size_t num_rhashes);
3940

4041
#endif /* LIGHTNING_CONTRIB_REMOTE_HSMD_DUMP_H */

contrib/remote_hsmd/hsmd.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,12 +686,15 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
686686
struct bitcoin_signature sig;
687687
struct pubkey remote_per_commit;
688688
bool option_static_remotekey;
689+
struct sha256 *rhashes;
690+
u64 commit_num;
689691

690692
if (!fromwire_hsm_sign_remote_commitment_tx(tmpctx, msg_in,
691693
&tx,
692694
&remote_funding_pubkey,
693695
&remote_per_commit,
694-
&option_static_remotekey))
696+
&option_static_remotekey,
697+
&rhashes, &commit_num))
695698
bad_req(conn, c, msg_in);
696699
tx->chainparams = c->chainparams;
697700

@@ -706,6 +709,7 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
706709
&c->id, c->dbid,
707710
&remote_per_commit,
708711
option_static_remotekey,
712+
rhashes, commit_num,
709713
&sig);
710714
if (PROXY_PERMANENT(rv))
711715
status_failed(STATUS_FAIL_INTERNAL_ERROR,

contrib/remote_hsmd/proxy.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ void marshal_single_input_tx(struct bitcoin_tx const *tx,
210210
}
211211
}
212212

213+
void marshal_rhashes(const struct sha256 *rhashes,
214+
RepeatedPtrField<string> *payment_hashes)
215+
{
216+
for (size_t ii = 0; ii < tal_count(rhashes); ++ii) {
217+
payment_hashes->Add(string((const char *) &rhashes[ii],
218+
sizeof(struct sha256)));
219+
}
220+
}
221+
213222
void unmarshal_secret(Secret const &ss, struct secret *o_sp)
214223
{
215224
assert(ss.data().size() == sizeof(o_sp->data));
@@ -671,22 +680,26 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
671680
u64 dbid,
672681
const struct pubkey *remote_per_commit,
673682
bool option_static_remotekey,
683+
struct sha256 *rhashes, u64 commit_num,
674684
struct bitcoin_signature *o_sig)
675685
{
676686
STATUS_DEBUG(
677687
"%s:%d %s { "
678688
"\"self_id\":%s, \"peer_id\":%s, \"dbid\":%" PRIu64 ", "
679689
"\"counterparty_funding_pubkey\":%s, "
680690
"\"remote_per_commit\":%s, "
681-
"\"option_static_remotekey\":%s, \"tx\":%s }",
691+
"\"option_static_remotekey\":%s, \"tx\":%s, "
692+
"\"rhashes\":%s, \"commit_num\":%" PRIu64 " }",
682693
__FILE__, __LINE__, __FUNCTION__,
683694
dump_node_id(&self_id).c_str(),
684695
dump_node_id(peer_id).c_str(),
685696
dbid,
686697
dump_pubkey(counterparty_funding_pubkey).c_str(),
687698
dump_pubkey(remote_per_commit).c_str(),
688699
(option_static_remotekey ? "true" : "false"),
689-
dump_tx(tx).c_str()
700+
dump_tx(tx).c_str(),
701+
dump_rhashes(rhashes, tal_count(rhashes)).c_str(),
702+
commit_num
690703
);
691704

692705
last_message = "";
@@ -696,6 +709,8 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
696709
marshal_pubkey(remote_per_commit,
697710
req.mutable_remote_per_commit_point());
698711
marshal_single_input_tx(tx, NULL, req.mutable_tx());
712+
marshal_rhashes(rhashes, req.mutable_payment_hashes());
713+
req.set_commit_num(commit_num);
699714

700715
ClientContext context;
701716
SignatureReply rsp;

contrib/remote_hsmd/proxy.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
8888
u64 dbid,
8989
const struct pubkey *remote_per_commit,
9090
bool option_static_remotekey,
91+
struct sha256 *rhashes,
92+
u64 commit_num,
9193
struct bitcoin_signature *o_sig);
9294

9395
proxy_stat proxy_handle_get_per_commitment_point(

hsmd/hsm_wire.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ msgdata,hsm_sign_remote_commitment_tx,tx,bitcoin_tx,
169169
msgdata,hsm_sign_remote_commitment_tx,remote_funding_key,pubkey,
170170
msgdata,hsm_sign_remote_commitment_tx,remote_per_commit,pubkey,
171171
msgdata,hsm_sign_remote_commitment_tx,option_static_remotekey,bool,
172+
msgdata,hsm_sign_remote_commitment_tx,num_htlc_rhash,u16,
173+
msgdata,hsm_sign_remote_commitment_tx,htlc_rhash,sha256,num_htlc_rhash
174+
msgdata,hsm_sign_remote_commitment_tx,commit_num,u64,
172175

173176
# channeld asks HSM to sign remote HTLC tx.
174177
msgtype,hsm_sign_remote_htlc_tx,20

hsmd/hsmd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,12 +988,15 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
988988
const u8 *funding_wscript;
989989
struct pubkey remote_per_commit;
990990
bool option_static_remotekey;
991+
struct sha256 *htlc_rhash;
992+
u64 commit_num;
991993

992994
if (!fromwire_hsm_sign_remote_commitment_tx(tmpctx, msg_in,
993995
&tx,
994996
&remote_funding_pubkey,
995997
&remote_per_commit,
996-
&option_static_remotekey))
998+
&option_static_remotekey,
999+
&htlc_rhash, &commit_num))
9971000
return bad_req(conn, c, msg_in);
9981001
tx->chainparams = c->chainparams;
9991002

openingd/openingd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,8 @@ static bool funder_finalize_channel_setup(struct state *state,
751751
*tx,
752752
&state->channel->funding_pubkey[REMOTE],
753753
&state->first_per_commitment_point[REMOTE],
754-
state->channel->option_static_remotekey);
754+
state->channel->option_static_remotekey,
755+
NULL, 0);
755756

756757
wire_sync_write(HSM_FD, take(msg));
757758
msg = wire_sync_read(tmpctx, HSM_FD);
@@ -1296,7 +1297,8 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
12961297
remote_commit,
12971298
&state->channel->funding_pubkey[REMOTE],
12981299
&state->first_per_commitment_point[REMOTE],
1299-
state->channel->option_static_remotekey);
1300+
state->channel->option_static_remotekey,
1301+
NULL, 0);
13001302

13011303
wire_sync_write(HSM_FD, take(msg));
13021304
msg = wire_sync_read(tmpctx, HSM_FD);

0 commit comments

Comments
 (0)