Skip to content

Commit 87a0941

Browse files
authored
adds payment_hashes and commit_num to sign_holder_commitment_tx (#26)
1 parent 2f78c0c commit 87a0941

13 files changed

+93
-24
lines changed

contrib/remote_hsmd/hsmd.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,11 +752,14 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
752752
u64 dbid;
753753
struct bitcoin_tx *tx;
754754
struct bitcoin_signature sig;
755+
struct sha256 *rhashes;
756+
u64 commit_num;
755757

756758
if (!fromwire_hsmd_sign_commitment_tx(tmpctx, msg_in,
757759
&peer_id, &dbid,
758760
&tx,
759-
&remote_funding_pubkey))
761+
&remote_funding_pubkey,
762+
&rhashes, &commit_num))
760763
return bad_req(conn, c, msg_in);
761764

762765
tx->chainparams = c->chainparams;
@@ -768,7 +771,8 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
768771
return bad_req_fmt(conn, c, msg_in, "tx must have > 0 outputs");
769772

770773
proxy_stat rv = proxy_handle_sign_commitment_tx(
771-
tx, &remote_funding_pubkey, &peer_id, dbid, &sig);
774+
tx, &remote_funding_pubkey, &peer_id, dbid,
775+
rhashes, commit_num, &sig);
772776
if (PROXY_PERMANENT(rv))
773777
status_failed(STATUS_FAIL_INTERNAL_ERROR,
774778
"proxy_%s failed: %s", __FUNCTION__,
@@ -821,7 +825,6 @@ static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
821825
tx, &remote_funding_pubkey,
822826
&c->id, c->dbid,
823827
&remote_per_commit,
824-
option_static_remotekey,
825828
rhashes, commit_num,
826829
&sig);
827830
if (PROXY_PERMANENT(rv))

