Skip to content

Commit

Permalink
MERGEBACK: descriptor: allow getting and setting the descriptor netwo…
Browse files Browse the repository at this point in the history
…rk, tests
  • Loading branch information
jgriffiths committed Feb 9, 2023
1 parent 864355b commit 08439fe
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 9 deletions.
12 changes: 12 additions & 0 deletions include/wally.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,12 @@ inline int descriptor_get_features(const DESCRIPTOR& descriptor, uint32_t* value
return ret;
}

template <class DESCRIPTOR>
inline int descriptor_get_network(const DESCRIPTOR& descriptor, uint32_t* value_out) {
int ret = ::wally_descriptor_get_network(detail::get_p(descriptor), value_out);
return ret;
}

template <class DESCRIPTOR>
inline int descriptor_get_num_ranges(const DESCRIPTOR& descriptor, uint32_t* value_out) {
int ret = ::wally_descriptor_get_num_ranges(detail::get_p(descriptor), value_out);
Expand All @@ -474,6 +480,12 @@ inline int descriptor_parse(const DESCRIPTOR& descriptor, const VARS_IN& vars_in
return ret;
}

template <class DESCRIPTOR>
inline int descriptor_set_network(const DESCRIPTOR& descriptor, uint32_t network) {
int ret = ::wally_descriptor_set_network(detail::get_p(descriptor), network);
return ret;
}

template <class DESCRIPTOR>
inline int descriptor_to_address(const DESCRIPTOR& descriptor, uint32_t variant, uint32_t range_index, uint32_t child_num, uint32_t flags, char** output) {
int ret = ::wally_descriptor_to_address(detail::get_p(descriptor), variant, range_index, child_num, flags, output);
Expand Down
32 changes: 31 additions & 1 deletion include/wally_descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct wally_descriptor;
*
* :param descriptor: Output descriptor or miniscript expression to parse.
* :param vars_in: Map of variable names to values, or NULL.
* :param network: ``WALLY_NETWORK_`` constant descripting the network the
* :param network: ``WALLY_NETWORK_`` constant describing the network the
*| descriptor belongs to, or WALLY_NETWORK_NONE for miniscript-only expressions.
* :param flags: Include ``WALLY_MINISCRIPT_ONLY`` to disallow descriptor
*| expressions, ``WALLY_MINISCRIPT_TAPSCRIPT`` to use x-only pubkeys, or 0.
Expand Down Expand Up @@ -79,6 +79,36 @@ WALLY_CORE_API int wally_descriptor_get_checksum(
uint32_t flags,
char **output);

/**
* Get the network used in a parsed output descriptor or miniscript expression.
*
* :param descriptor: Parsed output descriptor or miniscript expression.
* :param value_out: Destination for the resulting ``WALLY_NETWORK`` network.
*
* A descriptor parsed with ``WALLY_NETWORK_NONE`` will infer its network from
* the contained key expressions. If the descriptor does not contain network
* information (e.g. its keys are raw keys), then this function will
* return ``WALLY_NETWORK_NONE``, and `wally_descriptor_set_network` must be
* called to set a network for the descriptor before addresses can be
* generated from it.
*/
WALLY_CORE_API int wally_descriptor_get_network(
const struct wally_descriptor *descriptor,
uint32_t *value_out);

/**
* set the network for a parsed output descriptor or miniscript expression.
*
* :param descriptor: Parsed output descriptor or miniscript expression.
* :param network: The ``WALLY_NETWORK`` network constant describing the
*| network the descriptor should belong to.
*
* .. note:: The network can only be set if it is currently ``WALLY_NETWORK_NONE``.
*/
WALLY_CORE_API int wally_descriptor_set_network(
struct wally_descriptor *descriptor,
uint32_t network);

/**
* Get the features used in a parsed output descriptor or miniscript expression.
*
Expand Down
24 changes: 16 additions & 8 deletions src/ctest/test_descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1602,31 +1602,39 @@ static const struct address_test {
* Address error cases
*/
{
"address errchk - no network",
"wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)",
WALLY_NETWORK_NONE, 0, 0, ADDR("")
},{
"address errchk - invalid network",
"wpkh(02f9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9)",
0xf0, 0, 0, ADDR("")
},{
"address errchk - addr - unmatch network1",
"address errchk - addr - mismatched network 1",
"addr(bc1qrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3)",
WALLY_NETWORK_BITCOIN_TESTNET, 0, 0, ADDR("")
},{
"address errchk - addr - unmatched network 2",
"addr(ex1qwu7hp9vckakyuw6htsy244qxtztrlyez4l7qlrpg68v6drgvj39q06fgz7)",
WALLY_NETWORK_LIQUID_REGTEST, 0, 0, ADDR("")
},{
"address errchk - addr - network conflict",
"multi(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB,tpubD6NzVbkrYhZ4XJDrzRvuxHEyQaPd1mwwdDofEJwekX18tAdsqeKfxss79AJzg1431FybXg5rfpTrJF4iAhyR7RubberdzEQXiRmXGADH2eA)",
WALLY_NETWORK_NONE, 0, 0, ADDR("")
},{
"address errchk - addr - no HRP",
"addr(bcqrp33g0q5c5txsp9arysrx4k6zdkfs4nce4xj0gdcccefvpysxf3qccfmv3)",
WALLY_NETWORK_BITCOIN_TESTNET, 0, 0, ADDR("")
},{
"address errchk - addr - unmatch network2",
"addr(ex1qwu7hp9vckakyuw6htsy244qxtztrlyez4l7qlrpg68v6drgvj39q06fgz7)",
WALLY_NETWORK_LIQUID_REGTEST, 0, 0, ADDR("")
},{
"address errchk - unsupport address - p2pk",
"address errchk - unsupported address - p2pk",
"pk(0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798)",
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, ADDR("")
},{
"address errchk - unsupport address - uncompressed p2pk",
"address errchk - unsupported address - uncompressed p2pk",
"pk(uncompressed)",
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, ADDR("")
},{
"address errchk - unsupport address - raw",
"address errchk - unsupported address - raw",
"raw(6a4c4f54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e)#zf2avljj",
WALLY_NETWORK_BITCOIN_MAINNET, 0, 0, ADDR("")
},{
Expand Down
25 changes: 25 additions & 0 deletions src/descriptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -2526,6 +2526,31 @@ int wally_descriptor_canonicalize(const struct wally_descriptor *descriptor,
return WALLY_OK;
}

int wally_descriptor_get_network(const struct wally_descriptor *descriptor,
uint32_t *value_out)
{
if (value_out)
*value_out = 0;
if (!descriptor || !value_out)
return WALLY_EINVAL;
*value_out = descriptor->addr_ver ? descriptor->addr_ver->network : WALLY_NETWORK_NONE;
return WALLY_OK;
}

int wally_descriptor_set_network(struct wally_descriptor *descriptor,
uint32_t network)
{
/* Allow setting a non-NONE network only if there isn't one already */
if (!descriptor || network == WALLY_NETWORK_NONE)
return WALLY_EINVAL;
if (descriptor->addr_ver && descriptor->addr_ver->network == network)
return WALLY_OK; /* No-op */
if (descriptor->addr_ver)
return WALLY_EINVAL; /* Already have a network */
descriptor->addr_ver = addr_ver_from_network(network);
return descriptor->addr_ver ? WALLY_OK : WALLY_EINVAL;
}

int wally_descriptor_get_features(const struct wally_descriptor *descriptor,
uint32_t *value_out)
{
Expand Down
2 changes: 2 additions & 0 deletions src/swig_java/swig.i
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,10 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) {
%returns_string(wally_descriptor_canonicalize);
%returns_string(wally_descriptor_get_checksum);
%returns_size_t(wally_descriptor_get_features);
%returns_size_t(wally_descriptor_get_network);
%returns_size_t(wally_descriptor_get_num_ranges);
%returns_size_t(wally_descriptor_get_num_variants);
%returns_void__(wally_descriptor_set_network);
%returns_void__(wally_descriptor_free);
%returns_struct(wally_descriptor_parse, wally_descriptor);
%returns_string(wally_descriptor_to_address);
Expand Down
2 changes: 2 additions & 0 deletions src/test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,11 @@ class wally_psbt(Structure):
('wally_descriptor_free', c_int, [c_void_p]),
('wally_descriptor_get_checksum', c_int, [c_void_p, c_uint32, c_char_p_p]),
('wally_descriptor_get_features', c_int, [c_void_p, c_uint_p]),
('wally_descriptor_get_network', c_int, [c_void_p, c_uint_p]),
('wally_descriptor_get_num_ranges', c_int, [c_void_p, c_uint_p]),
('wally_descriptor_get_num_variants', c_int, [c_void_p, c_uint_p]),
('wally_descriptor_parse', c_int, [c_char_p, POINTER(wally_map), c_uint32, c_uint32, POINTER(c_void_p)]),
('wally_descriptor_set_network', c_int, [c_void_p, c_uint32]),
('wally_descriptor_to_address', c_int, [c_void_p, c_uint32, c_uint32, c_uint32, c_uint32, c_char_p_p]),
('wally_descriptor_to_addresses', c_int, [c_void_p, c_uint32, c_uint32, c_uint32, c_uint32, POINTER(c_char_p), c_size_t]),
('wally_descriptor_to_script', c_int, [c_void_p, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, c_void_p, c_size_t, c_size_t_p]),
Expand Down
2 changes: 2 additions & 0 deletions src/wasm_package/src/functions.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/wasm_package/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ export function descriptor_canonicalize(descriptor: Ref_wally_descriptor, flags:
export function descriptor_free(descriptor: Ref_wally_descriptor): void;
export function descriptor_get_checksum(descriptor: Ref_wally_descriptor, flags: number): string;
export function descriptor_get_features(descriptor: Ref_wally_descriptor): number;
export function descriptor_get_network(descriptor: Ref_wally_descriptor): number;
export function descriptor_get_num_ranges(descriptor: Ref_wally_descriptor): number;
export function descriptor_get_num_variants(descriptor: Ref_wally_descriptor): number;
export function descriptor_parse(descriptor: string, vars_in: Ref_wally_map, network: number, flags: number): Ref_wally_descriptor;
export function descriptor_set_network(descriptor: Ref_wally_descriptor, network: number): void;
export function descriptor_to_address(descriptor: Ref_wally_descriptor, variant: number, range_index: number, child_num: number, flags: number): string;
export function descriptor_to_addresses(descriptor: Ref_wally_descriptor, variant: number, range_index: number, child_num: number, flags: number, out_len: number): string;
export function descriptor_to_script_get_maximum_length(descriptor: Ref_wally_descriptor, flags: number): number;
Expand Down
2 changes: 2 additions & 0 deletions tools/wasm_exports.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \
,'_wally_descriptor_free' \
,'_wally_descriptor_get_checksum' \
,'_wally_descriptor_get_features' \
,'_wally_descriptor_get_network' \
,'_wally_descriptor_get_num_ranges' \
,'_wally_descriptor_get_num_variants' \
,'_wally_descriptor_parse' \
,'_wally_descriptor_set_network' \
,'_wally_descriptor_to_address' \
,'_wally_descriptor_to_addresses' \
,'_wally_descriptor_to_script' \
Expand Down

0 comments on commit 08439fe

Please sign in to comment.