From 917708450548c81bad537c07ee55f990087a6cb9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 21 Jul 2023 09:53:26 +0930 Subject: [PATCH] lightningd: new runes infrastructure. Signed-off-by: Rusty Russell --- lightningd/Makefile | 1 + lightningd/lightningd.c | 6 ++++ lightningd/lightningd.h | 3 ++ lightningd/runes.c | 43 +++++++++++++++++++++++++++ lightningd/runes.h | 7 +++++ lightningd/test/run-find_my_abspath.c | 3 ++ 6 files changed, 63 insertions(+) create mode 100644 lightningd/runes.c create mode 100644 lightningd/runes.h diff --git a/lightningd/Makefile b/lightningd/Makefile index 15bed238e..0085da531 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -37,6 +37,7 @@ LIGHTNINGD_SRC := \ lightningd/plugin_control.c \ lightningd/plugin_hook.c \ lightningd/routehint.c \ + lightningd/runes.c \ lightningd/subd.c \ lightningd/watch.c diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index f85cc94fe..c420337a5 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -71,6 +71,7 @@ #include #include #include +#include #include #include #include @@ -1118,6 +1119,11 @@ int main(int argc, char *argv[]) else if (max_blockheight != UINT32_MAX) max_blockheight -= ld->config.rescan; + /*~ We have bearer tokens called `runes` you can use to control access. They have + * a fascinating history which I shall not go into now, but they're derived from + * Macaroons which was a over-engineered Googlism. */ + ld->runes = runes_init(ld); + /*~ That's all of the wallet db operations for now. */ db_commit_transaction(ld->wallet->db); diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 6f8ffd992..ac70e252a 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -371,6 +371,9 @@ struct lightningd { /* For anchors: how much do we keep for spending close txs? */ struct amount_sat emergency_sat; + + /* runes! */ + struct runes *runes; }; /* Turning this on allows a tal allocation to return NULL, rather than aborting. diff --git a/lightningd/runes.c b/lightningd/runes.c new file mode 100644 index 000000000..837398ba5 --- /dev/null +++ b/lightningd/runes.c @@ -0,0 +1,43 @@ +#include "config.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* This is lightningd->runes */ +struct runes { + struct rune *master; + u64 next_unique_id; + struct rune_blacklist *blacklist; +}; + +struct runes *runes_init(struct lightningd *ld) +{ + const u8 *msg; + struct runes *runes = tal(ld, struct runes); + const u8 *data; + struct secret secret; + + runes->next_unique_id = db_get_intvar(ld->wallet->db, "runes_uniqueid", 0); + runes->blacklist = wallet_get_runes_blacklist(runes, ld->wallet); + + /* Runes came out of commando, hence the derivation key is 'commando' */ + data = tal_dup_arr(tmpctx, u8, (u8 *)"commando", strlen("commando"), 0); + msg = hsm_sync_req(tmpctx, ld, towire_hsmd_derive_secret(tmpctx, data)); + if (!fromwire_hsmd_derive_secret_reply(msg, &secret)) + fatal("Bad reply from HSM: %s", tal_hex(tmpctx, msg)); + + runes->master = rune_new(runes, secret.data, ARRAY_SIZE(secret.data), NULL); + + return runes; +} diff --git a/lightningd/runes.h b/lightningd/runes.h new file mode 100644 index 000000000..d38052b44 --- /dev/null +++ b/lightningd/runes.h @@ -0,0 +1,7 @@ +#ifndef LIGHTNING_LIGHTNINGD_RUNES_H +#define LIGHTNING_LIGHTNINGD_RUNES_H +#include "config.h" + +struct runes *runes_init(struct lightningd *ld); + +#endif /* LIGHTNING_LIGHTNINGD_RUNES_H */ diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index f43d3f660..b33f3ce50 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -187,6 +187,9 @@ void plugins_set_builtin_plugins_dir(struct plugins *plugins UNNEEDED, /* Generated stub for resend_closing_transactions */ void resend_closing_transactions(struct lightningd *ld UNNEEDED) { fprintf(stderr, "resend_closing_transactions called!\n"); abort(); } +/* Generated stub for runes_init */ +struct runes *runes_init(struct lightningd *ld UNNEEDED) +{ fprintf(stderr, "runes_init called!\n"); abort(); } /* Generated stub for setup_color_and_alias */ void setup_color_and_alias(struct lightningd *ld UNNEEDED) { fprintf(stderr, "setup_color_and_alias called!\n"); abort(); }