contrib/remote_hsmd/proxy.cc

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -691,24 +691,21 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
691691
struct node_id *peer_id,
692692
u64 dbid,
693693
const struct pubkey *remote_per_commit,
694-
bool option_static_remotekey,
695694
struct sha256 *rhashes, u64 commit_num,
696695
struct bitcoin_signature *o_sig)
697696
{
698697
STATUS_DEBUG(
699698
"%s:%d %s { "
700699
"\"self_id\":%s, \"peer_id\":%s, \"dbid\":%" PRIu64 ", "
701700
"\"counterparty_funding_pubkey\":%s, "
702-
"\"remote_per_commit\":%s, "
703-
"\"option_static_remotekey\":%s, \"tx\":%s, "
701+
"\"remote_per_commit\":%s, \"tx\":%s, "
704702
"\"rhashes\":%s, \"commit_num\":%" PRIu64 " }",
705703
__FILE__, __LINE__, __FUNCTION__,
706704
dump_node_id(&self_id).c_str(),
707705
dump_node_id(peer_id).c_str(),
708706
dbid,
709707
dump_pubkey(counterparty_funding_pubkey).c_str(),
710708
dump_pubkey(remote_per_commit).c_str(),
711-
(option_static_remotekey ? "true" : "false"),
712709
dump_tx(tx).c_str(),
713710
dump_rhashes(rhashes, tal_count(rhashes)).c_str(),
714711
commit_num
@@ -1020,25 +1017,31 @@ proxy_stat proxy_handle_sign_commitment_tx(
10201017
const struct pubkey *counterparty_funding_pubkey,
10211018
struct node_id *peer_id,
10221019
u64 dbid,
1020+
struct sha256 *rhashes, u64 commit_num,
10231021
struct bitcoin_signature *o_sig)
10241022
{
10251023
STATUS_DEBUG(
10261024
"%s:%d %s { "
10271025
"\"self_id\":%s, \"peer_id\":%s, \"dbid\":%" PRIu64 ", "
1028-
"\"counterparty_funding_pubkey\":%s, \"tx\":%s }",
1026+
"\"counterparty_funding_pubkey\":%s, \"tx\":%s, "
1027+
"\"rhashes\":%s, \"commit_num\":%" PRIu64 " }",
10291028
__FILE__, __LINE__, __FUNCTION__,
10301029
dump_node_id(&self_id).c_str(),
10311030
dump_node_id(peer_id).c_str(),
10321031
dbid,
10331032
dump_pubkey(counterparty_funding_pubkey).c_str(),
1034-
dump_tx(tx).c_str()
1033+
dump_tx(tx).c_str(),
1034+
dump_rhashes(rhashes, tal_count(rhashes)).c_str(),
1035+
commit_num
10351036
);
10361037

10371038
last_message = "";
10381039
SignHolderCommitmentTxRequest req;
10391040
marshal_node_id(&self_id, req.mutable_node_id());
10401041
marshal_channel_nonce(peer_id, dbid, req.mutable_channel_nonce());
10411042
marshal_single_input_tx(tx, NULL, req.mutable_tx());
1043+
marshal_rhashes(rhashes, req.mutable_payment_hashes());
1044+
req.set_commit_num(commit_num);
10421045

10431046
ClientContext context;
10441047
SignatureReply rsp;

contrib/remote_hsmd/proxy.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ proxy_stat proxy_handle_sign_remote_commitment_tx(
9191
struct node_id *peer_id,
9292
u64 dbid,
9393
const struct pubkey *remote_per_commit,
94-
bool option_static_remotekey,
9594
struct sha256 *rhashes,
9695
u64 commit_num,
9796
struct bitcoin_signature *o_sig);
@@ -130,6 +129,8 @@ proxy_stat proxy_handle_sign_commitment_tx(
130129
const struct pubkey *remote_funding_pubkey,
131130
struct node_id *peer_id,
132131
u64 dbid,
132+
struct sha256 *rhashes,
133+
u64 commit_num,
133134
struct bitcoin_signature *o_sig);
134135

135136
proxy_stat proxy_handle_cannouncement_sig(

hsmd/hsmd.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,13 +984,16 @@ static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
984984
struct secret channel_seed;
985985
struct bitcoin_tx *tx;
986986
struct bitcoin_signature sig;
987+
struct sha256 *rhashes;
988+
u64 commit_num;
987989
struct secrets secrets;
988990
const u8 *funding_wscript;
989991

990992
if (!fromwire_hsmd_sign_commitment_tx(tmpctx, msg_in,
991993
&peer_id, &dbid,
992994
&tx,
993-
&remote_funding_pubkey))
995+
&remote_funding_pubkey,
996+
&rhashes, &commit_num))
994997
return bad_req(conn, c, msg_in);
995998

996999
tx->chainparams = c->chainparams;

hsmd/hsmd_wire.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ msgdata,hsmd_sign_commitment_tx,peer_id,node_id,
133133
msgdata,hsmd_sign_commitment_tx,channel_dbid,u64,
134134
msgdata,hsmd_sign_commitment_tx,tx,bitcoin_tx,
135135
msgdata,hsmd_sign_commitment_tx,remote_funding_key,pubkey,
136+
msgdata,hsmd_sign_commitment_tx,num_htlc_rhash,u16,
137+
msgdata,hsmd_sign_commitment_tx,htlc_rhash,sha256,num_htlc_rhash
138+
msgdata,hsmd_sign_commitment_tx,commit_num,u64,
136139

137140
msgtype,hsmd_sign_commitment_tx_reply,105
138141
msgdata,hsmd_sign_commitment_tx_reply,sig,bitcoin_signature,

hsmd/hsmd_wiregen.c

Lines changed: 16 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

hsmd/hsmd_wiregen.h

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lightningd/peer_control.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,56 @@ static void sign_last_tx(struct channel *channel)
186186
struct bitcoin_signature sig;
187187
u8 *msg, **witness;
188188

189+
struct htlc_in_map *htlcs_in = &channel->peer->ld->htlcs_in;
190+
struct htlc_out_map *htlcs_out = &channel->peer->ld->htlcs_out;
191+
192+
// Count how many payment hashes we will be sending.
193+
size_t num_entries = 0;
194+
struct htlc_in_map_iter ini;
195+
struct htlc_in *hin;
196+
for (hin = htlc_in_map_first(htlcs_in, &ini);
197+
hin;
198+
hin = htlc_in_map_next(htlcs_in, &ini))
199+
if (hin->key.channel == channel)
200+
++num_entries;
201+
struct htlc_out_map_iter outi;
202+
struct htlc_out *hout;
203+
for (hout = htlc_out_map_first(htlcs_out, &outi);
204+
hout;
205+
hout = htlc_out_map_next(htlcs_out, &outi))
206+
if (hout->key.channel == channel)
207+
++num_entries;
208+
209+
// Gather the payment hashes.
210+
struct sha256 *rhashes = tal_arrz(tmpctx, struct sha256, num_entries);
211+
size_t nrhash = 0;
212+
for (hin = htlc_in_map_first(htlcs_in, &ini);
213+
hin;
214+
hin = htlc_in_map_next(htlcs_in, &ini)) {
215+
if (hin->key.channel != channel)
216+
continue;
217+
memcpy(&rhashes[nrhash], &hin->payment_hash, sizeof(rhashes[nrhash]));
218+
++nrhash;
219+
}
220+
for (hout = htlc_out_map_first(htlcs_out, &outi);
221+
hout;
222+
hout = htlc_out_map_next(htlcs_out, &outi)) {
223+
if (hout->key.channel != channel)
224+
continue;
225+
memcpy(&rhashes[nrhash], &hout->payment_hash, sizeof(rhashes[nrhash]));
226+
++nrhash;
227+
}
228+
assert(nrhash == num_entries);
229+
230+
u64 commit_index = channel->next_index[LOCAL] - 1;
231+
189232
assert(!channel->last_tx->wtx->inputs[0].witness);
190233
msg = towire_hsmd_sign_commitment_tx(tmpctx,
191234
&channel->peer->id,
192235
channel->dbid,
193236
channel->last_tx,
194-
&channel->channel_info
195-
.remote_fundingkey);
237+
&channel->channel_info.remote_fundingkey,
238+
rhashes, commit_index);
196239

197240
if (!wire_sync_write(ld->hsm_fd, take(msg)))
198241
fatal("Could not write to HSM: %s", strerror(errno));

lightningd/test/run-invoice-select-inchan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ u8 *towire_gossipd_get_incoming_channels(const tal_t *ctx UNNEEDED)
638638
u8 *towire_hsmd_sign_bolt12(const tal_t *ctx UNNEEDED, const wirestring *messagename UNNEEDED, const wirestring *fieldname UNNEEDED, const struct sha256 *merkleroot UNNEEDED, const u8 *publictweak UNNEEDED)
639639
{ fprintf(stderr, "towire_hsmd_sign_bolt12 called!\n"); abort(); }
640640
/* Generated stub for towire_hsmd_sign_commitment_tx */
641-
u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED)
641+
u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, const struct sha256 *htlc_rhash UNNEEDED, u64 commit_num UNNEEDED)
642642
{ fprintf(stderr, "towire_hsmd_sign_commitment_tx called!\n"); abort(); }
643643
/* Generated stub for towire_hsmd_sign_invoice */
644644
u8 *towire_hsmd_sign_invoice(const tal_t *ctx UNNEEDED, const u8 *u5bytes UNNEEDED, const u8 *hrp UNNEEDED)

