Skip to content

feat: output descriptor #202

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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions include/wally_descriptor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#ifndef LIBWALLY_CORE_DESCRIPTOR_H
#define LIBWALLY_CORE_DESCRIPTOR_H

#include "wally_core.h"
#include "wally_address.h"

#ifdef __cplusplus
extern "C" {
#endif

#define WALLY_NETWORK_BITCOIN_REGTEST 0xff /** Bitcoin regtest */

/* miniscript type flag */
#define WALLY_MINISCRIPT_WITNESS_SCRIPT 0x00
#define WALLY_MINISCRIPT_TAPSCRIPT 0x01

#ifdef SWIG
struct wally_descriptor_address_item;
struct wally_descriptor_addresses;
#else
/** A descriptor address */
struct wally_descriptor_address_item {
uint32_t child_num;
char *address;
size_t address_len;
};

/** A descriptor addresses */
struct wally_descriptor_addresses {
struct wally_descriptor_address_item *items;
size_t num_items;
};
#endif

#ifndef SWIG_PYTHON
/**
* Free addresses allocated by `wally_descriptor_to_addresses`.
*
* :param addresses: addresses to free.
*/
WALLY_CORE_API int wally_free_descriptor_addresses(
struct wally_descriptor_addresses *addresses);
#endif /* SWIG_PYTHON */

/**
* Create a script corresponding to a miniscript string.
*
* :param miniscript: Miniscript string.
* :param key_name_array: Array of key policy name string.
* :param key_value_array: Array of key mapped value string.
* :param array_len: Length of the array of key policy name.
* :param derive_child_num: Number of the derive path.
* :param flags: For analyze type.
* see WALLY_MINISCRIPT_WITNESS_SCRIPT, WALLY_MINISCRIPT_TAPSCRIPT.
* :param script: Destination for the resulting scriptpubkey.
* :param script_len: Length of the script array.
* :param written: Destination for the using scriptpubkey length.
*/
WALLY_CORE_API int wally_descriptor_parse_miniscript(
const char *miniscript,
const char **key_name_array,
const char **key_value_array,
size_t array_len,
uint32_t derive_child_num,
uint32_t flags,
unsigned char *script,
size_t script_len,
size_t *written);

/**
* Create a scriptpubkey corresponding to a output descriptor.
*
* :param descriptor: Output descriptor.
* :param key_name_array: Array of key policy name string.
* :param key_value_array: Array of key mapped value string.
* :param array_len: Length of the array of key policy name.
* :param derive_child_num: Number of the derive path.
* :param network: Number of the network. (bitcoin regtest is set ``0xff``)
* :param target_depth: Number of the descriptor depth. Default is 0.
* :param target_index: Number of the descriptor index. Default is 0.
* :param flags: For future use. Must be 0.
* :param script: Destination for the resulting scriptpubkey.
* :param script_len: Length of the script array.
* :param written: Destination for the using scriptpubkey length.
*/
WALLY_CORE_API int wally_descriptor_to_scriptpubkey(
const char *descriptor,
const char **key_name_array,
const char **key_value_array,
size_t array_len,
uint32_t derive_child_num,
uint32_t network,
uint32_t target_depth,
uint32_t target_index,
uint32_t flags,
unsigned char *script,
size_t script_len,
size_t *written);

/**
* Create an address corresponding to a output descriptor.
*
* :param descriptor: Output descriptor.
* :param key_name_array: Array of key policy name string.
* :param key_value_array: Array of key mapped value string.
* :param array_len: Length of the array of key policy name.
* :param derive_child_num: Number of the derive path.
* :param network: Number of the network. (bitcoin regtest is set ``0xff``)
* :param flags: For future use. Must be 0.
* :param output: Destination for the resulting address string.
*| The string returned should be freed using `wally_free_string`.
*/
WALLY_CORE_API int wally_descriptor_to_address(
const char *descriptor,
const char **key_name_array,
const char **key_value_array,
size_t array_len,
uint32_t derive_child_num,
uint32_t network,
uint32_t flags,
char **output);

/**
* Create addresses that corresponds to the derived range of a output descriptor.
*
* :param descriptor: Output descriptor.
* :param key_name_array: Array of key policy name string.
* :param key_value_array: Array of key mapped value string.
* :param array_len: Length of the array of key policy name.
* :param start_child_num: Number of the derive start path.
* :param end_child_num: Number of the derive end path.
* :param network: Number of the network. (bitcoin regtest is set ``0xff``)
* :param flags: For future use. Must be 0.
* :param addresses: Destination for the resulting addresses.
*| The string returned should be freed using `wally_free_descriptor_addresses`.
*/
WALLY_CORE_API int wally_descriptor_to_addresses(
const char *descriptor,
const char **key_name_array,
const char **key_value_array,
size_t array_len,
uint32_t start_child_num,
uint32_t end_child_num,
uint32_t network,
uint32_t flags,
struct wally_descriptor_addresses *addresses);

/**
* Create an output descriptor checksum.
*
* :param descriptor: Output descriptor.
* :param key_name_array: Array of key policy name string.
* :param key_value_array: Array of key mapped value string.
* :param array_len: Length of the array of key policy name.
* :param flags: For future use. Must be 0.
* :param output: Destination for the resulting descriptor string.
*/
WALLY_CORE_API int wally_descriptor_create_checksum(
const char *descriptor,
const char **key_name_array,
const char **key_value_array,
size_t array_len,
uint32_t flags,
char **output);

#ifdef __cplusplus
}
#endif

