Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to wally 1.0.0 #6882

Closed
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 6 additions & 8 deletions bitcoin/base58.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
75 changes: 19 additions & 56 deletions bitcoin/psbt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 */
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
30 changes: 4 additions & 26 deletions bitcoin/tx_parts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion common/interactivetx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion common/psbt_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
#include <sodium.h>
#include <wally_core.h>

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)
Expand Down
11 changes: 5 additions & 6 deletions external/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,21 @@ $(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
cd ${TARGET_DIR}/libwally-core-build \
&& 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
Expand Down
2 changes: 1 addition & 1 deletion external/libwally-core
Submodule libwally-core updated 105 files
2 changes: 1 addition & 1 deletion openingd/dualopend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading