Skip to content

Commit

Permalink
closingd: use hsmfd to get signatures, don't use seed.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jul 23, 2018
1 parent 6b700f9 commit 429aad8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
4 changes: 3 additions & 1 deletion closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ $(LIGHTNINGD_CLOSING_OBJS): $(LIGHTNINGD_HEADERS)
# Common source we use.
CLOSINGD_COMMON_OBJS := \
common/base32.o \
common/bip32.o \
common/close_tx.o \
common/crypto_state.o \
common/crypto_sync.o \
Expand All @@ -67,6 +68,7 @@ CLOSINGD_COMMON_OBJS := \
common/subdaemon.o \
common/type_to_string.o \
common/utils.o \
common/utxo.o \
common/version.o \
common/wire_error.o \
common/wireaddr.o \
Expand All @@ -81,7 +83,7 @@ closingd/gen_closing_wire.c: $(WIRE_GEN) closingd/closing_wire.csv

LIGHTNINGD_CLOSING_OBJS := $(LIGHTNINGD_CLOSING_SRC:.c=.o) $(LIGHTNINGD_CLOSING_GEN_SRC:.c=.o)

lightningd/lightning_closingd: $(LIGHTNINGD_CLOSING_OBJS) $(WIRE_ONION_OBJS) $(CLOSINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS)
lightningd/lightning_closingd: $(LIGHTNINGD_CLOSING_OBJS) $(WIRE_ONION_OBJS) $(CLOSINGD_COMMON_OBJS) $(WIRE_OBJS) $(BITCOIN_OBJS) $(LIGHTNINGD_HSM_CLIENT_OBJS)

check-source: $(LIGHTNINGD_CLOSING_SRC_NOGEN:%=check-src-include-order/%)
check-source-bolt: $(LIGHTNINGD_CLOSING_SRC:%=bolt-check/%) $(LIGHTNINGD_CLOSING_HEADERS:%=bolt-check/%)
Expand Down
30 changes: 18 additions & 12 deletions closingd/closing.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <common/version.h>
#include <common/wire_error.h>
#include <errno.h>
#include <hsmd/gen_hsm_client_wire.h>
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
Expand All @@ -26,6 +27,7 @@
#define REQ_FD STDIN_FILENO
#define PEER_FD 3
#define GOSSIP_FD 4
#define HSM_FD 5

