1from ctypes import *
2from binascii import hexlify, unhexlify
3from os.path import isfile, abspath
4from os import urandom
5import platform
6import sys
7
8# Allow to run from any sub dir
9SO_EXT = 'dylib' if platform.system() == 'Darwin' else 'so'
10for depth in [0, 1, 2]:
11    root_dir = '../' * depth
12    if isfile(root_dir + 'src/.libs/libwallycore.' + SO_EXT):
13        break
14
15if platform.system() == 'Darwin':
16    root_dir = abspath(root_dir) + '/'
17
18libwally = CDLL(root_dir + 'src/.libs/libwallycore.' + SO_EXT)
19
20wally_free_string = libwally.wally_free_string
21wally_free_string.restype, wally_free_string.argtypes = None, [c_char_p]
22
23WALLY_OK, WALLY_ERROR, WALLY_EINVAL, WALLY_ENOMEM = 0, -1, -2, -3
24
25_malloc_fn_t = CFUNCTYPE(c_void_p, c_ulong)
26_free_fn_t = CFUNCTYPE(c_void_p)
27_bzero_fn_t = CFUNCTYPE(c_void_p, c_ulong)
28_ec_nonce_fn_t = CFUNCTYPE(c_int, c_void_p, c_void_p, c_void_p, c_void_p, c_void_p, c_uint)
29_secp_context_fn_t = CFUNCTYPE(c_void_p)
30
31class wally_operations(Structure):
32    _fields_ = [('struct_size', c_size_t),
33                ('malloc_fn', _malloc_fn_t),
34                ('free_fn', _free_fn_t),
35                ('bzero_fn', _bzero_fn_t),
36                ('ec_nonce_fn', _ec_nonce_fn_t),
37                ('secp_context_fn', _secp_context_fn_t),
38                ('reserved_1', c_void_p),
39                ('reserved_2', c_void_p),
40                ('reserved_3', c_void_p),
41                ('reserved_4', c_void_p)]
42
43class ext_key(Structure):
44    _fields_ = [('chain_code', c_ubyte * 32),
45                ('parent160', c_ubyte * 20),
46                ('depth', c_ubyte),
47                ('pad1', c_ubyte * 10),
48                ('priv_key', c_ubyte * 33),
49                ('child_num', c_uint),
50                ('hash160', c_ubyte * 20),
51                ('version', c_uint),
52                ('pad2', c_ubyte * 3),
53                ('pub_key', c_ubyte * 33),
54                ('pub_key_tweak_sum', c_ubyte * 32)]
55
56# Sentinel classes for returning output parameters
57class c_char_p_p_class(object):
58    pass
59c_char_p_p = c_char_p_p_class()
60class c_ulong_p_class(object):
61    pass
62c_ulong_p = c_ulong_p_class()
63class c_uint64_p_class(object):
64    pass
65c_uint64_p = c_uint64_p_class()
66
67# ctypes is missing this for some reason
68c_uint_p = POINTER(c_uint)
69
70class wally_tx_witness_item(Structure):
71    _fields_ = [('witness', c_void_p),
72                ('len', c_ulong)]
73
74class wally_tx_witness_stack(Structure):
75    _fields_ = [('items', POINTER(wally_tx_witness_item)),
76                ('num_items', c_ulong),
77                ('items_allocation_len', c_ulong)]
78
79class wally_tx_input(Structure):
80    _fields_ = [('txhash', c_ubyte * 32),
81                ('index', c_uint),
82                ('sequence', c_uint),
83                ('script', c_void_p),
84                ('script_len', c_ulong),
85                ('witness',  POINTER(wally_tx_witness_stack)),
86                ('features', c_ubyte),
87                ('blinding_nonce', c_ubyte * 32),
88                ('entropy', c_ubyte * 32),
89                ('issuance_amount', c_void_p),
90                ('issuance_amount_len', c_ulong),
91                ('inflation_keys', c_void_p),
92                ('inflation_keys_len', c_ulong),
93                ('issuance_amount_rangeproof', c_void_p),
94                ('issuance_amount_rangeproof_len', c_ulong),
95                ('inflation_keys_rangeproof', c_void_p),
96                ('inflation_keys_rangeproof_len', c_ulong),
97                ('pegin_witness', POINTER(wally_tx_witness_stack))]
98
99class wally_tx_output(Structure):
100    _fields_ = [('satoshi', c_uint64),
101                ('script', c_void_p),
102                ('script_len', c_ulong),
103                ('features', c_ubyte),
104                ('asset', c_void_p),
105                ('asset_len', c_ulong),
106                ('value', c_void_p),
107                ('value_len', c_ulong),
108                ('nonce', c_void_p),
109                ('nonce_len', c_ulong),
110                ('surjectionproof', c_void_p),
111                ('surjectionproof_len', c_ulong),
112                ('rangeproof', c_void_p),
113                ('rangeproof_len', c_ulong)]
114
115class wally_tx(Structure):
116    _fields_ = [('version', c_uint),
117                ('locktime', c_uint),
118                ('inputs', POINTER(wally_tx_input)),
119                ('num_inputs', c_ulong),
120                ('inputs_allocation_len', c_ulong),
121                ('outputs', POINTER(wally_tx_output)),
122                ('num_outputs', c_ulong),
123                ('outputs_allocation_len', c_ulong),]
124
125class wally_map_item(Structure):
126    _fields_ = [('key', c_void_p),
127                ('key_len', c_ulong),
128                ('value', c_void_p),
129                ('value_len', c_ulong)]
130
131class wally_map(Structure):
132    _fields_ = [('items', POINTER(wally_map_item)),
133                ('num_items', c_ulong),
134                ('items_allocation_len', c_ulong)]
135
136class wally_psbt_input(Structure):
137    _fields_ = [('utxo', POINTER(wally_tx)),
138                ('witness_utxo', POINTER(wally_tx_output)),
139                ('redeem_script', c_void_p),
140                ('redeem_script_len', c_ulong),
141                ('witness_script', c_void_p),
142                ('witness_script_len', c_ulong),
143                ('final_scriptsig', c_void_p),
144                ('final_scriptsig_len', c_ulong),
145                ('final_witness', POINTER(wally_tx_witness_stack)),
146                ('keypaths', wally_map),
147                ('signatures', wally_map),
148                ('unknowns', wally_map),
149                ('sighash', c_ulong),
150                ('value', c_uint64),
151                ('has_value', c_ulong),
152                ('vbf', c_void_p),
153                ('vbf_len', c_ulong),
154                ('asset', c_void_p),
155                ('asset_len', c_ulong),
156                ('abf', c_void_p),
157                ('abf_len', c_ulong),
158                ('pegin_tx', POINTER(wally_tx)),
159                ('txoutproof', c_void_p),
160                ('txoutproof_len', c_ulong),
161                ('genesis_blockhash', c_void_p),
162                ('genesis_blockhash_len', c_ulong),
163                ('claim_script', c_void_p),
164                ('claim_script_len', c_ulong)]
165
166class wally_psbt_output(Structure):
167    _fields_ = [('redeem_script', c_void_p),
168                ('redeem_script_len', c_ulong),
169                ('witness_script', c_void_p),
170                ('witness_script_len', c_ulong),
171                ('keypaths', wally_map),
172                ('unknowns', wally_map),
173                ('blinding_pubkey', c_ubyte * 65),
174                ('has_blinding_pubkey', c_ulong),
175                ('value_commitment', c_void_p),
176                ('value_commitment_len', c_ulong),
177                ('vbf', c_void_p),
178                ('vbf_len', c_ulong),
179                ('asset_commitment', c_void_p),
180                ('asset_commitment_len', c_ulong),
181                ('abf', c_void_p),
182                ('abf_len', c_ulong),
183                ('nonce', c_void_p),
184                ('nonce_len', c_ulong),
185                ('rangeproof', c_void_p),
186                ('rangeproof_len', c_ulong),
187                ('surjectionproof', c_void_p),
188                ('surjectionproof_len', c_ulong)]
189
190class wally_psbt(Structure):
191    _fields_ = [('magic', c_ubyte * 5),
192                ('tx', POINTER(wally_tx)),
193                ('inputs', POINTER(wally_psbt_input)),
194                ('num_inputs', c_ulong),
195                ('inputs_allocation_len', c_ulong),
196                ('outputs', POINTER(wally_psbt_output)),
197                ('num_outputs', c_ulong),
198                ('outputs_allocation_len', c_ulong),
199                ('unknowns', POINTER(wally_map)),
200                ('version', c_ulong)]
201
202for f in (
203    # Internal functions
204    ('mnemonic_from_bytes', c_char_p, [c_void_p, c_void_p, c_ulong]),
205    ('mnemonic_to_bytes', c_int, [c_void_p, c_char_p, c_void_p, c_ulong, c_ulong_p]),
206    ('wordlist_free', None, [c_void_p]),
207    ('wordlist_init', c_void_p, [c_char_p]),
208    ('wordlist_lookup_index', c_char_p, [c_void_p, c_ulong]),
209    ('wordlist_lookup_word', c_ulong, [c_void_p, c_char_p]),
210    # Exported functions
211    ('wally_get_secp_context', c_void_p, []),
212    ('wally_get_new_secp_context', c_void_p, []),
213    ('wally_secp_context_free', None, [c_void_p]),
214    # BEGIN AUTOGENERATED
215    ('bip32_key_free', c_int, [POINTER(ext_key)]),
216    ('bip32_key_from_base58', c_int, [c_char_p, POINTER(ext_key)]),
217    ('bip32_key_from_base58_alloc', c_int, [c_char_p, POINTER(POINTER(ext_key))]),
218    ('bip32_key_from_parent', c_int, [POINTER(ext_key), c_uint, c_uint, POINTER(ext_key)]),
219    ('bip32_key_from_parent_alloc', c_int, [POINTER(ext_key), c_uint, c_uint, POINTER(POINTER(ext_key))]),
220    ('bip32_key_from_parent_path', c_int, [POINTER(ext_key), c_uint_p, c_ulong, c_uint, POINTER(ext_key)]),
221    ('bip32_key_from_parent_path_alloc', c_int, [POINTER(ext_key), c_uint_p, c_ulong, c_uint, POINTER(POINTER(ext_key))]),
222    ('bip32_key_from_seed', c_int, [c_void_p, c_ulong, c_uint, c_uint, POINTER(ext_key)]),
223    ('bip32_key_from_seed_alloc', c_int, [c_void_p, c_ulong, c_uint, c_uint, POINTER(POINTER(ext_key))]),
224    ('bip32_key_get_fingerprint', c_int, [POINTER(ext_key), c_void_p, c_ulong]),
225    ('bip32_key_init', c_int, [c_uint, c_uint, c_uint, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(ext_key)]),
226    ('bip32_key_init_alloc', c_int, [c_uint, c_uint, c_uint, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(POINTER(ext_key))]),
227    ('bip32_key_serialize', c_int, [POINTER(ext_key), c_uint, c_void_p, c_ulong]),
228    ('bip32_key_strip_private_key', c_int, [POINTER(ext_key)]),
229    ('bip32_key_to_base58', c_int, [POINTER(ext_key), c_uint, c_char_p_p]),
230    ('bip32_key_unserialize', c_int, [c_void_p, c_ulong, POINTER(ext_key)]),
231    ('bip32_key_unserialize_alloc', c_int, [c_void_p, c_ulong, POINTER(POINTER(ext_key))]),
232    ('bip32_key_with_tweak_from_parent_path', c_int, [POINTER(ext_key), c_uint_p, c_ulong, c_uint, POINTER(ext_key)]),
233    ('bip32_key_with_tweak_from_parent_path_alloc', c_int, [POINTER(ext_key), c_uint_p, c_ulong, c_uint, POINTER(POINTER(ext_key))]),
234    ('bip38_from_private_key', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_char_p_p]),
235    ('bip38_get_flags', c_int, [c_char_p, c_ulong_p]),
236    ('bip38_raw_from_private_key', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
237    ('bip38_raw_get_flags', c_int, [c_void_p, c_ulong, c_ulong_p]),
238    ('bip38_raw_to_private_key', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
239    ('bip38_to_private_key', c_int, [c_char_p, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
240    ('bip39_get_languages', c_int, [c_char_p_p]),
241    ('bip39_get_word', c_int, [c_void_p, c_ulong, c_char_p_p]),
242    ('bip39_get_wordlist', c_int, [c_char_p, POINTER(c_void_p)]),
243    ('bip39_mnemonic_from_bytes', c_int, [c_void_p, c_void_p, c_ulong, c_char_p_p]),
244    ('bip39_mnemonic_to_bytes', c_int, [c_void_p, c_char_p, c_void_p, c_ulong, c_ulong_p]),
245    ('bip39_mnemonic_to_seed', c_int, [c_char_p, c_char_p, c_void_p, c_ulong, c_ulong_p]),
246    ('bip39_mnemonic_validate', c_int, [c_void_p, c_char_p]),
247    ('wally_addr_segwit_from_bytes', c_int, [c_void_p, c_ulong, c_char_p, c_uint, c_char_p_p]),
248    ('wally_addr_segwit_to_bytes', c_int, [c_char_p, c_char_p, c_uint, c_void_p, c_ulong, c_ulong_p]),
249    ('wally_address_to_scriptpubkey', c_int, [c_char_p, c_uint, c_void_p, c_ulong, c_ulong_p]),
250    ('wally_ae_host_commit_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
251    ('wally_ae_sig_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
252    ('wally_ae_signer_commit_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
253    ('wally_ae_verify', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
254    ('wally_aes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
255    ('wally_aes_cbc', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
256    ('wally_asset_blinding_key_from_seed', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
257    ('wally_asset_blinding_key_to_ec_private_key', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
258    ('wally_asset_final_vbf', c_int, [POINTER(c_uint64), c_ulong, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
259    ('wally_asset_generator_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
260    ('wally_asset_pak_whitelistproof', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_ulong_p]),
261    ('wally_asset_pak_whitelistproof_size', c_int, [c_ulong, c_ulong_p]),
262    ('wally_asset_rangeproof', c_int, [c_uint64, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint64, c_int, c_int, c_void_p, c_ulong, c_ulong_p]),
263    ('wally_asset_surjectionproof', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_ulong_p]),
264    ('wally_asset_surjectionproof_size', c_int, [c_ulong, c_ulong_p]),
265    ('wally_asset_unblind', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint64_p]),
266    ('wally_asset_unblind_with_nonce', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint64_p]),
267    ('wally_asset_value_commitment', c_int, [c_uint64, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
268    ('wally_base58_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_char_p_p]),
269    ('wally_base58_get_length', c_int, [c_char_p, c_ulong_p]),
270    ('wally_base58_to_bytes', c_int, [c_char_p, c_uint, c_void_p, c_ulong, c_ulong_p]),
271    ('wally_base64_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_char_p_p]),
272    ('wally_base64_get_maximum_length', c_int, [c_char_p, c_uint, c_ulong_p]),
273    ('wally_base64_to_bytes', c_int, [c_char_p, c_uint, c_void_p, c_ulong, c_ulong_p]),
274    ('wally_bip32_key_to_addr_segwit', c_int, [POINTER(ext_key), c_char_p, c_uint, c_char_p_p]),
275    ('wally_bip32_key_to_address', c_int, [POINTER(ext_key), c_uint, c_uint, c_char_p_p]),
276    ('wally_bzero', c_int, [c_void_p, c_ulong]),
277    ('wally_cleanup', c_int, [c_uint]),
278    ('wally_confidential_addr_from_addr', c_int, [c_char_p, c_uint, c_void_p, c_ulong, c_char_p_p]),
279    ('wally_confidential_addr_from_addr_segwit', c_int, [c_char_p, c_char_p, c_char_p, c_void_p, c_ulong, c_char_p_p]),
280    ('wally_confidential_addr_segwit_to_ec_public_key', c_int, [c_char_p, c_char_p, c_void_p, c_ulong]),
281    ('wally_confidential_addr_to_addr', c_int, [c_char_p, c_uint, c_char_p_p]),
282    ('wally_confidential_addr_to_addr_segwit', c_int, [c_char_p, c_char_p, c_char_p, c_char_p_p]),
283    ('wally_confidential_addr_to_ec_public_key', c_int, [c_char_p, c_uint, c_void_p, c_ulong]),
284    ('wally_ec_private_key_verify', c_int, [c_void_p, c_ulong]),
285    ('wally_ec_public_key_decompress', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
286    ('wally_ec_public_key_from_private_key', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
287    ('wally_ec_public_key_negate', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
288    ('wally_ec_public_key_verify', c_int, [c_void_p, c_ulong]),
289    ('wally_ec_sig_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
290    ('wally_ec_sig_from_der', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
291    ('wally_ec_sig_normalize', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
292    ('wally_ec_sig_to_der', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_ulong_p]),
293    ('wally_ec_sig_to_public_key', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
294    ('wally_ec_sig_verify', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
295    ('wally_ecdh', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
296    ('wally_elements_pegin_contract_script_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
297    ('wally_elements_pegout_script_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
298    ('wally_elements_pegout_script_size', c_int, [c_ulong, c_ulong, c_ulong, c_ulong, c_ulong_p]),
299    ('wally_format_bitcoin_message', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
300    ('wally_free_string', c_int, [c_char_p]),
301    ('wally_get_operations', c_int, [POINTER(wally_operations)]),
302    ('wally_hash160', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
303    ('wally_hex_from_bytes', c_int, [c_void_p, c_ulong, c_char_p_p]),
304    ('wally_hex_to_bytes', c_int, [c_char_p, c_void_p, c_ulong, c_ulong_p]),
305    ('wally_hmac_sha256', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
306    ('wally_hmac_sha512', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
307    ('wally_init', c_int, [c_uint]),
308    ('wally_is_elements_build', c_int, [c_ulong_p]),
309    ('wally_map_add', c_int, [POINTER(wally_map), c_void_p, c_ulong, c_void_p, c_ulong]),
310    ('wally_map_add_keypath_item', c_int, [POINTER(wally_map), c_void_p, c_ulong, c_void_p, c_ulong, c_uint_p, c_ulong]),
311    ('wally_map_find', c_int, [POINTER(wally_map), c_void_p, c_ulong, c_ulong_p]),
312    ('wally_map_free', c_int, [POINTER(wally_map)]),
313    ('wally_map_init_alloc', c_int, [c_ulong, POINTER(POINTER(wally_map))]),
314    ('wally_map_sort', c_int, [POINTER(wally_map), c_uint]),
315    ('wally_pbkdf2_hmac_sha256', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong]),
316    ('wally_pbkdf2_hmac_sha512', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong]),
317    ('wally_psbt_add_input_at', c_int, [POINTER(wally_psbt), c_uint, c_uint, POINTER(wally_tx_input)]),
318    ('wally_psbt_add_output_at', c_int, [POINTER(wally_psbt), c_uint, c_uint, POINTER(wally_tx_output)]),
319    ('wally_psbt_clone_alloc', c_int, [POINTER(wally_psbt), c_uint, POINTER(POINTER(wally_psbt))]),
320    ('wally_psbt_combine', c_int, [POINTER(wally_psbt), POINTER(wally_psbt)]),
321    ('wally_psbt_elements_init_alloc', c_int, [c_uint, c_ulong, c_ulong, c_ulong, POINTER(POINTER(wally_psbt))]),
322    ('wally_psbt_extract', c_int, [POINTER(wally_psbt), POINTER(POINTER(wally_tx))]),
323    ('wally_psbt_finalize', c_int, [POINTER(wally_psbt)]),
324    ('wally_psbt_free', c_int, [POINTER(wally_psbt)]),
325    ('wally_psbt_from_base64', c_int, [c_char_p, POINTER(POINTER(wally_psbt))]),
326    ('wally_psbt_from_bytes', c_int, [c_void_p, c_ulong, POINTER(POINTER(wally_psbt))]),
327    ('wally_psbt_get_length', c_int, [POINTER(wally_psbt), c_uint, c_ulong_p]),
328    ('wally_psbt_init_alloc', c_int, [c_uint, c_ulong, c_ulong, c_ulong, POINTER(POINTER(wally_psbt))]),
329    ('wally_psbt_input_add_keypath_item', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong, c_void_p, c_ulong, c_uint_p, c_ulong]),
330    ('wally_psbt_input_add_signature', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong, c_void_p, c_ulong]),
331    ('wally_psbt_input_clear_value', c_int, [POINTER(wally_psbt_input)]),
332    ('wally_psbt_input_find_keypath', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong, c_ulong_p]),
333    ('wally_psbt_input_find_signature', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong, c_ulong_p]),
334    ('wally_psbt_input_find_unknown', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong, c_ulong_p]),
335    ('wally_psbt_input_is_finalized', c_int, [POINTER(wally_psbt_input), c_ulong_p]),
336    ('wally_psbt_input_set_abf', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
337    ('wally_psbt_input_set_asset', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
338    ('wally_psbt_input_set_claim_script', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
339    ('wally_psbt_input_set_final_scriptsig', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
340    ('wally_psbt_input_set_final_witness', c_int, [POINTER(wally_psbt_input), POINTER(wally_tx_witness_stack)]),
341    ('wally_psbt_input_set_genesis_blockhash', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
342    ('wally_psbt_input_set_keypaths', c_int, [POINTER(wally_psbt_input), POINTER(wally_map)]),
343    ('wally_psbt_input_set_pegin_tx', c_int, [POINTER(wally_psbt_input), POINTER(wally_tx)]),
344    ('wally_psbt_input_set_redeem_script', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
345    ('wally_psbt_input_set_sighash', c_int, [POINTER(wally_psbt_input), c_uint]),
346    ('wally_psbt_input_set_signatures', c_int, [POINTER(wally_psbt_input), POINTER(wally_map)]),
347    ('wally_psbt_input_set_txoutproof', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
348    ('wally_psbt_input_set_unknowns', c_int, [POINTER(wally_psbt_input), POINTER(wally_map)]),
349    ('wally_psbt_input_set_utxo', c_int, [POINTER(wally_psbt_input), POINTER(wally_tx)]),
350    ('wally_psbt_input_set_value', c_int, [POINTER(wally_psbt_input), c_uint64]),
351    ('wally_psbt_input_set_vbf', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
352    ('wally_psbt_input_set_witness_script', c_int, [POINTER(wally_psbt_input), c_void_p, c_ulong]),
353    ('wally_psbt_input_set_witness_utxo', c_int, [POINTER(wally_psbt_input), POINTER(wally_tx_output)]),
354    ('wally_psbt_is_elements', c_int, [POINTER(wally_psbt), c_ulong_p]),
355    ('wally_psbt_is_finalized', c_int, [POINTER(wally_psbt), c_ulong_p]),
356    ('wally_psbt_output_add_keypath_item', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong, c_void_p, c_ulong, c_uint_p, c_ulong]),
357    ('wally_psbt_output_find_keypath', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong, c_ulong_p]),
358    ('wally_psbt_output_find_unknown', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong, c_ulong_p]),
359    ('wally_psbt_output_set_abf', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
360    ('wally_psbt_output_set_asset_commitment', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
361    ('wally_psbt_output_set_blinding_pubkey', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
362    ('wally_psbt_output_set_keypaths', c_int, [POINTER(wally_psbt_output), POINTER(wally_map)]),
363    ('wally_psbt_output_set_nonce', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
364    ('wally_psbt_output_set_rangeproof', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
365    ('wally_psbt_output_set_redeem_script', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
366    ('wally_psbt_output_set_surjectionproof', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
367    ('wally_psbt_output_set_unknowns', c_int, [POINTER(wally_psbt_output), POINTER(wally_map)]),
368    ('wally_psbt_output_set_value_commitment', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
369    ('wally_psbt_output_set_vbf', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
370    ('wally_psbt_output_set_witness_script', c_int, [POINTER(wally_psbt_output), c_void_p, c_ulong]),
371    ('wally_psbt_remove_input', c_int, [POINTER(wally_psbt), c_uint]),
372    ('wally_psbt_remove_output', c_int, [POINTER(wally_psbt), c_uint]),
373    ('wally_psbt_set_global_tx', c_int, [POINTER(wally_psbt), POINTER(wally_tx)]),
374    ('wally_psbt_sign', c_int, [POINTER(wally_psbt), c_void_p, c_ulong, c_uint]),
375    ('wally_psbt_to_base64', c_int, [POINTER(wally_psbt), c_uint, c_char_p_p]),
376    ('wally_psbt_to_bytes', c_int, [POINTER(wally_psbt), c_uint, c_void_p, c_ulong, c_ulong_p]),
377    ('wally_s2c_commitment_verify', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint]),
378    ('wally_s2c_sig_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_void_p, c_ulong]),
379    ('wally_script_push_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
380    ('wally_scriptpubkey_csv_2of2_then_1_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, c_ulong_p]),
381    ('wally_scriptpubkey_csv_2of2_then_1_from_bytes_opt', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, c_ulong_p]),
382    ('wally_scriptpubkey_csv_2of3_then_2_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, c_ulong_p]),
383    ('wally_scriptpubkey_get_type', c_int, [c_void_p, c_ulong, c_ulong_p]),
384    ('wally_scriptpubkey_multisig_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, c_ulong_p]),
385    ('wally_scriptpubkey_op_return_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
386    ('wally_scriptpubkey_p2pkh_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
387    ('wally_scriptpubkey_p2sh_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
388    ('wally_scriptpubkey_to_address', c_int, [c_void_p, c_ulong, c_uint, c_char_p_p]),
389    ('wally_scriptsig_multisig_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
390    ('wally_scriptsig_p2pkh_from_der', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_ulong_p]),
391    ('wally_scriptsig_p2pkh_from_sig', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
392    ('wally_scrypt', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_uint, c_uint, c_void_p, c_ulong]),
393    ('wally_secp_randomize', c_int, [c_void_p, c_ulong]),
394    ('wally_set_operations', c_int, [POINTER(wally_operations)]),
395    ('wally_sha256', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
396    ('wally_sha256_midstate', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
397    ('wally_sha256d', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
398    ('wally_sha512', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
399    ('wally_symmetric_key_from_parent', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_void_p, c_ulong]),
400    ('wally_symmetric_key_from_seed', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
401    ('wally_tx_add_elements_raw_input', c_int, [POINTER(wally_tx), c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_uint]),
402    ('wally_tx_add_elements_raw_input_at', c_int, [POINTER(wally_tx), c_uint, c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_uint]),
403    ('wally_tx_add_elements_raw_output', c_int, [POINTER(wally_tx), c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint]),
404    ('wally_tx_add_elements_raw_output_at', c_int, [POINTER(wally_tx), c_uint, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint]),
405    ('wally_tx_add_input', c_int, [POINTER(wally_tx), POINTER(wally_tx_input)]),
406    ('wally_tx_add_input_at', c_int, [POINTER(wally_tx), c_uint, POINTER(wally_tx_input)]),
407    ('wally_tx_add_output', c_int, [POINTER(wally_tx), POINTER(wally_tx_output)]),
408    ('wally_tx_add_output_at', c_int, [POINTER(wally_tx), c_uint, POINTER(wally_tx_output)]),
409    ('wally_tx_add_raw_input', c_int, [POINTER(wally_tx), c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_uint]),
410    ('wally_tx_add_raw_input_at', c_int, [POINTER(wally_tx), c_uint, c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_uint]),
411    ('wally_tx_add_raw_output', c_int, [POINTER(wally_tx), c_uint64, c_void_p, c_ulong, c_uint]),
412    ('wally_tx_add_raw_output_at', c_int, [POINTER(wally_tx), c_uint, c_uint64, c_void_p, c_ulong, c_uint]),
413    ('wally_tx_clone_alloc', c_int, [POINTER(wally_tx), c_uint, POINTER(POINTER(wally_tx))]),
414    ('wally_tx_confidential_value_from_satoshi', c_int, [c_uint64, c_void_p, c_ulong]),
415    ('wally_tx_confidential_value_to_satoshi', c_int, [c_void_p, c_ulong, c_uint64_p]),
416    ('wally_tx_elements_input_init_alloc', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), POINTER(POINTER(wally_tx_input))]),
417    ('wally_tx_elements_input_is_pegin', c_int, [POINTER(wally_tx_input), c_ulong_p]),
418    ('wally_tx_elements_input_issuance_free', c_int, [POINTER(wally_tx_input)]),
419    ('wally_tx_elements_input_issuance_set', c_int, [POINTER(wally_tx_input), c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
420    ('wally_tx_elements_issuance_calculate_asset', c_int, [c_void_p, c_ulong, c_void_p, c_ulong]),
421    ('wally_tx_elements_issuance_calculate_reissuance_token', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong]),
422    ('wally_tx_elements_issuance_generate_entropy', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_void_p, c_ulong]),
423    ('wally_tx_elements_output_commitment_free', c_int, [POINTER(wally_tx_output)]),
424    ('wally_tx_elements_output_commitment_set', c_int, [POINTER(wally_tx_output), c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong]),
425    ('wally_tx_elements_output_init', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(wally_tx_output)]),
426    ('wally_tx_elements_output_init_alloc', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, POINTER(POINTER(wally_tx_output))]),
427    ('wally_tx_free', c_int, [POINTER(wally_tx)]),
428    ('wally_tx_from_bytes', c_int, [c_void_p, c_ulong, c_uint, POINTER(POINTER(wally_tx))]),
429    ('wally_tx_from_hex', c_int, [c_char_p, c_uint, POINTER(POINTER(wally_tx))]),
430    ('wally_tx_get_btc_signature_hash', c_int, [POINTER(wally_tx), c_ulong, c_void_p, c_ulong, c_uint64, c_uint, c_uint, c_void_p, c_ulong]),
431    ('wally_tx_get_elements_signature_hash', c_int, [POINTER(wally_tx), c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong]),
432    ('wally_tx_get_length', c_int, [POINTER(wally_tx), c_uint, c_ulong_p]),
433    ('wally_tx_get_signature_hash', c_int, [POINTER(wally_tx), c_ulong, c_void_p, c_ulong, c_void_p, c_ulong, c_uint, c_uint64, c_uint, c_uint, c_uint, c_void_p, c_ulong]),
434    ('wally_tx_get_total_output_satoshi', c_int, [POINTER(wally_tx), c_uint64_p]),
435    ('wally_tx_get_txid', c_int, [POINTER(wally_tx), c_void_p, c_ulong]),
436    ('wally_tx_get_vsize', c_int, [POINTER(wally_tx), c_ulong_p]),
437    ('wally_tx_get_weight', c_int, [POINTER(wally_tx), c_ulong_p]),
438    ('wally_tx_get_witness_count', c_int, [POINTER(wally_tx), c_ulong_p]),
439    ('wally_tx_init_alloc', c_int, [c_uint, c_uint, c_ulong, c_ulong, POINTER(POINTER(wally_tx))]),
440    ('wally_tx_input_free', c_int, [POINTER(wally_tx_input)]),
441    ('wally_tx_input_init_alloc', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_void_p, c_ulong, POINTER(wally_tx_witness_stack), POINTER(POINTER(wally_tx_input))]),
442    ('wally_tx_is_coinbase', c_int, [POINTER(wally_tx), c_ulong_p]),
443    ('wally_tx_is_elements', c_int, [POINTER(wally_tx), c_ulong_p]),
444    ('wally_tx_output_clone', c_int, [POINTER(wally_tx_output), POINTER(wally_tx_output)]),
445    ('wally_tx_output_clone_alloc', c_int, [POINTER(wally_tx_output), POINTER(POINTER(wally_tx_output))]),
446    ('wally_tx_output_free', c_int, [POINTER(wally_tx_output)]),
447    ('wally_tx_output_init', c_int, [c_uint64, c_void_p, c_ulong, POINTER(wally_tx_output)]),
448    ('wally_tx_output_init_alloc', c_int, [c_uint64, c_void_p, c_ulong, POINTER(POINTER(wally_tx_output))]),
449    ('wally_tx_remove_input', c_int, [POINTER(wally_tx), c_ulong]),
450    ('wally_tx_remove_output', c_int, [POINTER(wally_tx), c_ulong]),
451    ('wally_tx_set_input_script', c_int, [POINTER(wally_tx), c_ulong, c_void_p, c_ulong]),
452    ('wally_tx_set_input_witness', c_int, [POINTER(wally_tx), c_ulong, POINTER(wally_tx_witness_stack)]),
453    ('wally_tx_to_bytes', c_int, [POINTER(wally_tx), c_uint, c_void_p, c_ulong, c_ulong_p]),
454    ('wally_tx_to_hex', c_int, [POINTER(wally_tx), c_uint, c_char_p_p]),
455    ('wally_tx_vsize_from_weight', c_int, [c_ulong, c_ulong_p]),
456    ('wally_tx_witness_stack_add', c_int, [POINTER(wally_tx_witness_stack), c_void_p, c_ulong]),
457    ('wally_tx_witness_stack_add_dummy', c_int, [POINTER(wally_tx_witness_stack), c_uint]),
458    ('wally_tx_witness_stack_clone_alloc', c_int, [POINTER(wally_tx_witness_stack), POINTER(POINTER(wally_tx_witness_stack))]),
459    ('wally_tx_witness_stack_free', c_int, [POINTER(wally_tx_witness_stack)]),
460    ('wally_tx_witness_stack_init_alloc', c_int, [c_ulong, POINTER(POINTER(wally_tx_witness_stack))]),
461    ('wally_tx_witness_stack_set', c_int, [POINTER(wally_tx_witness_stack), c_ulong, c_void_p, c_ulong]),
462    ('wally_tx_witness_stack_set_dummy', c_int, [POINTER(wally_tx_witness_stack), c_ulong, c_uint]),
463    ('wally_varbuff_get_length', c_int, [c_void_p, c_ulong, c_ulong_p]),
464    ('wally_varbuff_to_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_ulong_p]),
465    ('wally_varint_get_length', c_int, [c_uint64, c_ulong_p]),
466    ('wally_varint_to_bytes', c_int, [c_uint64, c_void_p, c_ulong, c_ulong_p]),
467    ('wally_wif_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_uint, c_char_p_p]),
468    ('wally_wif_is_uncompressed', c_int, [c_char_p, c_ulong_p]),
469    ('wally_wif_to_address', c_int, [c_char_p, c_uint, c_uint, c_char_p_p]),
470    ('wally_wif_to_bytes', c_int, [c_char_p, c_uint, c_uint, c_void_p, c_ulong]),
471    ('wally_wif_to_public_key', c_int, [c_char_p, c_uint, c_void_p, c_ulong, c_ulong_p]),
472    ('wally_witness_multisig_from_bytes', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint_p, c_ulong, c_uint, POINTER(POINTER(wally_tx_witness_stack))]),
473    ('wally_witness_p2wpkh_from_der', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, POINTER(POINTER(wally_tx_witness_stack))]),
474    ('wally_witness_p2wpkh_from_sig', c_int, [c_void_p, c_ulong, c_void_p, c_ulong, c_uint, POINTER(POINTER(wally_tx_witness_stack))]),
475    ('wally_witness_program_from_bytes', c_int, [c_void_p, c_ulong, c_uint, c_void_p, c_ulong, c_ulong_p]),
476    # END AUTOGENERATED
477    ):
478
479    def bind_fn(name, res, args):
480        try:
481            fn = getattr(libwally, name)
482            fn.restype, fn.argtypes = res, args
483            return fn
484        except AttributeError:
485            # Internal function and 'configure --enable-export-all' not used,
486            # or an Elements function and Elements support not enabled.
487            return None
488
489    def in_string_fn_wrapper(fn, pos, *args):
490        if isinstance(args[pos], str):
491            new_args = [a for a in args]
492            new_args[pos] = utf8(new_args[pos])
493            return fn(*new_args)
494        return fn(*args)
495
496    def string_fn_wrapper(fn, *args):
497        # Return output string parameters directly without leaking
498        p = c_char_p()
499        new_args = [a for a in args] + [byref(p)]
500        ret = fn(*new_args)
501        ret_str = None if p.value is None else p.value.decode('utf-8')
502        wally_free_string(p)
503        return [ret_str, (ret, ret_str)][fn.restype is not None]
504
505    def int_fn_wrapper(fn, *args):
506        p = c_ulong()
507        new_args = [a for a in args] + [byref(p)]
508        ret = fn(*new_args)
509        return [p.value, (ret, p.value)][fn.restype is not None]
510
511    def int64_fn_wrapper(fn, *args):
512        p = c_uint64()
513        new_args = [a for a in args] + [byref(p)]
514        ret = fn(*new_args)
515        return [p.value, (ret, p.value)][fn.restype is not None]
516
517    name, restype, argtypes = f
518    is_str_fn = len(argtypes) and type(argtypes[-1]) is c_char_p_p_class
519    is_int_fn = len(argtypes) and type(argtypes[-1]) is c_ulong_p_class
520    is_int64_fn = len(argtypes) and type(argtypes[-1]) is c_uint64_p_class
521    in_str_pos = [i for (i, t) in enumerate(argtypes) if t == c_char_p]
522    if is_str_fn:
523        argtypes[-1] = POINTER(c_char_p)
524    elif is_int_fn:
525        argtypes[-1] = POINTER(c_ulong)
526    elif is_int64_fn:
527        argtypes[-1] = POINTER(c_uint64)
528    fn = bind_fn(name, restype, argtypes)
529    def mkstr(f): return lambda *args: string_fn_wrapper(f, *args)
530    def mkint(f): return lambda *args: int_fn_wrapper(f, *args)
531    def mkint64(f): return lambda *args: int64_fn_wrapper(f, *args)
532    def mkinstr(f, pos): return lambda *args: in_string_fn_wrapper(f, pos, *args)
533    if is_str_fn:
534        fn = mkstr(fn)
535    elif is_int_fn:
536        fn = mkint(fn)
537    elif is_int64_fn:
538        fn = mkint64(fn)
539    if len(in_str_pos) > 0 and fn:
540        for pos in in_str_pos:
541            fn = mkinstr(fn, pos)
542    globals()[name] = fn
543
544is_python3 = int(sys.version[0]) >= 3
545def load_words(lang):
546    kwargs = {'name': root_dir + 'src/data/wordlists/%s.txt' % lang, 'mode': 'r'}
547    if is_python3:
548        kwargs.update({'encoding': 'utf-8'})
549        kwargs['file'] = kwargs.pop('name')
550    with open(**kwargs) as f:
551        words_list = [l.strip() for l in f.readlines()]
552        return words_list, ' '.join(words_list)
553
554def h(s):
555    return hexlify(s)
556
557def make_cbuffer(hex_in):
558    if hex_in is None:
559        return None, 0
560    hex_len = len(hex_in) // 2
561    return unhexlify(hex_in), hex_len
562
563utf8 = lambda s: s
564if is_python3:
565    utf8 = lambda s: s.encode('utf-8')
566
567assert wally_secp_randomize(urandom(32), 32) == WALLY_OK, 'Random init failed'
568
569_original_ops = wally_operations()
570_new_ops = wally_operations()
571for ops in (_original_ops, _new_ops):
572    ops.struct_size = sizeof(wally_operations)
573    assert wally_get_operations(byref(ops)) == WALLY_OK
574
575# Disable internal tests if not available
576def internal_only():
577    def decorator(test_func):
578        def wrapped(*args):
579            if wordlist_init is None:
580                print (test_func.__name__ + ' disabled, use --enable-export-all to enable ')
581            else:
582                return test_func(*args)
583        return wrapped
584    return decorator
585
586# Support for malloc testing
587_fail_malloc_at = 0
588_fail_malloc_counter = 0
589
590def _failable_malloc(size):
591    global _fail_malloc_counter
592    _fail_malloc_counter += 1
593    if _fail_malloc_counter == _fail_malloc_at:
594        return None
595    return _original_ops.malloc_fn(size)
596
597_new_ops.malloc_fn = _malloc_fn_t(_failable_malloc)
598
599def malloc_fail(failures):
600    def decorator(test_func):
601        def wrapped(*args):
602            global _fail_malloc_at, _fail_malloc_counter
603            for fail_at in failures:
604                _fail_malloc_at, _fail_malloc_counter = fail_at, 0
605                test_func(*args)
606                _fail_malloc_at, _fail_malloc_counter = 0, 0
607        return wrapped
608    return decorator
609
610# Support for signing testing
611_fake_ec_nonce = None
612
613def set_fake_ec_nonce(nonce):
614    global _fake_ec_nonce
615    _fake_ec_nonce = nonce
616
617def _fake_ec_nonce_fn(nonce32, msg32, key32, algo16, data, attempt):
618    global _fake_ec_nonce
619    if _fake_ec_nonce is not None:
620        memmove(nonce32, _fake_ec_nonce, 32)
621        return 1
622    return _original_ops.ec_nonce_fn(nonce32, msg32, key32, algo16, data, attempt)
623
624_new_ops.ec_nonce_fn = _ec_nonce_fn_t(_fake_ec_nonce_fn)
625
626assert wally_set_operations(byref(_new_ops)) == WALLY_OK
627