1 #ifndef LIBWALLY_CORE_SCRIPT_H
2 #define LIBWALLY_CORE_SCRIPT_H
3 
4 #include "wally_core.h"
5 #include "wally_transaction.h"
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 /* Script types */
12 #define WALLY_SCRIPT_TYPE_UNKNOWN   0x0
13 #define WALLY_SCRIPT_TYPE_OP_RETURN 0x1
14 #define WALLY_SCRIPT_TYPE_P2PKH     0x2
15 #define WALLY_SCRIPT_TYPE_P2SH      0x4
16 #define WALLY_SCRIPT_TYPE_P2WPKH    0x8
17 #define WALLY_SCRIPT_TYPE_P2WSH     0x10
18 #define WALLY_SCRIPT_TYPE_MULTISIG  0x20
19 
20 /* Standard script lengths */
21 #define WALLY_SCRIPTPUBKEY_P2PKH_LEN  25 /** OP_DUP OP_HASH160 [HASH160] OP_EQUALVERIFY OP_CHECKSIG */
22 #define WALLY_SCRIPTPUBKEY_P2SH_LEN   23 /** OP_HASH160 [HASH160] OP_EQUAL */
23 #define WALLY_SCRIPTPUBKEY_P2WPKH_LEN 22 /** OP_0 [HASH160] */
24 #define WALLY_SCRIPTPUBKEY_P2WSH_LEN  34 /** OP_0 [SHA256] */
25 
26 #define WALLY_SCRIPTPUBKEY_OP_RETURN_MAX_LEN 83 /** OP_RETURN [80 bytes of data] */
27 
28 #define WALLY_MAX_OP_RETURN_LEN 80 /* Maximum length of OP_RETURN data push */
29 
30 #define WALLY_SCRIPTSIG_P2PKH_MAX_LEN 140 /** [SIG+SIGHASH] [PUBKEY] */
31 #define WALLY_WITNESSSCRIPT_MAX_LEN   35 /** (PUSH OF)0 [SHA256] */
32 
33 #define WALLY_SCRIPT_VARINT_MAX_SIZE 9
34 
35 /* Script creation flags */
36 #define WALLY_SCRIPT_HASH160          0x1 /** hash160 input bytes before using them */
37 #define WALLY_SCRIPT_SHA256           0x2 /** sha256 input bytes before using them */
38 #define WALLY_SCRIPT_AS_PUSH          0x4 /** Return a push of the generated script */
39 #define WALLY_SCRIPT_MULTISIG_SORTED  0x8 /** Sort public keys (BIP67) */
40 
41 /* Script opcodes */
42 #ifndef WALLY_DISABLE_OP_CODE
43 #define OP_0 0x00
44 #define OP_FALSE 0x00
45 #define OP_PUSHDATA1 0x4c
46 #define OP_PUSHDATA2 0x4d
47 #define OP_PUSHDATA4 0x4e
48 #define OP_1NEGATE 0x4f
49 #define OP_RESERVED 0x50
50 #define OP_1 0x51
51 #define OP_TRUE 0x51
52 #define OP_2 0x52
53 #define OP_3 0x53
54 #define OP_4 0x54
55 #define OP_5 0x55
56 #define OP_6 0x56
57 #define OP_7 0x57
58 #define OP_8 0x58
59 #define OP_9 0x59
60 #define OP_10 0x5a
61 #define OP_11 0x5b
62 #define OP_12 0x5c
63 #define OP_13 0x5d
64 #define OP_14 0x5e
65 #define OP_15 0x5f
66 #define OP_16 0x60
67 
68 #define OP_NOP 0x61
69 #define OP_VER 0x62
70 #define OP_IF 0x63
71 #define OP_NOTIF 0x64
72 #define OP_VERIF 0x65
73 #define OP_VERNOTIF 0x66
74 #define OP_ELSE 0x67
75 #define OP_ENDIF 0x68
76 #define OP_VERIFY 0x69
77 #define OP_RETURN 0x6a
78 
79 #define OP_TOALTSTACK 0x6b
80 #define OP_FROMALTSTACK 0x6c
81 #define OP_2DROP 0x6d
82 #define OP_2DUP 0x6e
83 #define OP_3DUP 0x6f
84 #define OP_2OVER 0x70
85 #define OP_2ROT 0x71
86 #define OP_2SWAP 0x72
87 #define OP_IFDUP 0x73
88 #define OP_DEPTH 0x74
89 #define OP_DROP 0x75
90 #define OP_DUP 0x76
91 #define OP_NIP 0x77
92 #define OP_OVER 0x78
93 #define OP_PICK 0x79
94 #define OP_ROLL 0x7a
95 #define OP_ROT 0x7b
96 #define OP_SWAP 0x7c
97 #define OP_TUCK 0x7d
98 
99 #define OP_CAT 0x7e
100 #define OP_SUBSTR 0x7f
101 #define OP_LEFT 0x80
102 #define OP_RIGHT 0x81
103 #define OP_SIZE 0x82
104 
105 #define OP_INVERT 0x83
106 #define OP_AND 0x84
107 #define OP_OR 0x85
108 #define OP_XOR 0x86
109 #define OP_EQUAL 0x87
110 #define OP_EQUALVERIFY 0x88
111 #define OP_RESERVED1 0x89
112 #define OP_RESERVED2 0x8a
113 
114 #define OP_1ADD 0x8b
115 #define OP_1SUB 0x8c
116 #define OP_2MUL 0x8d
117 #define OP_2DIV 0x8e
118 #define OP_NEGATE 0x8f
119 #define OP_ABS 0x90
120 #define OP_NOT 0x91
121 #define OP_0NOTEQUAL 0x92
122 
123 #define OP_ADD 0x93
124 #define OP_SUB 0x94
125 #define OP_MUL 0x95
126 #define OP_DIV 0x96
127 #define OP_MOD 0x97
128 #define OP_LSHIFT 0x98
129 #define OP_RSHIFT 0x99
130 
131 #define OP_BOOLAND 0x9a
132 #define OP_BOOLOR 0x9b
133 #define OP_NUMEQUAL 0x9c
134 #define OP_NUMEQUALVERIFY 0x9d
135 #define OP_NUMNOTEQUAL 0x9e
136 #define OP_LESSTHAN 0x9f
137 #define OP_GREATERTHAN 0xa0
138 #define OP_LESSTHANOREQUAL 0xa1
139 #define OP_GREATERTHANOREQUAL 0xa2
140 #define OP_MIN 0xa3
141 #define OP_MAX 0xa4
142 
143 #define OP_WITHIN 0xa5
144 
145 #define OP_RIPEMD160 0xa6
146 #define OP_SHA1 0xa7
147 #define OP_SHA256 0xa8
148 #define OP_HASH160 0xa9
149 #define OP_HASH256 0xaa
150 #define OP_CODESEPARATOR 0xab
151 #define OP_CHECKSIG 0xac
152 #define OP_CHECKSIGVERIFY 0xad
153 #define OP_CHECKMULTISIG 0xae
154 #define OP_CHECKMULTISIGVERIFY 0xaf
155 
156 #define OP_NOP1 0xb0
157 #define OP_CHECKLOCKTIMEVERIFY 0xb1
158 #define OP_NOP2 0xb1
159 #define OP_CHECKSEQUENCEVERIFY 0xb2
160 #define OP_NOP3 0xb2
161 #define OP_NOP4 0xb3
162 #define OP_NOP5 0xb4
163 #define OP_NOP6 0xb5
164 #define OP_NOP7 0xb6
165 #define OP_NOP8 0xb7
166 #define OP_NOP9 0xb8
167 #define OP_NOP10 0xb9
168 
169 #define OP_INVALIDOPCODE 0xff
170 #endif  /* WALLY_DISABLE_OP_CODE */
171 
172 /**
173  * Determine the type of a scriptPubkey script.
174  *
175  * :param bytes: Bytes of the scriptPubkey.
176  * :param bytes_len: Length of ``bytes`` in bytes.
177  * :param written: Destination for the ``WALLY_SCRIPT_TYPE_`` script type.
178  */
179 WALLY_CORE_API int wally_scriptpubkey_get_type(const unsigned char *bytes, size_t bytes_len,
180                                                size_t *written);
181 
182 /**
183  * Create a P2PKH scriptPubkey.
184  *
185  * :param bytes: Bytes to create a scriptPubkey for.
186  * :param bytes_len: The length of ``bytes`` in bytes. If
187  *|    ``WALLY_SCRIPT_HASH160`` is given in ``flags``, ``bytes`` is a public
188  *|    key to hash160 before creating the P2PKH, and ``bytes_len`` must be
189  *|    ``EC_PUBLIC_KEY_LEN`` or ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN``. Otherwise,
190  *|    ``bytes_len`` must be ``HASH160_LEN`` and ``bytes`` must contain the
191  *|    hash160 to use.
192  * :param flags: ``WALLY_SCRIPT_HASH160`` or 0.
193  * :param bytes_out: Destination for the resulting scriptPubkey.
194  * :param len: Length of ``bytes_out`` in bytes.
195  * :param written: Destination for the number of bytes written to ``bytes_out``.
196  */
197 WALLY_CORE_API int wally_scriptpubkey_p2pkh_from_bytes(
198     const unsigned char *bytes,
199     size_t bytes_len,
200     uint32_t flags,
201     unsigned char *bytes_out,
202     size_t len,
203     size_t *written);
204 
205 /**
206  * Create a P2PKH scriptSig from a pubkey and compact signature.
207  *
208  * This function creates the scriptSig by converting ``sig`` to DER
209  * encoding, appending the given sighash, then calling `wally_scriptsig_p2pkh_from_der`.
210  *
211  * :param pub_key: The public key to create a scriptSig with.
212  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_LEN``
213  *|    or ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN``.
214  * :param sig: The compact signature to create a scriptSig with.
215  * :param sig_len: The length of ``sig`` in bytes. Must be ``EC_SIGNATURE_LEN``.
216  * :param sighash: ``WALLY_SIGHASH_`` flags specifying the type of signature desired.
217  * :param bytes_out: Destination for the resulting scriptSig.
218  * :param len: The length of ``bytes_out`` in bytes.
219  * :param written: Destination for the number of bytes written to ``bytes_out``.
220  */
221 WALLY_CORE_API int wally_scriptsig_p2pkh_from_sig(
222     const unsigned char *pub_key,
223     size_t pub_key_len,
224     const unsigned char *sig,
225     size_t sig_len,
226     uint32_t sighash,
227     unsigned char *bytes_out,
228     size_t len,
229     size_t *written);
230 
231 /**
232  * Create a P2WPKH witness from a pubkey and compact signature.
233  *
234  * :param pub_key: The public key to create a witness with.
235  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_LEN``
236  *|    or ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN``.
237  * :param sig: The compact signature to create a witness with.
238  * :param sig_len: The length of ``sig`` in bytes. Must be ``EC_SIGNATURE_LEN``.
239  * :param sighash: ``WALLY_SIGHASH_`` flags specifying the type of signature desired.
240  * :param witness: Destination for the newly created witness.
241  */
242 WALLY_CORE_API int wally_witness_p2wpkh_from_sig(
243     const unsigned char *pub_key,
244     size_t pub_key_len,
245     const unsigned char *sig,
246     size_t sig_len,
247     uint32_t sighash,
248     struct wally_tx_witness_stack **witness);
249 
250 /**
251  * Create a P2PKH scriptSig from a pubkey and DER signature plus sighash.
252  *
253  * :param pub_key: The public key to create a scriptSig with.
254  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be
255  *|    ``EC_PUBLIC_KEY_LEN`` ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN``.
256  * :param sig: The DER encoded signature to create a scriptSig,
257  *|    with the sighash byte appended to it.
258  * :param sig_len: The length of ``sig`` in bytes.
259  * :param bytes_out: Destination for the resulting scriptSig.
260  * :param len: The length of ``bytes_out`` in bytes.
261  * :param written: Destination for the number of bytes written to ``bytes_out``.
262  */
263 WALLY_CORE_API int wally_scriptsig_p2pkh_from_der(
264     const unsigned char *pub_key,
265     size_t pub_key_len,
266     const unsigned char *sig,
267     size_t sig_len,
268     unsigned char *bytes_out,
269     size_t len,
270     size_t *written);
271 
272 /**
273  * Create a P2WPKH witness from a pubkey and DER signature plus sighash.
274  *
275  * :param pub_key: The public key to create a witness with.
276  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be
277  *|    ``EC_PUBLIC_KEY_LEN`` ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN``.
278  * :param sig: The DER encoded signature to create a witness,
279  *|    with the sighash byte appended to it.
280  * :param sig_len: The length of ``sig`` in bytes.
281  * :param witness: Destination for the newly created witness.
282  */
283 WALLY_CORE_API int wally_witness_p2wpkh_from_der(
284     const unsigned char *pub_key,
285     size_t pub_key_len,
286     const unsigned char *sig,
287     size_t sig_len,
288     struct wally_tx_witness_stack **witness);
289 
290 /**
291  * Create an OP_RETURN scriptPubkey.
292  *
293  * :param bytes: Bytes to create a scriptPubkey for.
294  * :param bytes_len: Length of ``bytes`` in bytes. Must be less
295  *|    than or equal to ``WALLY_MAX_OP_RETURN_LEN``.
296  * :param flags: Currently unused, must be 0.
297  * :param bytes_out: Destination for the resulting scriptPubkey.
298  * :param len: The length of ``bytes_out`` in bytes. Passing
299  *|    ``WALLY_SCRIPTPUBKEY_OP_RETURN_MAX_LEN`` will ensure there is always
300  *|    enough room for the resulting scriptPubkey.
301  * :param written: Destination for the number of bytes written to ``bytes_out``.
302  */
303 WALLY_CORE_API int wally_scriptpubkey_op_return_from_bytes(
304     const unsigned char *bytes,
305     size_t bytes_len,
306     uint32_t flags,
307     unsigned char *bytes_out,
308     size_t len,
309     size_t *written);
310 
311 /**
312  * Create a P2SH scriptPubkey.
313  *
314  * :param bytes: Bytes to create a scriptPubkey for.
315  * :param bytes_len: Length of ``bytes`` in bytes.
316  * :param flags: ``WALLY_SCRIPT_HASH160`` or 0.
317  * :param bytes_out: Destination for the resulting scriptPubkey.
318  * :param len: The length of ``bytes_out`` in bytes. If ``WALLY_SCRIPT_HASH160``
319  *|    is given, ``bytes`` is a script to hash160 before creating the P2SH.
320  *|    Otherwise, bytes_len must be ``HASH160_LEN`` and ``bytes`` must contain
321  *|    the hash160 to use.
322  * :param written: Destination for the number of bytes written to ``bytes_out``.
323  */
324 WALLY_CORE_API int wally_scriptpubkey_p2sh_from_bytes(
325     const unsigned char *bytes,
326     size_t bytes_len,
327     uint32_t flags,
328     unsigned char *bytes_out,
329     size_t len,
330     size_t *written);
331 
332 /**
333  * Create a multisig scriptPubkey.
334  *
335  * :param bytes: Compressed public keys to create a scriptPubkey from.
336  * :param bytes_len: Length of ``bytes`` in bytes. Must be a multiple of ``EC_PUBLIC_KEY_LEN``.
337  * :param threshold: The number of signatures that must match to satisfy the script.
338  * :param flags: Must be ``WALLY_SCRIPT_MULTISIG_SORTED`` for BIP67 sorting or 0.
339  * :param bytes_out: Destination for the resulting scriptPubkey.
340  * :param len: The length of ``bytes_out`` in bytes.
341  * :param written: Destination for the number of bytes written to ``bytes_out``.
342  *
343  * .. note:: A maximum of 15 keys are allowed to be passed.
344  */
345 WALLY_CORE_API int wally_scriptpubkey_multisig_from_bytes(
346     const unsigned char *bytes,
347     size_t bytes_len,
348     uint32_t threshold,
349     uint32_t flags,
350     unsigned char *bytes_out,
351     size_t len,
352     size_t *written);
353 
354 /**
355  * Create a multisig scriptSig.
356  *
357  * :param script: The redeem script this scriptSig provides signatures for.
358  * :param script_len: The length of ``script`` in bytes.
359  * :param bytes: Compact signatures to place in the scriptSig.
360  * :param bytes_len: Length of ``bytes`` in bytes. Must be a multiple of ``EC_SIGNATURE_LEN``.
361  * :param sighash: ``WALLY_SIGHASH_`` flags for each signature in ``bytes``.
362  * :param sighash_len: The number of sighash flags in ``sighash``.
363  * :param flags: Must be zero.
364  * :param bytes_out: Destination for the resulting scriptSig.
365  * :param len: The length of ``bytes_out`` in bytes.
366  * :param written: Destination for the number of bytes written to ``bytes_out``.
367  */
368 WALLY_CORE_API int wally_scriptsig_multisig_from_bytes(
369     const unsigned char *script,
370     size_t script_len,
371     const unsigned char *bytes,
372     size_t bytes_len,
373     const uint32_t *sighash,
374     size_t sighash_len,
375     uint32_t flags,
376     unsigned char *bytes_out,
377     size_t len,
378     size_t *written);
379 
380 /**
381  * Create a multisig scriptWitness.
382  *
383  * :param script: The witness script this scriptWitness provides signatures for.
384  * :param script_len: The length of ``script`` in bytes.
385  * :param bytes: Compact signatures to place in the scriptWitness.
386  * :param bytes_len: Length of ``bytes`` in bytes. Must be a multiple of ``EC_SIGNATURE_LEN``.
387  * :param sighash: ``WALLY_SIGHASH_`` flags for each signature in ``bytes``.
388  * :param sighash_len: The number of sighash flags in ``sighash``.
389  * :param flags: Must be zero.
390  * :param witness: Destination for newly allocated witness.
391  */
392 WALLY_CORE_API int wally_witness_multisig_from_bytes(
393     const unsigned char *script,
394     size_t script_len,
395     const unsigned char *bytes,
396     size_t bytes_len,
397     const uint32_t *sighash,
398     size_t sighash_len,
399     uint32_t flags,
400     struct wally_tx_witness_stack **witness);
401 
402 /**
403  * Create a CSV 2of2 multisig with a single key recovery scriptPubkey.
404  *
405  * The resulting output can be spent at any time with both of the two keys
406  * given, and by the last (recovery) key alone, ``csv_blocks`` after the
407  * output confirms.
408  *
409  * :param bytes: Compressed public keys to create a scriptPubkey from. The
410  *|    second key given will be used as the recovery key.
411  * :param bytes_len: Length of ``bytes`` in bytes. Must 2 * ``EC_PUBLIC_KEY_LEN``.
412  * :param csv_blocks: The number of blocks before the recovery key can be
413  *| used. Must be between 17 and 65536.
414  * :param flags: Must be zero.
415  * :param bytes_out: Destination for the resulting scriptPubkey.
416  * :param len: The length of ``bytes_out`` in bytes.
417  * :param written: Destination for the number of bytes written to ``bytes_out``.
418  */
419 WALLY_CORE_API int wally_scriptpubkey_csv_2of2_then_1_from_bytes(
420     const unsigned char *bytes,
421     size_t bytes_len,
422     uint32_t csv_blocks,
423     uint32_t flags,
424     unsigned char *bytes_out,
425     size_t len,
426     size_t *written);
427 
428 /**
429  * Create an optimised CSV 2of2 multisig with a single key recovery scriptPubkey.
430  *
431  * Works like ``wally_scriptpubkey_csv_2of2_then_1_from_bytes`` but produces a
432  * script that is smaller and compatible with the miniscript expression
433  * "and(pk(key_user),or(99@pk(key_service),older(<csv_blocks>)))".
434  */
435 WALLY_CORE_API int wally_scriptpubkey_csv_2of2_then_1_from_bytes_opt(
436     const unsigned char *bytes,
437     size_t bytes_len,
438     uint32_t csv_blocks,
439     uint32_t flags,
440     unsigned char *bytes_out,
441     size_t len,
442     size_t *written);
443 
444 /**
445  * Create a CSV 2of3 multisig with two key recovery scriptPubkey.
446  *
447  * The resulting output can be spent at any time with any two of the three keys
448  * given, and by either of the last two (recovery) keys alone, ``csv_blocks``
449  * after the output confirms.
450  *
451  * :param bytes: Compressed public keys to create a scriptPubkey from. The
452  *|    second and third keys given will be used as the recovery keys.
453  * :param bytes_len: Length of ``bytes`` in bytes. Must 3 * ``EC_PUBLIC_KEY_LEN``.
454  * :param csv_blocks: The number of blocks before the recovery keys can be
455  *| used. Must be between 17 and 65536.
456  * :param flags: Must be zero.
457  * :param bytes_out: Destination for the resulting scriptPubkey.
458  * :param len: The length of ``bytes_out`` in bytes.
459  * :param written: Destination for the number of bytes written to ``bytes_out``.
460  */
461 WALLY_CORE_API int wally_scriptpubkey_csv_2of3_then_2_from_bytes(
462     const unsigned char *bytes,
463     size_t bytes_len,
464     uint32_t csv_blocks,
465     uint32_t flags,
466     unsigned char *bytes_out,
467     size_t len,
468     size_t *written);
469 
470 /**
471  * Create a bitcoin script that pushes data to the stack.
472  *
473  * :param bytes: Bytes to create a push script for.
474  * :param bytes_len: Length of ``bytes`` in bytes.
475  * :param flags: ``WALLY_SCRIPT_HASH160`` or ``WALLY_SCRIPT_SHA256`` to
476  *|    hash ``bytes`` before pushing it.
477  * :param bytes_out: Destination for the resulting push script.
478  * :param len: The length of ``bytes_out`` in bytes.
479  * :param written: Destination for the number of bytes written to ``bytes_out``.
480  */
481 WALLY_CORE_API int wally_script_push_from_bytes(
482     const unsigned char *bytes,
483     size_t bytes_len,
484     uint32_t flags,
485     unsigned char *bytes_out,
486     size_t len,
487     size_t *written);
488 
489 /**
490  * Get the length of an integer serialized to a varint.
491  *
492  * :param value: The integer value to be find the length of.
493  * :param written: Destination for the length of the integer when serialized.
494  */
495 WALLY_CORE_API int wally_varint_get_length(
496     uint64_t value,
497     size_t *written);
498 
499 /**
500  * Serialize an integer to a buffer as a varint.
501  *
502  * :param value: The integer value to be serialized.
503  * :param bytes_out: Destination for the resulting serialized varint.
504  * :param len: The length of ``bytes_out`` in bytes. Must be at least
505  *|    wally_varint_get_length(value).
506  * :param written: Destination for the number of bytes written to ``bytes_out``.
507  */
508 WALLY_CORE_API int wally_varint_to_bytes(
509     uint64_t value,
510     unsigned char *bytes_out,
511     size_t len,
512     size_t *written);
513 
514 /**
515  * Get the length of a buffer serialized to a varbuff (varint size, followed by the buffer).
516  *
517  * :param bytes: The buffer to get the length of.
518  * :param bytes_len: Length of ``bytes`` in bytes.
519  * :param written: Destination for the length of the buffer when serialized.
520  */
521 WALLY_CORE_API int wally_varbuff_get_length(
522     const unsigned char *bytes,
523     size_t bytes_len,
524     size_t *written);
525 
526 /**
527  * Serialize a buffer to a varbuff (varint size, followed by the buffer).
528  *
529  * :param bytes: The buffer to be serialized.
530  * :param bytes_len: Length of ``bytes`` in bytes.
531  * :param bytes_out: Destination for the resulting serialized varbuff.
532  * :param len: The length of ``bytes_out`` in bytes. Must be at least
533  *|    wally_varbuff_get_length(bytes, bytes_len).
534  * :param written: Destination for the number of bytes written to ``bytes_out``.
535  */
536 WALLY_CORE_API int wally_varbuff_to_bytes(
537     const unsigned char *bytes,
538     size_t bytes_len,
539     unsigned char *bytes_out,
540     size_t len,
541     size_t *written);
542 
543 /**
544  * Create a segwit witness program from a script or hash.
545  *
546  * :param bytes: Script or hash bytes to create a witness program from.
547  * :param bytes_len: Length of ``bytes`` in bytes. Must be ``HASH160_LEN``
548  *|     or ``SHA256_LEN`` if neither ``WALLY_SCRIPT_HASH160`` or
549  *|     ``WALLY_SCRIPT_SHA256`` is given.
550  * :param flags: ``WALLY_SCRIPT_HASH160`` or ``WALLY_SCRIPT_SHA256`` to hash
551  *|    the input script before using it. ``WALLY_SCRIPT_AS_PUSH`` to generate
552  *|    a push of the generated script as used for the scriptSig in p2sh-p2wpkh
553  *|    and p2sh-p2wsh.
554  * :param bytes_out: Destination for the resulting witness program.
555  * :param len: The length of ``bytes_out`` in bytes.
556  * :param written: Destination for the number of bytes written to ``bytes_out``.
557  */
558 WALLY_CORE_API int wally_witness_program_from_bytes(
559     const unsigned char *bytes,
560     size_t bytes_len,
561     uint32_t flags,
562     unsigned char *bytes_out,
563     size_t len,
564     size_t *written);
565 
566 #ifdef BUILD_ELEMENTS
567 /**
568  * Get the pegout script size.
569  *
570  * :param genesis_blockhash_len: Length of ``genesis_blockhash`` in bytes. Must be ``SHA256_LEN``.
571  * :param mainchain_script_len: Length of ``mainchain_script`` in bytes.
572  * :param sub_pubkey_len: Length of ``sub_pubkey`` in bytes. Must be ``EC_PUBLIC_KEY_LEN``.
573  * :param whitelistproof_len: The length of ``whitelistproof`` in bytes.
574  * :param written: Destination for the number of bytes required to hold the pegout script.
575  */
576 WALLY_CORE_API int wally_elements_pegout_script_size(
577     size_t genesis_blockhash_len,
578     size_t mainchain_script_len,
579     size_t sub_pubkey_len,
580     size_t whitelistproof_len,
581     size_t *written);
582 
583 /**
584  * Create a pegout script.
585  *
586  * :param genesis_blockhash: The genesis blockhash of the parent chain.
587  * :param genesis_blockhash_len: Length of ``genesis_blockhash`` in bytes. Must be ``SHA256_LEN``.
588  * :param mainchain_script: The parent chain script.
589  * :param mainchain_script_len: Length of ``mainchain_script`` in bytes.
590  * :param sub_pubkey: The whitelisted public key.
591  * :param sub_pubkey_len: Length of ``sub_pubkey`` in bytes. Must be ``EC_PUBLIC_KEY_LEN``.
592  * :param whitelistproof: The whitelist proof.
593  * :param whitelistproof_len: The length of ``whitelistproof`` in bytes.
594  * :param flags: Must be zero.
595  * :param bytes_out: Destination for the resulting pegout script.
596  * :param len: The length of ``bytes_out`` in bytes.
597  * :param written: Destination for the number of bytes written to ``bytes_out``.
598  */
599 WALLY_CORE_API int wally_elements_pegout_script_from_bytes(
600     const unsigned char *genesis_blockhash,
601     size_t genesis_blockhash_len,
602     const unsigned char *mainchain_script,
603     size_t mainchain_script_len,
604     const unsigned char *sub_pubkey,
605     size_t sub_pubkey_len,
606     const unsigned char *whitelistproof,
607     size_t whitelistproof_len,
608     uint32_t flags,
609     unsigned char *bytes_out,
610     size_t len,
611     size_t *written);
612 
613 /**
614  * Create a script for P2CH pegin transactions.
615  *
616  * :param redeem_script: The federation redeem script.
617  * :param redeem_script_len: Length of ``redeem_script`` in bytes.
618  * :param script: The claim script.
619  * :param script_len: Length of ``script`` in bytes.
620  * :param flags: Must be zero.
621  * :param bytes_out: Destination for the resulting script.
622  * :param len: Length of ``bytes_out`` in bytes.
623  * :param written: Destination for the number of bytes written to ``bytes_out``.
624  */
625 WALLY_CORE_API int wally_elements_pegin_contract_script_from_bytes(
626     const unsigned char *redeem_script,
627     size_t redeem_script_len,
628     const unsigned char *script,
629     size_t script_len,
630     uint32_t flags,
631     unsigned char *bytes_out,
632     size_t len,
633     size_t *written);
634 #endif
635 
636 #ifdef __cplusplus
637 }
638 #endif
639 
640 #endif /* LIBWALLY_CORE_SCRIPT_H */
641