rusty-blockparser 0.12.0

Blockchain Parser for most common Cryptocurrencies based on Bitcoin
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
use bitcoin::hashes::{sha256d, Hash};
use std::borrow::BorrowMut;
use std::io::{self, Read, Seek};

use crate::blockchain::parser::types::CoinType;
use crate::blockchain::proto::block::{AuxPowExtension, Block};
use crate::blockchain::proto::header::BlockHeader;
use crate::blockchain::proto::tx::{RawTx, TxInput, TxOutpoint, TxOutput};
use crate::blockchain::proto::varuint::VarUint;
use crate::blockchain::proto::MerkleBranch;
use crate::common::Result;
use byteorder::{LittleEndian, ReadBytesExt};

/// Trait for structured reading of blockchain data
pub trait BlockchainRead: Read {
    #[inline]
    fn read_256hash(&mut self) -> Result<[u8; 32]> {
        let mut arr = [0u8; 32];
        self.read_exact(arr.borrow_mut())?;
        Ok(arr)
    }

    #[inline]
    fn read_u8_vec(&mut self, count: u32) -> Result<Vec<u8>> {
        let mut arr = vec![0u8; count as usize];
        self.read_exact(arr.borrow_mut())?;
        Ok(arr)
    }

    /// Reads a block as specified here: https://en.bitcoin.it/wiki/Protocol_specification#block
    fn read_block(&mut self, size: u32, coin: &CoinType) -> Result<Block> {
        let header = self.read_block_header()?;
        // Parse AuxPow data if present
        let aux_pow_extension = match coin.aux_pow_activation_version {
            Some(version) if header.version >= version => {
                Some(self.read_aux_pow_extension(coin.version_id)?)
            }
            _ => None,
        };
        let tx_count = VarUint::read_from(self)?;
        let txs = self.read_txs(tx_count.value, coin.version_id)?;
        Ok(Block::new(size, header, aux_pow_extension, tx_count, txs))
    }

    fn read_block_header(&mut self) -> Result<BlockHeader> {
        let version = self.read_u32::<LittleEndian>()?;
        let prev_hash = sha256d::Hash::from_byte_array(self.read_256hash()?);
        let merkle_root = sha256d::Hash::from_byte_array(self.read_256hash()?);
        let timestamp = self.read_u32::<LittleEndian>()?;
        let bits = self.read_u32::<LittleEndian>()?;
        let nonce = self.read_u32::<LittleEndian>()?;

        Ok(BlockHeader {
            version,
            prev_hash,
            merkle_root,
            timestamp,
            bits,
            nonce,
        })
    }

    fn read_txs(&mut self, tx_count: u64, version_id: u8) -> Result<Vec<RawTx>> {
        (0..tx_count).map(|_| self.read_tx(version_id)).collect()
    }

    /// Reads a transaction as specified here: https://en.bitcoin.it/wiki/Protocol_specification#tx
    fn read_tx(&mut self, version_id: u8) -> Result<RawTx> {
        let mut flags = 0u8;
        let version = self.read_u32::<LittleEndian>()?;

        // Parse transaction inputs and check if this transaction contains segwit data
        let mut in_count = VarUint::read_from(self)?;
        if in_count.value == 0 {
            flags = self.read_u8()?;
            // TODO: handle segwit data
            in_count = VarUint::read_from(self)?
        }
        let inputs = self.read_tx_inputs(in_count.value)?;

        // Parse transaction outputs
        let out_count = VarUint::read_from(self)?;
        let outputs = self.read_tx_outputs(out_count.value)?;

        // Check if the witness flag is present
        if flags & 1 > 0 {
            for _ in 0..in_count.value {
                let item_count = VarUint::read_from(self)?;
                for _ in 0..item_count.value {
                    let witness_len = VarUint::read_from(self)?;
                    let _ = self.read_u8_vec(witness_len.value as u32)?;
                }
            }
        }
        let locktime = self.read_u32::<LittleEndian>()?;
        let tx = RawTx {
            version,
            in_count,
            inputs,
            out_count,
            outputs,
            locktime,
            version_id,
        };
        Ok(tx)
    }

    fn read_tx_outpoint(&mut self) -> Result<TxOutpoint> {
        let txid = sha256d::Hash::from_byte_array(self.read_256hash()?);
        let index = self.read_u32::<LittleEndian>()?;

        Ok(TxOutpoint { txid, index })
    }

    fn read_tx_inputs(&mut self, input_count: u64) -> Result<Vec<TxInput>> {
        let mut inputs = Vec::with_capacity(input_count as usize);
        for _ in 0..input_count {
            let outpoint = self.read_tx_outpoint()?;
            let script_len = VarUint::read_from(self)?;
            let script_sig = self.read_u8_vec(script_len.value as u32)?;
            let seq_no = self.read_u32::<LittleEndian>()?;
            inputs.push(TxInput {
                outpoint,
                script_len,
                script_sig,
                seq_no,
            });
        }
        Ok(inputs)
    }

    fn read_tx_outputs(&mut self, output_count: u64) -> Result<Vec<TxOutput>> {
        let mut outputs = Vec::with_capacity(output_count as usize);
        for _ in 0..output_count {
            let value = self.read_u64::<LittleEndian>()?;
            let script_len = VarUint::read_from(self)?;
            let script_pubkey = self.read_u8_vec(script_len.value as u32)?;
            outputs.push(TxOutput {
                value,
                script_len,
                script_pubkey,
            });
        }
        Ok(outputs)
    }

    /// Reads a merkle branch as specified here https://en.bitcoin.it/wiki/Merged_mining_specification#Merkle_Branch
    /// This is mainly used for merged mining (AuxPoW).
    fn read_merkle_branch(&mut self) -> Result<MerkleBranch> {
        let branch_length = VarUint::read_from(self)?;
        let hashes = (0..branch_length.value)
            .map(|_| self.read_256hash())
            .collect::<Result<Vec<[u8; 32]>>>()?;
        let side_mask = self.read_u32::<LittleEndian>()?;
        Ok(MerkleBranch::new(hashes, side_mask))
    }

    /// Reads the additional AuxPow fields as specified here https://en.bitcoin.it/wiki/Merged_mining_specification#Aux_proof-of-work_block
    fn read_aux_pow_extension(&mut self, version_id: u8) -> Result<AuxPowExtension> {
        let coinbase_tx = self.read_tx(version_id)?;
        let block_hash = sha256d::Hash::from_byte_array(self.read_256hash()?);

        let coinbase_branch = self.read_merkle_branch()?;
        let blockchain_branch = self.read_merkle_branch()?;

        let parent_block = self.read_block_header()?;

        Ok(AuxPowExtension {
            coinbase_tx,
            block_hash,
            coinbase_branch,
            blockchain_branch,
            parent_block,
        })
    }
}

/// All types that implement `Read` and `Seek` get methods defined in `BlockchainRead`
/// for free.
impl<R: Read + Seek + ?Sized> BlockchainRead for R {}

/// Reader that XORs the data with a given key.
/// The block storage data is encrypted with a simple XOR operation
/// since Bitcoin Core 28.0.
/// See https://github.com/bitcoin/bitcoin/pull/28052
pub struct XorReader<R> {
    reader: R,
    xor_key: Option<Vec<u8>>,
    absolute_pos: u64,
}

impl<R: Seek + Read> XorReader<R> {
    pub fn new(reader: R, xor_key: Option<Vec<u8>>) -> XorReader<R> {
        Self {
            reader,
            xor_key,
            absolute_pos: 0,
        }
    }
}

impl<R: Read> Read for XorReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        let n = self.reader.read(buf)?;
        if let Some(ref xor_key) = self.xor_key {
            for i in 0..n {
                buf[i] ^= xor_key[((i as u64 + self.absolute_pos) % xor_key.len() as u64) as usize];
            }
        }
        self.absolute_pos += n as u64;
        Ok(n)
    }
}

impl<R: Seek> Seek for XorReader<R> {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        self.absolute_pos = self.reader.seek(pos)?;
        Ok(self.absolute_pos)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::blockchain::parser::types::{Bitcoin, Coin, Dogecoin};
    use crate::blockchain::proto::script;
    use crate::blockchain::proto::script::ScriptPattern;
    use crate::blockchain::proto::tx::EvaluatedTx;
    use crate::common::utils;
    use byteorder::{LittleEndian, ReadBytesExt};
    use seek_bufread::BufReader;
    use std::io::Cursor;
    use std::str::FromStr;

