Skip to content

Commit 5b5ed0b

Browse files
Add support for output descriptors/miniscript.
Co-authored-by: Jon Griffiths <[email protected]>
1 parent 99b25e0 commit 5b5ed0b

File tree

24 files changed

+4165
-15
lines changed

24 files changed

+4165
-15
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ docs/source/bip38.rst
7878
docs/source/bip39.rst
7979
docs/source/core.rst
8080
docs/source/crypto.rst
81+
docs/source/descriptor.rst
8182
docs/source/psbt.rst
8283
docs/source/script.rst
8384
docs/source/symmetric.rst

docs/source/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def extract_docs(infile, outfile):
6262
# Generate the documentation source files
6363
for m in [
6464
'core', 'crypto', 'address', 'bip32', 'bip38', 'bip39', 'script', 'psbt',
65-
'symmetric', 'transaction', 'elements', 'anti_exfil'
65+
'descriptor', 'symmetric', 'transaction', 'elements', 'anti_exfil'
6666
]:
6767
extract_docs('../../include/wally_%s.h' % m, '%s.rst' % m)
6868

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ libwally-core documentation
1313
bip39
1414
psbt
1515
script
16+
descriptor
1617
symmetric
1718
transaction
1819
elements

include/wally.hpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,37 @@ inline int cleanup(uint32_t flags) {
409409
return ret;
410410
}
411411

