Skip to content

Commit

Permalink
abi: force users to explicitly opt-out of exporting Elements function…
Browse files Browse the repository at this point in the history
…s (2)

Part 2, source changes.
  • Loading branch information
jgriffiths committed Aug 29, 2023
1 parent 4c01cd4 commit 87802c2
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 194 deletions.
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ $ brew install swig
interface. After building, see `src/swig_java/src/com/blockstream/libwally/Wally.java`
for the Java interface definition (default: no).
- `--disable-elements`. Disables support for [Elements](https://elementsproject.org/)
features, including [Liquid](https://blockstream.com/liquid/) support (default: no).
Note that to use Elements API calls from C/C++, the caller must define `BUILD_ELEMENTS`
when including the wally header files.
- `--disable-elements-abi`. Ensures that exposed library structures maintain the same ABI
regardless of `--disable-elements`. If disabled, elements support must be disabled and
the user must define `WALLY_ABI_NO_ELEMENTS` before including all wally header files.
This option *must be set if wally is being installed as a system/shared library*. (default: no).
features, including [Liquid](https://blockstream.com/liquid/) support. Elements
functions exported by the library will always return WALLY_ERROR (default: no).
- `--disable-elements-abi`. Changes the exposed library ABI to completely remove Elements
structure members and exported functions. When configured, elements support must be
disabled and the user must define `WALLY_ABI_NO_ELEMENTS` before including all wally
header files. This option *must not be given if wally is being installed as a system/shared library*. (default: no).
- `--enabled-standard-secp`. Excludes support for features that are unavailable in
the standard [libsecp256k1 library](https://github.com/bitcoin-core/secp256k1).
- `--enable-mbed-tls`. Use mbed-tls hashing functions if available. This typically
Expand All @@ -97,10 +96,10 @@ $ brew install swig
need [lcov](http://ltp.sourceforge.net/coverage/lcov.php) installed to
build with this option enabled and generate coverage reports.
- `--disable-shared`. Disables building a shared library and builds a static
library instead.
- `--disable-tests`. Disables building library tests.
library instead. (default: no)
- `--disable-tests`. Disables building library tests. (default: no)
- `--disable-clear-tests`. Disables just the test_clear test (required to pass
the test suite with some compilers).
the test suite with some compilers). (default: no)

### Recommended development configure options

Expand Down
2 changes: 2 additions & 0 deletions include/wally_psbt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2419,6 +2419,7 @@ WALLY_CORE_API int wally_psbt_clone_alloc(
uint32_t flags,
struct wally_psbt **output);

#ifndef WALLY_ABI_NO_ELEMENTS
/**
* Blind a PSBT.
*
Expand Down Expand Up @@ -2464,6 +2465,7 @@ WALLY_CORE_API int wally_psbt_blind_alloc(
uint32_t output_index,
uint32_t flags,
struct wally_map **output);
#endif /* WALLY_ABI_NO_ELEMENTS */

/**
* Sign PSBT inputs corresponding to a given private key.
Expand Down
20 changes: 17 additions & 3 deletions src/bip32.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,13 +837,16 @@ int bip32_key_from_parent_path_alloc(const struct ext_key *hdkey,
return ret;
}

#ifdef BUILD_ELEMENTS
#ifndef WALLY_ABI_NO_ELEMENTS
int bip32_key_with_tweak_from_parent_path(const struct ext_key *hdkey,
const uint32_t *child_path,
size_t child_path_len,
uint32_t flags,
struct ext_key *output)
{
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
const secp256k1_context *ctx;
secp256k1_pubkey pub_key;
size_t len = EC_PUBLIC_KEY_LEN;
Expand All @@ -865,13 +868,17 @@ int bip32_key_with_tweak_from_parent_path(const struct ext_key *hdkey,
return wipe_key_fail(output);

return WALLY_OK;
#endif /* BUILD_ELEMENTS */
}

int bip32_key_with_tweak_from_parent_path_alloc(const struct ext_key *hdkey,
const uint32_t *child_path, size_t child_path_len,
uint32_t flags,
struct ext_key **output)
{
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
int ret;

ALLOC_KEY();
Expand All @@ -882,8 +889,9 @@ int bip32_key_with_tweak_from_parent_path_alloc(const struct ext_key *hdkey,
*output = NULL;
}
return ret;
}
#endif /* BUILD_ELEMENTS */
}
#endif /* WALLY_ABI_NO_ELEMENTS */

int bip32_key_from_parent_path_str_n(const struct ext_key *hdkey,
const char *str, size_t str_len,
Expand Down Expand Up @@ -1137,9 +1145,15 @@ GET_B(chain_code)
GET_B(parent160)
GET_B(hash160)
GET_B(pub_key)
#ifdef BUILD_ELEMENTS
#ifndef WALLY_ABI_NO_ELEMENTS
#ifndef BUILD_ELEMENTS
int bip32_key_get_pub_key_tweak_sum(const struct ext_key *hdkey, unsigned char *bytes_out, size_t len) {
return WALLY_ERROR;
}
#else
GET_B(pub_key_tweak_sum)
#endif /* BUILD_ELEMENTS */
#endif /* WALLY_ABI_NO_ELEMENTS */

int bip32_key_get_priv_key(const struct ext_key *hdkey, unsigned char *bytes_out, size_t len) {
return getb_impl(hdkey, hdkey->priv_key + 1, sizeof(hdkey->priv_key) - 1, bytes_out, len);
Expand Down
4 changes: 2 additions & 2 deletions src/bip32_int.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ WALLY_CORE_API int bip32_key_get_hash160(const struct ext_key *hdkey, unsigned c
*/
WALLY_CORE_API int bip32_key_get_pub_key(const struct ext_key *hdkey, unsigned char *bytes_out, size_t len);

#ifdef BUILD_ELEMENTS
#ifndef WALLY_ABI_NO_ELEMENTS

/**
* FIXED_SIZED_OUTPUT(len, bytes_out, WALLY_BIP32_TWEAK_SUM_LEN)
*/
WALLY_CORE_API int bip32_key_get_pub_key_tweak_sum(const struct ext_key *hdkey, unsigned char *bytes_out, size_t len);

#endif /* BUILD_ELEMENTS */
#endif /* WALLY_ABI_NO_ELEMENTS */

WALLY_CORE_API int bip32_key_get_depth(const struct ext_key *hdkey, size_t *written);
WALLY_CORE_API int bip32_key_get_child_num(const struct ext_key *hdkey, size_t *written);
Expand Down
18 changes: 15 additions & 3 deletions src/blech32.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "script.h"

#ifdef BUILD_ELEMENTS

#define CHECKSUM_BLECH32 0x1
#define CHECKSUM_BLECH32M 0x455972a3350f7a1ull

Expand Down Expand Up @@ -208,13 +207,18 @@ static int blech32_addr_decode(uint8_t *witver, uint8_t *witdata, size_t *witdat
wally_clear_2(data, sizeof(data), hrp_actual, sizeof(hrp_actual));
return 0;
}
#endif /* BUILD_ELEMENTS */

#ifndef WALLY_ABI_NO_ELEMENTS
int wally_confidential_addr_to_addr_segwit(
const char *address,
const char *confidential_addr_family,
const char *addr_family,
char **output)
{
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
unsigned char buf[WALLY_BLECH32_MAXLEN];
unsigned char *hash_bytes_p = &buf[EC_PUBLIC_KEY_LEN - 2];
size_t written = 0;
Expand All @@ -241,6 +245,7 @@ int wally_confidential_addr_to_addr_segwit(

wally_clear(buf, sizeof(buf));
return ret;
#endif /* BUILD_ELEMENTS */
}

int wally_confidential_addr_segwit_to_ec_public_key(
Expand All @@ -249,6 +254,9 @@ int wally_confidential_addr_segwit_to_ec_public_key(
unsigned char *bytes_out,
size_t len)
{
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
unsigned char buf[WALLY_BLECH32_MAXLEN];
size_t written = 0;
int ret = WALLY_OK;
Expand All @@ -266,6 +274,7 @@ int wally_confidential_addr_segwit_to_ec_public_key(

wally_clear(buf, sizeof(buf));
return ret;
#endif /* BUILD_ELEMENTS */
}

int wally_confidential_addr_from_addr_segwit(
Expand All @@ -276,6 +285,9 @@ int wally_confidential_addr_from_addr_segwit(
size_t pub_key_len,
char **output)
{
#ifndef BUILD_ELEMENTS
return WALLY_ERROR;
#else
char result[WALLY_BLECH32_MAXLEN + 1];
unsigned char buf[EC_PUBLIC_KEY_LEN + SHA256_LEN];
unsigned char *hash_bytes_p = &buf[EC_PUBLIC_KEY_LEN - 2];
Expand Down Expand Up @@ -320,6 +332,6 @@ int wally_confidential_addr_from_addr_segwit(
wally_clear(buf, sizeof(buf));
wally_clear(result, sizeof(result));
return ret;
}

#endif /* BUILD_ELEMENTS */
}
#endif /* WALLY_ABI_NO_ELEMENTS */
Loading

0 comments on commit 87802c2

Please sign in to comment.