    #[test]
    fn test_bitcoin_parse_genesis_block() {
        let bitcoin = CoinType::from_str("bitcoin").unwrap();
        /********** Genesis block raw data for reference (Most fields are little endian) ***********
        version            0x01000000   big endian??
        prev_hash          0x0000000000000000000000000000000000000000000000000000000000000000
        merkle_root        0x3ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a
        timestamp          0x29ab5f49
        bits               0x1d00ffff
        nonce              0x1dac2b7c
        tx_count           0x01
        tx.version         0x01000000   big endian??
        tx.in_count        0x01
        tx.in.prev_hash    0x0000000000000000000000000000000000000000000000000000000000000000
        tx.in.out_id       0xffffffff
        tx.in.script_len   0x4d
        tx.in.script_sig   0x04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73
        tx.in.sequence     0xffffffff
        tx.out_count       0x01
        tx.out.value       0x00f2052a01000000   big endian??
        tx.out.script_len  0x43
        tx.out.script_pubkey      0x4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac
        tx.lock_time       0x00000000
        *******************************************************************************************/
        let raw_data = vec![
            0xf9, 0xbe, 0xb4, 0xd9, 0x1d, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, 0x7a, 0xc7, 0x2c, 0x3e,
            0x67, 0x76, 0x8f, 0x61, 0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32, 0x3a, 0x9f,
            0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, 0x29, 0xab, 0x5f, 0x49, 0xff, 0xff, 0x00, 0x1d,
            0x1d, 0xac, 0x2b, 0x7c, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0xff, 0xff, 0xff, 0xff, 0x4d, 0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x45, 0x54,
            0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, 0x73, 0x20, 0x30, 0x33, 0x2f, 0x4a, 0x61,
            0x6e, 0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, 0x61, 0x6e, 0x63, 0x65, 0x6c,
            0x6c, 0x6f, 0x72, 0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, 0x6b, 0x20, 0x6f,
            0x66, 0x20, 0x73, 0x65, 0x63, 0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, 0x6f,
            0x75, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x62, 0x61, 0x6e, 0x6b, 0x73, 0xff, 0xff,
            0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04,
            0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, 0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30,
            0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, 0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea,
            0x1f, 0x61, 0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, 0x38, 0xc4, 0xf3, 0x55,
            0x04, 0xe5, 0x1e, 0xc1, 0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, 0x8d, 0x57,
            0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, 0x1d, 0x5f, 0xac, 0x00, 0x00, 0x00, 0x00, 0xf9,
            0xbe, 0xb4, 0xd9, 0xd7, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x6f, 0xe2, 0x8c,
            0x0a, 0xb6, 0xf1, 0xb3, 0x72, 0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f, 0x93,
            0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c, 0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x98, 0x20, 0x51, 0xfd, 0x1e, 0x4b, 0xa7, 0x44, 0xbb, 0xbe, 0x68, 0x0e, 0x1f,
            0xee, 0x14, 0x67, 0x7b, 0xa1, 0xa3, 0xc3, 0x54, 0x0b, 0xf7, 0xb1, 0xcd, 0xb6, 0x06,
            0xe8, 0x57, 0x23, 0x3e, 0x0e, 0x61, 0xbc, 0x66, 0x49, 0xff, 0xff, 0x00, 0x1d, 0x01,
            0xe3, 0x62, 0x99, 0x01, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
            0xff, 0xff, 0xff, 0x07, 0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0xff, 0xff, 0xff,
            0xff, 0x01, 0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0x96,
            0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c, 0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
            0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c, 0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b,
            0xe6, 0x3c, 0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4, 0xe0, 0xa6, 0x04, 0xf8,
            0x14, 0x17, 0x81, 0xe6, 0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e, 0x73, 0xa8,
            0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58, 0xee, 0xac, 0x00, 0x00, 0x00, 0x0,
        ];
        let inner = Cursor::new(raw_data);
        let mut reader = BufReader::with_capacity(200, inner);

        let magic: u32 = reader.read_u32::<LittleEndian>().unwrap();
        let block_size: u32 = reader.read_u32::<LittleEndian>().unwrap();

        // Parse block
        let block = reader.read_block(block_size, &bitcoin).unwrap();

        // Block Metadata
        assert_eq!(0xd9b4bef9, magic);
        assert_eq!(285, block.size);

        // Block Header
        assert_eq!(0x00000001, block.header.value.version);
        assert_eq!(
            "0000000000000000000000000000000000000000000000000000000000000000",
            format!("{}", &block.header.value.prev_hash)
        );
        assert_eq!(
            "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
            format!("{}", &block.header.value.merkle_root)
        );
        assert_eq!(
            "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
            format!("{}", &block.header.hash)
        );

        // Check against computed merkle root
        assert_eq!(
            &block.header.value.merkle_root,
            &block.compute_merkle_root()
        );
        assert_eq!(1231006505, block.header.value.timestamp);
        assert_eq!(0x1d00ffff, block.header.value.bits);
        assert_eq!(2083236893, block.header.value.nonce);

        // Tx
        assert_eq!(0x01, block.tx_count.value);
        assert_eq!(0x00000001, block.txs[0].value.version);

        // Tx Inputs
        assert_eq!(0x01, block.txs[0].value.in_count.value);
        assert_eq!(
            "0000000000000000000000000000000000000000000000000000000000000000",
            format!("{}", &block.txs[0].value.inputs[0].outpoint.txid)
        );
        assert_eq!(0xffffffff, block.txs[0].value.inputs[0].outpoint.index);
        assert_eq!(0x4d, block.txs[0].value.inputs[0].script_len.value);
        assert_eq!("04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73",
                                utils::arr_to_hex(&block.txs[0].value.inputs[0].script_sig));
        assert_eq!(0xffffffff, block.txs[0].value.inputs[0].seq_no);

        // Tx Outputs
        assert_eq!(0x01, block.txs[0].value.out_count.value);
        assert_eq!(
            u64::from_be(0x00f2052a01000000),
            block.txs[0].value.outputs[0].out.value
        );
        assert_eq!(0x43, block.txs[0].value.outputs[0].out.script_len.value);

        let script_pubkey = &block.txs[0].value.outputs[0].out.script_pubkey;
        assert_eq!("4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac",
                                utils::arr_to_hex(&script_pubkey));
        assert_eq!(0x00000000, block.txs[0].value.locktime);

        assert_eq!(
            Some(String::from("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")),
            script::eval_from_bytes(script_pubkey, Bitcoin.version_id()).address
        );
    }

    #[test]
    fn test_bitcoin_parse_segwit_tx() {
        // See: https://en.bitcoin.it/wiki/Weight_units#Weight_for_segwit_transactions
        /*******************************************************************************************
        01000000 	        Version 1 	                                        4 	Non-witness
        00 	                SegWit marker 	                                    1 	Witness
        01 	                SegWit flag 	                                    1 	Witness
        01 	                Number of inputs (1) 	                            1 	Non-witness
        15..56 	            Previous output hash 	                            32 	Non-witness
        03000000 	        Previous output index (3) 	                        4 	Non-witness
        17 	                Script length (23 bytes) 	                        1 	Non-witness
        16..28 	            Script: P2SH-enclosed P2WPKH witness program 	    23 	Non-witness
        ffffffff 	        Sequence 	                                        4 	Non-witness
        01 	                Output count (1) 	                                1 	Non-witness
        9caef50500000000 	Output value (0.99987100 BTC) 	                    8 	Non-witness
        19 	                Output script size (25) 	                        1 	Non-witness
        76..ac 	            Script: DUP HASH160 0x1d7c... EQUALVERIFY CHECKSIG 	25 	Non-witness
        02 	                Number of stack items for input 0 (2) 	            1 	Witness
        48 	                Size of stack item 0 (72) 	                        1 	Witness
        304...ab01 	        Stack item 0, signature 	                        72 	Witness
        21 	                Size of stack item 1 (33) 	                        1 	Witness
        03..ac 	            Stack item 1, pubkey 	                            33 	Witness
        00000000 	        Locktime (0) 	                                    4 	Non-witness
        *******************************************************************************************/
        let raw_data = vec![
            0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x15, 0xe1, 0x80, 0xdc, 0x28, 0xa2, 0x32,
            0x7e, 0x68, 0x7f, 0xac, 0xc3, 0x3f, 0x10, 0xf2, 0xa2, 0x0d, 0xa7, 0x17, 0xe5, 0x54,
            0x84, 0x06, 0xf7, 0xae, 0x8b, 0x4c, 0x81, 0x10, 0x72, 0xf8, 0x56, 0x03, 0x00, 0x00,
            0x00, 0x17, 0x16, 0x00, 0x14, 0x1d, 0x7c, 0xd6, 0xc7, 0x5c, 0x2e, 0x86, 0xf4, 0xcb,
            0xf9, 0x8e, 0xae, 0xd2, 0x21, 0xb3, 0x0b, 0xd9, 0xa0, 0xb9, 0x28, 0xff, 0xff, 0xff,
            0xff, 0x01, 0x9c, 0xae, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14,
            0x1d, 0x7c, 0xd6, 0xc7, 0x5c, 0x2e, 0x86, 0xf4, 0xcb, 0xf9, 0x8e, 0xae, 0xd2, 0x21,
            0xb3, 0x0b, 0xd9, 0xa0, 0xb9, 0x28, 0x88, 0xac, 0x02, 0x48, 0x30, 0x45, 0x02, 0x21,
            0x00, 0xf7, 0x64, 0x28, 0x7d, 0x3e, 0x99, 0xb1, 0x47, 0x4d, 0xa9, 0xbe, 0xc7, 0xf7,
            0xed, 0x23, 0x6d, 0x6c, 0x81, 0xe7, 0x93, 0xb2, 0x0c, 0x4b, 0x5a, 0xa1, 0xf3, 0x05,
            0x1b, 0x9a, 0x7d, 0xaa, 0x63, 0x02, 0x20, 0x16, 0xa1, 0x98, 0x03, 0x1d, 0x55, 0x54,
            0xdb, 0xb8, 0x55, 0xbd, 0xbe, 0x85, 0x34, 0x77, 0x6a, 0x4b, 0xe6, 0x95, 0x8b, 0xd8,
            0xd5, 0x30, 0xdc, 0x00, 0x1c, 0x32, 0xb8, 0x28, 0xf6, 0xf0, 0xab, 0x01, 0x21, 0x03,
            0x82, 0x62, 0xa6, 0xc6, 0xce, 0xc9, 0x3c, 0x2d, 0x3e, 0xcd, 0x6c, 0x60, 0x72, 0xef,
            0xea, 0x86, 0xd0, 0x2f, 0xf8, 0xe3, 0x32, 0x8b, 0xbd, 0x02, 0x42, 0xb2, 0x0a, 0xf3,
            0x42, 0x59, 0x90, 0xac, 0x00, 0x00, 0x00, 0x00,
        ];
        let inner = Cursor::new(raw_data);
        let mut reader = BufReader::with_capacity(200, inner);
        let txs: Vec<EvaluatedTx> = reader
            .read_txs(1, 0x00)
            .unwrap()
            .into_iter()
            .map(|raw| EvaluatedTx::from(raw))
            .collect();
        assert_eq!(txs.len(), 1);

        let tx = txs.first().unwrap();
        assert_eq!(tx.version, 1);

        // Assert inputs
        assert_eq!(tx.in_count.value, 1);
        assert_eq!(tx.inputs.len(), 1);
        let prev_hash = [
            0x15, 0xe1, 0x80, 0xdc, 0x28, 0xa2, 0x32, 0x7e, 0x68, 0x7f, 0xac, 0xc3, 0x3f, 0x10,
            0xf2, 0xa2, 0x0d, 0xa7, 0x17, 0xe5, 0x54, 0x84, 0x06, 0xf7, 0xae, 0x8b, 0x4c, 0x81,
            0x10, 0x72, 0xf8, 0x56,
        ];
        assert_eq!(tx.inputs[0].outpoint.txid.as_ref(), prev_hash);
        assert_eq!(tx.inputs[0].outpoint.index, 3);
        assert_eq!(tx.inputs[0].script_len.value, 23);
        assert_eq!(tx.inputs[0].seq_no, 0xffffffff);

        // Assert outputs
        assert_eq!(tx.out_count.value, 1);
        assert_eq!(tx.outputs.len(), 1);
        assert_eq!(tx.outputs[0].out.value, 99987100);
        assert_eq!(tx.outputs[0].out.script_len.value, 25);
        assert_eq!(
            tx.outputs[0].script.pattern,
            ScriptPattern::Pay2PublicKeyHash
        );
        assert_eq!(
            tx.outputs[0].script.address,
            Some(String::from("13gv9XbKJPxxRF8Zm1LsVKeeiMCFguQPqm"))
        );

        assert_eq!(tx.locktime, 0);
    }

