forked from dogecoinfoundation/libdogecoin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtx.c
1146 lines (998 loc) · 34.5 KB
/
tx.c
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
/*
The MIT License (MIT)
Copyright (c) 2015 Douglas J. Bakkum
Copyright (c) 2015 Jonas Schnelli
Copyright (c) 2022 bluezr
Copyright (c) 2022 The Dogecoin Foundation
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include <assert.h>
#include <inttypes.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <dogecoin/base58.h>
#include <dogecoin/ecc.h>
#include <dogecoin/sha2.h>
#include <dogecoin/mem.h>
#include <dogecoin/serialize.h>
#include <dogecoin/tx.h>
#include <dogecoin/utils.h>
/**
* @brief This function frees the memory allocated
* for a transaction input.
*
* @param tx_in The pointer to the transaction input to be freed.
*
* @return Nothing.
*/
void dogecoin_tx_in_free(dogecoin_tx_in* tx_in)
{
if (!tx_in)
return;
dogecoin_mem_zero(&tx_in->prevout.hash, sizeof(tx_in->prevout.hash));
tx_in->prevout.n = 0;
if (tx_in->script_sig) {
cstr_free(tx_in->script_sig, true);
tx_in->script_sig = NULL;
}
dogecoin_mem_zero(tx_in, sizeof(*tx_in));
dogecoin_free(tx_in);
}
/**
* @brief This function casts data from a channel buffer to
* a transaction input object and then frees it by calling
* dogecoin_tx_in_free().
*
* @param data The pointer to the data to be freed.
*
* @return Nothing.
*/
void dogecoin_tx_in_free_cb(void* data)
{
if (!data) {
return;
}
dogecoin_tx_in* tx_in = data;
dogecoin_tx_in_free(tx_in);
}
/**
* @brief This function creates a new dogecoin transaction
* input object and initializes it to all zeroes.
*
* @return A pointer to the new transaction input object.
*/
dogecoin_tx_in* dogecoin_tx_in_new()
{
dogecoin_tx_in* tx_in;
tx_in = dogecoin_calloc(1, sizeof(*tx_in));
dogecoin_mem_zero(&tx_in->prevout, sizeof(tx_in->prevout));
tx_in->sequence = UINT32_MAX;
return tx_in;
}
/**
* @brief This function frees the memory allocated
* for a transaction output.
*
* @param tx_in The pointer to the transaction output to be freed.
*
* @return Nothing.
*/
void dogecoin_tx_out_free(dogecoin_tx_out* tx_out)
{
if (!tx_out) {
return;
}
tx_out->value = 0;
if (tx_out->script_pubkey) {
cstr_free(tx_out->script_pubkey, true);
tx_out->script_pubkey = NULL;
}
dogecoin_mem_zero(tx_out, sizeof(*tx_out));
dogecoin_free(tx_out);
}
/**
* @brief This function casts data from a channel buffer to
* a transaction output object and then frees it by calling
* dogecoin_tx_out_free().
*
* @param data The pointer to the data to be freed.
*
* @return Nothing.
*/
void dogecoin_tx_out_free_cb(void* data)
{
if (!data) {
return;
}
dogecoin_tx_out* tx_out = data;
dogecoin_tx_out_free(tx_out);
}
/**
* @brief This function creates a new dogecoin transaction
* output object and initializes it to all zeroes.
*
* @return A pointer to the new transaction output object.
*/
dogecoin_tx_out* dogecoin_tx_out_new()
{
dogecoin_tx_out* tx_out;
tx_out = dogecoin_calloc(1, sizeof(*tx_out));
return tx_out;
}
/**
* @brief This function frees the memory allocated
* for a full transaction.
*
* @param tx_in The pointer to the transaction to be freed.
*
* @return Nothing.
*/
void dogecoin_tx_free(dogecoin_tx* tx)
{
if (tx->vin) {
vector_free(tx->vin, true);
tx->vin = NULL;
}
if (tx->vout) {
vector_free(tx->vout, true);
tx->vout = NULL;
}
dogecoin_free(tx);
}
/**
* It takes a pointer to a dogecoin_tx_out, copies to a new dogecoin_tx_out
* converts dogecoin_tx_out->script_pubkey to a p2pkh address, and
* frees the copy.
*
* @param txout The data to be copied which contains the script hash we want.
* @param p2pkh The variable out we want to contain the converted script hash in.
*
* @return int
*/
int dogecoin_script_hash_to_p2pkh(dogecoin_tx_out* txout, char* p2pkh, int is_testnet) {
if (!txout) return false;
dogecoin_tx_out* copy = dogecoin_tx_out_new();
dogecoin_tx_out_copy(copy, txout);
size_t length = 2;
uint8_t* stripped_array[txout->script_pubkey->len];
dogecoin_mem_zero(stripped_array, sizeof(stripped_array));
// loop through 20 bytes of the script hash while stripping op codes
// and copy from index 2 to 21 after prefixing with version
// from chainparams:
for (; length < copy->script_pubkey->len - 4; length++) {
switch (copy->script_pubkey->str[length]) {
case OP_DUP:
break;
case (char)OP_HASH160:
break;
case (char)OP_EQUALVERIFY:
break;
case (char)OP_CHECKSIG:
break;
default:
copy->script_pubkey->str[2] = is_testnet ? 0x1e : 0x71;
memccpy(stripped_array, ©->script_pubkey->str[2], 2, 21);
break;
}
}
unsigned char d1[SHA256_DIGEST_LENGTH], checksum[4], unencoded_address[25];
// double sha256 stripped array into d1:
dogecoin_dblhash((const unsigned char *)stripped_array, strlen((const char *)stripped_array), d1);
// copy check sum (4 bytes) into checksum var:
memcpy(checksum, d1, 4);
// copy stripped array into final var before passing to out variable:
memcpy(unencoded_address, stripped_array, 21);
// copy checksum to the last 4 bytes of our unencoded_address:
unencoded_address[21] = checksum[0];
unencoded_address[22] = checksum[1];
unencoded_address[23] = checksum[2];
unencoded_address[24] = checksum[3];
size_t strsize = 35;
char script_hash_to_p2pkh[strsize];
// base 58 encode check our unencoded_address into the script_hash_to_p2pkh:
if (!dogecoin_base58_encode_check(unencoded_address, 21, script_hash_to_p2pkh, strsize)) {
return false;
}
debug_print("doublesha: %s\n", utils_uint8_to_hex(d1, sizeof(d1)));
debug_print("checksum: %s\n", utils_uint8_to_hex(checksum, sizeof(checksum)));
debug_print("unencoded_address: %s\n", utils_uint8_to_hex(unencoded_address, sizeof(unencoded_address)));
debug_print("scripthash2p2pkh: %s\n", script_hash_to_p2pkh);
// copy to out variable p2pkh, free tx_out copy and return true:
memcpy(p2pkh, script_hash_to_p2pkh, sizeof(script_hash_to_p2pkh));
dogecoin_tx_out_free(copy);
return memcmp(p2pkh, script_hash_to_p2pkh, strlen(script_hash_to_p2pkh)) == 0;
}
/**
* It takes a p2pkh address and converts it to a compressed public key in
* hexadecimal format. It then strips the network prefix and checksum and
* prepends OP_DUP and OP_HASH160 and appends OP_EQUALVERIFY and OP_CHECKSIG.
*
* @param p2pkh The variable out we want to contain the converted script hash in.
*
* @return int
*/
char* dogecoin_p2pkh_to_script_hash(char* p2pkh) {
if (!p2pkh) return false;
size_t len = 25;
unsigned char* dec[len];
if (!dogecoin_base58_decode_check(p2pkh, (uint8_t*)&dec, 35)) {
printf("failed base58 decode\n");
return false;
}
char* b58_decode_hex = utils_uint8_to_hex((const uint8_t*)dec, len - 4);
char* tmp = dogecoin_malloc(51);
char opcodes_and_pubkey_length_to_prepend[7], opcodes_to_append[5];
sprintf(opcodes_and_pubkey_length_to_prepend, "%x%x%x", OP_DUP, OP_HASH160, 20);
sprintf(opcodes_to_append, "%x%x", OP_EQUALVERIFY, OP_CHECKSIG);
for (size_t l = 0; l < 4; l += 2) {
if (l == 2) {
memccpy(tmp, &b58_decode_hex[l], 3, 48);
prepend(tmp, opcodes_and_pubkey_length_to_prepend);
append(tmp, opcodes_to_append);
}
}
return tmp;
}
/**
* It takes in a private key (WIF encoded) and a flag to indicate the correct network
* needed in order to select the correct base58 prefix from chainparams and then decodes
* the private key into a dogecoin_key struct which is then encoded. Then it derives the
* public hexadecimal key and generates the corresponding p2pkh address. Finally it converts
* the p2pkh to a script pubkey hash, frees the previous structs from memory and returns the
* script pubkey hash.
*
* @param p2pkh The variable out we want to contain the converted script hash in.
*
* @return char* The script public key hash.
*/
char* dogecoin_private_key_wif_to_script_hash(char* private_key_wif) {
if (!private_key_wif) {
return false;
}
const dogecoin_chainparams* chain = (private_key_wif[0] == 'c') ? &dogecoin_chainparams_test : &dogecoin_chainparams_main;
size_t sizeout = 53;
/* private key */
dogecoin_key key;
dogecoin_privkey_init(&key);
dogecoin_privkey_decode_wif(private_key_wif, chain, &key);
if (!dogecoin_privkey_is_valid(&key)) {
debug_print("private key is not valid!\nchain: %s\n", chain->chainname);
return false;
}
char new_wif_privkey[sizeout];
dogecoin_privkey_encode_wif(&key, chain, new_wif_privkey, &sizeout);
/* public key */
dogecoin_pubkey pubkey;
dogecoin_pubkey_init(&pubkey);
dogecoin_pubkey_from_key(&key, &pubkey);
if (!dogecoin_pubkey_is_valid(&pubkey)) {
debug_print("pubkey is not valid!\nchain: %s\n", chain->chainname);
return false;
}
char new_p2pkh_pubkey[sizeout];
dogecoin_pubkey_getaddr_p2pkh(&pubkey, chain, new_p2pkh_pubkey);
dogecoin_privkey_cleanse(&key);
dogecoin_pubkey_cleanse(&pubkey);
return dogecoin_p2pkh_to_script_hash(new_p2pkh_pubkey);
}
/**
* @brief This function creates a new dogecoin transaction
* object and initializes it to all zeroes except for the
* version, which is set to 1.
*
* @return A pointer to the new transaction object.
*/
dogecoin_tx* dogecoin_tx_new()
{
dogecoin_tx* tx;
tx = dogecoin_calloc(1, sizeof(*tx));
tx->vin = vector_new(8, dogecoin_tx_in_free_cb);
tx->vout = vector_new(8, dogecoin_tx_out_free_cb);
tx->version = 1;
tx->locktime = 0;
return tx;
}
/**
* @brief This function takes information from a buffer
* and deserializes it into a transaction input object.
*
* @param tx_in The pointer to the transaction input to deserialize into.
* @param buf The pointer to the buffer containing the transaction input information.
*
* @return 1 if deserialized successfully, 0 otherwise.
*/
dogecoin_bool dogecoin_tx_in_deserialize(dogecoin_tx_in* tx_in, struct const_buffer* buf)
{
deser_u256(tx_in->prevout.hash, buf);
if (!deser_u32(&tx_in->prevout.n, buf)) {
return false;
}
if (!deser_varstr(&tx_in->script_sig, buf)) {
return false;
}
if (!deser_u32(&tx_in->sequence, buf)) {
return false;
}
return true;
}
/**
* @brief This function takes information from a buffer
* and deserializes it into a transaction output object.
*
* @param tx_in The pointer to the transaction output to deserialize into.
* @param buf The pointer to the buffer containing the transaction output information.
*
* @return 1 if deserialized successfully, 0 otherwise.
*/
dogecoin_bool dogecoin_tx_out_deserialize(dogecoin_tx_out* tx_out, struct const_buffer* buf)
{
if (!deser_s64(&tx_out->value, buf)) {
return false;
}
if (!deser_varstr(&tx_out->script_pubkey, buf)) {
return false;
}
return true;
}
/**
* @brief This function takes information from a string
* and deserializes it into a full transaction object.
*
* @param tx_serialized The string containing the transaction information.
* @param inlen The length of the string to be read.
* @param tx The pointer to the transaction object to be deserialized into.
* @param consumed_length The pointer to the total number of characters successfully deserialized.
*
* @return 1 if deserialized successfully, 0 otherwise.
*/
int dogecoin_tx_deserialize(const unsigned char* tx_serialized, size_t inlen, dogecoin_tx* tx, size_t* consumed_length)
{
struct const_buffer buf = {tx_serialized, inlen};
if (consumed_length) {
*consumed_length = 0;
}
//tx needs to be initialized
deser_s32(&tx->version, &buf);
uint32_t vlen;
if (!deser_varlen(&vlen, &buf)) {
return false;
}
unsigned int i;
for (i = 0; i < vlen; i++) {
dogecoin_tx_in* tx_in = dogecoin_tx_in_new();
if (!dogecoin_tx_in_deserialize(tx_in, &buf)) {
dogecoin_tx_in_free(tx_in);
return false;
} else {
vector_add(tx->vin, tx_in);
}
}
if (!deser_varlen(&vlen, &buf))
return false;
for (i = 0; i < vlen; i++) {
dogecoin_tx_out* tx_out = dogecoin_tx_out_new();
if (!dogecoin_tx_out_deserialize(tx_out, &buf)) {
dogecoin_free(tx_out);
return false;
} else {
vector_add(tx->vout, tx_out);
}
}
if (!deser_u32(&tx->locktime, &buf)) {
return false;
}
if (consumed_length) {
*consumed_length = inlen - buf.len;
}
return true;
}
/**
* @brief This function serializes a transaction input.
*
* @param s The pointer to the cstring to serialize the data into.
* @param tx_in The pointer to the transaction input to serialize.
*
* @return Nothing.
*/
void dogecoin_tx_in_serialize(cstring* s, const dogecoin_tx_in* tx_in)
{
ser_u256(s, tx_in->prevout.hash);
ser_u32(s, tx_in->prevout.n);
ser_varstr(s, tx_in->script_sig);
ser_u32(s, tx_in->sequence);
}
/**
* @brief This function serializes a transaction output.
*
* @param s The pointer to the cstring to serialize the data into.
* @param tx_out The pointer to the transaction output to serialize.
*
* @return Nothing.
*/
void dogecoin_tx_out_serialize(cstring* s, const dogecoin_tx_out* tx_out)
{
ser_s64(s, tx_out->value);
ser_varstr(s, tx_out->script_pubkey);
}
/**
* @brief This function serializes a full transaction.
*
* @param s The pointer to the cstring to serialize the data into.
* @param tx The pointer to the transaction to serialize.
*
* @return Nothing.
*/
void dogecoin_tx_serialize(cstring* s, const dogecoin_tx* tx)
{
ser_s32(s, tx->version);
ser_varlen(s, tx->vin ? tx->vin->len : 0);
unsigned int i;
if (tx->vin) {
for (i = 0; i < tx->vin->len; i++) {
dogecoin_tx_in* tx_in;
tx_in = vector_idx(tx->vin, i);
dogecoin_tx_in_serialize(s, tx_in);
}
}
ser_varlen(s, tx->vout ? tx->vout->len : 0);
if (tx->vout) {
for (i = 0; i < tx->vout->len; i++) {
dogecoin_tx_out* tx_out;
tx_out = vector_idx(tx->vout, i);
dogecoin_tx_out_serialize(s, tx_out);
}
}
ser_u32(s, tx->locktime);
}
/**
* @brief This function performs a double SHA256 hash
* on a given transaction.
*
* @param tx The pointer to the transaction to hash.
* @param hashout The result of the hashing operation.
*
* @return Nothing.
*/
void dogecoin_tx_hash(const dogecoin_tx* tx, uint256 hashout)
{
cstring* txser = cstr_new_sz(1024);
dogecoin_tx_serialize(txser, tx);
sha256_raw((const uint8_t*)txser->str, txser->len, hashout);
sha256_raw(hashout, DOGECOIN_HASH_LENGTH, hashout);
cstr_free(txser, true);
}
/**
* @brief This function makes a copy of a given transaction
* input object.
*
* @param dest The pointer to the copy of the transaction input.
* @param src The pointer to the original transaction input.
*
* @return Nothing.
*/
void dogecoin_tx_in_copy(dogecoin_tx_in* dest, const dogecoin_tx_in* src)
{
memcpy_safe(&dest->prevout, &src->prevout, sizeof(dest->prevout));
dest->sequence = src->sequence;
if (!src->script_sig) {
dest->script_sig = NULL;
} else {
dest->script_sig = cstr_new_sz(src->script_sig->len);
cstr_append_buf(dest->script_sig,
src->script_sig->str,
src->script_sig->len);
}
}
/**
* @brief This function makes a copy of a given transaction
* output object.
*
* @param dest The pointer to the copy of the transaction output.
* @param src The pointer to the original transaction output.
*
* @return Nothing.
*/
void dogecoin_tx_out_copy(dogecoin_tx_out* dest, const dogecoin_tx_out* src)
{
dest->value = src->value;
if (!src->script_pubkey) {
dest->script_pubkey = NULL;
} else {
dest->script_pubkey = cstr_new_sz(src->script_pubkey->len);
cstr_append_buf(dest->script_pubkey,
src->script_pubkey->str,
src->script_pubkey->len);
}
}
/**
* @brief This function makes a copy of a given transaction
* object.
*
* @param dest The pointer to the copy of the transaction.
* @param src The pointer to the original transaction.
*
* @return Nothing.
*/
void dogecoin_tx_copy(dogecoin_tx* dest, const dogecoin_tx* src)
{
dest->version = src->version;
dest->locktime = src->locktime;
if (!src->vin) {
dest->vin = NULL;
} else {
unsigned int i;
if (dest->vin != NULL) {
vector_free(dest->vin, true);
}
dest->vin = vector_new(src->vin->len, dogecoin_tx_in_free_cb);
for (i = 0; i < src->vin->len; i++) {
dogecoin_tx_in *tx_in_old, *tx_in_new;
tx_in_old = vector_idx(src->vin, i);
tx_in_new = dogecoin_malloc(sizeof(*tx_in_new));
dogecoin_tx_in_copy(tx_in_new, tx_in_old);
vector_add(dest->vin, tx_in_new);
}
}
if (!src->vout) {
dest->vout = NULL;
} else {
unsigned int i;
if (dest->vout)
vector_free(dest->vout, true);
dest->vout = vector_new(src->vout->len,
dogecoin_tx_out_free_cb);
for (i = 0; i < src->vout->len; i++) {
dogecoin_tx_out *tx_out_old, *tx_out_new;
tx_out_old = vector_idx(src->vout, i);
tx_out_new = dogecoin_malloc(sizeof(*tx_out_new));
dogecoin_tx_out_copy(tx_out_new, tx_out_old);
vector_add(dest->vout, tx_out_new);
}
}
}
/**
* @brief This function performs a double SHA256 hash
* on the input data of a given transaction.
*
* @param tx The pointer to the transaction whose inputs will be hashed.
* @param hash The result of the hashed inputs.
*
* @return Nothing.
*/
void dogecoin_tx_prevout_hash(const dogecoin_tx* tx, uint256 hash)
{
cstring* s = cstr_new_sz(512);
unsigned int i;
for (i = 0; i < tx->vin->len; i++) {
dogecoin_tx_in* tx_in = vector_idx(tx->vin, i);
ser_u256(s, tx_in->prevout.hash);
ser_u32(s, tx_in->prevout.n);
}
dogecoin_hash((const uint8_t*)s->str, s->len, hash);
cstr_free(s, true);
}
/**
* @brief This function performs a double SHA256 hash
* on the sequence numbers of all the transaction's
* inputs
*
* @param tx The pointer to the transaction whose inputs will be hashed.
* @param hash The result of the hashed inputs.
*
* @return Nothing.
*/
void dogecoin_tx_sequence_hash(const dogecoin_tx* tx, uint256 hash)
{
cstring* s = cstr_new_sz(512);
unsigned int i;
for (i = 0; i < tx->vin->len; i++) {
dogecoin_tx_in* tx_in = vector_idx(tx->vin, i);
ser_u32(s, tx_in->sequence);
}
dogecoin_hash((const uint8_t*)s->str, s->len, hash);
cstr_free(s, true);
}
/**
* @brief This function perform a double SHA256 hash
* on the serialized outputs of a given transaction.
*
* @param tx The pointer to the transaction whose outputs will be hashed.
* @param hash The result of the hashed outputs.
*
* @return Nothing.
*/
void dogecoin_tx_outputs_hash(const dogecoin_tx* tx, uint256 hash)
{
if (!tx->vout || !hash) return;
cstring* s = cstr_new_sz(512);
size_t i, len = tx->vout->len;
for (i = 0; i < len; i++) {
dogecoin_tx_out* tx_out = vector_idx(tx->vout, i);
dogecoin_tx_out_serialize(s, tx_out);
}
dogecoin_hash((const uint8_t*)s->str, s->len, hash);
cstr_free(s, true);
}
/**
* @brief This function takes an existing transaction and
* generates a signature hash to lock its inputs from being
* double-spent.
*
* @param tx_to The pointer to the existing transaction.
* @param fromPubKey The pointer to the cstring containing the public key of the sender.
* @param in_num The index of the input being signed.
* @param hashtype The type of signature hash to perform.
* @param hash The generated signature hash.
*
* @return 1 if signature hash is generated successfully, 0 otherwise.
*/
dogecoin_bool dogecoin_tx_sighash(const dogecoin_tx* tx_to, const cstring* fromPubKey, unsigned int in_num, int hashtype, uint256 hash)
{
if (in_num >= tx_to->vin->len || !tx_to->vout) {
return false;
}
dogecoin_bool ret = true;
dogecoin_tx* tx_tmp = dogecoin_tx_new();
dogecoin_tx_copy(tx_tmp, tx_to);
// standard sighash (SIGVERSION_BASE)
cstring* new_script = cstr_new_sz(fromPubKey->len);
dogecoin_script_copy_without_op_codeseperator(fromPubKey, new_script);
unsigned int i;
dogecoin_tx_in* tx_in;
for (i = 0; i < tx_tmp->vin->len; i++) {
tx_in = vector_idx(tx_tmp->vin, i);
cstr_resize(tx_in->script_sig, 0);
if (i == in_num) {
cstr_append_buf(tx_in->script_sig,
new_script->str,
new_script->len);
}
}
cstr_free(new_script, true);
if ((hashtype & 0x1f) == SIGHASH_NONE) {
if (tx_tmp->vout) {
vector_free(tx_tmp->vout, true);
}
tx_tmp->vout = vector_new(1, dogecoin_tx_out_free_cb);
for (i = 0; i < tx_tmp->vin->len; i++) {
tx_in = vector_idx(tx_tmp->vin, i);
if (i != in_num) {
tx_in->sequence = 0;
}
}
} else if ((hashtype & 0x1f) == SIGHASH_SINGLE) {
unsigned int n_out = in_num;
if (n_out >= tx_tmp->vout->len) {
//TODO: set error code
ret = false;
goto out;
}
vector_resize(tx_tmp->vout, n_out + 1);
for (i = 0; i < n_out; i++) {
dogecoin_tx_out* tx_out;
tx_out = vector_idx(tx_tmp->vout, i);
tx_out->value = -1;
if (tx_out->script_pubkey) {
cstr_free(tx_out->script_pubkey, true);
tx_out->script_pubkey = NULL;
}
}
for (i = 0; i < tx_tmp->vin->len; i++) {
tx_in = vector_idx(tx_tmp->vin, i);
if (i != in_num) {
tx_in->sequence = 0;
}
}
}
if (hashtype & SIGHASH_ANYONECANPAY) {
if (in_num > 0) {
vector_remove_range(tx_tmp->vin, 0, in_num);
}
vector_resize(tx_tmp->vin, 1);
}
cstring* s = cstr_new_sz(512);
dogecoin_tx_serialize(s, tx_tmp);
ser_s32(s, hashtype);
dogecoin_hash((const uint8_t*)s->str, s->len, hash);
cstr_free(s, true);
out:
dogecoin_tx_free(tx_tmp);
return ret;
}
/**
* @brief This function adds another transaction output to
* an existing transaction.
*
* @param tx The pointer to the transaction which will be updated.
* @param amount The amount that will be sent to the new destination.
* @param data The pubkey data to be embedded in the new transaction.
* @param datalen The length of the pubkey data to be embedded.
*
* @return dogecoin_bool
*/
dogecoin_bool dogecoin_tx_add_data_out(dogecoin_tx* tx, const int64_t amount, const uint8_t* data, const size_t datalen)
{
if (datalen > 80) {
return false;
}
dogecoin_tx_out* tx_out = dogecoin_tx_out_new();
tx_out->script_pubkey = cstr_new_sz(1024);
dogecoin_script_append_op(tx_out->script_pubkey, OP_RETURN);
dogecoin_script_append_pushdata(tx_out->script_pubkey, (unsigned char*)data, datalen);
tx_out->value = amount;
vector_add(tx->vout, tx_out);
return true;
}
/**
* @brief This function adds another transaction output
* containing the puzzle hash for the miner to solve to
* an existing transaction.
*
* @param tx The pointer to the transaction which will be updated.
* @param amount The amount that will be sent in the transaction.
* @param puzzle The puzzle that the miner must solve in order to spend the coin.
* @param puzzlelen The length of the puzzle in bytes.
*
* @return 1 if the puzzle was added successfully, 0 otherwise.
*/
dogecoin_bool dogecoin_tx_add_puzzle_out(dogecoin_tx* tx, const int64_t amount, const uint8_t* puzzle, const size_t puzzlelen)
{
if (puzzlelen > DOGECOIN_HASH_LENGTH) {
return false;
}
dogecoin_tx_out* tx_out = dogecoin_tx_out_new();
tx_out->script_pubkey = cstr_new_sz(1024);
dogecoin_script_append_op(tx_out->script_pubkey, OP_HASH256);
dogecoin_script_append_pushdata(tx_out->script_pubkey, (unsigned char*)puzzle, puzzlelen);
dogecoin_script_append_op(tx_out->script_pubkey, OP_EQUAL);
tx_out->value = amount;
vector_add(tx->vout, tx_out);
return true;
}
/**
* @brief This function adds another transaction output
* containing the destination address to an existing
* transaction.
*
* @param tx The pointer to the transaction which will be updated.
* @param chain The pointer to the chainparams which contain the prefixes for the address types.
* @param amount The amount that will be sent in the transaction.
* @param address The address to send coins to, which can be a P2PKH, P2SH, or P2WPKH address.
*
* @return 1 if the address was added successfully, 0 otherwise.
*/
dogecoin_bool dogecoin_tx_add_address_out(dogecoin_tx* tx, const dogecoin_chainparams* chain, int64_t amount, const char* address)
{
const size_t buflen = sizeof(uint8_t) * strlen(address) * 2;
uint8_t* buf = dogecoin_calloc(1, buflen);
int r = dogecoin_base58_decode_check(address, buf, buflen);
if (r > 0 && buf[0] == chain->b58prefix_pubkey_address) {
dogecoin_tx_add_p2pkh_hash160_out(tx, amount, &buf[1]);
} else if (r > 0 && buf[0] == chain->b58prefix_script_address) {
dogecoin_tx_add_p2sh_hash160_out(tx, amount, &buf[1]);
}
dogecoin_free(buf);
return true;
}
/**
* @brief This function adds a new P2PKH output to an
* existing transaction.
*
* @param tx The pointer to the transaction which will be updated.
* @param amount The amount that will be sent in the transaction.
* @param hash160 The hash160 of the sender public key.
*
* @return 1 if the output is added successfully.
*/
dogecoin_bool dogecoin_tx_add_p2pkh_hash160_out(dogecoin_tx* tx, int64_t amount, uint160 hash160)
{
dogecoin_tx_out* tx_out = dogecoin_tx_out_new();
tx_out->script_pubkey = cstr_new_sz(1024);
dogecoin_script_build_p2pkh(tx_out->script_pubkey, hash160);
tx_out->value = amount;
vector_add(tx->vout, tx_out);
return true;
}
/**
* @brief This function adds a new P2SH output to an
* existing transaction.
*
* @param tx The pointer to the transaction which will be updated.
* @param amount The amount that will be sent in the transaction.
* @param hash160 The hash160 of the sender public key.
*
* @return 1 if the output is added successfully.
*/
dogecoin_bool dogecoin_tx_add_p2sh_hash160_out(dogecoin_tx* tx, int64_t amount, uint160 hash160)
{
dogecoin_tx_out* tx_out = dogecoin_tx_out_new();
tx_out->script_pubkey = cstr_new_sz(1024);
dogecoin_script_build_p2sh(tx_out->script_pubkey, hash160);
tx_out->value = amount;
vector_add(tx->vout, tx_out);
return true;
}
/**
* @brief This function adds an output to an existing
* transaction by hashing the given public key and calling
* dogecoin_tx_add_p2pkh_hash160_out() using this hash.
*
* @param tx The pointer to the transaction which will be updated.
* @param amount The amount that will be sent in the transaction.
* @param pubkey The pointer to the public key to be hashed.
*
* @return 1 if the output was added successfully.
*/
dogecoin_bool dogecoin_tx_add_p2pkh_out(dogecoin_tx* tx, int64_t amount, const dogecoin_pubkey* pubkey)
{
uint160 hash160;
dogecoin_pubkey_get_hash160(pubkey, hash160);
return dogecoin_tx_add_p2pkh_hash160_out(tx, amount, hash160);
}
/**
* @brief This function checks whether a transaction
* outpoint is null.
*
* @param tx The pointer to the transaction outpoint to check.
*
* @return 1 if the outpoint is null.
*/
dogecoin_bool dogecoin_tx_outpoint_is_null(dogecoin_tx_outpoint* tx)
{
(void)(tx);
return true;
}
/**
* @brief This function checks whether a transaction was
* a coinbase transaction, which is true if the transaction
* has only one input, the previous transaction hash is
* empty, and the transaction index is UINT32_MAX.
*