|
12 | 12 |
|
13 | 13 | from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction |
14 | 14 | from hiero_sdk_python.account.account_id import AccountId |
15 | | -from hiero_sdk_python.client.client import Client |
16 | 15 | from hiero_sdk_python.crypto.private_key import PrivateKey |
17 | 16 | from hiero_sdk_python.hapi.services.transaction_response_pb2 import ( |
18 | 17 | TransactionResponse as TransactionResponseProto, |
@@ -533,31 +532,6 @@ def test_multiple_signatures_increase_size(): |
533 | 532 | assert len(bytes_3_sig) > len(bytes_2_sig) |
534 | 533 |
|
535 | 534 |
|
536 | | -def test_changing_node_after_freeze_fails_for_to_bytes(): |
537 | | - """Test that changing node_account_id after freeze causes to_bytes() to fail.""" |
538 | | - operator_id = AccountId.from_string("0.0.1234") |
539 | | - node_id_1 = AccountId.from_string("0.0.3") |
540 | | - node_id_2 = AccountId.from_string("0.0.4") |
541 | | - receiver_id = AccountId.from_string("0.0.5678") |
542 | | - |
543 | | - transaction = ( |
544 | | - TransferTransaction().add_hbar_transfer(operator_id, -100_000_000).add_hbar_transfer(receiver_id, 100_000_000) |
545 | | - ) |
546 | | - |
547 | | - transaction.transaction_id = TransactionId.generate(operator_id) |
548 | | - transaction.node_account_id = node_id_1 |
549 | | - transaction.freeze() |
550 | | - |
551 | | - # This should work |
552 | | - bytes_node_1 = transaction.to_bytes() |
553 | | - assert isinstance(bytes_node_1, bytes) |
554 | | - |
555 | | - # Change to a different node that wasn't frozen |
556 | | - transaction.node_account_id = node_id_2 |
557 | | - |
558 | | - # TODO: Update this to new implementation |
559 | | - |
560 | | - |
561 | 535 | def test_unsigned_transaction_can_be_signed_after_to_bytes(): |
562 | 536 | """Test that you can call to_bytes(), then sign, then to_bytes() again.""" |
563 | 537 | operator_id = AccountId.from_string("0.0.1234") |
@@ -680,39 +654,218 @@ def test_map_response_raises_if_proto_request_is_not_transaction(): |
680 | 654 | ) |
681 | 655 |
|
682 | 656 |
|
683 | | -def test_serialization(): |
684 | | - client = Client.for_testnet() |
685 | | - client.set_operator(AccountId(0, 0, 5), PrivateKey.generate_ecdsa()) |
| 657 | +def test_to_bytes_with_base_fields(): |
| 658 | + """Test serialization and deserialization of an unfrozen transaction with base fields""" |
| 659 | + tx = ( |
| 660 | + AccountCreateTransaction() |
| 661 | + .set_key_without_alias(PrivateKey.generate_ecdsa()) |
| 662 | + .set_initial_balance(1) |
| 663 | + .set_account_memo("test_account") |
| 664 | + ) |
| 665 | + |
| 666 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 667 | + |
| 668 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 669 | + assert restored_tx.initial_balance == tx.initial_balance |
| 670 | + assert restored_tx.account_memo == tx.account_memo |
| 671 | + assert restored_tx.transaction_id is None |
| 672 | + assert len(restored_tx.node_account_ids) == 0 |
| 673 | + assert not restored_tx._transaction_body_bytes |
| 674 | + |
| 675 | + |
| 676 | +def test_to_bytes_without_freeze_multiple_node_account_ids(): |
| 677 | + """Test serialization and deserialization of an unfrozen transaction with multiple node account IDs.""" |
| 678 | + tx = ( |
| 679 | + AccountCreateTransaction() |
| 680 | + .set_key_without_alias(PrivateKey.generate_ecdsa()) |
| 681 | + .set_initial_balance(1) |
| 682 | + .set_account_memo("test_account") |
| 683 | + .set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 684 | + .set_node_account_ids([AccountId(0, 0, 3), AccountId(0, 0, 4)]) |
| 685 | + ) |
| 686 | + |
| 687 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 688 | + |
| 689 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 690 | + assert restored_tx.initial_balance == tx.initial_balance |
| 691 | + assert restored_tx.account_memo == tx.account_memo |
| 692 | + assert restored_tx.transaction_id == tx.transaction_id |
| 693 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 694 | + assert not restored_tx._transaction_body_bytes |
| 695 | + |
| 696 | + |
| 697 | +# Deprecated |
| 698 | +def test_to_bytes_without_freeze_single_node_account_id(): |
| 699 | + """Test serialization and deserialization of an unfrozen transaction with a single node account ID.""" |
| 700 | + tx = ( |
| 701 | + AccountCreateTransaction() |
| 702 | + .set_key_without_alias(PrivateKey.generate_ecdsa()) |
| 703 | + .set_initial_balance(1) |
| 704 | + .set_account_memo("test_account") |
| 705 | + .set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 706 | + .set_node_account_id(AccountId(0, 0, 3)) |
| 707 | + ) |
| 708 | + |
| 709 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 710 | + |
| 711 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 712 | + assert restored_tx.initial_balance == tx.initial_balance |
| 713 | + assert restored_tx.account_memo == tx.account_memo |
| 714 | + assert restored_tx.transaction_id == tx.transaction_id |
| 715 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 716 | + assert not restored_tx._transaction_body_bytes |
686 | 717 |
|
| 718 | + |
| 719 | +def test_to_bytes_after_freeze_multiple_node_account_ids(): |
| 720 | + """Test serialization and deserialization of a frozen transaction with multiple node account IDs.""" |
687 | 721 | tx = ( |
688 | 722 | AccountCreateTransaction() |
689 | | - .set_key_without_alias(PrivateKey.generate_ed25519()) |
| 723 | + .set_key_without_alias(PrivateKey.generate_ecdsa()) |
690 | 724 | .set_initial_balance(1) |
691 | 725 | .set_account_memo("test_account") |
692 | | - .freeze_with(client) |
693 | | - .sign(PrivateKey.generate_ed25519()) |
| 726 | + .set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 727 | + .set_node_account_ids([AccountId(0, 0, 3), AccountId(0, 0, 4)]) |
| 728 | + .freeze() |
694 | 729 | ) |
695 | 730 |
|
696 | | - print("Before serialization:") |
697 | | - print(tx.key) |
698 | | - print(tx.initial_balance) |
699 | | - print(tx.account_memo) |
700 | | - print(tx._transaction_body_bytes) |
701 | | - print(tx._signature_map) |
702 | | - print(tx.node_account_ids) |
703 | | - print(tx.transaction_id) |
704 | | - print("\n") |
705 | | - |
706 | | - tx_bytes = tx.to_bytes() |
707 | | - |
708 | | - new_tx = Transaction.from_bytes(tx_bytes) |
709 | | - |
710 | | - print("After serialization:") |
711 | | - print(new_tx.key) |
712 | | - print(new_tx.initial_balance) |
713 | | - print(new_tx.account_memo) |
714 | | - print(new_tx._transaction_body_bytes) |
715 | | - print(new_tx._signature_map) |
716 | | - print(new_tx.node_account_ids) |
717 | | - print(new_tx.transaction_id) |
718 | | - print("\n") |
| 731 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 732 | + |
| 733 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 734 | + assert restored_tx.initial_balance == tx.initial_balance |
| 735 | + assert restored_tx.account_memo == tx.account_memo |
| 736 | + assert restored_tx.transaction_id == tx.transaction_id |
| 737 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 738 | + |
| 739 | + assert restored_tx._transaction_body_bytes == tx._transaction_body_bytes |
| 740 | + assert restored_tx._signature_map == tx._signature_map |
| 741 | + |
| 742 | + |
| 743 | +# Deprecated |
| 744 | +def test_to_bytes_after_freeze_single_node_account_id(): |
| 745 | + """Test serialization and deserialization of a frozen transaction with a single node account ID.""" |
| 746 | + tx = ( |
| 747 | + AccountCreateTransaction() |
| 748 | + .set_key_without_alias(PrivateKey.generate_ecdsa()) |
| 749 | + .set_initial_balance(1) |
| 750 | + .set_account_memo("test_account") |
| 751 | + .set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 752 | + .set_node_account_id(AccountId(0, 0, 3)) |
| 753 | + .freeze() |
| 754 | + ) |
| 755 | + |
| 756 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 757 | + |
| 758 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 759 | + assert restored_tx.initial_balance == tx.initial_balance |
| 760 | + assert restored_tx.account_memo == tx.account_memo |
| 761 | + assert restored_tx.transaction_id == tx.transaction_id |
| 762 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 763 | + |
| 764 | + assert restored_tx._transaction_body_bytes == tx._transaction_body_bytes |
| 765 | + assert restored_tx._signature_map == tx._signature_map |
| 766 | + |
| 767 | + |
| 768 | +def test_to_bytes_after_freeze_and_sign_multiple_node_account_ids(): |
| 769 | + """Test serialization and deserialization of a signed frozen transaction with multiple node account IDs.""" |
| 770 | + private_key = PrivateKey.generate_ecdsa() |
| 771 | + |
| 772 | + tx = ( |
| 773 | + AccountCreateTransaction() |
| 774 | + .set_key_without_alias(private_key.public_key()) |
| 775 | + .set_initial_balance(1) |
| 776 | + .set_account_memo("test_account") |
| 777 | + .set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 778 | + .set_node_account_ids([AccountId(0, 0, 3), AccountId(0, 0, 4)]) |
| 779 | + ) |
| 780 | + |
| 781 | + tx.freeze() |
| 782 | + tx.sign(private_key) |
| 783 | + |
| 784 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 785 | + |
| 786 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 787 | + assert restored_tx.initial_balance == tx.initial_balance |
| 788 | + assert restored_tx.account_memo == tx.account_memo |
| 789 | + assert restored_tx.transaction_id == tx.transaction_id |
| 790 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 791 | + |
| 792 | + assert restored_tx._transaction_body_bytes == tx._transaction_body_bytes |
| 793 | + assert restored_tx._signature_map == tx._signature_map |
| 794 | + |
| 795 | + |
| 796 | +# Deprecated |
| 797 | +def test_to_bytes_after_freeze_and_sign_single_node_account_id(): |
| 798 | + """Test serialization and deserialization of a signed frozen transaction with a single node account ID.""" |
| 799 | + private_key = PrivateKey.generate_ecdsa() |
| 800 | + |
| 801 | + tx = ( |
| 802 | + AccountCreateTransaction() |
| 803 | + .set_key_without_alias(private_key.public_key()) |
| 804 | + .set_initial_balance(1) |
| 805 | + .set_account_memo("test_account") |
| 806 | + .set_transaction_id(TransactionId.generate(AccountId(0, 0, 1))) |
| 807 | + .set_node_account_id(AccountId(0, 0, 3)) |
| 808 | + ) |
| 809 | + |
| 810 | + tx.freeze() |
| 811 | + tx.sign(private_key) |
| 812 | + |
| 813 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 814 | + |
| 815 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 816 | + assert restored_tx.initial_balance == tx.initial_balance |
| 817 | + assert restored_tx.account_memo == tx.account_memo |
| 818 | + assert restored_tx.transaction_id == tx.transaction_id |
| 819 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 820 | + |
| 821 | + assert restored_tx._transaction_body_bytes == tx._transaction_body_bytes |
| 822 | + assert restored_tx._signature_map == tx._signature_map |
| 823 | + |
| 824 | + |
| 825 | +def test_to_bytes_after_freeze_with_client(mock_client): |
| 826 | + """Test serialization and deserialization of a transaction frozen with a client.""" |
| 827 | + tx = ( |
| 828 | + AccountCreateTransaction() |
| 829 | + .set_key_without_alias(PrivateKey.generate_ecdsa()) |
| 830 | + .set_initial_balance(1) |
| 831 | + .set_account_memo("test_account") |
| 832 | + ) |
| 833 | + |
| 834 | + tx.freeze_with(mock_client) |
| 835 | + |
| 836 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 837 | + |
| 838 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 839 | + assert restored_tx.initial_balance == tx.initial_balance |
| 840 | + assert restored_tx.account_memo == tx.account_memo |
| 841 | + assert restored_tx.transaction_id == tx.transaction_id |
| 842 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 843 | + |
| 844 | + assert restored_tx._transaction_body_bytes == tx._transaction_body_bytes |
| 845 | + assert restored_tx._signature_map == tx._signature_map |
| 846 | + |
| 847 | + |
| 848 | +def test_to_bytes_after_freeze_with_client_and_sign_multiple_node_account_ids(mock_client): |
| 849 | + """Test serialization and deserialization of a signed transaction frozen with a client.""" |
| 850 | + private_key = PrivateKey.generate_ecdsa() |
| 851 | + |
| 852 | + tx = ( |
| 853 | + AccountCreateTransaction() |
| 854 | + .set_key_without_alias(private_key.public_key()) |
| 855 | + .set_initial_balance(1) |
| 856 | + .set_account_memo("test_account") |
| 857 | + ) |
| 858 | + |
| 859 | + tx.freeze_with(mock_client) |
| 860 | + tx.sign(private_key) |
| 861 | + |
| 862 | + restored_tx = Transaction.from_bytes(tx.to_bytes()) |
| 863 | + |
| 864 | + assert restored_tx.key.to_bytes() == tx.key.to_bytes() |
| 865 | + assert restored_tx.initial_balance == tx.initial_balance |
| 866 | + assert restored_tx.account_memo == tx.account_memo |
| 867 | + assert restored_tx.transaction_id == tx.transaction_id |
| 868 | + assert restored_tx.node_account_ids == tx.node_account_ids |
| 869 | + |
| 870 | + assert restored_tx._transaction_body_bytes == tx._transaction_body_bytes |
| 871 | + assert restored_tx._signature_map == tx._signature_map |
0 commit comments