Skip to content

Commit ca959b1

Browse files
committed
Initial batch of remote-signer RPC commands. (squashed cpp-hsmd)
Implemented PassClientHSMFd Added proxy_handle_get_per_commitment_point. Added proxy_handle_sign_remote_htlc_tx (server side needed for testing) Modified proxy_handle_sign_remote_htlc_tx to use original code until server is implemented. Added proxy_handle_sign_invoice, needs final wiring when server implemented. Added proxy_handle_channel_update_sig. Hack to log un-proxied handlers. Added proxy_handle_get_channel_basepoints. Added proxy_handle_sign_mutual_close_tx. Fixed witscript present test wrt NULL witscript instead of NULL ptr. Improved the PROXY_IMPL warning code. Added SignCommitmentTx Reverted handle_sign_withdrawal_tx to PROXY_IMPL_MARSHALED due to problem signing P2SH Disabled num_output==2 assert in proxy_handle_sign_withdrawal_tx Made handle_sign_withdrawal_tx remote signing dependent at runtime on P2SH inputs. Updated NOTES and TODO. Don't attempt to sign withdraw_tx if close_info present. Added proxy_handle_cannouncement_sig Added proxy_handle_sign_node_announcement Added proxy_handle_sign_penalty_to_us Added proxy_handle_sign_delayed_payment_to_us Added proxy_handle_sign_local_htlc_tx Reconciled similar methods; added SignRemoteHTLCToUs Remove loops from single input cases.. Added proxy_handle_check_future_secret Rebased cpp-hsmd-3 onto master after prior PRs merged. Merged reconcile-api branch Added new command-line for simple test. Updated NOTES build instructions. Added handling for grpc::Status CANCELLED. Added more running notes. Factored common transaction fields into Transaction submessage Log grpc to stderr Factored into setup_single_input_tx Added Makefile dependency for the proxy.o on the generated grpc headers. Wrapped signatures w/ typed messages. Wrapped API method parameters w/ typed messages. Added missing SignMessage API call. Renamed methods to have consistent Sign prefix Implemented SignChannelUpdate using single channel_update argument. Made proxy marshal/unmarshal naming consistent. Adapted to remoteserver API cleanup. Implemented sign-node-announcement. API updates. Added proxy_handle_sign_invoice and proxy_handle_sign_remote_htlc_tx. Added get-channel-basepoints. Added get-per-commitment-point. Added sign-local-htlc-tx. Added sign-remote-htlc-to-us. Added sign-delayed-payment-to-us. Added sign-penalty-to-us. Added sign-commitment-tx and sign-mutual-close-tx. Added check-future-secret. Added sign-message. Added sign-channel-announcement. API mods: SpendType, Descriptors, Funding return Added p2sh-p2wpkh support in funding transactions. Added unilateral-close-info. Transposed hsmd changes from c-lightning master. Removed all private keys/secrets from remote_hsmd. Amended the GetExtPubKey interface, removed derivation_path. Added scripts/{run-all-tests,run-one-test}. Updated dependency instructions and test running instructions. Skip proxy_handle_pass_client_hsmfd for zero dbid (master, gossipd, connectd). Made contrib/remote_hsmd/hsm_wire.csv a symbolic link to hsmd/hsm_wire.csv.
1 parent ade10e7 commit ca959b1

File tree

19 files changed

+3739
-5
lines changed

19 files changed

+3739
-5
lines changed

.dir-locals.el

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,10 @@
44
(c-basic-offset . 8)
55
(tab-width . 8)
66
))
7-
)
7+
8+
(c++-mode . ((c-file-style . "linux")
9+
(indent-tabs-mode . t)
10+
(show-trailing-whitespace . t)
11+
(c-basic-offset . 8)
12+
(tab-width . 8)
13+
)))

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ ALL_C_SOURCES :=
222222
ALL_C_HEADERS := header_versions_gen.h version_gen.h
223223