412+
template <class DESCRIPTOR, class VARS_IN>
413+
inline int descriptor_canonicalize(const DESCRIPTOR& descriptor, const VARS_IN& vars_in, uint32_t flags, char** output) {
414+
int ret = ::wally_descriptor_canonicalize(detail::get_p(descriptor), detail::get_p(vars_in), flags, output);
415+
return ret;
416+
}
417+
418+
template <class DESCRIPTOR, class VARS_IN>
419+
inline int descriptor_get_checksum(const DESCRIPTOR& descriptor, const VARS_IN& vars_in, uint32_t flags, char** output) {
420+
int ret = ::wally_descriptor_get_checksum(detail::get_p(descriptor), detail::get_p(vars_in), flags, output);
421+
return ret;
422+
}
423+
424+
template <class DESCRIPTOR, class VARS_IN>
425+
inline int descriptor_to_address(const DESCRIPTOR& descriptor, const VARS_IN& vars_in, uint32_t child_num, uint32_t network, uint32_t flags, char** output) {
426+
int ret = ::wally_descriptor_to_address(detail::get_p(descriptor), detail::get_p(vars_in), child_num, network, flags, output);
427+
return ret;
428+
}
429+
430+
template <class DESCRIPTOR, class VARS_IN>
431+
inline int descriptor_to_addresses(const DESCRIPTOR& descriptor, const VARS_IN& vars_in, uint32_t child_num, uint32_t network, uint32_t flags, char** output, size_t num_outputs) {
432+
int ret = ::wally_descriptor_to_addresses(detail::get_p(descriptor), detail::get_p(vars_in), child_num, network, flags, output, num_outputs);
433+
return ret;
434+
}
435+
436+
template <class DESCRIPTOR, class VARS_IN, class BYTES_OUT>
437+
inline int descriptor_to_scriptpubkey(const DESCRIPTOR& descriptor, const VARS_IN& vars_in, uint32_t child_num, uint32_t network, uint32_t depth, uint32_t index, uint32_t flags, BYTES_OUT& bytes_out, size_t* written = 0) {
438+
size_t n;
439+
int ret = ::wally_descriptor_to_scriptpubkey(detail::get_p(descriptor), detail::get_p(vars_in), child_num, network, depth, index, flags, bytes_out.data(), bytes_out.size(), written ? written : &n);
440+
return written || ret != WALLY_OK ? ret : n == static_cast<size_t>(bytes_out.size()) ? WALLY_OK : WALLY_EINVAL;
441+
}
442+
412443
template <class PRIV_KEY>
413444
inline int ec_private_key_verify(const PRIV_KEY& priv_key) {
414445
int ret = ::wally_ec_private_key_verify(priv_key.data(), priv_key.size());
@@ -593,6 +624,13 @@ inline int map_sort(const MAP_IN& map_in, uint32_t flags) {
593624
return ret;
594625
}
595626

627+
template <class MINISCRIPT, class VARS_IN, class BYTES_OUT>
628+
inline int miniscript_to_script(const MINISCRIPT& miniscript, const VARS_IN& vars_in, uint32_t child_num, uint32_t flags, BYTES_OUT& bytes_out, size_t* written = 0) {
629+
size_t n;
630+
int ret = ::wally_miniscript_to_script(detail::get_p(miniscript), detail::get_p(vars_in), child_num, flags, bytes_out.data(), bytes_out.size(), written ? written : &n);
631+
return written || ret != WALLY_OK ? ret : n == static_cast<size_t>(bytes_out.size()) ? WALLY_OK : WALLY_EINVAL;
632+
}
633+
596634
template <class PASS, class SALT, class BYTES_OUT>
597635
inline int pbkdf2_hmac_sha256(const PASS& pass, const SALT& salt, uint32_t flags, uint32_t cost, BYTES_OUT& bytes_out) {
598636
int ret = ::wally_pbkdf2_hmac_sha256(pass.data(), pass.size(), salt.data(), salt.size(), flags, cost, bytes_out.data(), bytes_out.size());

include/wally_address.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ struct ext_key;
1616
#define WALLY_CA_PREFIX_LIQUID_REGTEST 0x04 /** Liquid v1 confidential address prefix for regtest */
1717
#define WALLY_CA_PREFIX_LIQUID_TESTNET 0x17 /** Liquid v1 confidential address prefix for testnet */
1818

19+
#define WALLY_NETWORK_NONE 0x00 /** Used for miniscript parsing only */
1920
#define WALLY_NETWORK_BITCOIN_MAINNET 0x01 /** Bitcoin mainnet */
21+
#define WALLY_NETWORK_BITCOIN_REGTEST 0xff /** Bitcoin regtest: Behaves as testnet except for segwit */
2022
#define WALLY_NETWORK_BITCOIN_TESTNET 0x02 /** Bitcoin testnet */
2123
#define WALLY_NETWORK_LIQUID 0x03 /** Liquid v1 */
2224
#define WALLY_NETWORK_LIQUID_REGTEST 0x04 /** Liquid v1 regtest */

include/wally_crypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ WALLY_CORE_API int wally_pbkdf2_hmac_sha512(
290290
#define EC_PUBLIC_KEY_LEN 33
291291
/** The length of an uncompressed public key */
292292
#define EC_PUBLIC_KEY_UNCOMPRESSED_LEN 65
293+
/** The length of an x-only public key */
294+
#define EC_PUBLIC_KEY_XONLY_LEN 32
293295
/** The length of a message hash to EC sign */
294296
#define EC_MESSAGE_HASH_LEN 32
295297
/** The length of a compact signature produced by EC signing */

include/wally_descriptor.h

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#ifndef LIBWALLY_CORE_DESCRIPTOR_H
2+
#define LIBWALLY_CORE_DESCRIPTOR_H
3+
4+
#include "wally_core.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
struct wally_map;
11+
12+
/* Miniscript type flag */
13+
#define WALLY_MINISCRIPT_WITNESS_SCRIPT 0x00 /** Witness script */
14+
#define WALLY_MINISCRIPT_TAPSCRIPT 0x01 /** Tapscript */
15+
16+
/**
17+
* Canonicalize a descriptor.
18+
*
19+
* :param descriptor: Output descriptor.
20+
* :param vars_in: Map of variable names to values, or NULL.
21+
* :param flags: For future use. Must be 0.
22+
* :param output: Destination for the resulting canonical descriptor.
23+
*| The string returned should be freed using `wally_free_string`.
24+
*
25+
* Replaces any variables from ``vars_in`` with their mapped values,
26+
* and adds a checksum if required. Key names for ``vars_in`` must be 16
27+
* characters or less and start with a letter.
28+
*
29+
* .. note:: Other canonicalization (hardened derivation indicator
30+
* mapping, and private to public key mapping) is not yet implemented.
31+
*/
32+
WALLY_CORE_API int wally_descriptor_canonicalize(
33+
const char *descriptor,
34+
const struct wally_map *vars_in,
35+
uint32_t flags,
36+
char **output);
37+
38+
/**
39+
* Create a script corresponding to a miniscript string.
40+
*
41+
* :param miniscript: Miniscript string.
42+
* :param vars_in: Map of variable names to values, or NULL.
43+
* :param child_num: The BIP32 child number to derive.
44+
* :param flags: Flags controlling the type of script to create. Use one of
45+
*| ``WALLY_MINISCRIPT_WITNESS_SCRIPT`` or ``WALLY_MINISCRIPT_TAPSCRIPT``.
46+
* :param bytes_out: Destination for the resulting scriptPubkey.
47+
* :param len: The length of ``bytes_out`` in bytes.
48+
* :param written: Destination for the number of bytes written to ``bytes_out``.
49+
*/
50+
WALLY_CORE_API int wally_miniscript_to_script(
51+
const char *miniscript,
52+
const struct wally_map *vars_in,
53+
uint32_t child_num,
54+
uint32_t flags,
55+
unsigned char *bytes_out,
56+
size_t len,
57+
size_t *written);
58+
59+
/**
60+
* Create a scriptPubKey corresponding to an output descriptor.
61+
*
62+
* :param descriptor: Output descriptor.
63+
* :param vars_in: Map of variable names to values, or NULL.
64+
* :param child_num: The BIP32 child number to derive.
65+
* :param network: ``WALLY_NETWORK_`` constant descripting the network to generate for.
66+
* :param depth: Number of the descriptor depth. Default is 0.
67+
* :param index: Number of the descriptor index. Default is 0.
68+
* :param flags: For future use. Must be 0.
69+
* :param bytes_out: Destination for the resulting scriptPubkey.
70+
* :param len: The length of ``bytes_out`` in bytes.
71+
* :param written: Destination for the number of bytes written to ``bytes_out``.
72+
*/
73+
WALLY_CORE_API int wally_descriptor_to_scriptpubkey(
74+
const char *descriptor,
75+
const struct wally_map *vars_in,
76+
uint32_t child_num,
77+
uint32_t network,
78+
uint32_t depth,
79+
uint32_t index,
80+
uint32_t flags,
81+
unsigned char *bytes_out,
82+
size_t len,
83+
size_t *written);
84+
85+
/**
86+
* Create an address corresponding to an output descriptor.
87+
*
88+
* :param descriptor: Output descriptor.
89+
* :param vars_in: Map of variable names to values, or NULL.
90+
* :param child_num: The BIP32 child number to derive.
91+
* :param network: ``WALLY_NETWORK_`` constant descripting the network to generate for.
92+
* :param flags: For future use. Must be 0.
93+
* :param output: Destination for the resulting addresss.
94+
*| The string returned should be freed using `wally_free_string`.
95+
*/
96+
WALLY_CORE_API int wally_descriptor_to_address(
97+
const char *descriptor,
98+
const struct wally_map *vars_in,
99+
uint32_t child_num,
100+
uint32_t network,
101+
uint32_t flags,
102+
char **output);
103+
104+
/**
105+
* Create addresses that correspond to the derived range of an output descriptor.
106+
*
107+
* :param descriptor: Output descriptor.
108+
* :param vars_in: Map of variable names to values, or NULL.
109+
* :param child_num: The first BIP32 child number to derive.
110+
* :param network: ``WALLY_NETWORK_`` constant descripting the network to generate for.
111+
* :param flags: For future use. Must be 0.
112+
* :param output: Destination for the resulting addresses.
113+
* :param num_outputs: The number of items in ``output``. Addresses will be
114+
*| generated into this array starting from child_num, incrementing by 1.
115+
*| The addresses returned should be freed using `wally_free_string`.
116+
*/
117+
WALLY_CORE_API int wally_descriptor_to_addresses(
118+
const char *descriptor,
119+
const struct wally_map *vars_in,
120+
uint32_t child_num,
121+
uint32_t network,
122+
uint32_t flags,
123+
char **output,
124+
size_t num_outputs);
125+
126+
/**
127+
* Create an output descriptor checksum.
128+
*
129+
* :param descriptor: Output descriptor.
130+
* :param vars_in: Map of variable names to values, or NULL.
131+
* :param flags: For future use. Must be 0.
132+
* :param output: Destination for the resulting descriptor checksum.
133+
*| The string returned should be freed using `wally_free_string`.
134+
*/
135+
WALLY_CORE_API int wally_descriptor_get_checksum(
136+
const char *descriptor,
137+
const struct wally_map *vars_in,
138+
uint32_t flags,
139+
char **output);
140+
141+
#ifdef __cplusplus
142+
}
143+
#endif
144+
145+
#endif /* LIBWALLY_CORE_DESCRIPTOR_H */

src/Makefile.am

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ include_HEADERS += $(top_srcdir)/include/wally_bip38.h
1414
include_HEADERS += $(top_srcdir)/include/wally_bip39.h
1515
include_HEADERS += $(top_srcdir)/include/wally_core.h
1616
include_HEADERS += $(top_srcdir)/include/wally_crypto.h
17+
include_HEADERS += $(top_srcdir)/include/wally_descriptor.h
1718
include_HEADERS += $(top_srcdir)/include/wally_elements.h
1819
include_HEADERS += $(top_srcdir)/include/wally_psbt.h
1920
include_HEADERS += $(top_srcdir)/include/wally_script.h
@@ -158,6 +159,7 @@ all: swig_java/wallycore.jar
158159

159160
SWIG_JAVA_TEST_DEPS = \
160161
$(sjs)/$(cbt)/test_bip32.class \
162+
$(sjs)/$(cbt)/test_descriptor.class \
161163
$(sjs)/$(cbt)/test_tx.class \
162164
$(sjs)/$(cbt)/test_scripts.class \
163165
$(sjs)/$(cbt)/test_mnemonic.class
@@ -190,6 +192,7 @@ libwallycore_la_SOURCES = \
190192
bip38.c \
191193
bip39.c \
192194
bech32.c \
195+
descriptor.c \
193196
ecdh.c \
194197
elements.c \
195198
blech32.c \
@@ -221,6 +224,7 @@ libwallycore_la_INCLUDES = \
221224
include/wally_bip39.h \
222225
include/wally_core.h \
223226
include/wally_crypto.h \
227+
include/wally_descriptor.h \
224228
include/wally_elements.h \
225229
include/wally_psbt.h \
226230
include/wally_script.h \
@@ -286,6 +290,14 @@ test_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
286290
if PYTHON_MANYLINUX
287291
test_tx_LDADD += $(PYTHON_LIBS)
288292
endif
293+
TESTS += test_descriptor
294+
noinst_PROGRAMS += test_descriptor
295+
test_descriptor_SOURCES = ctest/test_descriptor.c
296+
test_descriptor_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
297+
test_descriptor_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
298+
if PYTHON_MANYLINUX
299+
test_descriptor_LDADD += $(PYTHON_LIBS)
300+
endif
289301
if BUILD_ELEMENTS
290302
TESTS += test_elements_tx
291303
noinst_PROGRAMS += test_elements_tx
@@ -309,6 +321,7 @@ if RUN_PYTHON_TESTS
309321
$(AM_V_at)$(PYTHON_TEST) test/test_bip32.py
310322
$(AM_V_at)$(PYTHON_TEST) test/test_bip38.py
311323
$(AM_V_at)$(PYTHON_TEST) test/test_bip39.py
324+
$(AM_V_at)$(PYTHON_TEST) test/test_descriptor.py
312325
$(AM_V_at)$(PYTHON_TEST) test/test_ecdh.py
313326
$(AM_V_at)$(PYTHON_TEST) test/test_hash.py
314327
$(AM_V_at)$(PYTHON_TEST) test/test_hex.py
@@ -333,6 +346,7 @@ if BUILD_ELEMENTS
333346
endif
334347
if USE_SWIG_PYTHON
335348
$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/bip32.py
349+
$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/descriptor.py
336350
$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/mnemonic.py
337351
$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/psbt.py
338352
$(AM_V_at)$(PYTHON_SWIGTEST) swig_python/contrib/sha.py
@@ -353,6 +367,7 @@ if BUILD_ELEMENTS
353367
$(AM_V_at)$(JAVA_TEST)test_pegs
354368
endif
355369
$(AM_V_at)$(JAVA_TEST)test_bip32
370+
$(AM_V_at)$(JAVA_TEST)test_descriptor
356371
$(AM_V_at)$(JAVA_TEST)test_mnemonic
357372
$(AM_V_at)$(JAVA_TEST)test_scripts
358373
$(AM_V_at)$(JAVA_TEST)test_tx

src/address.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ int wally_scriptpubkey_to_address(const unsigned char *scriptpubkey, size_t scri
167167
case WALLY_NETWORK_BITCOIN_MAINNET:
168168
bytes[0] = WALLY_ADDRESS_VERSION_P2PKH_MAINNET;
169169
break;
170+
case WALLY_NETWORK_BITCOIN_REGTEST:
170171
case WALLY_NETWORK_BITCOIN_TESTNET:
171172
bytes[0] = WALLY_ADDRESS_VERSION_P2PKH_TESTNET;
172173
break;
@@ -189,6 +190,7 @@ int wally_scriptpubkey_to_address(const unsigned char *scriptpubkey, size_t scri
189190
case WALLY_NETWORK_BITCOIN_MAINNET:
190191
bytes[0] = WALLY_ADDRESS_VERSION_P2SH_MAINNET;
191192
break;
193+
case WALLY_NETWORK_BITCOIN_REGTEST:
192194
case WALLY_NETWORK_BITCOIN_TESTNET:
193195
bytes[0] = WALLY_ADDRESS_VERSION_P2SH_TESTNET;
194196
break;

0 commit comments

Comments
 (0)