Skip to content

Commit

Permalink
MERGEBACK: descriptor: test/codegen updates, test network get/set beh…
Browse files Browse the repository at this point in the history
…aviour
  • Loading branch information
jgriffiths committed Feb 9, 2023
1 parent 08439fe commit 31ad972
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 35 deletions.
36 changes: 35 additions & 1 deletion src/test/test_descriptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,40 @@ def test_parse_and_to_script(self):
wally_descriptor_free(d)
self.assertEqual(ret, WALLY_EINVAL)

def test_network(self):
addrs_len = 64
addrs = (c_char_p * addrs_len)()

# Start with a descriptor containing raw keys
descriptor = 'sh(multi(1,022f8bde4d1a07209355b4a7250a5c5128e88b84bddc619ab7cba8d569b240efe4,025cbdf0646e5db4eaa398f365f2ea7a0e3d419b7e0330e39ce92bddedcac4f9bc))'
d = c_void_p()
ret = wally_descriptor_parse(descriptor, None, NETWORK_NONE, 0, d)
self.assertEqual(ret, WALLY_OK)
# Get the network from the descriptor
ret, network = wally_descriptor_get_network(None)
self.assertEqual((ret, network), (WALLY_EINVAL, NETWORK_NONE))
ret, network = wally_descriptor_get_network(d)
self.assertEqual((ret, network), (WALLY_OK, NETWORK_NONE))
# We cannot generate an address for a descriptor without a network
addrs = (c_char_p * 1)()
ret = wally_descriptor_to_addresses(d, 0, 0, 0, 0, addrs, 1)
self.assertEqual(ret, WALLY_EINVAL)
# Set the network for the descriptor
for args in [(None, NETWORK_BTC_MAIN), (d, 0xf0)]:
ret = wally_descriptor_set_network(*args)
self.assertEqual(ret, WALLY_EINVAL)
ret = wally_descriptor_set_network(d, NETWORK_BTC_MAIN)
self.assertEqual(ret, WALLY_OK)
# Verify the network changed
ret, network = wally_descriptor_get_network(d)
self.assertEqual((ret, network), (WALLY_OK, NETWORK_BTC_MAIN))
# We can now generate an address
addrs = (c_char_p * 1)()
ret = wally_descriptor_to_addresses(d, 0, 0, 0, 0, addrs, 1)
self.assertEqual(ret, WALLY_OK)
self.assertEqual(addrs[0], utf8('3ETTzkMnuA4PguZeWYtdCT6Rva3yTHATyP'))
wally_descriptor_free(d)

def test_descriptor_to_addresses(self):
addrs_len = 64
addrs = (c_char_p * addrs_len)()
Expand Down Expand Up @@ -108,7 +142,7 @@ def test_descriptor_to_addresses(self):
len(expected))
self.assertEqual(ret, WALLY_OK)
for i in range(len(expected)):
self.assertEqual(expected[i].encode('utf-8'), addrs[i])
self.assertEqual(utf8(expected[i]), addrs[i])
wally_descriptor_free(d)

# Invalid args
Expand Down
76 changes: 43 additions & 33 deletions src/test/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ class c_char_p_p_class(object):
class c_size_t_p_class(object):
pass
c_size_t_p = c_size_t_p_class()
class c_uint32_p_class(object):
pass
c_uint32_p = c_uint32_p_class()
class c_uint64_p_class(object):
pass
c_uint64_p = c_uint64_p_class()

# ctypes is missing this for some reason
c_uint_p = POINTER(c_uint)