224224
CPPFLAGS += -DBINTOPKGLIBEXECDIR="\"$(shell sh tools/rel.sh $(bindir) $(pkglibexecdir))\""
225-
CFLAGS = $(CPPFLAGS) $(CWARNFLAGS) $(CDEBUGFLAGS) $(COPTFLAGS) -I $(CCANDIR) $(EXTERNAL_INCLUDE_FLAGS) -I . -I/usr/local/include $(SQLITE3_CFLAGS) $(POSTGRES_INCLUDE) $(FEATURES) $(COVFLAGS) $(DEV_CFLAGS) -DSHACHAIN_BITS=48 -DJSMN_PARENT_LINKS $(PIE_CFLAGS) $(COMPAT_CFLAGS) -DBUILD_ELEMENTS=1
225+
CMNFLAGS = $(CDEBUGFLAGS) $(COPTFLAGS) -I $(CCANDIR) $(EXTERNAL_INCLUDE_FLAGS) -I . -I/usr/local/include $(SQLITE3_CFLAGS) $(POSTGRES_INCLUDE) $(FEATURES) $(COVFLAGS) $(DEV_CFLAGS) -DSHACHAIN_BITS=48 -DJSMN_PARENT_LINKS $(PIE_CFLAGS) $(COMPAT_CFLAGS) -DBUILD_ELEMENTS=1
226+
CFLAGS = $(CPPFLAGS) $(CWARNFLAGS) $(CMNFLAGS)
226227
# If CFLAGS is already set in the environment of make (to whatever value, it
227228
# does not matter) then it would export it to subprocesses with the above value
228229
# we set, including CWARNFLAGS which by default contains -Wall -Werror. This
@@ -320,6 +321,7 @@ include devtools/Makefile
320321
include tools/Makefile
321322
include plugins/Makefile
322323
include tests/plugins/Makefile
324+
include contrib/remote_hsmd/Makefile
323325
ifneq ($(FUZZING),0)
324326
include tests/fuzz/Makefile
325327
endif
@@ -346,7 +348,8 @@ PKGLIBEXEC_PROGRAMS = \
346348
lightningd/lightning_gossipd \
347349
lightningd/lightning_hsmd \
348350
lightningd/lightning_onchaind \
349-
lightningd/lightning_openingd
351+
lightningd/lightning_openingd \
352+
lightningd/remote_hsmd
350353

351354
# Don't delete these intermediaries.
352355
.PRECIOUS: $(ALL_GEN_HEADERS) $(ALL_GEN_SOURCES)

contrib/pyln-testing/pyln/testing/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def env(name, default=None):
7979
SLOW_MACHINE = env("SLOW_MACHINE", "0") == "1"
8080
DEPRECATED_APIS = env("DEPRECATED_APIS", "0") == "1"
8181
TIMEOUT = int(env("TIMEOUT", 180 if SLOW_MACHINE else 60))
82-
82+
SUBDAEMON = env("SUBDAEMON", "")
8383

8484
def wait_for(success, timeout=TIMEOUT):
8585
start_time = time.time()
@@ -538,6 +538,9 @@ def __init__(self, lightning_dir, bitcoindproxy, port=9735, random_hsm=False, no
538538
'bitcoin-datadir': lightning_dir,
539539
}
540540

541+
if SUBDAEMON:
542+
opts['subdaemon'] = SUBDAEMON
543+
541544
for k, v in opts.items():
542545
self.opts[k] = v
543546

contrib/remote_hsmd/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/remotesigner.grpc.pb.cc
2+
/remotesigner.grpc.pb.h
3+
/remotesigner.pb.cc
4+
/remotesigner.pb.h
5+
/remotesigner.proto

