diff --git a/Makefile b/Makefile index fd2b38e41546..5a1ce0ec4234 100644 --- a/Makefile +++ b/Makefile @@ -250,7 +250,7 @@ LIBRARY_PATH := /usr/local/lib endif CPPFLAGS += -DBINTOPKGLIBEXECDIR="\"$(shell sh tools/rel.sh $(bindir) $(pkglibexecdir))\"" -CFLAGS = $(CPPFLAGS) $(CWARNFLAGS) $(CDEBUGFLAGS) $(COPTFLAGS) -I $(CCANDIR) $(EXTERNAL_INCLUDE_FLAGS) -I . -I$(CPATH) $(SQLITE3_CFLAGS) $(POSTGRES_INCLUDE) $(FEATURES) $(COVFLAGS) $(DEV_CFLAGS) -DSHACHAIN_BITS=48 -DJSMN_PARENT_LINKS $(PIE_CFLAGS) $(COMPAT_CFLAGS) $(CSANFLAGS) -DBUILD_ELEMENTS=1 +CFLAGS = $(CPPFLAGS) $(CWARNFLAGS) $(CDEBUGFLAGS) $(COPTFLAGS) -I $(CCANDIR) $(EXTERNAL_INCLUDE_FLAGS) -I . -I$(CPATH) $(SQLITE3_CFLAGS) $(POSTGRES_INCLUDE) $(FEATURES) $(COVFLAGS) $(DEV_CFLAGS) -DSHACHAIN_BITS=48 -DJSMN_PARENT_LINKS $(PIE_CFLAGS) $(COMPAT_CFLAGS) $(CSANFLAGS) # If CFLAGS is already set in the environment of make (to whatever value, it # does not matter) then it would export it to subprocesses with the above value diff --git a/bitcoin/base58.c b/bitcoin/base58.c index d1368289d0b9..a3da473a9690 100644 --- a/bitcoin/base58.c +++ b/bitcoin/base58.c @@ -47,16 +47,14 @@ static bool from_base58(u8 *version, struct ripemd160 *rmd, const char *base58, size_t base58_len) { - u8 buf[1 + sizeof(*rmd) + 4]; - /* Avoid memcheck complaining if decoding resulted in a short value */ - size_t buflen = sizeof(buf); - memset(buf, 0, buflen); - char *terminated_base58 = tal_dup_arr(NULL, char, base58, base58_len, 1); - terminated_base58[base58_len] = '\0'; + /* Initialize to avoid memcheck complaining if decoding a short value */ + u8 buf[1 + sizeof(*rmd) + 4] = { 0 }; + const size_t buflen = sizeof(buf); + const uint32_t flags = BASE58_FLAG_CHECKSUM; size_t written = 0; - int r = wally_base58_to_bytes(terminated_base58, BASE58_FLAG_CHECKSUM, buf, buflen, &written); - tal_free(terminated_base58); + int r = wally_base58_n_to_bytes(base58, base58_len, flags, + buf, buflen, &written); if (r != WALLY_OK || written > buflen) { return false; } diff --git a/bitcoin/psbt.c b/bitcoin/psbt.c index 42a89bfb8ffd..4d010508cd37 100644 --- a/bitcoin/psbt.c +++ b/bitcoin/psbt.c @@ -575,37 +575,18 @@ u8 *psbt_make_key(const tal_t *ctx, u8 key_subtype, const u8 *key_data) return key; } -static bool wally_map_set_unknown(const tal_t *ctx, - struct wally_map *map, - const u8 *key, - const void *value, - size_t value_len) +static void map_replace(const tal_t *ctx, + struct wally_map *map, + const u8 *key, + const void *value, + size_t value_len) { - size_t exists_at; - struct wally_map_item *item; - - assert(value_len != 0); - if (wally_map_find(map, key, tal_bytelen(key), &exists_at) != WALLY_OK) - return false; - - /* If not exists, add */ - if (exists_at == 0) { - bool ok; - tal_wally_start(); - ok = wally_map_add(map, key, tal_bytelen(key), - (unsigned char *) memcheck(value, value_len), value_len) - == WALLY_OK; - tal_wally_end(ctx); - return ok; - } - - /* Already in map, update entry */ - item = &map->items[exists_at - 1]; - tal_resize(&item->value, value_len); - memcpy(item->value, memcheck(value, value_len), value_len); - item->value_len = value_len; - - return true; + const unsigned char *checked_value = memcheck(value, value_len); + tal_wally_start(); + if (wally_map_replace(map, key, tal_bytelen(key), + checked_value, value_len) != WALLY_OK) + abort(); + tal_wally_end(ctx); } void psbt_input_set_unknown(const tal_t *ctx, @@ -614,48 +595,30 @@ void psbt_input_set_unknown(const tal_t *ctx, const void *value, size_t value_len) { - if (!wally_map_set_unknown(ctx, &in->unknowns, key, value, value_len)) - abort(); -} - -static void *psbt_get_unknown(const struct wally_map *map, - const u8 *key, - size_t *val_len) -{ - size_t index; - - if (wally_map_find(map, key, tal_bytelen(key), &index) != WALLY_OK) - return NULL; - - /* Zero: item not found. */ - if (index == 0) - return NULL; - - /* ++: item is at this index minus 1 */ - *val_len = map->items[index - 1].value_len; - return map->items[index - 1].value; + map_replace(ctx, &in->unknowns, key, value, value_len); } void *psbt_get_lightning(const struct wally_map *map, const u8 proprietary_type, size_t *val_len) { - void *res; u8 *key = psbt_make_key(NULL, proprietary_type, NULL); - res = psbt_get_unknown(map, key, val_len); + const struct wally_map_item *item; + item = wally_map_get(map, key, tal_bytelen(key)); tal_free(key); - return res; + if (!item) + return NULL; + *val_len = item->value_len; + return item->value; } - void psbt_output_set_unknown(const tal_t *ctx, struct wally_psbt_output *out, const u8 *key, const void *value, size_t value_len) { - if (!wally_map_set_unknown(ctx, &out->unknowns, key, value, value_len)) - abort(); + map_replace(ctx, &out->unknowns, key, value, value_len); } /* Use the destructor to free the wally_tx */ diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 1da37ab25856..b42309e766ae 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -302,8 +302,8 @@ void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum, wally_psbt_output_set_amount(&tx->psbt->outputs[outnum], satoshis); } -const u8 *wally_tx_output_get_script(const tal_t *ctx, - const struct wally_tx_output *output) +const u8 *cln_wally_tx_output_get_script(const tal_t *ctx, + const struct wally_tx_output *output) { if (output->script == NULL) { /* This can happen for coinbase transactions and pegin @@ -321,7 +321,7 @@ const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, assert(outnum < tx->wtx->num_outputs); output = &tx->wtx->outputs[outnum]; - return wally_tx_output_get_script(ctx, output); + return cln_wally_tx_output_get_script(ctx, output); } u8 *bitcoin_tx_output_get_witscript(const tal_t *ctx, const struct bitcoin_tx *tx, diff --git a/bitcoin/tx.h b/bitcoin/tx.h index c9abeb530f8b..eb6bfc6de131 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -156,8 +156,8 @@ const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, const struct bitcoin_tx * comfort of being able to call `tal_bytelen` and similar on a script we just * return a `tal_arr` clone of the original script. */ -const u8 *wally_tx_output_get_script(const tal_t *ctx, - const struct wally_tx_output *output); +const u8 *cln_wally_tx_output_get_script(const tal_t *ctx, + const struct wally_tx_output *output); /** * Helper to get a witness script for an output. */ diff --git a/bitcoin/tx_parts.c b/bitcoin/tx_parts.c index 970e489f8c72..4e7f068fd760 100644 --- a/bitcoin/tx_parts.c +++ b/bitcoin/tx_parts.c @@ -48,31 +48,6 @@ static void destroy_wally_tx_output(struct wally_tx_output *out) wally_tx_output_free(out); } -static struct wally_tx_output *clone_output(const struct wally_tx_output *src) -{ - struct wally_tx_output *out; - int ret; - - if (is_elements(chainparams)) { - ret = wally_tx_elements_output_init_alloc - (src->script, src->script_len, - src->asset, src->asset_len, - src->value, src->value_len, - src->nonce, src->nonce_len, - src->surjectionproof, src->surjectionproof_len, - src->rangeproof, src->rangeproof_len, - &out); - } else { - ret = wally_tx_output_init_alloc(src->satoshi, - src->script, src->script_len, - &out); - } - assert(ret == WALLY_OK); - - tal_add_destructor(out, destroy_wally_tx_output); - return out; -} - struct tx_parts *tx_parts_from_wally_tx(const tal_t *ctx, const struct wally_tx *wtx, int input, int output) @@ -93,7 +68,10 @@ struct tx_parts *tx_parts_from_wally_tx(const tal_t *ctx, for (size_t i = 0; i < wtx->num_outputs; i++) { if (output != -1 && output != i) continue; - txp->outputs[i] = clone_output(&wtx->outputs[i]); + if (wally_tx_output_clone_alloc(&wtx->outputs[i], + &txp->outputs[i]) != WALLY_OK) + abort(); + tal_add_destructor(txp->outputs[i], destroy_wally_tx_output); /* Cheat a bit by also setting the numeric satoshi * value, otherwise we end up converting a diff --git a/common/interactivetx.c b/common/interactivetx.c index 3442d8526537..9c249e64696f 100644 --- a/common/interactivetx.c +++ b/common/interactivetx.c @@ -84,7 +84,7 @@ static bool is_segwit_output(const tal_t *ctx, if (tal_bytelen(redeemscript) > 0) maybe_witness = redeemscript; else - maybe_witness = wally_tx_output_get_script(ctx, output); + maybe_witness = cln_wally_tx_output_get_script(ctx, output); return is_known_segwit_scripttype(maybe_witness); } diff --git a/common/psbt_open.c b/common/psbt_open.c index f5af7f67fbb7..a5eb44420ed8 100644 --- a/common/psbt_open.c +++ b/common/psbt_open.c @@ -396,7 +396,7 @@ bool psbt_has_required_fields(struct wally_psbt *psbt) /* If is P2SH, redeemscript must be present */ assert(psbt->inputs[i].index < input->utxo->num_outputs); const u8 *outscript = - wally_tx_output_get_script(tmpctx, + cln_wally_tx_output_get_script(tmpctx, &input->utxo->outputs[psbt->inputs[i].index]); redeem_script = wally_map_get_integer(&psbt->inputs[i].psbt_fields, /* PSBT_IN_REDEEM_SCRIPT */ 0x04); if (is_p2sh(outscript, NULL) && (!redeem_script || redeem_script->value_len == 0)) diff --git a/common/setup.c b/common/setup.c index 8ae05bf4327f..8db367eec9d6 100644 --- a/common/setup.c +++ b/common/setup.c @@ -8,21 +8,21 @@ #include #include -static void *wally_tal(size_t size) +static void *cln_wally_tal(size_t size) { assert(wally_tal_ctx); - return tal_arr_label(wally_tal_ctx, u8, size, "wally_tal"); + return tal_arr_label(wally_tal_ctx, u8, size, "cln_wally_tal"); } -static void wally_free(void *ptr) +static void cln_wally_free(void *ptr) { tal_free(ptr); } static struct wally_operations wally_tal_ops = { .struct_size = sizeof(struct wally_operations), - .malloc_fn = wally_tal, - .free_fn = wally_free, + .malloc_fn = cln_wally_tal, + .free_fn = cln_wally_free, }; static void *htable_tal(struct htable *ht, size_t len) diff --git a/external/Makefile b/external/Makefile index ce8310dbe2ec..2767cc06885d 100644 --- a/external/Makefile +++ b/external/Makefile @@ -79,6 +79,10 @@ $(TARGET_DIR)/libsecp256k1.% $(TARGET_DIR)/libwallycore.%: $(TARGET_DIR)/libwall $(MAKE) -C $(TARGET_DIR)/libwally-core-build DESTDIR=$$(pwd)/$(TARGET_DIR) install-exec # Build libwally-core. +ifeq ($(DEBUGBUILD),1) +WALLY_OPTS=--enable-debug +endif + $(TARGET_DIR)/libwally-core-build/src/libwallycore.% $(TARGET_DIR)/libwally-core-build/src/secp256k1/libsecp256k1.%: $(LIBWALLY_HEADERS) $(LIBSECP_HEADERS) cd external/libwally-core && ./tools/autogen.sh mkdir -p ${TARGET_DIR}/libwally-core-build @@ -86,15 +90,10 @@ $(TARGET_DIR)/libwally-core-build/src/libwallycore.% $(TARGET_DIR)/libwally-core && PYTHON_VERSION=3 CFLAGS="-std=c99 $(FUZZFLAGS)" LDFLAGS="$(FUZZFLAGS)" ${TOP}/libwally-core/configure CC="$(CC)" \ --enable-static=yes \ $(CROSSCOMPILE_OPTS) \ - --enable-module-recovery \ - --enable-module-extrakeys \ - --enable-module-schnorrsig \ - --enable-elements \ --enable-shared=no \ --prefix=/ \ --libdir=/ \ - --enable-debug \ - && cp src/secp256k1/src/libsecp256k1-config.h ../../libwally-core/src/secp256k1/src/libsecp256k1-config.h \ + $(WALLY_OPTS) \ && $(MAKE) # If we tell Make that the above builds both, it runs it twice in diff --git a/external/libwally-core b/external/libwally-core index bb4cd3ac802c..6f67467299a0 160000 --- a/external/libwally-core +++ b/external/libwally-core @@ -1 +1 @@ -Subproject commit bb4cd3ac802c7beb58f63307c5ed6ca116cf0dd0 +Subproject commit 6f67467299a038f9ceaa12272387a7ce71fd533f diff --git a/openingd/dualopend.c b/openingd/dualopend.c index b26b8544b2c6..80ca85f015ee 100644 --- a/openingd/dualopend.c +++ b/openingd/dualopend.c @@ -990,7 +990,7 @@ static char *check_balances(const tal_t *ctx, static bool is_segwit_output(struct wally_tx_output *output) { - const u8 *script = wally_tx_output_get_script(tmpctx, output); + const u8 *script = cln_wally_tx_output_get_script(tmpctx, output); return is_known_segwit_scripttype(script); } diff --git a/wallet/wallet.c b/wallet/wallet.c index 12723c336725..f7af8fea48fd 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2725,8 +2725,8 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx, if (!amount_asset_is_main(&asset)) continue; - script = wally_tx_output_get_script(tmpctx, - &wtx->outputs[output]); + script = cln_wally_tx_output_get_script(tmpctx, + &wtx->outputs[output]); if (!script) continue;