1 #ifndef LIBWALLY_CORE_PSBT_H
2 #define LIBWALLY_CORE_PSBT_H
3 
4 #include "wally_transaction.h"
5 #include "wally_bip32.h"
6 
7 #ifdef __cplusplus
8 extern "C" {
9 #endif
10 
11 /* PSBT Version number */
12 #define WALLY_PSBT_HIGHEST_VERSION 0
13 
14 /* Ignore scriptsig and witness when adding an input */
15 #define WALLY_PSBT_FLAG_NON_FINAL 0x1
16 
17 /* Key prefix for proprietary keys in our unknown maps */
18 #define PSBT_PROPRIETARY_TYPE 0xFC
19 
20 #ifdef SWIG
21 struct wally_map;
22 struct wally_psbt_input;
23 struct wally_psbt_output;
24 struct wally_psbt;
25 #else
26 
27 /** A map item */
28 struct wally_map_item {
29     unsigned char *key;
30     size_t key_len;
31     unsigned char *value;
32     size_t value_len;
33 };
34 
35 /** A map of key,value pairs */
36 struct wally_map {
37     struct wally_map_item *items;
38     size_t num_items;
39     size_t items_allocation_len;
40 };
41 
42 /** A PSBT input */
43 struct wally_psbt_input {
44     struct wally_tx *utxo;
45     struct wally_tx_output *witness_utxo;
46     unsigned char *redeem_script;
47     size_t redeem_script_len;
48     unsigned char *witness_script;
49     size_t witness_script_len;
50     unsigned char *final_scriptsig;
51     size_t final_scriptsig_len;
52     struct wally_tx_witness_stack *final_witness;
53     struct wally_map keypaths;
54     struct wally_map signatures;
55     struct wally_map unknowns;
56     uint32_t sighash;
57 #ifdef BUILD_ELEMENTS
58     uint64_t value;
59     uint32_t has_value;
60     unsigned char *vbf;
61     size_t vbf_len;
62     unsigned char *asset;
63     size_t asset_len;
64     unsigned char *abf;
65     size_t abf_len;
66     struct wally_tx *pegin_tx;
67     unsigned char *txoutproof;
68     size_t txoutproof_len;
69     unsigned char *genesis_blockhash;
70     size_t genesis_blockhash_len;
71     unsigned char *claim_script;
72     size_t claim_script_len;
73 #endif /* BUILD_ELEMENTS */
74 };
75 
76 /** A PSBT output */
77 struct wally_psbt_output {
78     unsigned char *redeem_script;
79     size_t redeem_script_len;
80     unsigned char *witness_script;
81     size_t witness_script_len;
82     struct wally_map keypaths;
83     struct wally_map unknowns;
84 #ifdef BUILD_ELEMENTS
85     unsigned char *blinding_pubkey;
86     size_t blinding_pubkey_len;
87     unsigned char *value_commitment;
88     size_t value_commitment_len;
89     unsigned char *vbf;
90     size_t vbf_len;
91     unsigned char *asset_commitment;
92     size_t asset_commitment_len;
93     unsigned char *abf;
94     size_t abf_len;
95     unsigned char *nonce;
96     size_t nonce_len;
97     unsigned char *rangeproof;
98     size_t rangeproof_len;
99     unsigned char *surjectionproof;
100     size_t surjectionproof_len;
101 #endif /* BUILD_ELEMENTS */
102 };
103 
104 /** A partially signed bitcoin transaction */
105 struct wally_psbt {
106     unsigned char magic[5];
107     struct wally_tx *tx;
108     struct wally_psbt_input *inputs;
109     size_t num_inputs;
110     size_t inputs_allocation_len;
111     struct wally_psbt_output *outputs;
112     size_t num_outputs;
113     size_t outputs_allocation_len;
114     struct wally_map unknowns;
115     uint32_t version;
116 };
117 #endif /* SWIG */
118 
119 /**
120  * Allocate and initialize a new map.
121  *
122  * :param allocation_len: The number of items to allocate.
123  * :param output: Destination for the new map.
124  */
125 WALLY_CORE_API int wally_map_init_alloc(
126     size_t allocation_len,
127     struct wally_map **output);
128 
129 #ifndef SWIG_PYTHON
130 /**
131  * Free a map allocated by `wally_map_init_alloc`.
132  *
133  * :param map_in: The map to free.
134  */
135 WALLY_CORE_API int wally_map_free(
136     struct wally_map *map_in);
137 #endif /* SWIG_PYTHON */
138 
139 /**
140  * Find an item in a map.
141  *
142  * :param map_in: The map to find ``key`` in.
143  * :param key: The key to find.
144  * :param key_len: Length of ``key`` in bytes.
145  * :param written: On success, set to zero if the item is not found, otherwise
146  *|    the index of the item plus one.
147  */
148 WALLY_CORE_API int wally_map_find(
149     const struct wally_map *map_in,
150     const unsigned char *key,
151     size_t key_len,
152     size_t *written);
153 
154 /**
155  * Add an item to a map.
156  *
157  * :param map_in: The map to add to.
158  * :param key: The key to add.
159  * :param key_len: Length of ``key`` in bytes.
160  * :param value: The value to add.
161  * :param value_len: Length of ``value`` in bytes.
162  */
163 WALLY_CORE_API int wally_map_add(
164     struct wally_map *map_in,
165     const unsigned char *key,
166     size_t key_len,
167     const unsigned char *value,
168     size_t value_len);
169 
170 /**
171  * Convert and add a pubkey/keypath to a map.
172  *
173  * :param map_in: The map to add to.
174  * :param pub_key: The pubkey to add.
175  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
176  * :param fingerprint: The master key fingerprint for the pubkey.
177  * :param fingerprint_len: Length of ``fingerprint`` in bytes. Must be ``BIP32_KEY_FINGERPRINT_LEN``.
178  * :param child_path: The BIP32 derivation path for the pubkey.
179  * :param child_path_len: The number of items in ``child_path``.
180  */
181 WALLY_CORE_API int wally_map_add_keypath_item(
182     struct wally_map *map_in,
183     const unsigned char *pub_key,
184     size_t pub_key_len,
185     const unsigned char *fingerprint,
186     size_t fingerprint_len,
187     const uint32_t *child_path,
188     size_t child_path_len);
189 
190 /**
191  * Sort the items in a map.
192  *
193  * :param map_in: The map to sort.
194  * :param flags: Flags controlling sorting. Must be 0.
195  */
196 WALLY_CORE_API int wally_map_sort(
197     struct wally_map *map_in,
198     uint32_t flags);
199 
200 #ifndef SWIG
201 /**
202  * Determine if a PSBT input is finalized.
203  *
204  * :param input: The input to check.
205  * :param written: On success, set to one if the input is finalized, otherwise zero.
206  */
207 WALLY_CORE_API int wally_psbt_input_is_finalized(
208     const struct wally_psbt_input *input,
209     size_t *written);
210 
211 /**
212  * Set the utxo in an input.
213  *
214  * :param input: The input to update.
215  * :param utxo: The (non witness) utxo for this input if it exists.
216  */
217 WALLY_CORE_API int wally_psbt_input_set_utxo(
218     struct wally_psbt_input *input,
219     const struct wally_tx *utxo);
220 
221 /**
222  * Set the witness_utxo in an input.
223  *
224  * :param input: The input to update.
225  * :param witness_utxo: The witness utxo for this input if it exists.
226  */
227 WALLY_CORE_API int wally_psbt_input_set_witness_utxo(
228     struct wally_psbt_input *input,
229     const struct wally_tx_output *witness_utxo);
230 
231 /**
232  * Set the redeem_script in an input.
233  *
234  * :param input: The input to update.
235  * :param script: The redeem script for this input.
236  * :param script_len: Length of ``script`` in bytes.
237  */
238 WALLY_CORE_API int wally_psbt_input_set_redeem_script(
239     struct wally_psbt_input *input,
240     const unsigned char *script,
241     size_t script_len);
242 
243 /**
244  * Set the witness_script in an input.
245  *
246  * :param input: The input to update.
247  * :param script: The witness script for this input.
248  * :param script_len: Length of ``script`` in bytes.
249  */
250 WALLY_CORE_API int wally_psbt_input_set_witness_script(
251     struct wally_psbt_input *input,
252     const unsigned char *script,
253     size_t script_len);
254 
255 /**
256  * Set the final_scriptsig in an input.
257  *
258  * :param input: The input to update.
259  * :param final_scriptsig: The scriptSig for this input.
260  * :param final_scriptsig_len: Length of ``final_scriptsig`` in bytes.
261  */
262 WALLY_CORE_API int wally_psbt_input_set_final_scriptsig(
263     struct wally_psbt_input *input,
264     const unsigned char *final_scriptsig,
265     size_t final_scriptsig_len);
266 
267 /**
268  * Set the final_witness in an input.
269  *
270  * :param input: The input to update.
271  * :param final_witness: The witness stack for the input, or NULL if no witness is present.
272  */
273 WALLY_CORE_API int wally_psbt_input_set_final_witness(
274     struct wally_psbt_input *input,
275     const struct wally_tx_witness_stack *final_witness);
276 
277 /**
278  * Set the keypaths in an input.
279  *
280  * :param input: The input to update.
281  * :param map_in: The HD keypaths for this input.
282  */
283 WALLY_CORE_API int wally_psbt_input_set_keypaths(
284     struct wally_psbt_input *input,
285     const struct wally_map *map_in);
286 
287 /**
288  * Find a keypath matching a pubkey in an input.
289  *
290  * :param input: The input to search in.
291  * :param pub_key: The pubkey to find.
292  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
293  * :param written: On success, set to zero if the item is not found, otherwise
294  *|    the index of the item plus one.
295  */
296 WALLY_CORE_API int wally_psbt_input_find_keypath(
297     struct wally_psbt_input *input,
298     const unsigned char *pub_key,
299     size_t pub_key_len,
300     size_t *written);
301 
302 /**
303  * Convert and add a pubkey/keypath to an input.
304  *
305  * :param input: The input to add to.
306  * :param pub_key: The pubkey to add.
307  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
308  * :param fingerprint: The master key fingerprint for the pubkey.
309  * :param fingerprint_len: Length of ``fingerprint`` in bytes. Must be ``BIP32_KEY_FINGERPRINT_LEN``.
310  * :param child_path: The BIP32 derivation path for the pubkey.
311  * :param child_path_len: The number of items in ``child_path``.
312  */
313 WALLY_CORE_API int wally_psbt_input_add_keypath_item(
314     struct wally_psbt_input *input,
315     const unsigned char *pub_key,
316     size_t pub_key_len,
317     const unsigned char *fingerprint,
318     size_t fingerprint_len,
319     const uint32_t *child_path,
320     size_t child_path_len);
321 
322 /**
323  * Set the partial signatures in an input.
324  *
325  * :param input: The input to update.
326  * :param map_in: The partial signatures for this input.
327  */
328 WALLY_CORE_API int wally_psbt_input_set_signatures(
329     struct wally_psbt_input *input,
330     const struct wally_map *map_in);
331 
332 /**
333  * Find a partial signature matching a pubkey in an input.
334  *
335  * :param input: The input to search in.
336  * :param pub_key: The pubkey to find.
337  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
338  * :param written: On success, set to zero if the item is not found, otherwise
339  *|    the index of the item plus one.
340  */
341 WALLY_CORE_API int wally_psbt_input_find_signature(
342     struct wally_psbt_input *input,
343     const unsigned char *pub_key,
344     size_t pub_key_len,
345     size_t *written);
346 
347 /**
348  * Add a pubkey/partial signature item to an input.
349  *
350  * :param input: The input to add the partial signature to.
351  * :param pub_key: The pubkey to find.
352  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
353  * :param sig: The DER-encoded signature plus sighash byte to add.
354  * :param sig_len: The length of ``sig`` in bytes.
355  */
356 WALLY_CORE_API int wally_psbt_input_add_signature(
357     struct wally_psbt_input *input,
358     const unsigned char *pub_key,
359     size_t pub_key_len,
360     const unsigned char *sig,
361     size_t sig_len);
362 
363 /**
364  * Set the unknown values in an input.
365  *
366  * :param input: The input to update.
367  * :param map_in: The unknown key value pairs for this input.
368  */
369 WALLY_CORE_API int wally_psbt_input_set_unknowns(
370     struct wally_psbt_input *input,
371     const struct wally_map *map_in);
372 
373 /**
374  * Find an unknown item matching a key in an input.
375  *
376  * :param input: The input to search in.
377  * :param key: The key to find.
378  * :param key_len: Length of ``key`` in bytes.
379  * :param written: On success, set to zero if the item is not found, otherwise
380  *|    the index of the item plus one.
381  */
382 WALLY_CORE_API int wally_psbt_input_find_unknown(
383     struct wally_psbt_input *input,
384     const unsigned char *key,
385     size_t key_len,
386     size_t *written);
387 
388 /**
389  * Set the sighash type in an input.
390  *
391  * :param input: The input to update.
392  * :param sighash: The sighash type for this input.
393  */
394 WALLY_CORE_API int wally_psbt_input_set_sighash(
395     struct wally_psbt_input *input,
396     uint32_t sighash);
397 
398 /**
399  * Set the redeem_script in an output.
400  *
401  * :param output: The input to update.
402  * :param script: The redeem script for this output.
403  * :param script_len: Length of ``script`` in bytes.
404  */
405 WALLY_CORE_API int wally_psbt_output_set_redeem_script(
406     struct wally_psbt_output *output,
407     const unsigned char *script,
408     size_t script_len);
409 
410 /**
411  * Set the witness_script in an output.
412  *
413  * :param output: The output to update.
414  * :param script: The witness script for this output.
415  * :param script_len: Length of ``script`` in bytes.
416  */
417 WALLY_CORE_API int wally_psbt_output_set_witness_script(
418     struct wally_psbt_output *output,
419     const unsigned char *script,
420     size_t script_len);
421 
422 /**
423  * Set the keypaths in an output.
424  *
425  * :param output: The output to update.
426  * :param map_in: The HD keypaths for this output.
427  */
428 WALLY_CORE_API int wally_psbt_output_set_keypaths(
429     struct wally_psbt_output *output,
430     const struct wally_map *map_in);
431 
432 /**
433  * Find a keypath matching a pubkey in an output.
434  *
435  * :param output: The output to search in.
436  * :param pub_key: The pubkey to find.
437  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
438  * :param written: On success, set to zero if the item is not found, otherwise
439  *|    the index of the item plus one.
440  */
441 WALLY_CORE_API int wally_psbt_output_find_keypath(
442     struct wally_psbt_output *output,
443     const unsigned char *pub_key,
444     size_t pub_key_len,
445     size_t *written);
446 
447 /**
448  * Convert and add a pubkey/keypath to an output.
449  *
450  * :param output: The output to add to.
451  * :param pub_key: The pubkey to add.
452  * :param pub_key_len: Length of ``pub_key`` in bytes. Must be ``EC_PUBLIC_KEY_UNCOMPRESSED_LEN`` or ``EC_PUBLIC_KEY_LEN``.
453  * :param fingerprint: The master key fingerprint for the pubkey.
454  * :param fingerprint_len: Length of ``fingerprint`` in bytes. Must be ``BIP32_KEY_FINGERPRINT_LEN``.
455  * :param child_path: The BIP32 derivation path for the pubkey.
456  * :param child_path_len: The number of items in ``child_path``.
457  */
458 WALLY_CORE_API int wally_psbt_output_add_keypath_item(
459     struct wally_psbt_output *output,
460     const unsigned char *pub_key,
461     size_t pub_key_len,
462     const unsigned char *fingerprint,
463     size_t fingerprint_len,
464     const uint32_t *child_path,
465     size_t child_path_len);
466 
467 /**
468  * Set the unknown map in an output.
469  *
470  * :param output: The output to update.
471  * :param map_in: The unknown key value pairs for this output.
472  */
473 WALLY_CORE_API int wally_psbt_output_set_unknowns(
474     struct wally_psbt_output *output,
475     const struct wally_map *map_in);
476 
477 /**
478  * Find an unknown item matching a key in an output.
479  *
480  * :param output: The output to search in.
481  * :param key: The key to find.
482  * :param key_len: Length of ``key`` in bytes.
483  * :param written: On success, set to zero if the item is not found, otherwise
484  *|    the index of the item plus one.
485  */
486 WALLY_CORE_API int wally_psbt_output_find_unknown(
487     struct wally_psbt_output *output,
488     const unsigned char *key,
489     size_t key_len,
490     size_t *written);
491 #endif /* SWIG */
492 
493 /**
494  * Allocate and initialize a new PSBT.
495  *
496  * :param version: The version of the PSBT. Must be 0.
497  * :param inputs_allocation_len: The number of inputs to pre-allocate space for.
498  * :param outputs_allocation_len: The number of outputs to pre-allocate space for.
499  * :param global_unknowns_allocation_len: The number of global unknowns to allocate space for.
500  * :param output: Destination for the resulting PSBT output.
501  */
502 WALLY_CORE_API int wally_psbt_init_alloc(
503     uint32_t version,
504     size_t inputs_allocation_len,
505     size_t outputs_allocation_len,
506     size_t global_unknowns_allocation_len,
507     struct wally_psbt **output);
508 
509 #ifndef SWIG_PYTHON
510 /**
511  * Free a PSBT allocated by `wally_psbt_init_alloc`.
512  *
513  * :param psbt: The PSBT to free.
514  */
515 WALLY_CORE_API int wally_psbt_free(
516     struct wally_psbt *psbt);
517 #endif /* SWIG_PYTHON */
518 
519 /**
520  * Determine if all PSBT inputs are finalized.
521  *
522  * :param psbt: The PSBT to check.
523  * :param written: On success, set to one if the PSBT is finalized, otherwise zero.
524  */
525 WALLY_CORE_API int wally_psbt_is_finalized(
526     const struct wally_psbt *psbt,
527     size_t *written);
528 
529 /**
530  * Set the global transaction for a PSBT.
531  *
532  * :param psbt: The PSBT to set the transaction for.
533  * :param tx: The transaction to set.
534  *
535  * The global transaction can only be set on a newly created PSBT. After this
536  * call completes the PSBT will have empty inputs and outputs for each input
537  * and output in the transaction ``tx`` given.
538  */
539 WALLY_CORE_API int wally_psbt_set_global_tx(
540     struct wally_psbt *psbt,
541     const struct wally_tx *tx);
542 
543 /**
544  * Add a transaction input to PBST at a given position.
545  *
546  * :param psbt: The PSBT to add the input to.
547  * :param index: The zero-based index of the position to add the input at.
548  * :param flags: Flags controlling input insertion. Must be 0 or ``WALLY_PSBT_FLAG_NON_FINAL``.
549  * :param input: The transaction input to add.
550  */
551 WALLY_CORE_API int wally_psbt_add_input_at(
552     struct wally_psbt *psbt,
553     uint32_t index,
554     uint32_t flags,
555     const struct wally_tx_input *input);
556 
557 /**
558  * Remove a transaction input from a PBST.
559  *
560  * :param psbt: The PSBT to remove the input from.
561  * :param index: The zero-based index of the input to remove.
562  */
563 WALLY_CORE_API int wally_psbt_remove_input(
564     struct wally_psbt *psbt,
565     uint32_t index);
566 
567 /**
568  * Add a transaction output to PBST at a given position.
569  *
570  * :param psbt: The PSBT to add the output to.
571  * :param index: The zero-based index of the position to add the output at.
572  * :param flags: Flags controlling output insertion. Must be 0.
573  * :param output: The transaction output to add.
574  */
575 WALLY_CORE_API int wally_psbt_add_output_at(
576     struct wally_psbt *psbt,
577     uint32_t index,
578     uint32_t flags,
579     const struct wally_tx_output *output);
580 
581 /**
582  * Remove a transaction output from a PBST.
583  *
584  * :param psbt: The PSBT to remove the output from.
585  * :param index: The zero-based index of the output to remove.
586  */
587 WALLY_CORE_API int wally_psbt_remove_output(
588     struct wally_psbt *psbt,
589     uint32_t index);
590 
591 /**
592  * Create a PSBT from its serialized bytes.
593  *
594  * :param bytes: Bytes to create the PSBT from.
595  * :param bytes_len: Length of ``bytes`` in bytes.
596  * :param output: Destination for the resulting PSBT.
597  */
598 WALLY_CORE_API int wally_psbt_from_bytes(
599     const unsigned char *bytes,
600     size_t bytes_len,
601     struct wally_psbt **output);
602 
603 /**
604  * Get the length of a PSBT when serialized to bytes.
605  *
606  * :param psbt: the PSBT.
607  * :param flags: Flags controlling length determination. Must be 0.
608  * :param written: Destination for the length in bytes when serialized.
609  */
610 WALLY_CORE_API int wally_psbt_get_length(
611     const struct wally_psbt *psbt,
612     uint32_t flags,
613     size_t *written);
614 
615 /**
616  * Serialize a PSBT to bytes.
617  *
618  * :param psbt: the PSBT to serialize.
619  * :param flags: Flags controlling serialization. Must be 0.
620  * :param bytes_out: Bytes to create the transaction from.
621  * :param len: Length of ``bytes`` in bytes (use `wally_psbt_get_length`).
622  * :param written: number of bytes written to bytes_out.
623  */
624 WALLY_CORE_API int wally_psbt_to_bytes(
625     const struct wally_psbt *psbt,
626     uint32_t flags,
627     unsigned char *bytes_out,
628     size_t len,
629     size_t *written);
630 
631 /**
632  * Create a PSBT from its serialized base64 string.
633  *
634  * :param base64: Base64 string to create the PSBT from.
635  * :param output: Destination for the resulting PSBT.
636  */
637 WALLY_CORE_API int wally_psbt_from_base64(
638     const char *base64,
639     struct wally_psbt **output);
640 
641 /**
642  * Serialize a PSBT to a base64 string.
643  *
644  * :param psbt: the PSBT to serialize.
645  * :param flags: Flags controlling serialization. Must be 0.
646  * :param output: Destination for the resulting serialized PSBT.
647  */
648 WALLY_CORE_API int wally_psbt_to_base64(
649     const struct wally_psbt *psbt,
650     uint32_t flags,
651     char **output);
652 
653 /**
654  * Combine the metadata from a source PSBT into another PSBT.
655  *
656  * :param psbt: the PSBT to combine into.
657  * :param source: the PSBT to copy data from.
658  */
659 WALLY_CORE_API int wally_psbt_combine(
660     struct wally_psbt *psbt,
661     const struct wally_psbt *src);
662 
663 /**
664  * Clone a PSBT into a newly allocated copy.
665  *
666  * :param psbt: the PSBT to clone.
667  * :param flags: Flags controlling PSBT creation. Must be 0.
668  * :param output: Destination for the resulting cloned PSBT.
669  */
670 WALLY_CORE_API int wally_psbt_clone_alloc(
671     const struct wally_psbt *psbt,
672     uint32_t flags,
673     struct wally_psbt **output);
674 
675 /**
676  * Sign a PSBT using the simple signer algorithm.
677  *
678  * :param psbt: PSBT to sign. Directly modifies this PSBT.
679  * :param key: Private key to sign PSBT with.
680  * :param key_len: Length of key in bytes. Must be ``EC_PRIVATE_KEY_LEN``.
681  * :param flags: Flags controlling sigining. Must be 0 or EC_FLAG_GRIND_R.
682  *
683  * .. note:: See https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki#simple-signer-algorithm
684  *|    for a description of the simple signer algorithm.
685  */
686 WALLY_CORE_API int wally_psbt_sign(
687     struct wally_psbt *psbt,
688     const unsigned char *key,
689     size_t key_len,
690     uint32_t flags);
691 
692 /**
693  * Finalize a PSBT.
694  *
695  * :param psbt: PSBT to finalize. Directly modifies this PSBT.
696  */
697 WALLY_CORE_API int wally_psbt_finalize(
698     struct wally_psbt *psbt);
699 
700 /**
701  * Extract a network transaction from a finalized PSBT.
702  *
703  * :param psbt: PSBT to extract from.
704  * :param output: Destination for the resulting transaction.
705  */
706 WALLY_CORE_API int wally_psbt_extract(
707     const struct wally_psbt *psbt,
708     struct wally_tx **output);
709 
710 /**
711  * Determine if a PSBT is an elements PSBT.
712  *
713  * :param psbt: The PSBT to check.
714  * :param written: 1 if the PSBT is an elements PSBT, otherwise 0.
715  */
716 WALLY_CORE_API int wally_psbt_is_elements(
717     const struct wally_psbt *psbt,
718     size_t *written);
719 
720 #ifdef BUILD_ELEMENTS
721 /**
722  * Allocate and initialize a new elements PSBT.
723  *
724  * :param version: The version of the PSBT. Must be 0.
725  * :param inputs_allocation_len: The number of inputs to pre-allocate space for.
726  * :param outputs_allocation_len: The number of outputs to pre-allocate space for.
727  * :param global_unknowns_allocation_len: The number of global unknowns to allocate space for.
728  * :param output: Destination for the resulting PSBT output.
729  */
730 WALLY_CORE_API int wally_psbt_elements_init_alloc(
731     uint32_t version,
732     size_t inputs_allocation_len,
733     size_t outputs_allocation_len,
734     size_t global_unknowns_allocation_len,
735     struct wally_psbt **output);
736 
737 #ifndef SWIG
738 /**
739  * Set the value in an elements input.
740  *
741  * :param input: The input to update.
742  * :param value: The value for this input.
743  */
744 WALLY_CORE_API int wally_psbt_input_set_value(
745     struct wally_psbt_input *input,
746     uint64_t value);
747 
748 /**
749  * Clear the value in an elements input.
750  *
751  * :param input: The input to update.
752  */
753 WALLY_CORE_API int wally_psbt_input_clear_value(
754     struct wally_psbt_input *input);
755 
756 /**
757  * Set the value blinding factor in an elements input.
758  *
759  * :param input: The input to update.
760  * :param vbf: The value blinding factor.
761  * :param vbf_len: Length of ``vbf``. Must be ``BLINDING_FACTOR_LEN``.
762  */
763 WALLY_CORE_API int wally_psbt_input_set_vbf(
764     struct wally_psbt_input *input,
765     const unsigned char *vbf,
766     size_t vbf_len);
767 
768 /**
769  * Set the asset in an elements input.
770  *
771  * :param input: The input to update.
772  * :param asset: The asset for this input.
773  * :param asset_len: Length of ``asset`` in bytes.
774  */
775 WALLY_CORE_API int wally_psbt_input_set_asset(
776     struct wally_psbt_input *input,
777     const unsigned char *asset,
778     size_t asset_len);
779 
780 /**
781  * Set the asset blinding factor in an elements input
782  *
783  * :param input: The input to update.
784  * :param abf: The asset blinding factor.
785  * :param abf_len: Length of ``abf`` in bytes. Must be ``BLINDING_FACTOR_LEN``.
786  */
787 WALLY_CORE_API int wally_psbt_input_set_abf(
788     struct wally_psbt_input *input,
789     const unsigned char *abf,
790     size_t abf_len);
791 
792 /**
793  * Set the peg in tx in an input.
794  *
795  * :param input: The input to update.
796  * :param pegin_tx: The peg in tx for this input if it exists.
797  */
798 WALLY_CORE_API int wally_psbt_input_set_pegin_tx(
799     struct wally_psbt_input *input,
800     const struct wally_tx *pegin_tx);
801 
802 /**
803  * Set the txout proof in an elements input.
804  *
805  * :param input: The input to update.
806  * :param proof: The txout proof for this input.
807  * :param proof_len: Length of ``proof`` in bytes.
808  */
809 WALLY_CORE_API int wally_psbt_input_set_txoutproof(
810     struct wally_psbt_input *input,
811     const unsigned char *proof,
812     size_t proof_len);
813 
814 /**
815  * Set the genesis hash in an elements input.
816  *
817  * :param input: The input to update.
818  * :param genesis_blockhash: The genesis hash for this input.
819  * :param genesis_blockhash_len: Length of ``genesis_blockhash`` in bytes. Must be ``SHA256_LEN``.
820  */
821 WALLY_CORE_API int wally_psbt_input_set_genesis_blockhash(
822     struct wally_psbt_input *input,
823     const unsigned char *genesis_blockhash,
824     size_t genesis_blockhash_len);
825 
826 /**
827  * Set the claim script in an elements input.
828  *
829  * :param input: The input to update.
830  * :param script: The claim script for this input.
831  * :param script_len: Length of ``script`` in bytes.
832  */
833 WALLY_CORE_API int wally_psbt_input_set_claim_script(
834     struct wally_psbt_input *input,
835     const unsigned char *script,
836     size_t script_len);
837 
838 /**
839  * Set the blinding pubkey in an elements output.
840  *
841  * :param output: The output to update.
842  * :param pub_key: The blinding pubkey for this output.
843  * :param pub_key_len: Length of ``pub_key`` in bytes.
844  */
845 WALLY_CORE_API int wally_psbt_output_set_blinding_pubkey(
846     struct wally_psbt_output *output,
847     const unsigned char *pub_key,
848     size_t pub_key_len);
849 
850 /**
851  * Set the value commitment in an elements output.
852  *
853  * :param output: The output to update.
854  * :param commitment: The value commitment for this output.
855  * :param commitment_len: Length of ``commitment`` in bytes.
856  */
857 WALLY_CORE_API int wally_psbt_output_set_value_commitment(
858     struct wally_psbt_output *output,
859     const unsigned char *commitment,
860     size_t commitment_len);
861 
862 /**
863  * Set the value blinding factor in an elements output.
864  *
865  * :param output: The output to update.
866  * :param vbf: The value blinding factor.
867  * :param vbf_len: Length of ``vbf``. Must be ``BLINDING_FACTOR_LEN``.
868  */
869 WALLY_CORE_API int wally_psbt_output_set_vbf(
870     struct wally_psbt_output *output,
871     const unsigned char *vbf,
872     size_t vbf_len);
873 
874 /**
875  * Set the asset commitment in an elements output.
876  *
877  * :param output: The output to update.
878  * :param commitment: The asset commitment for this output.
879  * :param commitment_len: Length of ``commitment`` in bytes.
880  */
881 WALLY_CORE_API int wally_psbt_output_set_asset_commitment(
882     struct wally_psbt_output *output,
883     const unsigned char *commitment,
884     size_t commitment_len);
885 
886 /**
887  * Set the asset blinding factor in an elements output.
888  *
889  * :param output: The output to update.
890  * :param abf: The asset blinding factor.
891  * :param abf_len: Length of ``abf`` in bytes. Must be ``BLINDING_FACTOR_LEN``.
892  */
893 WALLY_CORE_API int wally_psbt_output_set_abf(
894     struct wally_psbt_output *output,
895     const unsigned char *abf,
896     size_t abf_len);
897 
898 /**
899  * Set the nonce commitment in an elements output.
900  *
901  * :param output: The output to update.
902  * :param nonce: The commitment used to create the nonce (with the blinding key) for the range proof.
903  * :param nonce_len: Size of ``nonce`` in bytes. Must be ``WALLY_TX_ASSET_CT_NONCE_LEN``.
904  */
905 WALLY_CORE_API int wally_psbt_output_set_nonce(
906     struct wally_psbt_output *output,
907     const unsigned char *nonce,
908     size_t nonce_len);
909 
910 /**
911  * Set the range proof in an elements output.
912  *
913  * :param output: The output to update.
914  * :param proof: The range proof for this output.
915  * :param proof_len: Length of ``proof`` in bytes.
916  */
917 WALLY_CORE_API int wally_psbt_output_set_rangeproof(
918     struct wally_psbt_output *output,
919     const unsigned char *proof,
920     size_t proof_len);
921 
922 /**
923  * Set the surjection proof in an elements output.
924  *
925  * :param output: The output to update.
926  * :param proof: The surjection proof for this output.
927  * :param proof_len: Length of ``proof`` in bytes.
928  */
929 WALLY_CORE_API int wally_psbt_output_set_surjectionproof(
930     struct wally_psbt_output *output,
931     const unsigned char *proof,
932     size_t proof_len);
933 #endif /* SWIG */
934 
935 #endif /* BUILD_ELEMENTS */
936 
937 #ifdef __cplusplus
938 }
939 #endif
940 
941 #endif /* LIBWALLY_CORE_PSBT_H */
942