diff --git a/hsmd/Makefile b/hsmd/Makefile index 5982302d4..6e1870524 100644 --- a/hsmd/Makefile +++ b/hsmd/Makefile @@ -1,7 +1,9 @@ #! /usr/bin/make HSMD_SRC := hsmd/hsmd.c \ - hsmd/hsmd_wiregen.c + hsmd/hsmd_wiregen.c \ + hsmd/libhsmd.c + HSMD_HEADERS := hsmd/hsmd_wiregen.h HSMD_OBJS := $(HSMD_SRC:.c=.o) diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index 9ea8079f1..7e499b324 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -43,6 +43,7 @@ #include /*~ _wiregen files are autogenerated by tools/generate-wire.py */ #include +#include #include #include #include @@ -61,29 +62,20 @@ * stream from lightningd. */ #define REQ_FD 3 -/*~ Nobody will ever find it here! hsm_secret is our root secret, the bip32 - * tree and bolt12 payer_id keys are derived from that, and cached here. */ -static struct { - struct secret hsm_secret; - struct ext_key bip32; - secp256k1_keypair bolt12; -} secretstuff; - -/* Have we initialized the secretstuff? */ -static bool initialized = false; - /* Version codes for BIP32 extended keys in libwally-core. * It's not suitable to add this struct into client struct, * so set it static.*/ -static struct bip32_key_version bip32_key_version; +extern struct bip32_key_version bip32_key_version; #if DEVELOPER /* If they specify --dev-force-privkey it ends up in here. */ -static struct privkey *dev_force_privkey; +extern struct privkey *dev_force_privkey; /* If they specify --dev-force-bip32-seed it ends up in here. */ -static struct secret *dev_force_bip32_seed; +extern struct secret *dev_force_bip32_seed; #endif +extern bool initialized; + /*~ We keep track of clients, but there's not much to keep. */ struct client { /* The ccan/io async io connection for this client: it closes, we die. */ @@ -110,6 +102,9 @@ struct client { /* Params to apply to all transactions for this client */ const struct chainparams *chainparams; + + /* Client context to pass over to libhsmd for its calls. */ + struct hsmd_client *hsmd_client; }; /*~ We keep a map of nonzero dbid -> clients, mainly for leak detection. @@ -266,6 +261,7 @@ static struct client *new_client(const tal_t *ctx, if (dbid == 0) { assert(num_dbid_zero_clients < ARRAY_SIZE(dbid_zero_clients)); dbid_zero_clients[num_dbid_zero_clients++] = c; + c->hsmd_client = hsmd_client_new_main(c, c->capabilities, c); } else { struct client *old_client = uintmap_get(&clients, dbid); @@ -277,6 +273,8 @@ static struct client *new_client(const tal_t *ctx, status_failed(STATUS_FAIL_INTERNAL_ERROR, "Failed inserting dbid %"PRIu64, dbid); tal_add_destructor(c, destroy_client); + c->hsmd_client = + hsmd_client_new_peer(c, c->capabilities, dbid, id, c); } return c; diff --git a/hsmd/libhsmd.c b/hsmd/libhsmd.c new file mode 100644 index 000000000..f26eb7953 --- /dev/null +++ b/hsmd/libhsmd.c @@ -0,0 +1,46 @@ +#include +#include + +/* Version codes for BIP32 extended keys in libwally-core. + * It's not suitable to add this struct into client struct, + * so set it static.*/ +struct bip32_key_version bip32_key_version; + +#if DEVELOPER +/* If they specify --dev-force-privkey it ends up in here. */ +struct privkey *dev_force_privkey; +/* If they specify --dev-force-bip32-seed it ends up in here. */ +struct secret *dev_force_bip32_seed; +#endif + +/* Have we initialized the secretstuff? */ +bool initialized = false; + +struct hsmd_client *hsmd_client_new_main(const tal_t *ctx, u64 capabilities, + void *extra) +{ + struct hsmd_client *c = tal(ctx, struct hsmd_client); + c->dbid = 0; + c->capabilities = capabilities; + c->extra = extra; + return c; +} + +struct hsmd_client *hsmd_client_new_peer(const tal_t *ctx, u64 capabilities, + u64 dbid, + const struct node_id *peer_id, + void *extra) +{ + struct hsmd_client *c = tal(ctx, struct hsmd_client); + c->dbid = dbid; + c->capabilities = capabilities; + c->id = *peer_id; + c->extra = extra; + return c; +} + +u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client, + const u8 *msg) +{ + return NULL; +} diff --git a/hsmd/libhsmd.h b/hsmd/libhsmd.h new file mode 100644 index 000000000..35da38866 --- /dev/null +++ b/hsmd/libhsmd.h @@ -0,0 +1,73 @@ +#ifndef LIGHTNING_HSMD_LIBHSMD_H +#define LIGHTNING_HSMD_LIBHSMD_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/*~ A struct that holds some context about the origin of an + * incoming request. It can either be a main daemon client, which is + * not associated with a peer or channel, or a peer client, which does + * have an association. */ +struct hsmd_client { + /*~ Useful for logging, but also used to derive the per-channel seed. */ + struct node_id id; + + /*~ This is a unique value handed to us from lightningd, used for + * per-channel seed generation (a single id may have multiple channels + * over time). + * + * It's actually zero for the initial lightningd client connection and + * the ones for gossipd and connectd, which don't have channels + * associated. */ + u64 dbid; + + /* What is this client allowed to ask for? */ + u64 capabilities; + + /* Params to apply to all transactions for this client */ + const struct chainparams *chainparams; + + /* A pointer to extra context that is to be passed around with + * the request. Used in `hsmd` to determine which connection + * originated the request. It is passed to the `hsmd_status_*` + * functions to allow reporting errors to the client. */ + void *extra; +}; + +struct hsmd_client *hsmd_client_new_main(const tal_t *ctx, u64 capabilities, + void *extra); + +struct hsmd_client *hsmd_client_new_peer(const tal_t *ctx, u64 capabilities, + u64 dbid, + const struct node_id *peer_id, + void *extra); + +/* Handle an incoming request with the provided context. Upon + * successful processing we return a response message that is + * allocated off of `ctx`. Failures return a `NULL` pointer, and the + * failure details were passed to `hsmd_failed`. */ +u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client, + const u8 *msg); + +/* The following declarations are here only temporarily while we migrate logic from hsmd.c to libhsmd.c */ + +/*~ Nobody will ever find it here! hsm_secret is our root secret, the bip32 + * tree and bolt12 payer_id keys are derived from that, and cached here. */ +/* TODO: Move into the libhsmd.c file as soon as hsmd.c doesn't need + * it anymore. */ +struct { + struct secret hsm_secret; + struct ext_key bip32; + secp256k1_keypair bolt12; +} secretstuff; + +/* end of temporary global declarations. The above will be removed once we complete the migration. */ +#endif /* LIGHTNING_HSMD_LIBHSMD_H */