class wally_tx_witness_item(Structure):
_fields_ = [('witness', c_void_p),
('len', c_size_t)]
Expand Down Expand Up @@ -214,8 +214,8 @@ class wally_psbt(Structure):
('bip32_key_from_base58_n_alloc', c_int, [c_char_p, c_size_t, POINTER(POINTER(ext_key))]),
('bip32_key_from_parent', c_int, [POINTER(ext_key), c_uint32, c_uint32, POINTER(ext_key)]),
('bip32_key_from_parent_alloc', c_int, [POINTER(ext_key), c_uint32, c_uint32, POINTER(POINTER(ext_key))]),
('bip32_key_from_parent_path', c_int, [POINTER(ext_key), c_uint_p, c_size_t, c_uint32, POINTER(ext_key)]),
('bip32_key_from_parent_path_alloc', c_int, [POINTER(ext_key), c_uint_p, c_size_t, c_uint32, POINTER(POINTER(ext_key))]),
('bip32_key_from_parent_path', c_int, [POINTER(ext_key), POINTER(c_uint32), c_size_t, c_uint32, POINTER(ext_key)]),
('bip32_key_from_parent_path_alloc', c_int, [POINTER(ext_key), POINTER(c_uint32), c_size_t, c_uint32, POINTER(POINTER(ext_key))]),
('bip32_key_from_parent_path_str', c_int, [POINTER(ext_key), c_char_p, c_uint32, c_uint32, POINTER(ext_key)]),
('bip32_key_from_parent_path_str_alloc', c_int, [POINTER(ext_key), c_char_p, c_uint32, c_uint32, POINTER(POINTER(ext_key))]),
('bip32_key_from_parent_path_str_n', c_int, [POINTER(ext_key), c_char_p, c_size_t, c_uint32, c_uint32, POINTER(ext_key)]),
Expand All @@ -232,8 +232,8 @@ class wally_psbt(Structure):
('bip32_key_to_base58', c_int, [POINTER(ext_key), c_uint32, c_char_p_p]),
('bip32_key_unserialize', c_int, [c_void_p, c_size_t, POINTER(ext_key)]),
('bip32_key_unserialize_alloc', c_int, [c_void_p, c_size_t, POINTER(POINTER(ext_key))]),
('bip32_key_with_tweak_from_parent_path', c_int, [POINTER(ext_key), c_uint_p, c_size_t, c_uint32, POINTER(ext_key)]),
('bip32_key_with_tweak_from_parent_path_alloc', c_int, [POINTER(ext_key), c_uint_p, c_size_t, c_uint32, POINTER(POINTER(ext_key))]),
('bip32_key_with_tweak_from_parent_path', c_int, [POINTER(ext_key), POINTER(c_uint32), c_size_t, c_uint32, POINTER(ext_key)]),
('bip32_key_with_tweak_from_parent_path_alloc', c_int, [POINTER(ext_key), POINTER(c_uint32), c_size_t, c_uint32, POINTER(POINTER(ext_key))]),
('bip38_from_private_key', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_char_p_p]),
('bip38_get_flags', c_int, [c_char_p, c_size_t_p]),
('bip38_raw_from_private_key', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_void_p, c_size_t]),
Expand Down Expand Up @@ -298,10 +298,10 @@ class wally_psbt(Structure):
('wally_descriptor_canonicalize', c_int, [c_void_p, c_uint32, c_char_p_p]),
('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_get_features', c_int, [c_void_p, c_uint32_p]),
('wally_descriptor_get_network', c_int, [c_void_p, c_uint32_p]),
('wally_descriptor_get_num_ranges', c_int, [c_void_p, c_uint32_p]),
('wally_descriptor_get_num_variants', c_int, [c_void_p, c_uint32_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]),
Expand Down Expand Up @@ -352,7 +352,7 @@ class wally_psbt(Structure):
('wally_is_elements_build', c_int, [c_size_t_p]),
('wally_keypath_bip32_verify', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_keypath_get_fingerprint', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_keypath_get_path', c_int, [c_void_p, c_size_t, c_uint_p, c_size_t, c_size_t_p]),
('wally_keypath_get_path', c_int, [c_void_p, c_size_t, POINTER(c_uint32), c_size_t, c_size_t_p]),
('wally_keypath_get_path_len', c_int, [c_void_p, c_size_t, c_size_t_p]),
('wally_keypath_public_key_verify', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_keypath_xonly_public_key_verify', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
Expand All @@ -375,11 +375,11 @@ class wally_psbt(Structure):
('wally_map_hash_preimage_verify', c_int, [c_void_p, c_size_t, c_void_p, c_size_t]),
('wally_map_init', c_int, [c_size_t, c_void_p, POINTER(wally_map)]),
('wally_map_init_alloc', c_int, [c_size_t, c_void_p, POINTER(POINTER(wally_map))]),
('wally_map_keypath_add', c_int, [POINTER(wally_map), c_void_p, c_size_t, c_void_p, c_size_t, c_uint_p, c_size_t]),
('wally_map_keypath_add', c_int, [POINTER(wally_map), c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t]),
('wally_map_keypath_bip32_init_alloc', c_int, [c_size_t, POINTER(POINTER(wally_map))]),
('wally_map_keypath_get_bip32_key_from_alloc', c_int, [POINTER(wally_map), c_size_t, POINTER(ext_key), POINTER(POINTER(ext_key))]),
('wally_map_keypath_get_item_fingerprint', c_int, [POINTER(wally_map), c_size_t, c_void_p, c_size_t]),
('wally_map_keypath_get_item_path', c_int, [POINTER(wally_map), c_size_t, c_uint_p, c_size_t, c_size_t_p]),
('wally_map_keypath_get_item_path', c_int, [POINTER(wally_map), c_size_t, POINTER(c_uint32), c_size_t, c_size_t_p]),
('wally_map_keypath_get_item_path_len', c_int, [POINTER(wally_map), c_size_t, c_size_t_p]),
('wally_map_keypath_public_key_init_alloc', c_int, [c_size_t, POINTER(POINTER(wally_map))]),
('wally_map_preimage_hash160_add', c_int, [POINTER(wally_map), c_void_p, c_size_t]),
Expand Down Expand Up @@ -473,7 +473,7 @@ class wally_psbt(Structure):
('wally_psbt_input_get_utxo_rangeproof', c_int, [POINTER(wally_psbt_input), c_void_p, c_size_t, c_size_t_p]),
('wally_psbt_input_get_utxo_rangeproof_len', c_int, [POINTER(wally_psbt_input), c_size_t_p]),
('wally_psbt_input_is_finalized', c_int, [POINTER(wally_psbt_input), c_size_t_p]),
('wally_psbt_input_keypath_add', c_int, [POINTER(wally_psbt_input), c_void_p, c_size_t, c_void_p, c_size_t, c_uint_p, c_size_t]),
('wally_psbt_input_keypath_add', c_int, [POINTER(wally_psbt_input), c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t]),
('wally_psbt_input_set_amount', c_int, [POINTER(wally_psbt_input), c_uint64]),
('wally_psbt_input_set_amount_rangeproof', c_int, [POINTER(wally_psbt_input), c_void_p, c_size_t]),
('wally_psbt_input_set_asset', c_int, [POINTER(wally_psbt_input), c_void_p, c_size_t]),
Expand Down Expand Up @@ -545,7 +545,7 @@ class wally_psbt(Structure):
('wally_psbt_output_get_value_commitment_len', c_int, [POINTER(wally_psbt_output), c_size_t_p]),
('wally_psbt_output_get_value_rangeproof', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_size_t_p]),
('wally_psbt_output_get_value_rangeproof_len', c_int, [POINTER(wally_psbt_output), c_size_t_p]),
('wally_psbt_output_keypath_add', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_void_p, c_size_t, c_uint_p, c_size_t]),
('wally_psbt_output_keypath_add', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t]),
('wally_psbt_output_set_amount', c_int, [POINTER(wally_psbt_output), c_uint64]),
('wally_psbt_output_set_asset', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]),
('wally_psbt_output_set_asset_blinding_surjectionproof', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]),
Expand Down Expand Up @@ -589,7 +589,7 @@ class wally_psbt(Structure):
('wally_scriptpubkey_p2pkh_from_bytes', c_int, [c_void_p, c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]),
('wally_scriptpubkey_p2sh_from_bytes', c_int, [c_void_p, c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]),
('wally_scriptpubkey_to_address', c_int, [c_void_p, c_size_t, c_uint32, c_char_p_p]),
('wally_scriptsig_multisig_from_bytes', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint_p, c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]),
('wally_scriptsig_multisig_from_bytes', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]),
('wally_scriptsig_p2pkh_from_der', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_size_t_p]),
('wally_scriptsig_p2pkh_from_sig', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]),
('wally_scrypt', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_uint32, c_uint32, c_void_p, c_size_t]),
Expand Down Expand Up @@ -673,7 +673,7 @@ class wally_psbt(Structure):
('wally_wif_to_address', c_int, [c_char_p, c_uint32, c_uint32, c_char_p_p]),
('wally_wif_to_bytes', c_int, [c_char_p, c_uint32, c_uint32, c_void_p, c_size_t]),
('wally_wif_to_public_key', c_int, [c_char_p, c_uint32, c_void_p, c_size_t, c_size_t_p]),
('wally_witness_multisig_from_bytes', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint_p, c_size_t, c_uint32, POINTER(POINTER(wally_tx_witness_stack))]),
('wally_witness_multisig_from_bytes', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t, c_uint32, POINTER(POINTER(wally_tx_witness_stack))]),
('wally_witness_p2wpkh_from_der', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, POINTER(POINTER(wally_tx_witness_stack))]),
('wally_witness_p2wpkh_from_sig', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, POINTER(POINTER(wally_tx_witness_stack))]),
('wally_witness_program_from_bytes', c_int, [c_void_p, c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]),
Expand Down Expand Up @@ -801,7 +801,7 @@ class wally_psbt(Structure):
('wally_psbt_get_output_asset_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]),
('wally_psbt_get_output_asset_surjectionproof', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t, c_size_t_p]),
('wally_psbt_get_output_asset_surjectionproof_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]),
('wally_psbt_get_output_blinder_index', c_int, [POINTER(wally_psbt), c_size_t, c_uint_p]),
('wally_psbt_get_output_blinder_index', c_int, [POINTER(wally_psbt), c_size_t, c_uint32_p]),
('wally_psbt_get_output_blinding_public_key', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t, c_size_t_p]),
('wally_psbt_get_output_blinding_public_key_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]),
('wally_psbt_get_output_blinding_status', c_int, [POINTER(wally_psbt), c_size_t, c_uint32, c_size_t_p]),
Expand Down Expand Up @@ -1009,12 +1009,18 @@ def string_fn_wrapper(fn, *args):
wally_free_string(p)
return [ret_str, (ret, ret_str)][fn.restype is not None]

def int_fn_wrapper(fn, *args):
def size_t_fn_wrapper(fn, *args):
p = c_size_t()
new_args = [a for a in args] + [byref(p)]
ret = fn(*new_args)
return [p.value, (ret, p.value)][fn.restype is not None]

def int32_fn_wrapper(fn, *args):
p = c_uint32()
new_args = [a for a in args] + [byref(p)]
ret = fn(*new_args)
return [p.value, (ret, p.value)][fn.restype is not None]

def int64_fn_wrapper(fn, *args):
p = c_uint64()
new_args = [a for a in args] + [byref(p)]
Expand All @@ -1023,37 +1029,43 @@ def int64_fn_wrapper(fn, *args):

name, restype, argtypes = f
is_str_fn = len(argtypes) and type(argtypes[-1]) is c_char_p_p_class
is_int_fn = len(argtypes) and type(argtypes[-1]) is c_size_t_p_class
is_size_t_fn = len(argtypes) and type(argtypes[-1]) is c_size_t_p_class
is_int32_fn = len(argtypes) and type(argtypes[-1]) is c_uint32_p_class
is_int64_fn = len(argtypes) and type(argtypes[-1]) is c_uint64_p_class
in_str_pos = [i for (i, t) in enumerate(argtypes) if t == c_char_p]
if is_str_fn:
argtypes[-1] = POINTER(c_char_p)
elif is_int_fn:
elif is_size_t_fn:
argtypes[-1] = POINTER(c_size_t)
elif is_int32_fn:
argtypes[-1] = POINTER(c_uint32)
elif is_int64_fn:
argtypes[-1] = POINTER(c_uint64)
fn = bind_fn(name, restype, argtypes)
def mkstr(f): return lambda *args: string_fn_wrapper(f, *args)
def mkint(f): return lambda *args: int_fn_wrapper(f, *args)
def mksize_t(f): return lambda *args: size_t_fn_wrapper(f, *args)
def mkint32(f): return lambda *args: int32_fn_wrapper(f, *args)
def mkint64(f): return lambda *args: int64_fn_wrapper(f, *args)
def mkinstr(f, pos): return lambda *args: in_string_fn_wrapper(f, pos, *args)
if is_str_fn:
fn = mkstr(fn)
elif is_int_fn:
fn = mkint(fn)
elif is_size_t_fn:
fn = mksize_t(fn)
elif is_int32_fn:
fn = mkint32(fn)
elif is_int64_fn:
fn = mkint64(fn)
if len(in_str_pos) > 0 and fn:
for pos in in_str_pos:
fn = mkinstr(fn, pos)
globals()[name] = fn

is_python3 = int(sys.version[0]) >= 3
def load_words(lang):
kwargs = {'name': root_dir + 'src/data/wordlists/%s.txt' % lang, 'mode': 'r'}
if is_python3:
kwargs.update({'encoding': 'utf-8'})
kwargs['file'] = kwargs.pop('name')
kwargs = {
'file': root_dir + 'src/data/wordlists/%s.txt' % lang,
'mode': 'r',
'encoding': 'utf-8'
}
with open(**kwargs) as f:
words_list = [l.strip() for l in f.readlines()]
return words_list, ' '.join(words_list)
Expand All @@ -1067,9 +1079,7 @@ def make_cbuffer(hex_in):
hex_len = len(hex_in) // 2
return unhexlify(hex_in), hex_len

utf8 = lambda s: s
if is_python3:
utf8 = lambda s: s.encode('utf-8')
utf8 = lambda s: s.encode('utf-8')

assert wally_secp_randomize(urandom(32), 32) == WALLY_OK, 'Random init failed'

Expand Down
4 changes: 3 additions & 1 deletion tools/build_wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def gen_python_cffi(funcs, all_funcs, internal_only):
u'int' : u'c_int',
u'size_t*' : u'c_size_t_p',
u'size_t' : u'c_size_t',
u'uint32_t*' : u'c_uint_p',
u'uint32_t*' : u'c_uint32_p',
u'uint32_t' : u'c_uint32',
u'uint64_t*' : u'c_uint64_p',
u'uint64_t' : u'c_uint64',
Expand All @@ -178,6 +178,8 @@ def gen_python_cffi(funcs, all_funcs, internal_only):
}
def map_arg(arg, n, num_args):
argtype = arg.type[6:] if arg.is_const else arg.type # Strip const
if argtype == u'uint32_t*' and n != num_args - 1:
return u'POINTER(c_uint32)'
if argtype == u'uint64_t*' and n != num_args - 1:
return u'POINTER(c_uint64)'
if argtype == u'char**' and n != num_args - 1:
Expand Down

0 comments on commit 31ad972

Please sign in to comment.