@@ -454,6 +454,177 @@ TEST(btc_inputs_validator_tests, btc_txn_helper_verify_input_p2wpkh_fail) {
454
454
TEST_ASSERT_EQUAL (BTC_VALIDATE_ERR_INVALID_TX_HASH , status );
455
455
}
456
456
457
+ TEST (btc_inputs_validator_tests , btc_txn_helper_verify_input_p2wpkh_in_p2sh ) {
458
+ /* Test data source: Bip143
459
+ * https://blockstream.info/testnet/tx/b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a2?expand
460
+ */
461
+ uint8_t raw_txn [2000 ] = {0 };
462
+ hex_string_to_byte_array (
463
+ "0200000000010258afb1ece76f01c24f4935f453d210518163cb1d0383eaec331b202ebe"
464
+ "b5e389"
465
+ "0000000017160014a76cad25cb569bb47305513ebedd6011dc419deeffffffff2b3682b3"
466
+ "592588"
467
+ "5001f0e321df28d4ac675a9cbbccef2a69533bea7c5e5ad2c40000000017160014a76cad"
468
+ "25cb56"
469
+ "9bb47305513ebedd6011dc419deeffffffff02941100000000000017a914bd7aabdeeef2"
470
+ "11b1bd"
471
+ "ad7218e14fea6c032101c087f22f00000000000017a914eaf97514c5ac1e41e413502e97"
472
+ "ae42eb"
473
+ "f27ace3a870247304402206e038f4712541d699697ed55efc41219df4f244fc72caa5edd"
474
+ "653837"
475
+ "f6555f6f02201cd8ea15b65fda17992abafaed86e066c3271ac16b9c46c54c2192438843"
476
+ "dd0401"
477
+ "21029f75e1ef6b04e004a308b1f59215a8a3a5b7958bbcf184cc24ba7ab6574448780248"
478
+ "304502"
479
+ "2100d15ce61648edc28b8b5a3531b80a1e8fc3b3eebe7d3fc4ca962cb04afc770dda0220"
480
+ "7c7eaf8"
481
+ "82d7fac45d2752f20e48d2f896715cbc5a3b0f5de3e19fea0da99beac0121029f75e1ef6"
482
+ "b04e004"
483
+ "a308b1f59215a8a3a5b7958bbcf184cc24ba7ab65744487800000000" ,
484
+ 838 ,
485
+ raw_txn );
486
+ // only fill necessary values
487
+ btc_sign_txn_input_t input [] = {{
488
+ .value = 4500 ,
489
+ .prev_output_index = 0x00000000 ,
490
+ .script_pub_key = {.size = 23 },
491
+ },
492
+ {
493
+ .value = 12274 ,
494
+ .prev_output_index = 0x00000001 ,
495
+ .script_pub_key = {.size = 23 },
496
+ }};
497
+
498
+ hex_string_to_byte_array (
499
+ "b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a2" ,
500
+ 64 ,
501
+ input [0 ].prev_txn_hash );
502
+ // Reverse order of txn-id:
503
+ // A244A293475AC245DEE8B3343F51EE2296BAB24FCC3FE0A49628DF605E2295B0
504
+ cy_reverse_byte_array (input [0 ].prev_txn_hash , sizeof (input [0 ].prev_txn_hash ));
505
+
506
+ hex_string_to_byte_array (
507
+ "b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a2" ,
508
+ 64 ,
509
+ input [1 ].prev_txn_hash );
510
+ // Reverse order of txn-id:
511
+ // A244A293475AC245DEE8B3343F51EE2296BAB24FCC3FE0A49628DF605E2295B0
512
+ cy_reverse_byte_array (input [1 ].prev_txn_hash , sizeof (input [1 ].prev_txn_hash ));
513
+
514
+ hex_string_to_byte_array ("a914bd7aabdeeef211b1bdad7218e14fea6c032101c087" ,
515
+ 46 ,
516
+ input [0 ].script_pub_key .bytes );
517
+ hex_string_to_byte_array ("a914eaf97514c5ac1e41e413502e97ae42ebf27ace3a87" ,
518
+ 46 ,
519
+ input [1 ].script_pub_key .bytes );
520
+
521
+ data_ptr = raw_txn ;
522
+ data_total_size = 838 / 2 ;
523
+
524
+ byte_stream_t stream = {
525
+ .stream_pointer = raw_txn ,
526
+ .writer = generic_writer ,
527
+ .offset = 0 ,
528
+ .capacity = 100 ,
529
+ };
530
+
531
+ btc_validation_error_e status = btc_validate_inputs (& stream , input );
532
+
533
+ TEST_ASSERT_EQUAL (BTC_VALIDATE_SUCCESS , status );
534
+
535
+ status = btc_validate_inputs (& stream , input + 1 );
536
+
537
+ TEST_ASSERT_EQUAL (BTC_VALIDATE_SUCCESS , status );
538
+ }
539
+
540
+ TEST (btc_inputs_validator_tests ,
541
+ btc_txn_helper_verify_input_p2wpkh_in_p2sh_fail ) {
542
+ /* Test data source: Bip143
543
+ * https://blockstream.info/testnet/tx/b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a2?expand
544
+ */
545
+ uint8_t raw_txn [2000 ] = {0 };
546
+ hex_string_to_byte_array (
547
+ "0200000000010258afb1ece76f01c24f4935f453d210518163cb1d0383eaec331b202ebe"
548
+ "b5e389"
549
+ "0000000017160014a76cad25cb569bb47305513ebedd6011dc419deeffffffff2b3682b3"
550
+ "592588"
551
+ "5001f0e321df28d4ac675a9cbbccef2a69533bea7c5e5ad2c40000000017160014a76cad"
552
+ "25cb56"
553
+ "9bb47305513ebedd6011dc419deeffffffff02941100000000000017a914bd7aabdeeef2"
554
+ "11b1bd"
555
+ "ad7218e14fea6c032101c087f22f00000000000017a914eaf97514c5ac1e41e413502e97"
556
+ "ae42eb"
557
+ "f27ace3a870247304402206e038f4712541d699697ed55efc41219df4f244fc72caa5edd"
558
+ "653837"
559
+ "f6555f6f02201cd8ea15b65fda17992abafaed86e066c3271ac16b9c46c54c2192438843"
560
+ "dd0401"
561
+ "21029f75e1ef6b04e004a308b1f59215a8a3a5b7958bbcf184cc24ba7ab6574448780248"
562
+ "304502"
563
+ "2100d15ce61648edc28b8b5a3531b80a1e8fc3b3eebe7d3fc4ca962cb04afc770dda0220"
564
+ "7c7eaf8"
565
+ "82d7fac45d2752f20e48d2f896715cbc5a3b0f5de3e19fea0da99beac0121029f75e1ef6"
566
+ "b04e004"
567
+ "a308b1f59215a8a3a5b7958bbcf184cc24ba7ab65744487800000000" ,
568
+ 838 ,
569
+ raw_txn );
570
+ // only fill necessary values
571
+ btc_sign_txn_input_t input [] = {{
572
+ .value = 4500 ,
573
+ .prev_output_index = 0x00000000 ,
574
+ .script_pub_key = {.size = 23 },
575
+ },
576
+ {
577
+ .value = 12274 ,
578
+ .prev_output_index = 0x00000001 ,
579
+ .script_pub_key = {.size = 23 },
580
+ }};
581
+
582
+ hex_string_to_byte_array (
583
+ // invalid txn hash test. valid txn hash/id:
584
+ // b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a2
585
+ "b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a3" ,
586
+ 64 ,
587
+ input [0 ].prev_txn_hash );
588
+ // Reverse order of txn-id:
589
+ // A344A293475AC245DEE8B3343F51EE2296BAB24FCC3FE0A49628DF605E2295B0
590
+ cy_reverse_byte_array (input [0 ].prev_txn_hash , sizeof (input [0 ].prev_txn_hash ));
591
+
592
+ hex_string_to_byte_array (
593
+ // invalid txn hash test. valid txn hash/id:
594
+ // b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a2
595
+ "b095225e60df2896a4e03fcc4fb2ba9622ee513f34b3e8de45c25a4793a244a3" ,
596
+ 64 ,
597
+ input [1 ].prev_txn_hash );
598
+ // Reverse order of txn-id:
599
+ // A344A293475AC245DEE8B3343F51EE2296BAB24FCC3FE0A49628DF605E2295B0
600
+ cy_reverse_byte_array (input [1 ].prev_txn_hash , sizeof (input [1 ].prev_txn_hash ));
601
+
602
+ hex_string_to_byte_array ("a914bd7aabdeeef211b1bdad7218e14fea6c032101c087" ,
603
+ 46 ,
604
+ input [0 ].script_pub_key .bytes );
605
+ hex_string_to_byte_array ("a914eaf97514c5ac1e41e413502e97ae42ebf27ace3a87" ,
606
+ 46 ,
607
+ input [1 ].script_pub_key .bytes );
608
+
609
+ data_ptr = raw_txn ;
610
+ data_total_size = 838 / 2 ;
611
+
612
+ byte_stream_t stream = {
613
+ .stream_pointer = raw_txn ,
614
+ .writer = generic_writer ,
615
+ .offset = 0 ,
616
+ .capacity = 500 ,
617
+ };
618
+
619
+ btc_validation_error_e status = btc_validate_inputs (& stream , input );
620
+
621
+ TEST_ASSERT_EQUAL (BTC_VALIDATE_ERR_INVALID_TX_HASH , status );
622
+
623
+ status = btc_validate_inputs (& stream , input + 1 );
624
+
625
+ TEST_ASSERT_EQUAL (BTC_VALIDATE_ERR_INVALID_TX_HASH , status );
626
+ }
627
+
457
628
TEST (btc_inputs_validator_tests ,
458
629
btc_validate_inputs_for_a_transaction_with_witness_data ) {
459
630
/* Test data source: rawTxn -
0 commit comments