static struct bitcoin_tx *close_tx(const tal_t *ctx,
struct crypto_state *cs,
Expand Down Expand Up @@ -147,7 +149,6 @@ static void send_offer(struct crypto_state *cs,
const u64 satoshi_out[NUM_SIDES],
enum side funder,
uint64_t our_dust_limit,
const struct secrets *secrets,
uint64_t fee_to_offer)
{
struct bitcoin_tx *tx;
Expand Down Expand Up @@ -176,10 +177,16 @@ static void send_offer(struct crypto_state *cs,
* own output.
*/
/* (We don't do this). */
sign_tx_input(tx, 0, NULL, funding_wscript,
&secrets->funding_privkey,
&funding_pubkey[LOCAL],
&our_sig);
wire_sync_write(HSM_FD,
take(towire_hsm_sign_mutual_close_tx(NULL,
tx,
&funding_pubkey[REMOTE],
funding_satoshi)));
msg = wire_sync_read(tmpctx, HSM_FD);
if (!fromwire_hsm_sign_tx_reply(msg, &our_sig))
status_failed(STATUS_FAIL_HSM_IO,
"Bad hsm_sign_mutual_close_tx reply %s",
tal_hex(tmpctx, msg));

status_trace("sending fee offer %"PRIu64, fee_to_offer);

Expand Down Expand Up @@ -424,7 +431,6 @@ int main(int argc, char *argv[])
struct crypto_state cs;
const tal_t *ctx = tal(NULL, char);
u8 *msg;
struct secret seed;
struct pubkey funding_pubkey[NUM_SIDES];
struct bitcoin_txid funding_txid;
u16 funding_txout;
Expand All @@ -435,7 +441,6 @@ int main(int argc, char *argv[])
enum side funder;
u8 *scriptpubkey[NUM_SIDES], *funding_wscript;
struct channel_id channel_id;
struct secrets secrets;
bool reconnected;
u64 next_index[NUM_SIDES], revocations_received;
enum side whose_turn;
Expand All @@ -448,9 +453,10 @@ int main(int argc, char *argv[])

msg = wire_sync_read(tmpctx, REQ_FD);
if (!fromwire_closing_init(ctx, msg,
&cs, &seed,
&cs,
&funding_txid, &funding_txout,
&funding_satoshi,
&funding_pubkey[LOCAL],
&funding_pubkey[REMOTE],
&funder,
&satoshi_out[LOCAL],
Expand All @@ -473,8 +479,6 @@ int main(int argc, char *argv[])
status_trace("dustlimit = %"PRIu64, our_dust_limit);
status_trace("fee = %"PRIu64, offer[LOCAL]);
derive_channel_id(&channel_id, &funding_txid, funding_txout);
derive_basepoints(&seed, &funding_pubkey[LOCAL], NULL,
&secrets, NULL);

funding_wscript = bitcoin_redeem_2of2(ctx,
&funding_pubkey[LOCAL],
Expand Down Expand Up @@ -504,7 +508,8 @@ int main(int argc, char *argv[])
funding_wscript,
scriptpubkey, &funding_txid, funding_txout,
funding_satoshi, satoshi_out, funder,
our_dust_limit, &secrets, offer[LOCAL]);
our_dust_limit,
offer[LOCAL]);
} else {
if (i == 0)
peer_billboard(false, "Waiting for their initial"
Expand Down Expand Up @@ -552,7 +557,8 @@ int main(int argc, char *argv[])
funding_wscript,
scriptpubkey, &funding_txid, funding_txout,
funding_satoshi, satoshi_out, funder,
our_dust_limit, &secrets, offer[LOCAL]);
our_dust_limit,
offer[LOCAL]);
} else {
peer_billboard(false, "Waiting for another"
" closing fee offer:"
Expand Down
2 changes: 1 addition & 1 deletion closingd/closing_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# Begin! (passes peer fd, gossipd-client fd)
closing_init,2001
closing_init,,crypto_state,struct crypto_state
closing_init,,seed,struct secret
closing_init,,funding_txid,struct bitcoin_txid
closing_init,,funding_txout,u16
closing_init,,funding_satoshi,u64
closing_init,,local_fundingkey,struct pubkey
closing_init,,remote_fundingkey,struct pubkey
closing_init,,funder,enum side
closing_init,,local_msatoshi,u64
Expand Down
8 changes: 7 additions & 1 deletion lightningd/closing_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <lightningd/chaintopology.h>
#include <lightningd/channel.h>
#include <lightningd/closing_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
#include <lightningd/options.h>
Expand Down Expand Up @@ -133,6 +134,7 @@ void peer_start_closingd(struct channel *channel,
u64 minfee, startfee, feelimit;
u64 num_revocations;
u64 funding_msatoshi, our_msatoshi, their_msatoshi;
int hsmfd;
struct lightningd *ld = channel->peer->ld;

if (!channel->remote_shutdown_scriptpubkey) {
Expand All @@ -141,6 +143,9 @@ void peer_start_closingd(struct channel *channel,
return;
}

hsmfd = hsm_get_client_fd(ld, &channel->peer->id, channel->dbid,
HSM_CAP_SIGN_CLOSING_TX);

channel_set_owner(channel,
new_channel_subd(ld,
"lightning_closingd",
Expand All @@ -149,6 +154,7 @@ void peer_start_closingd(struct channel *channel,
channel_errmsg,
channel_set_billboard,
take(&peer_fd), take(&gossip_fd),
take(&hsmfd),
NULL));
if (!channel->owner) {
log_unusual(channel->log, "Could not subdaemon closing: %s",
Expand Down Expand Up @@ -191,10 +197,10 @@ void peer_start_closingd(struct channel *channel,
their_msatoshi = funding_msatoshi - our_msatoshi;
initmsg = towire_closing_init(tmpctx,
cs,
&channel->seed,
&channel->funding_txid,
channel->funding_outnum,
channel->funding_satoshi,
&channel->local_funding_pubkey,
&channel->channel_info.remote_fundingkey,
channel->funder,
our_msatoshi / 1000, /* Rounds down */
Expand Down

0 comments on commit 429aad8

Please sign in to comment.