    #[test]
    fn test_namecoin_parse_auxpow_block() {
        let namecoin = CoinType::from_str("namecoin").unwrap();
        // See: https://en.bitcoin.it/wiki/Merged_mining_specification#Example
        /*******************************************************************************************
        Block Header:
         01 01 01 00                                                                                        // Version
         36 90 9a c0 7a 16 73 da f6 5f a7 d8 28 88 2e 66 c9 e8 9f 85 46 cd d5 0a 9f b1 00 00 00 00 00 00    // Previous block hash
         0f 5c 65 49 bc d6 08 ab 7c 4e ac 59 3e 5b d5 a7 3b 2d 43 2e b6 35 18 70 8f 77 8f c7 dc df af 88    // Merkle root
         8d 1a 90 4e                                                                                        // Timestamp
         69 b2 00 1b                                                                                        // Bits
         00 00 00 00                                                                                        // Nonce

        Parent Block Coinbase Transaction:
         01 00 00 00                                              // Version
         01                                                       // TxIn Count

         00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    // Previous Out
         00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff

         35                                                                                                 // Script size
         04 5d ee 09 1a 01 4d 52 2c fa be 6d 6d d8 a7 c3 e0 1e 1e 95 bc ee 01 5e 6f cc 75 83 a2 ca 60 b7    // Script
         9e 5a 3a a0 a1 71 ed dd 34 4a da 90 3d 01 00 00 00 00 00 00 00

         ff ff ff ff                                              // Sequence Number
         01                                                       // TxOut Count
         60 a0 10 2a 01 00 00 00                                  // Amount

         43                                                                                                 // Script Size
         41 04 f8 bb e9 7e d2 ac bc 5b ba 11 c6 8f 6f 1a 03 13 f9 18 f3 d3 c0 e8 47 50 55 e3 51 e3 bf 44    // Script
         2f 8c 8d ce e6 82 d2 45 7b dc 53 51 b7 0d d9 e3 40 26 76 6e ba 18 b0 6e ae e2 e1 02 ef d1 ab 63
         46 67 ac

         00 00 00 00                                              // Lock Time

        Coinbase Link:
         a9 03 ef 9d e1 91 8e 4b 44 f6 17 6a 30 c0 e7 c7 e3 43 9c 96 fb 59 73 27 47 3d 00 00 00 00 00 00    // Hash of parent block header
         05                                                                                                 // Number of links in branch
         05 0a c4 a1 a1 e1 bc e0 c4 8e 55 5b 1a 9f 93 52 81 96 8c 72 d6 37 9b 24 72 9c a0 42 5a 3f c3 cb    // Hash #1
         43 3c d3 48 b3 5e a2 28 06 cf 21 c7 b1 46 48 9a ef 69 89 55 1e b5 ad 23 73 ab 61 21 06 0f 30 34    // Hash #2
         1d 64 87 57 c0 21 7d 43 e6 6c 57 ea ed 64 fc 18 20 ec 65 d1 57 f3 3b 74 19 65 18 3a 5e 0c 85 06    // Hash #3
         ac 26 02 df e2 f5 47 01 2d 1c c7 50 04 d4 8f 97 ab a4 6b d9 93 0f f2 85 c9 f2 76 f5 bd 09 f3 56    // Hash #4
         df 19 72 45 79 d6 5e c7 cb 62 bf 97 94 6d fc 6f b0 e3 b2 83 9b 7f da b3 7c db 60 e5 51 22 d3 5b    // Hash #5
         00 00 00 00                                                                                        // Branch sides bitmask

        Aux Blockchain Link:
         00             // Number of links in branch
         00 00 00 00    // Branch sides bitmask

        Parent Block Header:
         01 00 00 00                                                                                        // Version
         08 be 13 29 5c 03 e6 7c b7 0d 00 da e8 1e a0 6e 78 b9 01 4e 5c eb 7d 9b a5 04 00 00 00 00 00 00    // Previous block hash
         e0 fd 42 db 8e f6 d7 83 f0 79 d1 26 be a1 2e 2d 10 c1 04 c0 92 7c d6 8f 95 4d 85 6f 9e 81 11 e5    // Merkle root
         9a 23 90 4e                                                                                        // Timestamp
         5d ee 09 1a                                                                                        // Bits
         1c 65 50 86                                                                                        // Nonce

        Transactions:
         01                                                       // Tx  Count
         01 00 00 00                                              // Version
         01                                                       // TxIn Count

         00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    // Previous Out
         00 00 00 00 00 00 00 00 00 00 00 00 00 00 ff ff ff ff

         08                                                       // Script size
         04 69 b2 00 1b 01 01 52                                  // Script
         ff ff ff ff                                              // Sequence number
         01                                                       // TxOut Count
         00 f2 05 2a 01 00 00 00                                  // Amount

         43                                                                                                 // Script size
         41 04 89 fe 91 e6 28 47 57 5c 98 de ea b0 20 f6 5f df f1 7a 3a 87 0e bb 05 82 0b 41 4f 3d 80 97    // Script
         21 8e c9 a6 5f 1e 0a e0 ac 35 af 72 47 bd 79 ed 1f 2a 24 67 5f ff b5 aa 6f 96 20 e1 92 0a d4 bf
         5a a6 ac

         00 00 00 00                                              // Lock Time

        *******************************************************************************************/
        let raw_data = vec![
            0x01, 0x01, 0x01, 0x00, 0x36, 0x90, 0x9a, 0xc0, 0x7a, 0x16, 0x73, 0xda, 0xf6, 0x5f,
            0xa7, 0xd8, 0x28, 0x88, 0x2e, 0x66, 0xc9, 0xe8, 0x9f, 0x85, 0x46, 0xcd, 0xd5, 0x0a,
            0x9f, 0xb1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x5c, 0x65, 0x49, 0xbc, 0xd6,
            0x08, 0xab, 0x7c, 0x4e, 0xac, 0x59, 0x3e, 0x5b, 0xd5, 0xa7, 0x3b, 0x2d, 0x43, 0x2e,
            0xb6, 0x35, 0x18, 0x70, 0x8f, 0x77, 0x8f, 0xc7, 0xdc, 0xdf, 0xaf, 0x88, 0x8d, 0x1a,
            0x90, 0x4e, 0x69, 0xb2, 0x00, 0x1b, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x35, 0x04, 0x5d, 0xee, 0x09,
            0x1a, 0x01, 0x4d, 0x52, 0x2c, 0xfa, 0xbe, 0x6d, 0x6d, 0xd8, 0xa7, 0xc3, 0xe0, 0x1e,
            0x1e, 0x95, 0xbc, 0xee, 0x01, 0x5e, 0x6f, 0xcc, 0x75, 0x83, 0xa2, 0xca, 0x60, 0xb7,
            0x9e, 0x5a, 0x3a, 0xa0, 0xa1, 0x71, 0xed, 0xdd, 0x34, 0x4a, 0xda, 0x90, 0x3d, 0x01,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x60, 0xa0,
            0x10, 0x2a, 0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0xf8, 0xbb, 0xe9, 0x7e, 0xd2,
            0xac, 0xbc, 0x5b, 0xba, 0x11, 0xc6, 0x8f, 0x6f, 0x1a, 0x03, 0x13, 0xf9, 0x18, 0xf3,
            0xd3, 0xc0, 0xe8, 0x47, 0x50, 0x55, 0xe3, 0x51, 0xe3, 0xbf, 0x44, 0x2f, 0x8c, 0x8d,
            0xce, 0xe6, 0x82, 0xd2, 0x45, 0x7b, 0xdc, 0x53, 0x51, 0xb7, 0x0d, 0xd9, 0xe3, 0x40,
            0x26, 0x76, 0x6e, 0xba, 0x18, 0xb0, 0x6e, 0xae, 0xe2, 0xe1, 0x02, 0xef, 0xd1, 0xab,
            0x63, 0x46, 0x67, 0xac, 0x00, 0x00, 0x00, 0x00, 0xa9, 0x03, 0xef, 0x9d, 0xe1, 0x91,
            0x8e, 0x4b, 0x44, 0xf6, 0x17, 0x6a, 0x30, 0xc0, 0xe7, 0xc7, 0xe3, 0x43, 0x9c, 0x96,
            0xfb, 0x59, 0x73, 0x27, 0x47, 0x3d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x05,
            0x0a, 0xc4, 0xa1, 0xa1, 0xe1, 0xbc, 0xe0, 0xc4, 0x8e, 0x55, 0x5b, 0x1a, 0x9f, 0x93,
            0x52, 0x81, 0x96, 0x8c, 0x72, 0xd6, 0x37, 0x9b, 0x24, 0x72, 0x9c, 0xa0, 0x42, 0x5a,
            0x3f, 0xc3, 0xcb, 0x43, 0x3c, 0xd3, 0x48, 0xb3, 0x5e, 0xa2, 0x28, 0x06, 0xcf, 0x21,
            0xc7, 0xb1, 0x46, 0x48, 0x9a, 0xef, 0x69, 0x89, 0x55, 0x1e, 0xb5, 0xad, 0x23, 0x73,
            0xab, 0x61, 0x21, 0x06, 0x0f, 0x30, 0x34, 0x1d, 0x64, 0x87, 0x57, 0xc0, 0x21, 0x7d,
            0x43, 0xe6, 0x6c, 0x57, 0xea, 0xed, 0x64, 0xfc, 0x18, 0x20, 0xec, 0x65, 0xd1, 0x57,
            0xf3, 0x3b, 0x74, 0x19, 0x65, 0x18, 0x3a, 0x5e, 0x0c, 0x85, 0x06, 0xac, 0x26, 0x02,
            0xdf, 0xe2, 0xf5, 0x47, 0x01, 0x2d, 0x1c, 0xc7, 0x50, 0x04, 0xd4, 0x8f, 0x97, 0xab,
            0xa4, 0x6b, 0xd9, 0x93, 0x0f, 0xf2, 0x85, 0xc9, 0xf2, 0x76, 0xf5, 0xbd, 0x09, 0xf3,
            0x56, 0xdf, 0x19, 0x72, 0x45, 0x79, 0xd6, 0x5e, 0xc7, 0xcb, 0x62, 0xbf, 0x97, 0x94,
            0x6d, 0xfc, 0x6f, 0xb0, 0xe3, 0xb2, 0x83, 0x9b, 0x7f, 0xda, 0xb3, 0x7c, 0xdb, 0x60,
            0xe5, 0x51, 0x22, 0xd3, 0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x00, 0x00, 0x08, 0xbe, 0x13, 0x29, 0x5c, 0x03, 0xe6, 0x7c, 0xb7, 0x0d,
            0x00, 0xda, 0xe8, 0x1e, 0xa0, 0x6e, 0x78, 0xb9, 0x01, 0x4e, 0x5c, 0xeb, 0x7d, 0x9b,
            0xa5, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0xfd, 0x42, 0xdb, 0x8e, 0xf6,
            0xd7, 0x83, 0xf0, 0x79, 0xd1, 0x26, 0xbe, 0xa1, 0x2e, 0x2d, 0x10, 0xc1, 0x04, 0xc0,
            0x92, 0x7c, 0xd6, 0x8f, 0x95, 0x4d, 0x85, 0x6f, 0x9e, 0x81, 0x11, 0xe5, 0x9a, 0x23,
            0x90, 0x4e, 0x5d, 0xee, 0x09, 0x1a, 0x1c, 0x65, 0x50, 0x86, 0x01, 0x01, 0x00, 0x00,
            0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x08, 0x04, 0x69, 0xb2,
            0x00, 0x1b, 0x01, 0x01, 0x52, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00, 0xf2, 0x05, 0x2a,
            0x01, 0x00, 0x00, 0x00, 0x43, 0x41, 0x04, 0x89, 0xfe, 0x91, 0xe6, 0x28, 0x47, 0x57,
            0x5c, 0x98, 0xde, 0xea, 0xb0, 0x20, 0xf6, 0x5f, 0xdf, 0xf1, 0x7a, 0x3a, 0x87, 0x0e,
            0xbb, 0x05, 0x82, 0x0b, 0x41, 0x4f, 0x3d, 0x80, 0x97, 0x21, 0x8e, 0xc9, 0xa6, 0x5f,
            0x1e, 0x0a, 0xe0, 0xac, 0x35, 0xaf, 0x72, 0x47, 0xbd, 0x79, 0xed, 0x1f, 0x2a, 0x24,
            0x67, 0x5f, 0xff, 0xb5, 0xaa, 0x6f, 0x96, 0x20, 0xe1, 0x92, 0x0a, 0xd4, 0xbf, 0x5a,
            0xa6, 0xac, 0x00, 0x00, 0x00, 0x00,
        ];
        let block_size = 678;
        let inner = Cursor::new(raw_data);
        let mut reader = BufReader::with_capacity(block_size, inner);

        // Parse block
        let block = reader.read_block(block_size as u32, &namecoin).unwrap();

        // Block Header
        assert_eq!(0x00010101, block.header.value.version);
        assert_eq!(
            "000000000000b19f0ad5cd46859fe8c9662e8828d8a75ff6da73167ac09a9036",
            format!("{}", &block.header.value.prev_hash)
        );
        assert_eq!(
            "88afdfdcc78f778f701835b62e432d3ba7d55b3e59ac4e7cab08d6bc49655c0f",
            format!("{}", &block.header.value.merkle_root)
        );
        assert_eq!(
            "d8a7c3e01e1e95bcee015e6fcc7583a2ca60b79e5a3aa0a171eddd344ada903d",
            format!("{}", &block.header.hash)
        );

        // Check against computed merkle root
        //assert_eq!(&block.header.merkle_root, &block.compute_merkle_root());
        assert_eq!(1318066829, block.header.value.timestamp);
        assert_eq!(0x1b00b269, block.header.value.bits);
        assert_eq!(0, block.header.value.nonce);

        // AuxPoWBlock
        let aux_pow_block = block.aux_pow_extension.unwrap();
        // AuxPoWBlock coinbase tx
        assert_eq!(0x01, aux_pow_block.coinbase_tx.version);
        assert_eq!(0x00, aux_pow_block.coinbase_tx.locktime);
        assert_eq!(
            "0000000000003d47277359fb969c43e3c7e7c0306a17f6444b8e91e19def03a9",
            format!("{}", &aux_pow_block.block_hash)
        );

        // TODO: verify AuxPowBlock merkle branches

        assert_eq!(
            "00000000000004a59b7deb5c4e01b9786ea01ee8da000db77ce6035c2913be08",
            format!("{}", &aux_pow_block.parent_block.prev_hash)
        );

        // Tx
        assert_eq!(0x01, block.tx_count.value);
        assert_eq!(0x00000001, block.txs[0].value.version);

        // Tx Inputs
        assert_eq!(0x01, block.txs[0].value.in_count.value);
        assert_eq!(
            "0000000000000000000000000000000000000000000000000000000000000000",
            format!("{}", &block.txs[0].value.inputs[0].outpoint.txid)
        );
        assert_eq!(0xffffffff, block.txs[0].value.inputs[0].outpoint.index);
        assert_eq!(8, block.txs[0].value.inputs[0].script_len.value);
        assert_eq!(
            "0469b2001b010152",
            utils::arr_to_hex(&block.txs[0].value.inputs[0].script_sig)
        );
        assert_eq!(0xffffffff, block.txs[0].value.inputs[0].seq_no);

        // Tx Outputs
        assert_eq!(0x01, block.txs[0].value.out_count.value);
        assert_eq!(
            u64::from_be(0x00f2052a01000000),
            block.txs[0].value.outputs[0].out.value
        );
        assert_eq!(0x43, block.txs[0].value.outputs[0].out.script_len.value);

        let script_pubkey = &block.txs[0].value.outputs[0].out.script_pubkey;
        assert_eq!("410489fe91e62847575c98deeab020f65fdff17a3a870ebb05820b414f3d8097218ec9a65f1e0ae0ac35af7247bd79ed1f2a24675fffb5aa6f9620e1920ad4bf5aa6ac",
                                utils::arr_to_hex(&script_pubkey));
        assert_eq!(0x00000000, block.txs[0].value.locktime);

        /*
        FIXME: assertion failed
            Left:  Some("N1hd3xArZM8BaX2PGGvoTWDr7C66Payv7b")
            Right: Some("NHk86XHZ77H2uNgESo4ut598orZq8rcVKL")
        assert_eq!(
            Some(String::from("N1hd3xArZM8BaX2PGGvoTWDr7C66Payv7b")),
            script::eval_from_bytes(script_pubkey, Namecoin.version_id()).address
        );*/
    }

