-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathwally_transaction.h
1463 lines (1365 loc) · 53.8 KB
/
wally_transaction.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef LIBWALLY_CORE_TRANSACTION_H
#define LIBWALLY_CORE_TRANSACTION_H
#include "wally_core.h"
#include "wally_crypto.h"
#ifdef __cplusplus
extern "C" {
#endif
#define WALLY_TX_SEQUENCE_FINAL 0xffffffff
#define WALLY_TX_VERSION_1 1
#define WALLY_TX_VERSION_2 2
#define WALLY_TX_IS_ELEMENTS 1
#define WALLY_TX_IS_ISSUANCE 2
#define WALLY_TX_IS_PEGIN 4
#define WALLY_TX_IS_COINBASE 8
#define WALLY_SATOSHI_PER_BTC 100000000
#define WALLY_BTC_MAX 21000000
#define WALLY_TXHASH_LEN 32 /** Size of a transaction hash in bytes */
#define WALLY_TX_FLAG_USE_WITNESS 0x1 /* Encode witness data if present */
#define WALLY_TX_FLAG_USE_ELEMENTS 0x2 /* Encode/Decode as an elements transaction */
#define WALLY_TX_FLAG_ALLOW_PARTIAL 0x4 /* Allow partially complete transactions */
/* Note: This flag encodes/parses transactions that are ambiguous to decode.
Unless you have a good reason to do so you will most likely not need it */
#define WALLY_TX_FLAG_PRE_BIP144 0x8 /* Encode/Decode using pre-BIP 144 serialization */
#define WALLY_TX_FLAG_BLINDED_INITIAL_ISSUANCE 0x1
/*** tx-clone Transaction cloning flags */
#define WALLY_TX_CLONE_FLAG_NON_FINAL 0x1 /* Ignore scriptsig and witness when cloning */
#define WALLY_TX_DUMMY_NULL 0x1 /* An empty witness item */
#define WALLY_TX_DUMMY_SIG 0x2 /* A dummy signature */
#define WALLY_TX_DUMMY_SIG_LOW_R 0x4 /* A dummy signature created with EC_FLAG_GRIND_R */
/** Sighash flags for transaction signing */
#define WALLY_SIGHASH_DEFAULT 0x00
#define WALLY_SIGHASH_ALL 0x01
#define WALLY_SIGHASH_NONE 0x02
#define WALLY_SIGHASH_SINGLE 0x03
#define WALLY_SIGHASH_FORKID 0x40
#define WALLY_SIGHASH_RANGEPROOF 0x40 /* Liquid/Elements only */
#define WALLY_SIGHASH_ANYPREVOUT 0x40 /* BIP118 only */
#define WALLY_SIGHASH_ANYPREVOUTANYSCRIPT 0xc0 /* BIP118 only */
#define WALLY_SIGHASH_ANYONECANPAY 0x80
#define WALLY_SIGHASH_MASK 0x1f /* Mask for determining ALL/NONE/SINGLE */
#define WALLY_SIGHASH_TR_IN_MASK 0xc0 /* Taproot mask for determining input hash type */
/*** tx-sighash-type Transaction signature hash flags */
#define WALLY_SIGTYPE_PRE_SW 0x1 /* Pre-segwit signature hash */
#define WALLY_SIGTYPE_SW_V0 0x2 /* Segwit v0 signature hash */
#define WALLY_SIGTYPE_SW_V1 0x3 /* Segwit v1 (taproot) signature hash */
#define WALLY_SIGTYPE_MASK 0xf /* Mask for signature hash in signature hash flags */
#define WALLY_TX_ASSET_CT_EMPTY_PREFIX 0x00
#define WALLY_TX_ASSET_CT_EXPLICIT_PREFIX 0x01
#define WALLY_TX_ASSET_CT_VALUE_PREFIX_A 0x08
#define WALLY_TX_ASSET_CT_VALUE_PREFIX_B 0x09
#define WALLY_TX_ASSET_CT_ASSET_PREFIX_A 0x0a
#define WALLY_TX_ASSET_CT_ASSET_PREFIX_B 0x0b
#define WALLY_TX_ASSET_CT_NONCE_PREFIX_A 0x02
#define WALLY_TX_ASSET_CT_NONCE_PREFIX_B 0x03
#define WALLY_TX_ASSET_TAG_LEN 32
#define WALLY_TX_ASSET_CT_VALUE_LEN 33 /* version byte + 32 bytes */
#define WALLY_TX_ASSET_CT_VALUE_UNBLIND_LEN 9 /* version byte + 8 bytes */
#define WALLY_TX_ASSET_CT_ASSET_LEN 33 /* version byte + 32 bytes */
#define WALLY_TX_ASSET_CT_NONCE_LEN 33 /* version byte + 32 bytes */
#define WALLY_TX_ASSET_CT_LEN 33 /* version byte + 32 bytes */
#define WALLY_TX_ISSUANCE_FLAG 0x80000000
#define WALLY_TX_PEGIN_FLAG 0x40000000
#define WALLY_TX_INDEX_MASK 0x3fffffff
#define WALLY_NO_CODESEPARATOR 0xffffffff /* No BIP342 code separator position */
struct wally_map;
#ifdef SWIG
struct wally_tx_witness_item;
struct wally_tx_witness_stack;
struct wally_tx_input;
struct wally_tx_output;
struct wally_tx;
#else
/** A transaction witness item */
struct wally_tx_witness_item {
unsigned char *witness;
size_t witness_len;
};
/** A transaction witness stack */
struct wally_tx_witness_stack {
struct wally_tx_witness_item *items;
size_t num_items;
size_t items_allocation_len;
};
/** A transaction input */
struct wally_tx_input {
unsigned char txhash[WALLY_TXHASH_LEN];
uint32_t index;
uint32_t sequence;
unsigned char *script;
size_t script_len;
struct wally_tx_witness_stack *witness;
uint8_t features;
#ifndef WALLY_ABI_NO_ELEMENTS
unsigned char blinding_nonce[SHA256_LEN];
unsigned char entropy[SHA256_LEN];
unsigned char *issuance_amount;
size_t issuance_amount_len;
unsigned char *inflation_keys;
size_t inflation_keys_len;
unsigned char *issuance_amount_rangeproof;
size_t issuance_amount_rangeproof_len;
unsigned char *inflation_keys_rangeproof;
size_t inflation_keys_rangeproof_len;
struct wally_tx_witness_stack *pegin_witness;
#endif /* WALLY_ABI_NO_ELEMENTS */
};
/** A transaction output */
struct wally_tx_output {
uint64_t satoshi;
unsigned char *script;
size_t script_len;
uint8_t features;
#ifndef WALLY_ABI_NO_ELEMENTS
unsigned char *asset;
size_t asset_len;
unsigned char *value;
size_t value_len;
unsigned char *nonce;
size_t nonce_len;
unsigned char *surjectionproof;
size_t surjectionproof_len;
unsigned char *rangeproof;
size_t rangeproof_len;
#endif /* WALLY_ABI_NO_ELEMENTS */
};
/** A parsed bitcoin transaction */
struct wally_tx {
uint32_t version;
uint32_t locktime;
struct wally_tx_input *inputs;
size_t num_inputs;
size_t inputs_allocation_len;
struct wally_tx_output *outputs;
size_t num_outputs;
size_t outputs_allocation_len;
};
#endif /* SWIG */
/**
* Allocate and initialize a new witness stack.
*
* :param allocation_len: The number of items to pre-allocate space for.
* :param output: Destination for the resulting witness stack.
*/
WALLY_CORE_API int wally_tx_witness_stack_init_alloc(
size_t allocation_len,
struct wally_tx_witness_stack **output);
/**
* Create a copy of a witness stack.
*
* :param stack: The witness stack to copy.
* :param output: Destination for the resulting copy.
*/
WALLY_CORE_API int wally_tx_witness_stack_clone_alloc(
const struct wally_tx_witness_stack *stack,
struct wally_tx_witness_stack **output);
/**
* Return the number of witness items in a witness stack.
*
* :param stack: The witness stack to get the number of items from.
* :param written: Destination for the number of items.
*/
WALLY_CORE_API int wally_tx_witness_stack_get_num_items(
const struct wally_tx_witness_stack *stack,
size_t *written);
/**
* Add a witness to a witness stack.
*
* :param stack: The witness stack to add to.
* :param witness: The witness data to add to the stack.
* :param witness_len: Length of ``witness`` in bytes.
*/
WALLY_CORE_API int wally_tx_witness_stack_add(
struct wally_tx_witness_stack *stack,
const unsigned char *witness,
size_t witness_len);
/**
* Add a dummy witness item to a witness stack.
*
* :param stack: The witness stack to add to.
* :param flags: ``WALLY_TX_DUMMY_`` Flags indicating the type of dummy to add.
*/
WALLY_CORE_API int wally_tx_witness_stack_add_dummy(
struct wally_tx_witness_stack *stack,
uint32_t flags);
/**
* Set a witness item to a witness stack.
*
* :param stack: The witness stack to add to.
* :param index: Index of the item to set. The stack will grow if needed to this many items.
* :param witness: The witness data to add to the stack.
* :param witness_len: Length of ``witness`` in bytes.
*/
WALLY_CORE_API int wally_tx_witness_stack_set(
struct wally_tx_witness_stack *stack,
size_t index,
const unsigned char *witness,
size_t witness_len);
/**
* Set a dummy witness item to a witness stack.
*
* :param stack: The witness stack to add to.
* :param index: Index of the item to set. The stack will grow if needed to this many items.
* :param flags: ``WALLY_TX_DUMMY_`` Flags indicating the type of dummy to set.
*/
WALLY_CORE_API int wally_tx_witness_stack_set_dummy(
struct wally_tx_witness_stack *stack,
size_t index,
uint32_t flags);
/**
* Create a new witness stack from its BIP 144 serialization.
*
* :param bytes: Bytes to create the witness stack from.
* :param bytes_len: Length of ``bytes`` in bytes.
* :param output: Destination for the resulting witness stack.
*/
WALLY_CORE_API int wally_tx_witness_stack_from_bytes(
const unsigned char *bytes,
size_t bytes_len,
struct wally_tx_witness_stack **output);
/**
* Return the length of a witness stacks BIP 144 serialization.
*
* :param stack: The witness stack to find the serialized length of.
* :param written: Destination for the length of the serialized bytes.
*/
WALLY_CORE_API int wally_tx_witness_stack_get_length(
const struct wally_tx_witness_stack *stack,
size_t *written);
/**
* Serialize a witness stack to its BIP 144 serialization.
*
* :param stack: The witness stack to serialize.
* :param bytes_out: Destination for the serialized witness stack.
* :param len: Size of ``bytes_out`` in bytes.
* :param written: Destination for the length of the serialized witness stack.
*/
WALLY_CORE_API int wally_tx_witness_stack_to_bytes(
const struct wally_tx_witness_stack *stack,
unsigned char *bytes_out,
size_t len,
size_t *written);
/**
* Free a transaction witness stack allocated by `wally_tx_witness_stack_init_alloc`.
*
* :param stack: The transaction witness stack to free.
*/
WALLY_CORE_API int wally_tx_witness_stack_free(
struct wally_tx_witness_stack *stack);
/**
* Allocate and initialize a new transaction input.
*
* :param txhash: The transaction hash of the transaction this input comes from.
* :param txhash_len: Size of ``txhash`` in bytes. Must be `WALLY_TXHASH_LEN`.
* :param utxo_index: The zero-based index of the transaction output in ``txhash`` that
*| this input comes from.
* :param sequence: The sequence number for the input.
* :param script: The scriptSig for the input.
* :param script_len: Size of ``script`` in bytes.
* :param witness: The witness stack for the input, or NULL if no witness is present.
* :param output: Destination for the resulting transaction input.
*/
WALLY_CORE_API int wally_tx_input_init_alloc(
const unsigned char *txhash,
size_t txhash_len,
uint32_t utxo_index,
uint32_t sequence,
const unsigned char *script,
size_t script_len,
const struct wally_tx_witness_stack *witness,
struct wally_tx_input **output);
/**
* Create a new copy of a transaction input.
*
* :param tx_input_in: The transaction input to clone.
* :param input: Destination for the resulting transaction input copy.
*/
WALLY_CORE_API int wally_tx_input_clone_alloc(
const struct wally_tx_input *tx_input_in,
struct wally_tx_input **input);
/**
* Create a new copy of a transaction input in place.
*
* :param tx_input_in: The transaction input to clone.
* :param input: Destination for the resulting transaction input copy.
*
* .. note:: ``input`` is overwritten in place, and not cleared first.
*/
WALLY_CORE_API int wally_tx_input_clone(
const struct wally_tx_input *tx_input_in,
struct wally_tx_input *input);
/**
* Free a transaction input allocated by `wally_tx_input_init_alloc`.
*
* :param input: The transaction input to free.
*/
WALLY_CORE_API int wally_tx_input_free(struct wally_tx_input *input);
/**
* Initialize a new transaction output.
*
* :param satoshi: The amount of the output in satoshi.
* :param script: The scriptPubkey for the output.
* :param script_len: Size of ``script`` in bytes.
* :param output: Transaction output to initialize.
*/
WALLY_CORE_API int wally_tx_output_init(uint64_t satoshi,
const unsigned char *script,
size_t script_len,
struct wally_tx_output *output);
/**
* Allocate and initialize a new transaction output.
*
* :param satoshi: The amount of the output in satoshi.
* :param script: The scriptPubkey for the output.
* :param script_len: Size of ``script`` in bytes.
* :param output: Destination for the resulting transaction output.
*/
WALLY_CORE_API int wally_tx_output_init_alloc(
uint64_t satoshi,
const unsigned char *script,
size_t script_len,
struct wally_tx_output **output);
/**
* Create a new copy of a transaction output.
*
* :param tx_output_in: The transaction output to clone.
* :param output: Destination for the resulting transaction output copy.
*/
WALLY_CORE_API int wally_tx_output_clone_alloc(
const struct wally_tx_output *tx_output_in,
struct wally_tx_output **output);
/**
* Create a new copy of a transaction output in place.
*
* :param tx_output_in: The transaction output to clone.
* :param output: Destination for the resulting transaction output copy.
*
* .. note:: ``output`` is overwritten in place, and not cleared first.
*/
WALLY_CORE_API int wally_tx_output_clone(
const struct wally_tx_output *tx_output_in,
struct wally_tx_output *output);
/**
* Free a transaction output allocated by `wally_tx_output_init_alloc`.
*
* :param output: The transaction output to free.
*/
WALLY_CORE_API int wally_tx_output_free(struct wally_tx_output *output);
/**
* Allocate and initialize a new transaction.
*
* :param version: The version of the transaction.
* :param locktime: The locktime of the transaction.
* :param inputs_allocation_len: The number of inputs to pre-allocate space for.
* :param outputs_allocation_len: The number of outputs to pre-allocate space for.
* :param output: Destination for the resulting transaction.
*/
WALLY_CORE_API int wally_tx_init_alloc(
uint32_t version,
uint32_t locktime,
size_t inputs_allocation_len,
size_t outputs_allocation_len,
struct wally_tx **output);
/**
* Create a new copy of a transaction.
*
* :param tx: The transaction to clone.
* :param flags: :ref:`tx-clone` controlling new transaction creation.
* :param output: Destination for the resulting transaction copy.
*/
WALLY_CORE_API int wally_tx_clone_alloc(
const struct wally_tx *tx,
uint32_t flags,
struct wally_tx **output);
/**
* Add a transaction input to a transaction.
*
* :param tx: The transaction to add the input to.
* :param input: The transaction input to add to ``tx``.
*/
WALLY_CORE_API int wally_tx_add_input(
struct wally_tx *tx,
const struct wally_tx_input *input);
/**
* Add a transaction input to a transaction at a given position.
*
* :param tx: The transaction to add the input to.
* :param index: The zero-based index of the position to add the input at.
* :param input: The transaction input to add to ``tx``.
*/
WALLY_CORE_API int wally_tx_add_input_at(
struct wally_tx *tx,
uint32_t index,
const struct wally_tx_input *input);
/**
* Add a transaction input to a transaction.
*
* :param tx: The transaction to add the input to.
* :param txhash: The transaction hash of the transaction this input comes from.
* :param txhash_len: Size of ``txhash`` in bytes. Must be `WALLY_TXHASH_LEN`.
* :param utxo_index: The zero-based index of the transaction output in ``txhash`` that
*| this input comes from.
* :param sequence: The sequence number for the input.
* :param script: The scriptSig for the input.
* :param script_len: Size of ``script`` in bytes.
* :param witness: The witness stack for the input, or NULL if no witness is present.
* :param flags: Flags controlling input creation. Must be 0.
*/
WALLY_CORE_API int wally_tx_add_raw_input(
struct wally_tx *tx,
const unsigned char *txhash,
size_t txhash_len,
uint32_t utxo_index,
uint32_t sequence,
const unsigned char *script,
size_t script_len,
const struct wally_tx_witness_stack *witness,
uint32_t flags);
/**
* Add a transaction input to a transaction in a given position.
*
* :param tx: The transaction to add the input to.
* :param index: The zero-based index of the position to add the input at.
* :param txhash: The transaction hash of the transaction this input comes from.
* :param txhash_len: Size of ``txhash`` in bytes. Must be `WALLY_TXHASH_LEN`.
* :param utxo_index: The zero-based index of the transaction output in ``txhash`` that
*| this input comes from.
* :param sequence: The sequence number for the input.
* :param script: The scriptSig for the input.
* :param script_len: Size of ``script`` in bytes.
* :param witness: The witness stack for the input, or NULL if no witness is present.
* :param flags: Flags controlling input creation. Must be 0.
*/
WALLY_CORE_API int wally_tx_add_raw_input_at(
struct wally_tx *tx,
uint32_t index,
const unsigned char *txhash,
size_t txhash_len,
uint32_t utxo_index,
uint32_t sequence,
const unsigned char *script,
size_t script_len,
const struct wally_tx_witness_stack *witness,
uint32_t flags);
/**
* Remove a transaction input from a transaction.
*
* :param tx: The transaction to remove the input from.
* :param index: The zero-based index of the input to remove.
*/
WALLY_CORE_API int wally_tx_remove_input(
struct wally_tx *tx,
size_t index);
/**
* Set the scriptsig for an input in a transaction.
*
* :param tx: The transaction to operate on.
* :param index: The zero-based index of the input to set the script on.
* :param script: The scriptSig for the input.
* :param script_len: Size of ``script`` in bytes.
*/
WALLY_CORE_API int wally_tx_set_input_script(
const struct wally_tx *tx,
size_t index,
const unsigned char *script,
size_t script_len);
/**
* Set the witness stack for an input in a transaction.
*
* :param tx: The transaction to operate on.
* :param index: The zero-based index of the input to set the witness stack on.
* :param stack: The transaction witness stack to set.
*/
WALLY_CORE_API int wally_tx_set_input_witness(
const struct wally_tx *tx,
size_t index,
const struct wally_tx_witness_stack *stack);
/**
* Add a transaction output to a transaction.
*
* :param tx: The transaction to add the output to.
* :param output: The transaction output to add to ``tx``.
*/
WALLY_CORE_API int wally_tx_add_output(
struct wally_tx *tx,
const struct wally_tx_output *output);
/**
* Add a transaction output to a transaction at a given position.
*
* :param tx: The transaction to add the output to.
* :param index: The zero-based index of the position to add the output at.
* :param output: The transaction output to add to ``tx``.
*/
WALLY_CORE_API int wally_tx_add_output_at(
struct wally_tx *tx,
uint32_t index,
const struct wally_tx_output *output);
/**
* Add a transaction output to a transaction.
*
* :param tx: The transaction to add the output to.
* :param satoshi: The amount of the output in satoshi.
* :param script: The scriptPubkey for the output.
* :param script_len: Size of ``script`` in bytes.
* :param flags: Flags controlling output creation. Must be 0.
*/
WALLY_CORE_API int wally_tx_add_raw_output(
struct wally_tx *tx,
uint64_t satoshi,
const unsigned char *script,
size_t script_len,
uint32_t flags);
/**
* Add a transaction output to a transaction at a given position.
*
* :param tx: The transaction to add the output to.
* :param index: The zero-based index of the position to add the output at.
* :param satoshi: The amount of the output in satoshi.
* :param script: The scriptPubkey for the output.
* :param script_len: Size of ``script`` in bytes.
* :param flags: Flags controlling output creation. Must be 0.
*/
WALLY_CORE_API int wally_tx_add_raw_output_at(
struct wally_tx *tx,
uint32_t index,
uint64_t satoshi,
const unsigned char *script,
size_t script_len,
uint32_t flags);
/**
* Remove a transaction output from a transaction.
*
* :param tx: The transaction to remove the output from.
* :param index: The zero-based index of the output to remove.
*/
WALLY_CORE_API int wally_tx_remove_output(
struct wally_tx *tx,
size_t index);
/**
* Get the number of inputs in a transaction that have witness data.
*
* :param tx: The transaction to get the witnesses count from.
* :param written: Destination for the number of witness-containing inputs.
*/
WALLY_CORE_API int wally_tx_get_witness_count(
const struct wally_tx *tx,
size_t *written);
/**
* Free a transaction allocated by `wally_tx_init_alloc`.
*
* :param tx: The transaction to free.
*/
WALLY_CORE_API int wally_tx_free(struct wally_tx *tx);
/**
* Return the txid of a transaction.
*
* :param tx: The transaction to compute the txid of.
* :param bytes_out: Destination for the txid.
* FIXED_SIZED_OUTPUT(len, bytes_out, WALLY_TXHASH_LEN)
*
* .. note:: The txid is expensive to compute.
*/
WALLY_CORE_API int wally_tx_get_txid(
const struct wally_tx *tx,
unsigned char *bytes_out,
size_t len);
/**
* Calculate the BIP 143 hashPrevouts of a list of input txids and output indices.
*
* :param txhashes: The input txids to compute the hash from.
* :param txhashes_len: Length of ``txhashes`` in bytes. Must be a multiple of `WALLY_TXHASH_LEN`.
* :param utxo_indices: The output indices of the txids in ``txhashes``.
* :param num_utxo_indices: The number of output indices in ``utxo_indices``. You must
*| pass one index for every txhash in ``txhashes``.
* :param bytes_out: Destination for the hashPrevouts bytes.
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
*/
WALLY_CORE_API int wally_get_hash_prevouts(
const unsigned char *txhashes,
size_t txhashes_len,
const uint32_t *utxo_indices,
size_t num_utxo_indices,
unsigned char *bytes_out,
size_t len);
/**
* Return the BIP 143 hashPrevouts of a transaction.
*
* :param tx: The transaction to compute the hashPrevouts of.
* :param index: The zero-based index of the input to start hashing from.
*| Pass 0 to start from the first input.
* :param num_inputs: The number of inputs to hash starting from the first.
*| If ``index`` is given as 0, you can pass 0xffffffff to use all inputs.
* :param bytes_out: Destination for the hashPrevouts bytes.
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
*
* .. note:: The hash is computed without reference to any sighash flags,
*| and so will not match BIP143 for `WALLY_SIGHASH_ANYONECANPAY`.
*/
WALLY_CORE_API int wally_tx_get_hash_prevouts(
const struct wally_tx *tx,
size_t index,
size_t num_inputs,
unsigned char *bytes_out,
size_t len);
/**
* Return the length of transaction once serialized into bytes.
*
* :param tx: The transaction to find the serialized length of.
* :param flags: ``WALLY_TX_FLAG_`` Flags controlling serialization options.
* :param written: Destination for the length of the serialized bytes.
*/
WALLY_CORE_API int wally_tx_get_length(
const struct wally_tx *tx,
uint32_t flags,
size_t *written);
/**
* Create a transaction from its serialized bytes.
*
* :param bytes: Bytes to create the transaction from.
* :param bytes_len: Length of ``bytes`` in bytes.
* :param flags: ``WALLY_TX_FLAG_`` Flags controlling serialization options.
* :param output: Destination for the resulting transaction.
*/
WALLY_CORE_API int wally_tx_from_bytes(
const unsigned char *bytes,
size_t bytes_len,
uint32_t flags,
struct wally_tx **output);
/**
* Create a transaction from its serialized bytes in hexadecimal.
*
* :param hex: Hexadecimal string containing the transaction.
* :param flags: ``WALLY_TX_FLAG_`` Flags controlling serialization options.
* :param output: Destination for the resulting transaction.
*/
WALLY_CORE_API int wally_tx_from_hex(
const char *hex,
uint32_t flags,
struct wally_tx **output);
/**
* Serialize a transaction to bytes.
*
* :param tx: The transaction to serialize.
* :param flags: ``WALLY_TX_FLAG_`` Flags controlling serialization options.
* :param bytes_out: Destination for the serialized transaction.
* :param len: Size of ``bytes_out`` in bytes.
* :param written: Destination for the length of the serialized transaction.
*/
WALLY_CORE_API int wally_tx_to_bytes(
const struct wally_tx *tx,
uint32_t flags,
unsigned char *bytes_out,
size_t len,
size_t *written);
/**
* Serialize a transaction to hex.
*
* :param tx: The transaction to serialize.
* :param flags: ``WALLY_TX_FLAG_`` Flags controlling serialization options.
* :param output: Destination for the resulting hexadecimal string.
*
* .. note:: The string returned should be freed using `wally_free_string`.
*/
WALLY_CORE_API int wally_tx_to_hex(
const struct wally_tx *tx,
uint32_t flags,
char **output);
/**
* Get the weight of a transaction.
*
* :param tx: The transaction to get the weight of.
* :param written: Destination for the weight.
*/
WALLY_CORE_API int wally_tx_get_weight(
const struct wally_tx *tx,
size_t *written);
/**
* Get the virtual size of a transaction.
*
* :param tx: The transaction to get the virtual size of.
* :param written: Destination for the virtual size.
*/
WALLY_CORE_API int wally_tx_get_vsize(
const struct wally_tx *tx,
size_t *written);
/**
* Compute transaction vsize from transaction weight.
*
* :param weight: The weight to convert to a virtual size.
* :param written: Destination for the virtual size.
*/
WALLY_CORE_API int wally_tx_vsize_from_weight(
size_t weight,
size_t *written);
/**
* Compute the total sum of all outputs in a transaction.
*
* :param tx: The transaction to compute the total from.
* :param value_out: Destination for the output total.
*/
WALLY_CORE_API int wally_tx_get_total_output_satoshi(
const struct wally_tx *tx,
uint64_t *value_out);
/**
* Get the hash of the preimage for signing a BTC transaction input.
*
* Deprecated, this call will be removed in a future release. Please
* use ``wally_tx_get_input_signature_hash``.
*
* :param tx: The transaction to generate the signature hash from.
* :param index: The input index of the input being signed for.
* :param script: The (unprefixed) scriptCode for the input being signed.
* :param script_len: Size of ``script`` in bytes.
* :param satoshi: The amount spent by the input being signed for. Only used if
*| flags includes `WALLY_TX_FLAG_USE_WITNESS`, pass 0 otherwise.
* :param sighash: ``WALLY_SIGHASH_`` flags specifying the type of signature desired.
* :param flags: `WALLY_TX_FLAG_USE_WITNESS` to generate a BIP 143 signature, or 0
*| to generate a pre-segwit Bitcoin signature.
* :param bytes_out: Destination for the signature hash.
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
*/
WALLY_CORE_API int wally_tx_get_btc_signature_hash(
const struct wally_tx *tx,
size_t index,
const unsigned char *script,
size_t script_len,
uint64_t satoshi,
uint32_t sighash,
uint32_t flags,
unsigned char *bytes_out,
size_t len);
/**
* Get the hash of the preimage for signing a BTC taproot transaction input.
*
* Deprecated, this call will be removed in a future release. Please
* use ``wally_tx_get_input_signature_hash``.
*
* :param tx: The transaction to generate the signature hash from.
* :param index: The input index of the input being signed for.
* :param scripts: Map of input index to (unprefixed) scriptCodes for each input in ``tx``.
* :param values: The value in satoshi for each input in ``tx``.
* :param num_values: The number of elements in ``values``.
* :param tapleaf_script: BIP342 tapscript being spent.
* :param tapleaf_script_len: Length of ``tapleaf_script`` in bytes.
* :param key_version: Version of pubkey in tapscript. Must be set to 0x00 or 0x01.
* :param codesep_position: BIP342 codeseparator position or ``WALLY_NO_CODESEPARATOR`` if none.
* :param annex: BIP341 annex, or NULL if none.
* :param annex_len: Length of ``annex`` in bytes.
* :param sighash: ``WALLY_SIGHASH_`` flags specifying the type of signature desired.
* :param flags: Flags controlling signature generation. Must be 0.
* :param bytes_out: Destination for the resulting signature hash.
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
*/
WALLY_CORE_API int wally_tx_get_btc_taproot_signature_hash(
const struct wally_tx *tx,
size_t index,
const struct wally_map *scripts,
const uint64_t *values,
size_t num_values,
const unsigned char *tapleaf_script,
size_t tapleaf_script_len,
uint32_t key_version,
uint32_t codesep_position,
const unsigned char *annex,
size_t annex_len,
uint32_t sighash,
uint32_t flags,
unsigned char *bytes_out,
size_t len);
/**
* Get the hash of the preimage for signing a BTC transaction input.
*
* Deprecated, this call will be removed in a future release. Please
* use ``wally_tx_get_input_signature_hash``.
*
* :param tx: The transaction to generate the signature hash from.
* :param index: The input index of the input being signed for.
* :param script: The (unprefixed) scriptCode for the input being signed.
* :param script_len: Size of ``script`` in bytes.
* :param extra: Extra bytes to include in the transaction preimage.
* :param extra_len: Size of ``extra`` in bytes.
* :param extra_offset: Offset within the preimage to store ``extra``. To store
*| it at the end of the preimage, use 0xffffffff.
* :param satoshi: The amount spent by the input being signed for. Only used if
*| flags includes `WALLY_TX_FLAG_USE_WITNESS`, pass 0 otherwise.
* :param sighash: ``WALLY_SIGHASH_`` flags specifying the type of signature desired.
* :param tx_sighash: The 32bit sighash value to include in the preimage to hash.
*| This must be given in host CPU endianness; For normal Bitcoin signing
*| the value of ``sighash`` should be given.
* :param flags: `WALLY_TX_FLAG_USE_WITNESS` to generate a BIP 143 signature, or 0
*| to generate a pre-segwit Bitcoin signature.
* :param bytes_out: Destination for the signature hash.
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
*/
WALLY_CORE_API int wally_tx_get_signature_hash(
const struct wally_tx *tx,
size_t index,
const unsigned char *script,
size_t script_len,
const unsigned char *extra,
size_t extra_len,
uint32_t extra_offset,
uint64_t satoshi,
uint32_t sighash,
uint32_t tx_sighash,
uint32_t flags,
unsigned char *bytes_out,
size_t len);
/**
* Get the hash of the preimage for signing a transaction input.
*
* :param tx: The transaction to generate the signature hash from.
* :param index: The input index of the input being signed for.
* :param scripts: The scriptpubkeys of each input in the transaction, indexed
*| by their 0-based input index. For non-taproot signing, only the
*| scriptpubkey of ``index`` is required.
* :param assets: The asset commitments of each input in the transaction,
*| or NULL for non-Elements transactions. Ignored for non-taproot signing.
* :param values: The satoshi values(BTC) or value commitments(Elements) of
*| each input in the transaction. BTC values must be stored as bytes with
*| uint64/host endiannes. For non-taproot signing, only the value
*| of ``index`` is required.
* :param script: For segwit v0 signing, the scriptcode of the input to sign
*| for. For taproot, the leaf script to sign with if any. Ignored for
*| pre-segwit signing.
* :param script_len: Length of ``script`` in bytes.
* :param key_version: For taproot signing, the version of the pubkey
*| in ``script`` when signing with a script path. Currently must be ``1``
*| for this case. For non-taproot or keypath signing, it must be ``0``.
* :param codesep_position: BIP342 codeseparator position
*| or ``WALLY_NO_CODESEPARATOR`` if none. Only used for taproot signing.
* :param annex: BIP341 annex, or NULL if none.
* :param annex_len: Length of ``annex`` in bytes. Only used for taproot signing.
* :param genesis_blockhash: The genesis blockhash of the chain to sign for,
*| or NULL for non-Elements transactions. Only used for taproot signing.
* :param genesis_blockhash_len: Length of ``genesis_blockhash`` in bytes. Must
*| be `SHA256_LEN` or 0.
* :param sighash: ``WALLY_SIGHASH_`` flags specifying the sighash flags
*| to sign with.
* :param flags: :ref:`tx-sighash-type` controlling signature hash generation.
* :param cache: An opaque cache for faster generation, or NULL to disable
*| caching. Must be empty on the first call to this function for a given
*| transaction, and only used for signing the inputs of the same ``tx``.
* :param bytes_out: Destination for the resulting signature hash.
* FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN)
*/
WALLY_CORE_API int wally_tx_get_input_signature_hash(
const struct wally_tx *tx,
size_t index,
const struct wally_map *scripts,
const struct wally_map *assets,
const struct wally_map *values,
const unsigned char *script,
size_t script_len,
uint32_t key_version,
uint32_t codesep_position,
const unsigned char *annex,
size_t annex_len,
const unsigned char *genesis_blockhash,
size_t genesis_blockhash_len,
uint32_t sighash,
uint32_t flags,
struct wally_map *cache,
unsigned char *bytes_out,
size_t len);
/**
* Determine if a transaction is a coinbase transaction.
*
* :param tx: The transaction to check.
* :param written: 1 if the transaction is a coinbase transaction, otherwise 0.
*/
WALLY_CORE_API int wally_tx_is_coinbase(
const struct wally_tx *tx,
size_t *written);
#ifndef WALLY_ABI_NO_ELEMENTS
/**
* Calculate any applicable transaction weight discount for an Elements transaction.
*
* :param tx: The transaction to compute the weight discount for.
* :param flags: Unused, must be 0.
* :param written: Destination for the weight discount.
*
* .. note:: The discount may be 0 if the transaction has no confidential outputs.
*/
WALLY_CORE_API int wally_tx_get_elements_weight_discount(
const struct wally_tx *tx,
uint32_t flags,
size_t *written);
/**
* Set issuance data on an input.
*
* :param input: The input to add to.
* :param nonce: Asset issuance or revelation blinding factor.
* :param nonce_len: Size of ``nonce`` in bytes. Must be `SHA256_LEN`.
* :param entropy: Entropy for the asset tag calculation.
* :param entropy_len: Size of ``entropy`` in bytes. Must be `SHA256_LEN`.
* :param issuance_amount: The (blinded) issuance amount.
* :param issuance_amount_len: Size of ``issuance_amount`` in bytes.
* :param inflation_keys: The (blinded) token reissuance amount.
* :param inflation_keys_len: Size of ``ìnflation_keys`` in bytes.
* :param issuance_amount_rangeproof: Issuance amount rangeproof.
* :param issuance_amount_rangeproof_len: Size of ``issuance_amount_rangeproof`` in bytes.
* :param inflation_keys_rangeproof: Inflation keys rangeproof.
* :param inflation_keys_rangeproof_len: Size of ``inflation_keys_rangeproof`` in bytes.
*/
WALLY_CORE_API int wally_tx_elements_input_issuance_set(
struct wally_tx_input *input,
const unsigned char *nonce,
size_t nonce_len,
const unsigned char *entropy,
size_t entropy_len,
const unsigned char *issuance_amount,
size_t issuance_amount_len,
const unsigned char *inflation_keys,
size_t inflation_keys_len,
const unsigned char *issuance_amount_rangeproof,
size_t issuance_amount_rangeproof_len,
const unsigned char *inflation_keys_rangeproof,
size_t inflation_keys_rangeproof_len);
/**
* Free issuance data on an input.
*
* :param input: The input issuance data to free.