Skip to content

Commit

Permalink
tools/hsmtool.c: Add to hsmtools to get content of emergency.recover …
Browse files Browse the repository at this point in the history
…in bech32 format.

Changelog-Added: `hsmtool`: new command `getemergencyrecover` to extract emergency.recover in bech32 format (clnemerge1...)
  • Loading branch information
Aditya Sharma authored and adi2011 committed Oct 26, 2023
1 parent 0341079 commit 2c2683f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ tools/headerversions: $(FORCE) tools/headerversions.o libccan.a
tools/headerversions.o: ccan/config.h
tools/check-bolt: tools/check-bolt.o $(TOOLS_COMMON_OBJS)

tools/hsmtool: tools/hsmtool.o $(TOOLS_COMMON_OBJS) $(BITCOIN_OBJS) common/amount.o common/autodata.o common/bech32.o common/bigsize.o common/codex32.o common/configdir.o common/configvar.o common/derive_basepoints.o common/descriptor_checksum.o common/hsm_encryption.o common/node_id.o common/type_to_string.o common/version.o wire/fromwire.o wire/towire.o
tools/hsmtool: tools/hsmtool.o $(TOOLS_COMMON_OBJS) $(BITCOIN_OBJS) common/amount.o common/autodata.o common/bech32.o common/bech32_util.o common/bigsize.o common/codex32.o common/configdir.o common/configvar.o common/derive_basepoints.o common/descriptor_checksum.o common/hsm_encryption.o common/node_id.o common/type_to_string.o common/version.o wire/fromwire.o wire/towire.o

tools/lightning-hsmtool: tools/hsmtool
cp $< $@
Expand Down
31 changes: 31 additions & 0 deletions tools/hsmtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/bech32.h>
#include <common/bech32_util.h>
#include <common/codex32.h>
#include <common/configdir.h>
#include <common/derive_basepoints.h>
Expand Down Expand Up @@ -46,6 +47,7 @@ static void show_usage(const char *progname)
printf(" - dumponchaindescriptors <path/to/hsm_secret> [network]\n");
printf(" - makerune <path/to/hsm_secret>\n");
printf(" - getcodexsecret <path/to/hsm_secret> <id>\n");
printf(" - getemergencyrecover <path/to/emergency.recover>\n");
exit(0);
}

Expand Down Expand Up @@ -262,6 +264,28 @@ static int make_codexsecret(const char *hsm_secret_path,
return 0;
}

static int getemergencyrecover(const char *emer_rec_path)
{
u8 *scb = grab_file(tmpctx, emer_rec_path);
char *output, *hrp = "clnemerg";
if (!scb) {
errx(EXITCODE_ERROR_HSM_FILE, "Reading emergency.recover");
} else {
/* grab_file adds nul term */
tal_resize(&scb, tal_bytelen(scb) - 1);
}
u5 *data = tal_arr(tmpctx, u5, 0);

bech32_push_bits(&data, scb, tal_bytelen(scb) * 8);
output = tal_arr(tmpctx, char, strlen(hrp) + tal_count(data) + 8);

bech32_encode(output, hrp, data, tal_count(data), (size_t)-1,
BECH32_ENCODING_BECH32);

printf("%s\n", output);
return 0;
}

static int encrypt_hsm(const char *hsm_secret_path)
{
int fd;
Expand Down Expand Up @@ -768,5 +792,12 @@ int main(int argc, char *argv[])
show_usage(argv[0]);
return make_codexsecret(argv[2], argv[3]);
}

if(streq(method, "getemergencyrecover")) {
if (argc < 3)
show_usage(argv[0]);
return getemergencyrecover(argv[2]);
}

show_usage(argv[0]);
}

0 comments on commit 2c2683f

Please sign in to comment.