    #[test]
    fn test_dogecoin_parse_auxpow_block() {
        let dogecoin = CoinType::from_str("dogecoin").unwrap();
        // See:https://dogechain.info/block/3983721
        let raw_data = vec![
            0x04, 0x01, 0x62, 0x00, 0xee, 0x00, 0x53, 0x04, 0x59, 0x8f, 0x0f, 0x02, 0xbb, 0x7c,
            0xd4, 0xab, 0xd5, 0x35, 0x87, 0x0d, 0x10, 0x88, 0x6c, 0x8d, 0x15, 0x6e, 0x77, 0xf4,
            0xf0, 0x48, 0xac, 0xd6, 0x85, 0xa0, 0x4f, 0x20, 0xd9, 0x5b, 0xca, 0x86, 0x7d, 0x52,
            0xa9, 0x02, 0xcd, 0x75, 0x5a, 0x6e, 0x31, 0x65, 0xac, 0x10, 0xa7, 0x22, 0x20, 0xf4,
            0xf3, 0x38, 0xf5, 0xa4, 0xb6, 0x56, 0x92, 0x2b, 0x17, 0x31, 0x93, 0x5c, 0xa5, 0x37,
            0x95, 0x61, 0x1b, 0xa5, 0x04, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x60, 0x03, 0x5d, 0xf4, 0x20,
            0x19, 0x2f, 0x56, 0x69, 0x61, 0x42, 0x54, 0x43, 0x2f, 0x4d, 0x69, 0x6e, 0x65, 0x64,
            0x20, 0x62, 0x79, 0x20, 0x65, 0x64, 0x72, 0x69, 0x73, 0x35, 0x36, 0x2f, 0x2c, 0xfa,
            0xbe, 0x6d, 0x6d, 0xee, 0x91, 0x3d, 0x62, 0x04, 0xd6, 0x84, 0xa3, 0xb7, 0x23, 0x09,
            0x02, 0x17, 0xdc, 0x3d, 0x94, 0x23, 0x01, 0x44, 0xcd, 0x05, 0x6f, 0xe7, 0xd8, 0xce,
            0xd6, 0x64, 0xb6, 0x01, 0x5f, 0x11, 0x53, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x10, 0xe7, 0x8a, 0xc3, 0x02, 0x09, 0x5a, 0xa9, 0x8c, 0xf7, 0x1c, 0x9d, 0xf3,
            0x2c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x02, 0x30,
            0xb4, 0x92, 0x4a, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xe1, 0x6c, 0x28,
            0x14, 0x6e, 0xd4, 0x86, 0x9c, 0x19, 0x0b, 0x3f, 0x0b, 0xdc, 0x18, 0xd8, 0x0d, 0x45,
            0xf9, 0x21, 0x34, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26,
            0x6a, 0x24, 0xaa, 0x21, 0xa9, 0xed, 0xc2, 0x96, 0x8b, 0xc8, 0xfb, 0xed, 0xc8, 0xb0,
            0xe4, 0xe9, 0x14, 0x37, 0x61, 0xf4, 0x50, 0xac, 0x4c, 0x6d, 0x93, 0xf9, 0x3a, 0x09,
            0xd6, 0x72, 0xae, 0x32, 0xd0, 0xc4, 0x74, 0x56, 0x93, 0xe3, 0x00, 0x00, 0x00, 0x00,
            0x1c, 0x49, 0x39, 0x32, 0x30, 0x71, 0xe1, 0xb5, 0x3c, 0x0e, 0x0f, 0x51, 0x40, 0xe4,
            0xe4, 0xfe, 0x5d, 0x2e, 0x93, 0xdc, 0xcf, 0x9c, 0x5e, 0x80, 0x94, 0x17, 0x69, 0x0f,
            0xdf, 0x90, 0xc2, 0xc5, 0x08, 0x4b, 0x0e, 0x4c, 0x88, 0x33, 0xab, 0xf0, 0xde, 0xb8,
            0x79, 0x4f, 0x83, 0x5e, 0x87, 0xb6, 0xe3, 0x5e, 0x6f, 0x10, 0x93, 0x8b, 0x01, 0xb8,
            0xf5, 0x6c, 0x83, 0x59, 0xf2, 0x7d, 0x58, 0x55, 0xc9, 0x5c, 0x8b, 0x85, 0x69, 0x23,
            0xa3, 0x7f, 0xf0, 0xd0, 0x95, 0x88, 0x74, 0x14, 0xe8, 0x61, 0x67, 0x52, 0xf5, 0x80,
            0x7f, 0xe5, 0x1b, 0x00, 0x66, 0xba, 0x59, 0x93, 0xb6, 0x88, 0xdc, 0xd8, 0x5b, 0x39,
            0x0c, 0xca, 0xf4, 0x63, 0x29, 0x00, 0x74, 0xeb, 0xd3, 0xfa, 0xcf, 0xf7, 0x5d, 0x8f,
            0x5b, 0xbe, 0xb9, 0x00, 0x37, 0x4e, 0x30, 0x84, 0x61, 0x7b, 0x52, 0xd3, 0xb0, 0x4e,
            0x68, 0x6e, 0x51, 0xe6, 0x42, 0xb0, 0x10, 0x23, 0xcb, 0xaf, 0x38, 0x39, 0xdf, 0x84,
            0xf6, 0xa5, 0x49, 0xf2, 0x86, 0xe5, 0xbd, 0x02, 0x25, 0x64, 0xc8, 0x46, 0x20, 0xac,
            0x99, 0xa7, 0xaf, 0xf1, 0x1f, 0xb7, 0x33, 0x26, 0xc8, 0xbd, 0x9c, 0x07, 0xe0, 0xcb,
            0x41, 0x41, 0x18, 0xaf, 0xfa, 0x47, 0xde, 0xff, 0x0d, 0xa3, 0xf5, 0x87, 0x54, 0x5a,
            0x78, 0x52, 0x9b, 0x5e, 0x4e, 0x16, 0x0d, 0xa0, 0x09, 0x1e, 0xc9, 0x22, 0x51, 0xb3,
            0xfd, 0x44, 0x28, 0xf4, 0xbb, 0xca, 0x3f, 0x15, 0x97, 0xf1, 0x1b, 0x0d, 0x31, 0xbf,
            0x52, 0x47, 0x97, 0xb1, 0xed, 0x76, 0x96, 0x26, 0x88, 0x79, 0x07, 0x3b, 0x14, 0x9b,
            0x59, 0x45, 0xb9, 0x29, 0xe8, 0x58, 0x8b, 0x67, 0xf5, 0x79, 0xff, 0xf8, 0xa1, 0xd0,
            0xed, 0xd1, 0x69, 0x66, 0x03, 0xf8, 0x1f, 0x55, 0x77, 0x39, 0xe7, 0x7e, 0xd9, 0x45,
            0x50, 0x33, 0xda, 0xcb, 0xbf, 0x14, 0xfc, 0x31, 0xc4, 0x41, 0xfd, 0x9d, 0x2e, 0xd6,
            0x72, 0xd5, 0xef, 0xc0, 0x74, 0x0a, 0x17, 0x27, 0x00, 0xc0, 0x48, 0x78, 0xa7, 0x6c,
            0xa9, 0x78, 0x21, 0xce, 0x40, 0x6b, 0x38, 0x2c, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x04,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0xe2, 0xf6, 0x1c, 0x3f, 0x71, 0xd1, 0xde, 0xfd, 0x3f, 0xa9,
            0x99, 0xdf, 0xa3, 0x69, 0x53, 0x75, 0x5c, 0x69, 0x06, 0x89, 0x79, 0x99, 0x62, 0xb4,
            0x8b, 0xeb, 0xd8, 0x36, 0x97, 0x4e, 0x8c, 0xf9, 0x7d, 0x24, 0xdb, 0x2b, 0xfa, 0x41,
            0x47, 0x4b, 0xfb, 0x2f, 0x87, 0x7d, 0x68, 0x8f, 0xac, 0x5f, 0xaa, 0x5e, 0x10, 0xa2,
            0x80, 0x8c, 0xf9, 0xde, 0x30, 0x73, 0x70, 0xb9, 0x33, 0x52, 0xe5, 0x48, 0x94, 0x85,
            0x7d, 0x3e, 0x08, 0x91, 0x8f, 0x70, 0x39, 0x5d, 0x92, 0x06, 0x41, 0x0f, 0xbf, 0xa9,
            0x42, 0xf1, 0xa8, 0x89, 0xaa, 0x5a, 0xb8, 0x18, 0x8e, 0xc3, 0x3c, 0x2f, 0x6e, 0x20,
            0x7d, 0xc7, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x9e, 0xfd, 0xde, 0x6a,
            0x19, 0x52, 0x8e, 0xe8, 0xde, 0x91, 0xb8, 0x04, 0x1a, 0x46, 0x2b, 0x29, 0xd7, 0x62,
            0x37, 0x91, 0x2a, 0x67, 0xfe, 0x88, 0x92, 0x59, 0x6d, 0xb8, 0x67, 0x65, 0xf4, 0xbc,
            0x2e, 0xe8, 0x6b, 0x58, 0x2b, 0x9a, 0xd6, 0x33, 0x95, 0xc7, 0xcd, 0x3b, 0xa3, 0xbd,
            0x72, 0xd2, 0xdd, 0x35, 0x48, 0x6e, 0xde, 0x77, 0xd5, 0x23, 0x64, 0xb6, 0xce, 0x2d,
            0x33, 0xa4, 0xf3, 0x66, 0xd9, 0x37, 0x95, 0x61, 0x9f, 0x6f, 0x01, 0x1a, 0xa5, 0x04,
            0x1d, 0x41, 0x08, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
            0xff, 0xff, 0x06, 0x03, 0x69, 0xc9, 0x3c, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0x01,
            0x10, 0x25, 0xab, 0xe0, 0xe8, 0x00, 0x00, 0x00, 0x23, 0x21, 0x03, 0x82, 0x91, 0x25,
            0xd3, 0x5f, 0xad, 0x23, 0xdc, 0xc6, 0x52, 0x6e, 0x73, 0xbc, 0xe0, 0xb1, 0x8a, 0xa7,
            0xc0, 0x89, 0x7e, 0x0f, 0xc5, 0xd3, 0x9e, 0x75, 0xe4, 0x3a, 0xf9, 0x6b, 0x95, 0x07,
            0x48, 0xac, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x3e, 0x5a, 0x6b,
            0x7e, 0x84, 0x2d, 0x8c, 0x07, 0xe8, 0x4e, 0x43, 0x93, 0x5c, 0xdc, 0xbf, 0x80, 0xf6,
            0x30, 0x08, 0x52, 0xfc, 0x25, 0xf3, 0x36, 0x7d, 0x32, 0x84, 0xd8, 0x33, 0x7a, 0xe6,
            0xf0, 0x01, 0x00, 0x00, 0x00, 0xd9, 0x00, 0x47, 0x30, 0x44, 0x02, 0x20, 0x0d, 0xf5,
            0x9d, 0xc1, 0x94, 0x6a, 0xb9, 0x1f, 0xad, 0x82, 0x00, 0xdd, 0xd9, 0xb8, 0x89, 0xec,
            0xa1, 0x92, 0xa3, 0xa6, 0x42, 0xa9, 0x4a, 0x75, 0x55, 0xe6, 0x25, 0x1f, 0x89, 0x89,
            0x6d, 0x7a, 0x02, 0x20, 0x59, 0xc3, 0xc5, 0x41, 0x31, 0xf5, 0xdf, 0xc8, 0x7c, 0x90,
            0x99, 0x26, 0x33, 0x4d, 0x13, 0x2e, 0x3a, 0x21, 0x8b, 0x68, 0x0b, 0x6e, 0x5c, 0x79,
            0x6e, 0x62, 0x90, 0x88, 0x10, 0x15, 0x64, 0x6d, 0x01, 0x47, 0x30, 0x44, 0x02, 0x20,
            0x70, 0xe7, 0x51, 0xc6, 0x2e, 0x5b, 0x82, 0x85, 0x0d, 0x08, 0xc3, 0x93, 0x57, 0x70,
            0x80, 0xba, 0x33, 0x0e, 0x93, 0x8b, 0x0a, 0x9a, 0x34, 0xe0, 0xaa, 0xe3, 0x78, 0xe1,
            0x5b, 0x53, 0x20, 0x9e, 0x02, 0x20, 0x45, 0x85, 0x5e, 0x90, 0xbb, 0xde, 0x5a, 0x02,
            0x1f, 0x63, 0xe0, 0x63, 0xe2, 0x57, 0xa7, 0x0e, 0x9a, 0x96, 0x04, 0xde, 0x69, 0x8a,
            0xf4, 0x64, 0x9e, 0xaf, 0x26, 0x0e, 0xba, 0x5a, 0x8c, 0x38, 0x01, 0x47, 0x52, 0x21,
            0x02, 0xad, 0xf2, 0xcb, 0x5a, 0xfd, 0x73, 0x01, 0x71, 0xa4, 0x25, 0xd2, 0x63, 0x01,
            0x4e, 0x93, 0x07, 0xd7, 0x1e, 0xdc, 0x35, 0x2b, 0x4c, 0x3c, 0x9b, 0x75, 0x0e, 0xec,
            0xdc, 0x95, 0xef, 0x70, 0xd0, 0x21, 0x02, 0x71, 0x05, 0x67, 0x2d, 0x0c, 0xf8, 0x26,
            0x9c, 0xa3, 0xea, 0x75, 0x77, 0x54, 0x04, 0x9e, 0xee, 0xc8, 0xda, 0x3a, 0x7e, 0x96,
            0x3d, 0x27, 0x86, 0xf0, 0x65, 0x47, 0xcd, 0x78, 0xc2, 0x8b, 0xe2, 0x52, 0xae, 0xff,
            0xff, 0xff, 0xff, 0x02, 0x71, 0x47, 0x5a, 0xd2, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76,
            0xa9, 0x14, 0x68, 0x01, 0x91, 0xe9, 0xa6, 0x0f, 0xd3, 0xb7, 0x52, 0x87, 0x59, 0x1e,
            0x09, 0xbe, 0x1f, 0x6b, 0xd5, 0xef, 0x8e, 0x32, 0x88, 0xac, 0x69, 0xa4, 0xcf, 0x26,
            0x75, 0x05, 0x00, 0x00, 0x17, 0xa9, 0x14, 0xdb, 0x72, 0x65, 0x34, 0x36, 0xf2, 0x58,
            0x84, 0xf2, 0xab, 0x2b, 0xf0, 0x50, 0xd0, 0x6e, 0x80, 0x59, 0x07, 0xdd, 0x0e, 0x87,
            0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x3d, 0xe7, 0xf4, 0x0e, 0x3f,
            0x59, 0x17, 0xc8, 0xec, 0xcc, 0x1b, 0x79, 0xae, 0x10, 0x78, 0x49, 0xb5, 0xca, 0xf8,
            0x07, 0xe4, 0x89, 0xd6, 0x98, 0xd4, 0x84, 0xcb, 0xae, 0xeb, 0x62, 0xa0, 0x98, 0x01,
            0x00, 0x00, 0x00, 0x6b, 0x48, 0x30, 0x45, 0x02, 0x21, 0x00, 0xa5, 0x5c, 0x5d, 0xb2,
            0x95, 0x90, 0x56, 0xe6, 0xfb, 0xfc, 0xee, 0x67, 0xc4, 0xc8, 0x07, 0xcd, 0x48, 0x17,
            0x02, 0x2b, 0x28, 0x54, 0x60, 0xe1, 0x4c, 0x9b, 0x6e, 0x73, 0xaf, 0xf3, 0x0b, 0x2f,
            0x02, 0x20, 0x44, 0xe8, 0x5f, 0x1a, 0x28, 0xd1, 0x44, 0xa2, 0x09, 0x21, 0xb2, 0xa3,
            0x7b, 0x7e, 0xe1, 0x01, 0x2c, 0x9f, 0xe5, 0x91, 0x33, 0x25, 0x33, 0x85, 0xdb, 0x2e,
            0xe8, 0xf2, 0x7e, 0xbd, 0xb8, 0x95, 0x01, 0x21, 0x03, 0x98, 0xdf, 0xc5, 0xe9, 0x3a,
            0x5a, 0x54, 0x12, 0xd5, 0x41, 0x03, 0xb1, 0x08, 0xcf, 0xd9, 0x6a, 0x40, 0x4b, 0xc2,
            0xf7, 0x42, 0x50, 0x8e, 0x1f, 0xe1, 0xca, 0xa0, 0x23, 0x16, 0xdb, 0x5a, 0x39, 0xfe,
            0xff, 0xff, 0xff, 0x02, 0x00, 0x88, 0x52, 0x6a, 0x74, 0x00, 0x00, 0x00, 0x19, 0x76,
            0xa9, 0x14, 0x48, 0x87, 0xab, 0x64, 0x91, 0x25, 0x30, 0xc2, 0xe9, 0xbf, 0xe7, 0x25,
            0xcb, 0x88, 0x8a, 0xfd, 0x06, 0x84, 0x27, 0x9b, 0x88, 0xac, 0xd1, 0xa2, 0xe1, 0x2a,
            0x23, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xbb, 0x4f, 0x24, 0xd3, 0xbd, 0xd2,
            0x8a, 0x7f, 0xae, 0x0d, 0x41, 0x5e, 0x41, 0x29, 0x24, 0xce, 0x37, 0x4f, 0x52, 0xd6,
            0x88, 0xac, 0x65, 0xc9, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x75, 0xc8, 0x22,
            0xc4, 0x11, 0xe3, 0x24, 0xc8, 0x55, 0xde, 0x50, 0x1b, 0xce, 0xa3, 0x65, 0xa7, 0x6c,
            0xec, 0x3e, 0xaa, 0xef, 0xe6, 0x4c, 0xbd, 0xc6, 0x66, 0xf7, 0xdc, 0x6a, 0xe3, 0x40,
            0x91, 0x00, 0x00, 0x00, 0x00, 0x6a, 0x47, 0x30, 0x44, 0x02, 0x20, 0x5a, 0x85, 0x3b,
            0x71, 0x0c, 0x83, 0x50, 0x4b, 0x53, 0xfe, 0x81, 0x7c, 0xce, 0x15, 0x1a, 0x47, 0xd7,
            0x59, 0x92, 0x27, 0xe6, 0x87, 0x99, 0xa5, 0xf0, 0xd1, 0xac, 0x98, 0x55, 0x3a, 0xc9,
            0xae, 0x02, 0x20, 0x0d, 0xa1, 0x24, 0x49, 0x33, 0xd3, 0x56, 0xae, 0xe4, 0xbd, 0x2c,
            0xee, 0x85, 0x1b, 0x09, 0x18, 0x21, 0xf3, 0xa9, 0xff, 0x6c, 0xc0, 0x28, 0x71, 0xd0,
            0xe9, 0x6a, 0xeb, 0x83, 0x25, 0xa4, 0xd8, 0x01, 0x21, 0x03, 0x98, 0xdf, 0xc5, 0xe9,
            0x3a, 0x5a, 0x54, 0x12, 0xd5, 0x41, 0x03, 0xb1, 0x08, 0xcf, 0xd9, 0x6a, 0x40, 0x4b,
            0xc2, 0xf7, 0x42, 0x50, 0x8e, 0x1f, 0xe1, 0xca, 0xa0, 0x23, 0x16, 0xdb, 0x5a, 0x39,
            0xfe, 0xff, 0xff, 0xff, 0x02, 0x00, 0x88, 0x52, 0x6a, 0x74, 0x00, 0x00, 0x00, 0x19,
            0x76, 0xa9, 0x14, 0x01, 0xca, 0xcd, 0x40, 0xa9, 0xe3, 0x7f, 0x95, 0x4f, 0x40, 0x2b,
            0x34, 0xc8, 0x7b, 0x62, 0x35, 0xa0, 0x7a, 0xec, 0x98, 0x88, 0xac, 0x38, 0xfc, 0x3b,
            0x90, 0x95, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xa4, 0x7f, 0x59, 0xad, 0x6b,
            0xa4, 0xa8, 0xe9, 0x1d, 0x1c, 0xd4, 0xea, 0xb0, 0x63, 0xac, 0x57, 0xca, 0x20, 0xda,
            0x8e, 0x88, 0xac, 0x65, 0xc9, 0x3c, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x11, 0xc5,
            0x58, 0x34, 0x3e, 0x27, 0x24, 0x96, 0x57, 0x1b, 0xb6, 0x83, 0x10, 0x05, 0x73, 0x53,
            0x7f, 0xb8, 0xef, 0x72, 0x86, 0x07, 0xde, 0xa2, 0x44, 0xf1, 0x5e, 0xed, 0xac, 0xec,
            0x4f, 0x9b, 0x01, 0x00, 0x00, 0x00, 0xd9, 0x00, 0x47, 0x30, 0x44, 0x02, 0x20, 0x34,
            0x47, 0xed, 0xa7, 0xf0, 0x04, 0x86, 0x56, 0x91, 0x38, 0x9a, 0x8d, 0x0f, 0x96, 0xb0,
            0x3b, 0xe7, 0x7a, 0x3e, 0x7b, 0x4a, 0x4a, 0x6c, 0x5c, 0x99, 0xd1, 0xdf, 0x8e, 0x2f,
            0x41, 0xbd, 0x8e, 0x02, 0x20, 0x05, 0x12, 0xee, 0x26, 0x04, 0x9b, 0xe6, 0xd5, 0x4d,
            0x9f, 0x98, 0xf8, 0x5a, 0xb1, 0xd5, 0x57, 0x32, 0x85, 0xf4, 0x46, 0x78, 0xac, 0x98,
            0x7b, 0xbf, 0x60, 0x6b, 0x3d, 0x26, 0x9b, 0x30, 0xbf, 0x01, 0x47, 0x30, 0x44, 0x02,
            0x20, 0x34, 0x6b, 0xbb, 0x3f, 0x1b, 0xaa, 0x62, 0x54, 0x99, 0x74, 0xe7, 0x5b, 0x10,
            0x90, 0x6a, 0x07, 0xce, 0xe6, 0x4b, 0x5e, 0x08, 0xdc, 0xc2, 0x91, 0x96, 0x10, 0x0a,
            0xbd, 0x6e, 0x74, 0x86, 0xc5, 0x02, 0x20, 0x1d, 0xe2, 0x89, 0x32, 0xd8, 0x6a, 0x7f,
            0xb8, 0x2f, 0xba, 0xa8, 0xd4, 0x3c, 0xea, 0x59, 0x54, 0x00, 0xe2, 0xe9, 0xa5, 0x83,
            0x9c, 0xcd, 0xa4, 0x37, 0x39, 0x2c, 0x94, 0x77, 0x0b, 0x16, 0xc8, 0x01, 0x47, 0x52,
            0x21, 0x02, 0xe6, 0xbd, 0xe2, 0x11, 0x6c, 0x3a, 0xa6, 0x7f, 0xd4, 0x90, 0x6d, 0xa2,
            0x8b, 0xb7, 0x44, 0xa5, 0xed, 0x80, 0x8a, 0x15, 0x24, 0xe7, 0x71, 0x81, 0xee, 0xb1,
            0x7a, 0xf4, 0xdb, 0x11, 0x92, 0xac, 0x21, 0x02, 0xa0, 0xb0, 0x15, 0x98, 0x09, 0x7d,
            0xe0, 0x8a, 0x08, 0x38, 0xb7, 0x79, 0x15, 0x5c, 0x40, 0x5d, 0x6d, 0xac, 0xd3, 0xa8,
            0x16, 0xb6, 0x1d, 0x18, 0x80, 0x90, 0x17, 0x1c, 0x00, 0xb1, 0xae, 0x7c, 0x52, 0xae,
            0xff, 0xff, 0xff, 0xff, 0x02, 0xe5, 0x55, 0x5b, 0x7b, 0x00, 0x00, 0x00, 0x00, 0x19,
            0x76, 0xa9, 0x14, 0x6e, 0xec, 0xe0, 0x18, 0x04, 0x45, 0xac, 0x8b, 0x38, 0x36, 0x53,
            0x96, 0xed, 0x74, 0x30, 0xbd, 0x59, 0x09, 0xc5, 0xf6, 0x88, 0xac, 0x4e, 0x68, 0x15,
            0xa4, 0x94, 0x08, 0x00, 0x00, 0x17, 0xa9, 0x14, 0x0a, 0xa2, 0x6a, 0x00, 0x2d, 0x22,
            0xf8, 0x8c, 0x1f, 0x83, 0xd3, 0x52, 0x98, 0xdc, 0x64, 0x76, 0x9f, 0xe6, 0xe8, 0x1a,
            0x87, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x80, 0x44, 0x6f, 0x4f,
            0xb9, 0xd2, 0xbb, 0x5b, 0xcf, 0x7b, 0x8b, 0x19, 0xf7, 0x67, 0x0b, 0x7c, 0x84, 0xe1,
            0x9b, 0x10, 0x6e, 0x6a, 0xeb, 0x4b, 0x1e, 0x0e, 0x9b, 0xe1, 0x64, 0xb2, 0xc7, 0x27,
            0x00, 0x00, 0x00, 0x00, 0x6a, 0x47, 0x30, 0x44, 0x02, 0x20, 0x32, 0xe6, 0xa4, 0x69,
            0xa1, 0x9c, 0x41, 0xb3, 0x49, 0x0e, 0xfd, 0x29, 0x68, 0xb5, 0x29, 0x3e, 0x24, 0x73,
            0xcd, 0x5e, 0xad, 0xb7, 0xb1, 0x9c, 0x1f, 0x7c, 0x85, 0xae, 0xdd, 0x0f, 0x9d, 0x3a,
            0x02, 0x20, 0x76, 0x40, 0x7f, 0xd7, 0x54, 0x02, 0x9d, 0xcd, 0x54, 0x20, 0x6b, 0x25,
            0x44, 0x5f, 0x42, 0x7b, 0xdd, 0x47, 0xbe, 0x9b, 0x68, 0xb5, 0x5f, 0xf8, 0x10, 0x4f,
            0x43, 0xfb, 0x05, 0xf2, 0x95, 0x70, 0x01, 0x21, 0x02, 0x87, 0x16, 0x19, 0xfc, 0xca,
            0x09, 0xa3, 0x91, 0xa0, 0xf9, 0x41, 0x56, 0xe2, 0xf6, 0xa0, 0x3e, 0x11, 0x43, 0xf7,
            0x31, 0xbb, 0xe0, 0xce, 0xa5, 0xaa, 0xd3, 0x88, 0x30, 0xdf, 0x7f, 0x9c, 0xaa, 0xff,
            0xff, 0xff, 0xff, 0x01, 0x00, 0x84, 0xd7, 0x17, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76,
            0xa9, 0x14, 0xad, 0xa1, 0xdf, 0x3b, 0x87, 0xe6, 0x3c, 0x79, 0xfb, 0x44, 0x3b, 0xdf,
            0xfe, 0x46, 0xfd, 0x13, 0xfc, 0x83, 0x66, 0x98, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00,
            0x01, 0x00, 0x00, 0x00, 0x01, 0x7c, 0xa2, 0xd9, 0xcb, 0x3a, 0x4e, 0x9c, 0x68, 0x86,
            0x2c, 0x76, 0x3e, 0x87, 0xe2, 0xad, 0x1e, 0xd0, 0x8e, 0x44, 0x70, 0xb4, 0x46, 0x43,
            0x83, 0x32, 0xef, 0x0c, 0xc2, 0xba, 0x04, 0xae, 0xaf, 0x01, 0x00, 0x00, 0x00, 0xd8,
            0x00, 0x47, 0x30, 0x44, 0x02, 0x20, 0x0b, 0x05, 0x7f, 0xdc, 0xc9, 0x72, 0xb8, 0xb0,
            0xf0, 0x51, 0x11, 0xda, 0x0d, 0x9f, 0x86, 0xf4, 0x09, 0x7a, 0xdc, 0x3a, 0xb2, 0xb5,
            0x9f, 0x63, 0x87, 0x0a, 0x12, 0x6d, 0xa0, 0x80, 0x73, 0xd3, 0x02, 0x20, 0x35, 0x54,
            0xa2, 0x0a, 0xa7, 0xcd, 0x6c, 0x7b, 0x20, 0x09, 0x8b, 0x80, 0x1b, 0xc8, 0x76, 0x43,
            0x67, 0x04, 0x86, 0x4b, 0x77, 0x2d, 0xca, 0x20, 0x19, 0x9b, 0xbf, 0x60, 0x6e, 0x34,
            0x12, 0x63, 0x01, 0x46, 0x30, 0x43, 0x02, 0x20, 0x12, 0xc2, 0xff, 0xf7, 0x8b, 0xc7,
            0xd8, 0x38, 0x75, 0xb2, 0x1d, 0xc7, 0x2f, 0x7b, 0xc7, 0x6a, 0x2d, 0x9f, 0xd1, 0x96,
            0x84, 0xe1, 0xc6, 0x00, 0x80, 0x7a, 0xe4, 0xd5, 0xda, 0x7d, 0xa3, 0x8d, 0x02, 0x1f,
            0x2e, 0x12, 0xf0, 0x71, 0x22, 0xc1, 0x6b, 0x14, 0xc6, 0x4e, 0xa5, 0x23, 0xae, 0x79,
            0xda, 0x5e, 0xb8, 0x46, 0xc7, 0x34, 0x46, 0x3c, 0x64, 0x8f, 0x8b, 0x2c, 0x9c, 0x89,
            0xef, 0xa1, 0xc2, 0x01, 0x47, 0x52, 0x21, 0x02, 0xe6, 0xbd, 0xe2, 0x11, 0x6c, 0x3a,
            0xa6, 0x7f, 0xd4, 0x90, 0x6d, 0xa2, 0x8b, 0xb7, 0x44, 0xa5, 0xed, 0x80, 0x8a, 0x15,
            0x24, 0xe7, 0x71, 0x81, 0xee, 0xb1, 0x7a, 0xf4, 0xdb, 0x11, 0x92, 0xac, 0x21, 0x02,
            0xa0, 0xb0, 0x15, 0x98, 0x09, 0x7d, 0xe0, 0x8a, 0x08, 0x38, 0xb7, 0x79, 0x15, 0x5c,
            0x40, 0x5d, 0x6d, 0xac, 0xd3, 0xa8, 0x16, 0xb6, 0x1d, 0x18, 0x80, 0x90, 0x17, 0x1c,
            0x00, 0xb1, 0xae, 0x7c, 0x52, 0xae, 0xff, 0xff, 0xff, 0xff, 0x02, 0xa0, 0x95, 0x5e,
            0x39, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0x80, 0xfa, 0x46, 0xec, 0x1f,
            0xfb, 0x24, 0xac, 0x6c, 0xd4, 0xb8, 0x8e, 0x3c, 0xae, 0xb3, 0x9f, 0x1a, 0xb1, 0xe1,
            0xf5, 0x88, 0xac, 0xd0, 0xcb, 0xef, 0xef, 0x5b, 0x00, 0x00, 0x00, 0x17, 0xa9, 0x14,
            0x0a, 0xa2, 0x6a, 0x00, 0x2d, 0x22, 0xf8, 0x8c, 0x1f, 0x83, 0xd3, 0x52, 0x98, 0xdc,
            0x64, 0x76, 0x9f, 0xe6, 0xe8, 0x1a, 0x87, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
            0x00, 0x01, 0xe8, 0xcd, 0xd5, 0x06, 0x02, 0xb1, 0xa6, 0x1d, 0xe1, 0xf3, 0xc6, 0xa2,
            0x38, 0xcb, 0xf1, 0x27, 0xcf, 0x58, 0xfc, 0x67, 0xe5, 0xbd, 0x32, 0x0d, 0x9e, 0xfd,
            0x1a, 0xd7, 0xe0, 0xa6, 0x01, 0xdf, 0x00, 0x00, 0x00, 0x00, 0x6a, 0x47, 0x30, 0x44,
            0x02, 0x20, 0x6c, 0xba, 0xee, 0xf4, 0xc6, 0x80, 0x0f, 0x8e, 0x50, 0x9d, 0x67, 0xb8,
            0x13, 0xda, 0x2d, 0xf6, 0x09, 0xb6, 0x43, 0x0a, 0xd3, 0xe5, 0x7d, 0x5c, 0x8d, 0xb9,
            0x96, 0xbc, 0x92, 0x70, 0xb2, 0xe6, 0x02, 0x20, 0x54, 0x2e, 0xb0, 0xac, 0xfd, 0xd0,
            0xf8, 0x25, 0x9b, 0x7b, 0x43, 0xd4, 0xaf, 0x60, 0x24, 0x82, 0xf0, 0x29, 0x57, 0x7d,
            0xee, 0x7c, 0xd7, 0x7a, 0xea, 0x55, 0xf7, 0xb4, 0xe4, 0xca, 0x6e, 0x00, 0x01, 0x21,
            0x03, 0x9b, 0xf9, 0xfe, 0x5b, 0x8e, 0x5b, 0xe2, 0x3b, 0x40, 0x6e, 0x4c, 0x48, 0x25,
            0x67, 0x5a, 0xa5, 0x72, 0xcd, 0xcb, 0x38, 0xea, 0xb0, 0x6d, 0xb4, 0x5e, 0xd1, 0x4e,
            0x1d, 0x69, 0x41, 0xa8, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0x03, 0x00, 0x0c, 0x77, 0x42,
            0x03, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xac, 0xc7, 0xb7, 0x8c, 0x37, 0x51,
            0x10, 0x3f, 0xa6, 0xf8, 0x55, 0x33, 0xc7, 0x61, 0x3c, 0x4c, 0x19, 0xe0, 0x4f, 0x2f,
            0x88, 0xac, 0x8e, 0x68, 0xde, 0x13, 0x19, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14,
            0x60, 0x5b, 0xd7, 0xcb, 0x9e, 0x96, 0xb1, 0x1b, 0xb0, 0xeb, 0x01, 0xc6, 0x5f, 0xe0,
            0x49, 0xf8, 0x39, 0xec, 0xaf, 0x13, 0x88, 0xac, 0x00, 0x3b, 0xdf, 0x9b, 0x03, 0x00,
            0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0x6c, 0x53, 0xcd, 0x10, 0x02, 0x5e, 0xe8, 0x69,
            0xfc, 0xb8, 0x3d, 0xb9, 0x4d, 0x9f, 0xf5, 0x1e, 0xc8, 0xd3, 0x22, 0xb7, 0x88, 0xac,
            0x65, 0xc9, 0x3c, 0x00,
        ];
        let block_size = 2818;
        let inner = Cursor::new(raw_data);
        let mut reader = BufReader::with_capacity(block_size, inner);

        // Parse block
        let block = reader.read_block(block_size as u32, &dogecoin).unwrap();

        // Block Header
        assert_eq!(0x620104, block.header.value.version);
        assert_eq!(
            "204fa085d6ac48f0f4776e158d6c88100d8735d5abd47cbb020f8f59045300ee",
            format!("{}", &block.header.value.prev_hash)
        );
        assert_eq!(
            "5c9331172b9256b6a4f538f3f42022a710ac65316e5a75cd02a9527d86ca5bd9",
            format!("{}", &block.header.value.merkle_root)
        );
        assert_eq!(
            "038ca8bfebd5d35e9c676b459f2c6ba4c03975ba653414b303d7bc4ac6fa787f",
            format!("{}", &block.header.hash)
        );

        // Check against computed merkle root
        assert_eq!(
            &block.header.value.merkle_root,
            &block.compute_merkle_root()
        );
        assert_eq!(1637169061, block.header.value.timestamp);
        assert_eq!(0x1a04a51b, block.header.value.bits);
        assert_eq!(0, block.header.value.nonce);

        // AuxPoWBlock
        let aux_pow_block = block.aux_pow_extension.unwrap();
        // AuxPoWBlock coinbase tx
        assert_eq!(0x01, aux_pow_block.coinbase_tx.version);
        assert_eq!(0x00, aux_pow_block.coinbase_tx.locktime);
        assert_eq!(
            "c5c290df0f691794805e9ccfdc932e5dfee4e440510f0e3cb5e171303239491c",
            format!("{}", &aux_pow_block.block_hash)
        );

        // TODO: verify AuxPowBlock merkle branches

        assert_eq!(
            "bcf46567b86d599288fe672a913762d7292b461a04b891dee88e52196adefd9e",
            format!("{}", &aux_pow_block.parent_block.prev_hash)
        );

        // Tx
        assert_eq!(
            "dc8dbed0461ec54a9524fc12fbed7466e6acb0f0637fcb2a0111174c84753fec",
            format!("{}", &block.txs[0].hash)
        );
        assert_eq!(8, block.tx_count.value);
        assert_eq!(0x00000001, block.txs[0].value.version);

        // Tx Inputs
        assert_eq!(0x01, block.txs[0].value.in_count.value);
        assert_eq!(
            "0000000000000000000000000000000000000000000000000000000000000000",
            format!("{}", &block.txs[0].value.inputs[0].outpoint.txid)
        );
        assert_eq!(0xffffffff, block.txs[0].value.inputs[0].outpoint.index);
        assert_eq!(6, block.txs[0].value.inputs[0].script_len.value);
        assert_eq!(
            "0369c93c0101",
            utils::arr_to_hex(&block.txs[0].value.inputs[0].script_sig)
        );
        assert_eq!(0xffffffff, block.txs[0].value.inputs[0].seq_no);

        // Tx Outputs
        assert_eq!(0x01, block.txs[0].value.out_count.value);
        assert_eq!(1000201725200, block.txs[0].value.outputs[0].out.value);
        assert_eq!(0x23, block.txs[0].value.outputs[0].out.script_len.value);

        let script_pubkey = &block.txs[0].value.outputs[0].out.script_pubkey;
        assert_eq!(
            "2103829125d35fad23dcc6526e73bce0b18aa7c0897e0fc5d39e75e43af96b950748ac",
            utils::arr_to_hex(&script_pubkey)
        );
        assert_eq!(0x00000000, block.txs[0].value.locktime);

        assert_eq!(
            Some(String::from("DEfXb18bE8RoC6edc9jXaMpEpuvVkcjJFq")),
            script::eval_from_bytes(script_pubkey, Dogecoin.version_id()).address
        );
    }

    #[test]
    fn test_xor_reader_with_key() {
        let data = Cursor::new(vec![
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
        ]);
        let mut reader = XorReader::new(
            data,
            Some(vec![0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
        );

        let mut buf = vec![0; 14];
        reader.read_exact(&mut buf).unwrap();
        assert_eq!(buf, vec![0; 14]);
    }

    #[test]
    fn test_xor_reader_without_key() {
        let data = Cursor::new(vec![
            0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
        ]);
        let mut reader = XorReader::new(data, None);

        let mut buf = vec![0; 14];
        reader.read_exact(&mut buf).unwrap();
        assert_eq!(
            buf,
            vec![
                0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
            ]
        );
    }
}