#endif /* LIBWALLY_CORE_DESCRIPTOR_H */
12 changes: 12 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ include_HEADERS += $(top_srcdir)/include/wally_bip38.h
include_HEADERS += $(top_srcdir)/include/wally_bip39.h
include_HEADERS += $(top_srcdir)/include/wally_core.h
include_HEADERS += $(top_srcdir)/include/wally_crypto.h
include_HEADERS += $(top_srcdir)/include/wally_descriptor.h
include_HEADERS += $(top_srcdir)/include/wally_elements.h
include_HEADERS += $(top_srcdir)/include/wally_psbt.h
include_HEADERS += $(top_srcdir)/include/wally_script.h
Expand Down Expand Up @@ -183,6 +184,7 @@ libwallycore_la_SOURCES = \
bip38.c \
bip39.c \
bech32.c \
descriptor.c \
ecdh.c \
elements.c \
blech32.c \
Expand Down Expand Up @@ -214,6 +216,7 @@ libwallycore_la_INCLUDES = \
include/wally_bip39.h \
include/wally_core.h \
include/wally_crypto.h \
include/wally_descriptor.h \
include/wally_elements.h \
include/wally_psbt.h \
include/wally_script.h \
Expand Down Expand Up @@ -271,6 +274,14 @@ test_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_tx_LDADD += $(PYTHON_LIBS)
endif
TESTS += test_descriptor
noinst_PROGRAMS += test_descriptor
test_descriptor_SOURCES = ctest/test_descriptor.c
test_descriptor_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_descriptor_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_descriptor_LDADD += $(PYTHON_LIBS)
endif
if BUILD_ELEMENTS
TESTS += test_elements_tx
noinst_PROGRAMS += test_elements_tx
Expand Down Expand Up @@ -302,6 +313,7 @@ if RUN_PYTHON_TESTS
$(AM_V_at)$(PYTHON_TEST) test/test_bip32.py
$(AM_V_at)$(PYTHON_TEST) test/test_bip38.py
$(AM_V_at)$(PYTHON_TEST) test/test_bip39.py
$(AM_V_at)$(PYTHON_TEST) test/test_descriptor.py
$(AM_V_at)$(PYTHON_TEST) test/test_ecdh.py
$(AM_V_at)$(PYTHON_TEST) test/test_hash.py
$(AM_V_at)$(PYTHON_TEST) test/test_hex.py
Expand Down
Loading