2021-02-01 03:58:50 +01:00
|
|
|
#include "config.h"
|
2020-09-23 12:43:28 +02:00
|
|
|
#include <assert.h>
|
2020-05-16 00:13:22 +02:00
|
|
|
#include <ccan/ccan/err/err.h>
|
2021-11-14 18:51:46 +01:00
|
|
|
#include <common/autodata.h>
|
2020-05-16 00:13:22 +02:00
|
|
|
#include <common/setup.h>
|
|
|
|
#include <common/utils.h>
|
|
|
|
#include <sodium.h>
|
|
|
|
#include <wally_core.h>
|
|
|
|
|
|
|
|
static void *wally_tal(size_t size)
|
|
|
|
{
|
2020-09-23 12:43:28 +02:00
|
|
|
assert(wally_tal_ctx);
|
2020-09-23 03:37:04 +02:00
|
|
|
return tal_arr_label(wally_tal_ctx, u8, size, "wally_tal");
|
2020-05-16 00:13:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void wally_free(void *ptr)
|
|
|
|
{
|
|
|
|
tal_free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wally_operations wally_tal_ops = {
|
2021-06-01 19:14:48 +02:00
|
|
|
.struct_size = sizeof(struct wally_operations),
|
2020-05-16 00:13:22 +02:00
|
|
|
.malloc_fn = wally_tal,
|
|
|
|
.free_fn = wally_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void common_setup(const char *argv0)
|
|
|
|
{
|
2021-05-11 00:29:58 +02:00
|
|
|
int wally_ret;
|
|
|
|
|
2020-05-16 00:13:22 +02:00
|
|
|
setup_locale();
|
|
|
|
err_set_progname(argv0);
|
|
|
|
|
|
|
|
/* We rely on libsodium for some of the crypto stuff, so we'd better
|
|
|
|
* not start if it cannot do its job correctly. */
|
|
|
|
if (sodium_init() == -1)
|
|
|
|
errx(1, "Could not initialize libsodium. Maybe not enough entropy"
|
|
|
|
" available ?");
|
|
|
|
|
|
|
|
/* We set up Wally, the bitcoin wallet lib */
|
2021-05-11 00:29:58 +02:00
|
|
|
wally_ret = wally_init(0);
|
|
|
|
if (wally_ret != WALLY_OK)
|
|
|
|
errx(1, "Error initializing libwally: %i", wally_ret);
|
|
|
|
wally_ret = wally_set_operations(&wally_tal_ops);
|
|
|
|
if (wally_ret != WALLY_OK)
|
|
|
|
errx(1, "Error setting libwally operations: %i", wally_ret);
|
2020-05-16 00:13:22 +02:00
|
|
|
secp256k1_ctx = wally_get_secp_context();
|
|
|
|
|
|
|
|
setup_tmpctx();
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_shutdown(void)
|
|
|
|
{
|
2020-12-02 01:09:04 +01:00
|
|
|
const char *p = taken_any();
|
|
|
|
if (p)
|
|
|
|
errx(1, "outstanding taken(): %s", p);
|
|
|
|
take_cleanup();
|
2020-05-16 00:13:22 +02:00
|
|
|
tal_free(tmpctx);
|
|
|
|
wally_cleanup(0);
|
|
|
|
tal_free(wally_tal_ctx);
|
2021-11-14 18:51:46 +01:00
|
|
|
autodata_cleanup();
|
2020-05-16 00:13:22 +02:00
|
|
|
}
|