Skip to content

Commit a306e58

Browse files
committed
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 9ec670a commit a306e58

File tree

9 files changed

+60
-8
lines changed

9 files changed

+60
-8
lines changed

channeld/channeld.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,10 +820,22 @@ static struct bitcoin_signature *calc_commitsigs(const tal_t *ctx,
820820
const u8 *msg;
821821
struct bitcoin_signature *htlc_sigs;
822822

823+
size_t num_entries = tal_count(htlc_map);
824+
struct sha256 *rhashes = tal_arrz(tmpctx, struct sha256, num_entries);
825+
size_t nrhash = 0;
826+
for (size_t ndx = 0; ndx < num_entries; ++ndx) {
827+
if (htlc_map[ndx]) {
828+
memcpy(&rhashes[nrhash], &htlc_map[ndx]->rhash, sizeof(rhashes[nrhash]));
829+
++nrhash;
830+
}
831+
}
832+
tal_resize(&rhashes, nrhash);
833+
823834
msg = towire_hsmd_sign_remote_commitment_tx(NULL, txs[0],
824835
&peer->channel->funding_pubkey[REMOTE],
825836
&peer->remote_per_commit,
826-
peer->channel->option_static_remotekey);
837+
peer->channel->option_static_remotekey,
838+
rhashes, commit_index);
827839

828840
msg = hsm_req(tmpctx, take(msg));
829841
if (!fromwire_hsmd_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/hsmd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,12 +1041,15 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
10411041
const u8 *funding_wscript;
10421042
struct pubkey remote_per_commit;
10431043
bool option_static_remotekey;
1044+
struct sha256 *htlc_rhash;
1045+
u64 commit_num;
10441046

10451047
if (!fromwire_hsmd_sign_remote_commitment_tx(tmpctx, msg_in,
10461048
&tx,
10471049
&remote_funding_pubkey,
10481050
&remote_per_commit,
1049-
&option_static_remotekey))
1051+
&option_static_remotekey,
1052+
&htlc_rhash, &commit_num))
10501053
return bad_req(conn, c, msg_in);
10511054
tx->chainparams = c->chainparams;
10521055

openingd/openingd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@ static bool funder_finalize_channel_setup(struct state *state,
586586
*tx,
587587
&state->channel->funding_pubkey[REMOTE],
588588
&state->first_per_commitment_point[REMOTE],
589-
state->channel->option_static_remotekey);
589+
state->channel->option_static_remotekey,
590+
NULL, 0);
590591

591592
wire_sync_write(HSM_FD, take(msg));
592593
msg = wire_sync_read(tmpctx, HSM_FD);
@@ -1138,7 +1139,8 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
11381139
remote_commit,
11391140
&state->channel->funding_pubkey[REMOTE],
11401141
&state->first_per_commitment_point[REMOTE],
1141-
state->channel->option_static_remotekey);
1142+
state->channel->option_static_remotekey,
1143+
NULL, 0);
11421144

11431145
wire_sync_write(HSM_FD, take(msg));
11441146
msg = wire_sync_read(tmpctx, HSM_FD);

0 commit comments

Comments
 (0)