contrib/remote_hsmd/Makefile

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#! /usr/bin/make
2+
3+
# Designed to be run at the top
4+
rmthsmd-wrongdir:
5+
$(MAKE) -C ../.. lightningd/hsm-all
6+
7+
default: rmthsmd-all
8+
9+
LIGHTNINGD_RMTHSM_SRC := contrib/remote_hsmd/hsmd.c \
10+
contrib/remote_hsmd/gen_hsm_wire.c \
11+
contrib/remote_hsmd/remotesigner.pb.cc \
12+
contrib/remote_hsmd/remotesigner.grpc.pb.cc \
13+
contrib/remote_hsmd/dump.cc \
14+
contrib/remote_hsmd/proxy.cc
15+
LIGHTNINGD_RMTHSM_HEADERS := contrib/remote_hsmd/gen_hsm_wire.h \
16+
contrib/remote_hsmd/remotesigner.pb.h \
17+
contrib/remote_hsmd/remotesigner.grpc.pb.h
18+
LIGHTNINGD_RMTHSM_CCOBJS := $(LIGHTNINGD_RMTHSM_SRC:.cc=.o)
19+
LIGHTNINGD_RMTHSM_OBJS := $(LIGHTNINGD_RMTHSM_CCOBJS:.c=.o)
20+
21+
HOST_SYSTEM = $(shell uname | cut -f 1 -d_)
22+
SYSTEM ?= $(HOST_SYSTEM)
23+
CXX = g++
24+
CXXFLAGS += -std=c++11 \
25+
-I. \
26+
-Iccan \
27+
-Iexternal/libwally-core/include \
28+
-Iexternal/libwally-core/src/secp256k1/include \
29+
-g
30+
PROTOC = protoc
31+
GRPC_CPP_PLUGIN = grpc_cpp_plugin
32+
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`
33+
PROTOS_PATH = contrib/remote_hsmd
34+
35+
# Common source we use.
36+
RMTHSMD_COMMON_OBJS := \
37+
common/amount.o \
38+
common/bigsize.o \
39+
common/bip32.o \
40+
common/daemon.o \
41+
common/daemon_conn.o \
42+
common/derive_basepoints.o \
43+
common/funding_tx.o \
44+
common/gen_status_wire.o \
45+
common/hash_u5.o \
46+
common/key_derive.o \
47+
common/memleak.o \
48+
common/msg_queue.o \
49+
common/node_id.o \
50+
common/permute_tx.o \
51+
common/status.o \
52+
common/status_wire.o \
53+
common/subdaemon.o \
54+
common/type_to_string.o \
55+
common/utils.o \
56+
common/utxo.o \
57+
common/version.o \
58+
common/withdraw_tx.o
59+
60+
# For checking
61+
LIGHTNINGD_RMTHSM_ALLSRC_NOGEN := $(filter-out contrib/remote_hsmd/gen_%, $(LIGHTNINGD_RMTHSM_SRC) $(LIGHTNINGD_RMTHSM_SRC))
62+
LIGHTNINGD_RMTHSM_ALLHEADERS_NOGEN := $(filter-out contrib/remote_hsmd/gen_%, $(LIGHTNINGD_RMTHSM_HEADERS))
63+
64+
$(LIGHTNINGD_RMTHSM_OBJS): $(LIGHTNINGD_HEADERS)
65+
66+
# Make sure these depend on everything.
67+
ALL_OBJS += $(LIGHTNINGD_RMTHSM_OBJS)
68+
ALL_PROGRAMS += lightningd/remote_hsmd
69+
ALL_GEN_HEADERS += contrib/remote_hsmd/gen_hsm_wire.h
70+
ALL_GEN_HEADERS += contrib/remote_hsmd/remotesigner.pb.h contrib/remote_hsmd/remotesigner.grpc.pb.h
71+
72+
rmthsmd-all: lightningd/remote_hsmd
73+
74+
lightningd/remote_hsmd: $(LIGHTNINGD_RMTHSM_OBJS) $(LIGHTNINGD_LIB_OBJS) $(RMTHSMD_COMMON_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS)
75+
76+
contrib/remote_hsmd/remotesigner.pb.o: $(ALL_GEN_HEADERS)
77+
78+
contrib/remote_hsmd/remotesigner.grpc.pb.o contrib/remote_hsmd/remotesigner.pb.o contrib/remote_hsmd/proxy.o contrib/remote_hsmd/dump.o : CPPFLAGS += $(CMNFLAGS) `pkg-config --cflags protobuf grpc`
79+
lightningd/remote_hsmd: LDLIBS += -L/usr/local/lib \
80+
`pkg-config --libs protobuf grpc++`\
81+
-pthread\
82+
-Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
83+
-ldl
84+
lightningd/remote_hsmd: LDLIBS += -lstdc++
85+
86+
contrib/remote_hsmd/gen_hsm_wire.h: $(WIRE_GEN) contrib/remote_hsmd/hsm_wire.csv
87+
$(WIRE_GEN) --page header $@ hsm_wire_type < contrib/remote_hsmd/hsm_wire.csv > $@
88+
89+
contrib/remote_hsmd/gen_hsm_wire.c: $(WIRE_GEN) contrib/remote_hsmd/hsm_wire.csv
90+
$(WIRE_GEN) --page impl ${@:.c=.h} hsm_wire_type < contrib/remote_hsmd/hsm_wire.csv > $@
91+
92+
check-source: $(LIGHTNINGD_RMTHSM_ALLSRC_NOGEN:%=check-src-include-order/%) $(LIGHTNINGD_RMTHSM_ALLHEADERS_NOGEN:%=check-hdr-include-order/%)
93+
check-source-bolt: $(LIGHTNINGD_RMTHSM_SRC:%=bolt-check/%)
94+
95+
check-whitespace: $(LIGHTNINGD_RMTHSM_ALLSRC_NOGEN:%=check-whitespace/%) $(LIGHTNINGD_RMTHSM_ALLHEADERS_NOGEN:%=check-whitespace/%)
96+
97+
clean: lightningd/rmthsm-clean
98+
99+
lightningd/rmthsm-clean:
100+
$(RM) $(LIGHTNINGD_RMTHSM_OBJS) contrib/remote_hsmd/gen_*
101+
$(RM) contrib/remote_hsmd/*.pb.cc contrib/remote_hsmd/*.pb.h
102+
103+
.PRECIOUS: %.grpc.pb.cc
104+
%.grpc.pb.cc: %.proto
105+
$(PROTOC) -I $(PROTOS_PATH) --grpc_out=contrib/remote_hsmd --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<
106+
107+
.PRECIOUS: %.pb.cc
108+
%.pb.cc: %.proto
109+
$(PROTOC) -I $(PROTOS_PATH) --cpp_out=contrib/remote_hsmd $<
110+
111+
-include contrib/remote_hsmd/test/Makefile

contrib/remote_hsmd/NOTES.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
c-lightning
2+
----------------------------------------------------------------
3+
4+
Setup
5+
6+
cd contrib/remote_hsmd && \
7+
ln -s /path/to/rust-lightning-signer/src/server/remotesigner.proto
8+
9+
Building
10+
11+
./configure --enable-developer
12+
make
13+
14+
Dependencies
15+
16+
Build libsecp256k1 with `./configure --enable-module-recovery`, see
17+
https://github.com/golemfactory/golem/issues/2168 for background.
18+
19+
pip3 install --user base58
20+
pip3 install --user bitstring
21+
pip3 install --user secp256k1
22+
23+
Run all of the integration tests:
24+
25+
./contrib/remote_hsmd/scripts/run-all-tests |& tee log
26+
27+
Run a single test:
28+
29+
./contrib/remote_hsmd/scripts/run-one-test $THETEST |& tee log
30+
31+
Some popular tests:
32+
33+
# sign-invoice, handle-sign-remote-htlc-tx
34+
export THETEST=tests/test_connection.py::test_balance
35+
export THETEST=tests/test_pay.py::test_sendpay
36+
export THETEST=tests/test_pay.py::test_pay
37+
38+
# sign-local-htlc-tx
39+
export THETEST=tests/test_closing.py::test_onchain_different_fees
40+
41+
# sign-remote-htlc-to-us
42+
export THETEST=tests/test_closing.py::test_onchain_feechange
43+
export THETEST=tests/test_closing.py::test_onchain_all_dust
44+
export THETEST=tests/test_closing.py::test_permfail_new_commit
45+
46+
# sign-delayed-payment-to-us
47+
export THETEST=tests/test_closing.py::test_onchain_multihtlc_our_unilateral
48+
export THETEST=tests/test_closing.py::test_onchain_multihtlc_their_unilateral
49+
export THETEST=tests/test_closing.py::test_permfail_htlc_in
50+
export THETEST=tests/test_closing.py::test_permfail_htlc_out
51+
52+
# sign-penalty-to-us
53+
export THETEST=tests/test_closing.py::test_penalty_inhtlc
54+
export THETEST=tests/test_closing.py::test_penalty_outhtlc
55+
export THETEST=tests/test_closing.py::test_closing
56+
57+
# sign-mutual-close
58+
export THETEST=tests/test_closing.py::test_closing
59+
60+
# check-future-secret
61+
export THETEST=tests/test_connection.py::test_dataloss_protection
62+
63+
# sign-message
64+
export THETEST=tests/test_misc.py::test_signmessage
65+
66+
# sign-channel-announcement
67+
export THETEST=tests/test_closing.py::test_closing_different_fees
68+
69+
# P2SH_P2WPKH
70+
export THETEST=tests/test_closing.py::test_onchain_first_commit
71+
export THETEST=tests/test_connection.py::test_disconnect_funder
72+
export THETEST=tests/test_connection.py::test_disconnect_fundee
73+
export THETEST=tests/test_connection.py::test_reconnect_signed
74+
export THETEST=tests/test_connection.py::test_reconnect_openingd
75+
export THETEST=tests/test_connection.py::test_shutdown_awaiting_lockin
76+
77+
# unilateral_close_info option_static_remotekey
78+
export THETEST=tests/test_connection.py::test_fee_limits
79+
export THETEST=tests/test_closing.py::test_option_upfront_shutdown_script
80+
81+
rust-lightning-signer
82+
----------------------------------------------------------------
83+
84+
cargo run --bin server |& tee log3
85+
86+
87+
Signing Formats
88+
```
89+
rust-lightning c-lightning rust-lightning-signer
90+
p2pkh P2PKH
91+
p2sh
92+
p2wpkh p2wpkh P2WPKH
93+
p2shwpkh p2sh-p2wpkh P2SH_P2WPKH
94+
p2wsh
95+
p2shwsh
96+
```
97+
98+
99+
Failing tests after removing seed from hsmd:
100+
```
101+
export THETEST=tests/test_misc.py::test_blockchaintrack
102+
export THETEST=tests/test_misc.py::test_new_node_is_mainnet
103+
export THETEST=tests/test_misc.py::test_getsharedsecret
104+
export THETEST=tests/test_wallet.py::test_hsm_secret_encryption
105+
export THETEST=tests/test_wallet.py::test_hsmtool_secret_decryption
106+
```

contrib/remote_hsmd/TODO.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
API Coverage
3+
----------------------------------------------------------------
4+
5+
## Failing Tests
6+
7+
# Frequently Intermittent
8+
tests/test_connection.py::test_funding_cancel_race
9+
tests/test_misc.py::test_bad_onion_immediate_peer
10+
11+
## Proxy Scoreboard
12+
13+
```
14+
COMPLETE proxy_stat proxy_handle_ecdh
15+
COMPLETE proxy_stat proxy_handle_pass_client_hsmfd
16+
COMPLETE proxy_stat proxy_handle_sign_remote_commitment_tx
17+
COMPLETE proxy_stat proxy_handle_channel_update_sig
18+
COMPLETE proxy_stat proxy_handle_sign_node_announcement
19+
COMPLETE proxy_stat proxy_handle_sign_remote_htlc_tx
20+
COMPLETE proxy_stat proxy_handle_sign_invoice
21+
COMPLETE proxy_stat proxy_handle_get_channel_basepoints
22+
COMPLETE proxy_stat proxy_handle_get_per_commitment_point
23+
COMPLETE proxy_stat proxy_handle_sign_local_htlc_tx
24+
COMPLETE proxy_stat proxy_handle_sign_remote_htlc_to_us
25+
COMPLETE proxy_stat proxy_handle_sign_delayed_payment_to_us
26+
COMPLETE proxy_stat proxy_handle_sign_penalty_to_us
27+
COMPLETE proxy_stat proxy_handle_sign_commitment_tx
28+
COMPLETE proxy_stat proxy_handle_sign_mutual_close_tx
29+
COMPLETE proxy_stat proxy_handle_check_future_secret
30+
COMPLETE proxy_stat proxy_handle_cannouncement_sig
31+
COMPLETE proxy_stat proxy_handle_sign_message
32+
33+
PARTIAL (-P2SH) proxy_stat proxy_handle_sign_withdrawal_tx
34+
35+
MARSHALED proxy_stat proxy_init_hsm
36+
```
37+
38+
Improvements
39+
----------------------------------------------------------------
40+
41+
#### Remove contrib/remote_signer/hsm_wire.csv
42+
43+
Generate gen_hsm_wire.{h,c} from c-lightning/hsmd/hsm_wire.csv instead.
44+

contrib/remote_hsmd/capabilities.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef LIGHTNING_HSMD_CAPABILITIES_H
2+
#define LIGHTNING_HSMD_CAPABILITIES_H
3+
4+
#define HSM_CAP_ECDH 1
5+
#define HSM_CAP_SIGN_GOSSIP 2
6+
#define HSM_CAP_SIGN_ONCHAIN_TX 4
7+
#define HSM_CAP_COMMITMENT_POINT 8
8+
#define HSM_CAP_SIGN_REMOTE_TX 16
9+
#define HSM_CAP_SIGN_CLOSING_TX 32
10+
11+
#define HSM_CAP_MASTER 1024
12+
#endif /* LIGHTNING_HSMD_CAPABILITIES_H */

0 commit comments

Comments
 (0)