|
| 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 | +/** An opaque type holding a parsed minscript/descriptor expression */ |
| 12 | +struct wally_descriptor; |
| 13 | + |
| 14 | +/* Miniscript type flag */ |
| 15 | +#define WALLY_MINISCRIPT_WITNESS_SCRIPT 0x00 /** Witness script */ |
| 16 | +#define WALLY_MINISCRIPT_TAPSCRIPT 0x01 /** Tapscript */ |
| 17 | +#define WALLY_MINISCRIPT_ONLY 0x02 /** Only allow miniscript (not descriptor) expressions */ |
| 18 | + |
| 19 | +#define WALLY_MS_IS_RANGED 0x01 /** Allows key ranges via '*' */ |
| 20 | + |
| 21 | + |
| 22 | +/** |
| 23 | + * Parse an output descriptor or miniscript expression. |
| 24 | + * |
| 25 | + * :param descriptor: Output descriptor or miniscript expression to parse. |
| 26 | + * :param vars_in: Map of variable names to values, or NULL. |
| 27 | + * :param network: ``WALLY_NETWORK_`` constant descripting the network the |
| 28 | + *| descriptor belongs to, or WALLY_NETWORK_NONE for miniscript-only expressions. |
| 29 | + * :param flags: Include ``WALLY_MINISCRIPT_ONLY`` to disallow descriptor |
| 30 | + *| expressions, ``WALLY_MINISCRIPT_TAPSCRIPT`` to use x-only pubkeys, or 0. |
| 31 | + * :param output: Destination for the resulting parsed descriptor. |
| 32 | + *| The descriptor returned should be freed using `wally_descriptor_free`. |
| 33 | + * |
| 34 | + * Variable names can be used in the descriptor string and will be substituted |
| 35 | + * with the contents of ``vars_in`` during parsing. Key names for ``vars_in`` |
| 36 | + * must be 16 characters or less in length and start with a letter. |
| 37 | + */ |
| 38 | +WALLY_CORE_API int wally_descriptor_parse( |
| 39 | + const char *descriptor, |
| 40 | + const struct wally_map *vars_in, |
| 41 | + uint32_t network, |
| 42 | + uint32_t flags, |
| 43 | + struct wally_descriptor **output); |
| 44 | + |
| 45 | +/** |
| 46 | + * Free a parsed output descriptor or miniscript expression. |
| 47 | + * |
| 48 | + * :param descriptor: Parsed output descriptor or miniscript expression to free. |
| 49 | + */ |
| 50 | +WALLY_CORE_API int wally_descriptor_free( |
| 51 | + struct wally_descriptor *descriptor); |
| 52 | + |
| 53 | +/** |
| 54 | + * Canonicalize a descriptor. |
| 55 | + * |
| 56 | + * :param descriptor: Parsed output descriptor or miniscript expression. |
| 57 | + * :param flags: For future use. Must be 0. |
| 58 | + * :param output: Destination for the resulting canonical descriptor. |
| 59 | + *| The string returned should be freed using `wally_free_string`. |
| 60 | + * |
| 61 | + * .. note:: Other canonicalization (hardened derivation indicator |
| 62 | + * mapping, and private to public key mapping) is not yet implemented. |
| 63 | + */ |
| 64 | +WALLY_CORE_API int wally_descriptor_canonicalize( |
| 65 | + const struct wally_descriptor *descriptor, |
| 66 | + uint32_t flags, |
| 67 | + char **output); |
| 68 | + |
| 69 | +/** |
| 70 | + * Create an output descriptor checksum. |
| 71 | + * |
| 72 | + * :param descriptor: Parsed output descriptor or miniscript expression. |
| 73 | + * :param flags: For future use. Must be 0. |
| 74 | + * :param output: Destination for the resulting descriptor checksum. |
| 75 | + *| The string returned should be freed using `wally_free_string`. |
| 76 | + */ |
| 77 | +WALLY_CORE_API int wally_descriptor_get_checksum( |
| 78 | + const struct wally_descriptor *descriptor, |
| 79 | + uint32_t flags, |
| 80 | + char **output); |
| 81 | + |
| 82 | +/** |
| 83 | + * Get the features used in a parsed output descriptor or miniscript expression. |
| 84 | + * |
| 85 | + * :param descriptor: Parsed output descriptor or miniscript expression. |
| 86 | + * :param value_out: Destination for the resulting ``WALLY_MS_`` feature flags. |
| 87 | + */ |
| 88 | +WALLY_CORE_API int wally_descriptor_get_features( |
| 89 | + const struct wally_descriptor *descriptor, |
| 90 | + uint32_t *value_out); |
| 91 | + |
| 92 | +/** |
| 93 | + * Get the maximum length of a script corresponding to an output descriptor. |
| 94 | + * |
| 95 | + * :param descriptor: Parsed output descriptor or miniscript expression. |
| 96 | + * :param flags: For future use. Must be 0. |
| 97 | + * :param written: Destination for the resulting maximum script length. |
| 98 | + * |
| 99 | + * .. note:: This function overestimates the script size required, but is |
| 100 | + *| cheap to compute (does not require script generation). |
| 101 | + */ |
| 102 | +WALLY_CORE_API int wally_descriptor_to_script_get_maximum_length( |
| 103 | + const struct wally_descriptor *descriptor, |
| 104 | + uint32_t flags, |
| 105 | + size_t *written); |
| 106 | + |
| 107 | +/** |
| 108 | + * Get the length of a script corresponding to an output descriptor. |
| 109 | + * |
| 110 | + * :param descriptor: Parsed output descriptor or miniscript expression. |
| 111 | + * :param depth: Depth of the expression tree to generate from. Pass 0 to generate from the root. |
| 112 | + * :param index: The zero-based index of the child at depth ``depth`` to generate from. |
| 113 | + * :param variant: The variant of descriptor to generate. Pass 0 for the default. |
| 114 | + * :param child_num: The BIP32 child number to derive, or 0 for static descriptors. |
| 115 | + * :param flags: For future use. Must be 0. |
| 116 | + * :param written: Destination for the resulting script length. |
| 117 | + * |
| 118 | + * .. note:: This function may return a size slightly larger than the final |
| 119 | + *| scripts generated size, typically by 1-2 bytes if so. |
| 120 | + * .. note:: Computing the script length using this function is expensive, as |
| 121 | + *| it must partially generate the script. Prefer to use |
| 122 | + *| either `wally_descriptor_to_script_get_maximum_length` |
| 123 | + *| or pass a large buffer to `wally_descriptor_to_script` and retry |
| 124 | + *| the call with a larger buffer if the it was too small. |
| 125 | + */ |
| 126 | +WALLY_CORE_API int wally_descriptor_to_script_len( |
| 127 | + struct wally_descriptor *descriptor, |
| 128 | + uint32_t depth, |
| 129 | + uint32_t index, |
| 130 | + uint32_t variant, |
| 131 | + uint32_t child_num, |
| 132 | + uint32_t flags, |
| 133 | + size_t *written); |
| 134 | + |
| 135 | +/** |
| 136 | + * Create a script corresponding to an output descriptor or miniscript expression. |
| 137 | + * |
| 138 | + * :param descriptor: Parsed output descriptor or miniscript expression. |
| 139 | + * :param depth: Depth of the expression tree to generate from. Pass 0 to generate from the root. |
| 140 | + * :param index: The zero-based index of the child at depth ``depth`` to generate from. |
| 141 | + * :param variant: The variant of descriptor to generate. Pass 0 for the default. |
| 142 | + * :param child_num: The BIP32 child number to derive, or 0 for static descriptors. |
| 143 | + * :param flags: For future use. Must be 0. |
| 144 | + * :param bytes_out: Destination for the resulting scriptPubKey or script. |
| 145 | + * :param len: The length of ``bytes_out`` in bytes. |
| 146 | + * :param written: Destination for the number of bytes written to ``bytes_out``. |
| 147 | + */ |
| 148 | +WALLY_CORE_API int wally_descriptor_to_script( |
| 149 | + struct wally_descriptor *descriptor, |
| 150 | + uint32_t depth, |
| 151 | + uint32_t index, |
| 152 | + uint32_t variant, |
| 153 | + uint32_t child_num, |
| 154 | + uint32_t flags, |
| 155 | + unsigned char *bytes_out, |
| 156 | + size_t len, |
| 157 | + size_t *written); |
| 158 | + |
| 159 | +/** |
| 160 | + * Create an address corresponding to an output descriptor. |
| 161 | + * |
| 162 | + * :param descriptor: Parsed output descriptor. |
| 163 | + * :param variant: The variant of descriptor to generate. Pass 0 for the default. |
| 164 | + * :param child_num: The BIP32 child number to derive, or zero for static descriptors. |
| 165 | + * :param flags: For future use. Must be 0. |
| 166 | + * :param output: Destination for the resulting addresss. |
| 167 | + *| The string returned should be freed using `wally_free_string`. |
| 168 | + */ |
| 169 | +WALLY_CORE_API int wally_descriptor_to_address( |
| 170 | + struct wally_descriptor *descriptor, |
| 171 | + uint32_t variant, |
| 172 | + uint32_t child_num, |
| 173 | + uint32_t flags, |
| 174 | + char **output); |
| 175 | + |
| 176 | +/** |
| 177 | + * Create addresses that correspond to the derived range of an output descriptor. |
| 178 | + * |
| 179 | + * :param descriptor: Parsed output descriptor. |
| 180 | + * :param variant: The variant of descriptor to generate. Pass 0 for the default. |
| 181 | + * :param child_num: The BIP32 child number to derive, or zero for static descriptors. |
| 182 | + * :param flags: For future use. Must be 0. |
| 183 | + * :param output: Destination for the resulting addresses. |
| 184 | + * :param num_outputs: The number of items in ``output``. Addresses will be |
| 185 | + *| generated into this array starting from child_num, incrementing by 1. |
| 186 | + *| The addresses returned should be freed using `wally_free_string`. |
| 187 | + */ |
| 188 | +WALLY_CORE_API int wally_descriptor_to_addresses( |
| 189 | + struct wally_descriptor *descriptor, |
| 190 | + uint32_t variant, |
| 191 | + uint32_t child_num, |
| 192 | + uint32_t flags, |
| 193 | + char **output, |
| 194 | + size_t num_outputs); |
| 195 | + |
| 196 | +#ifdef __cplusplus |
| 197 | +} |
| 198 | +#endif |
| 199 | + |
| 200 | +#endif /* LIBWALLY_CORE_DESCRIPTOR_H */ |
0 commit comments