wallet/db_postgres_sqlgen.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wallet/db_sqlite3_sqlgen.c

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wallet/statements_gettextgen.po

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

wallet/test/run-wallet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ u8 *towire_gossipd_get_stripped_cupdate(const tal_t *ctx UNNEEDED, const struct
789789
u8 *towire_hsmd_get_output_scriptpubkey(const tal_t *ctx UNNEEDED, u64 channel_id UNNEEDED, const struct node_id *peer_id UNNEEDED, const struct pubkey *commitment_point UNNEEDED)
790790
{ fprintf(stderr, "towire_hsmd_get_output_scriptpubkey called!\n"); abort(); }
791791
/* Generated stub for towire_hsmd_sign_commitment_tx */
792-
u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED)
792+
u8 *towire_hsmd_sign_commitment_tx(const tal_t *ctx UNNEEDED, const struct node_id *peer_id UNNEEDED, u64 channel_dbid UNNEEDED, const struct bitcoin_tx *tx UNNEEDED, const struct pubkey *remote_funding_key UNNEEDED, const struct sha256 *htlc_rhash UNNEEDED, u64 commit_num UNNEEDED)
793793
{ fprintf(stderr, "towire_hsmd_sign_commitment_tx called!\n"); abort(); }
794794
/* Generated stub for towire_incorrect_cltv_expiry */
795795
u8 *towire_incorrect_cltv_expiry(const tal_t *ctx UNNEEDED, u32 cltv_expiry UNNEEDED, const u8 *channel_update UNNEEDED)

0 commit comments

Comments
 (0)