33from src .tokens .token_create_transaction import TokenCreateTransaction
44from src .proto import basic_types_pb2 , timestamp_pb2
55from src .transaction .transaction_id import TransactionId
6+ from src .proto import transaction_pb2 , transaction_body_pb2
7+ from cryptography .hazmat .primitives import serialization
68
79def generate_transaction_id (account_id_proto ):
810 """Generate a unique transaction ID based on the account ID and the current timestamp."""
@@ -19,7 +21,28 @@ def generate_transaction_id(account_id_proto):
1921 )
2022 return tx_id
2123
22- def test_build_transaction_body (mock_account_ids ):
24+ def test_build_transaction_body_without_admin_key (mock_account_ids ):
25+ """Test building a token creation transaction body without an admin key."""
26+ treasury_account , _ , node_account_id , _ , _ = mock_account_ids
27+
28+ token_tx = TokenCreateTransaction ()
29+ token_tx .set_token_name ("MyToken" )
30+ token_tx .set_token_symbol ("MTK" )
31+ token_tx .set_decimals (2 )
32+ token_tx .set_initial_supply (1000 )
33+ token_tx .set_treasury_account_id (treasury_account )
34+ token_tx .transaction_id = generate_transaction_id (treasury_account )
35+ token_tx .node_account_id = node_account_id
36+
37+ transaction_body = token_tx .build_transaction_body ()
38+
39+ assert transaction_body .tokenCreation .name == "MyToken"
40+ assert transaction_body .tokenCreation .symbol == "MTK"
41+ assert transaction_body .tokenCreation .decimals == 2
42+ assert transaction_body .tokenCreation .initialSupply == 1000
43+ assert not transaction_body .tokenCreation .HasField ("adminKey" )
44+
45+ def test_build_transaction_body (mock_account_ids , admin_key ):
2346 """Test building a token creation transaction body with valid values."""
2447 treasury_account , _ , node_account_id , _ , _ = mock_account_ids
2548
@@ -30,6 +53,7 @@ def test_build_transaction_body(mock_account_ids):
3053 token_tx .set_initial_supply (1000 )
3154 token_tx .set_treasury_account_id (treasury_account )
3255 token_tx .transaction_id = generate_transaction_id (treasury_account )
56+ token_tx .set_admin_key (admin_key [0 ])
3357 token_tx .node_account_id = node_account_id
3458
3559 transaction_body = token_tx .build_transaction_body ()
@@ -38,6 +62,7 @@ def test_build_transaction_body(mock_account_ids):
3862 assert transaction_body .tokenCreation .symbol == "MTK"
3963 assert transaction_body .tokenCreation .decimals == 2
4064 assert transaction_body .tokenCreation .initialSupply == 1000
65+ assert transaction_body .tokenCreation .adminKey .ed25519 == admin_key [1 ]
4166
4267def test_missing_fields ():
4368 """Test that building a transaction without required fields raises a ValueError."""
@@ -48,6 +73,7 @@ def test_missing_fields():
4873def test_sign_transaction (mock_account_ids ):
4974 """Test signing the token creation transaction with a private key."""
5075 treasury_account , _ , node_account_id , _ , _ = mock_account_ids
76+
5177 token_tx = TokenCreateTransaction ()
5278 token_tx .set_token_name ("MyToken" )
5379 token_tx .set_token_symbol ("MTK" )
@@ -61,32 +87,79 @@ def test_sign_transaction(mock_account_ids):
6187 private_key .sign .return_value = b'signature'
6288 private_key .public_key ().public_bytes .return_value = b'public_key'
6389
90+ private_key_admin = MagicMock ()
91+ private_key_admin .sign .return_value = b'admin_signature'
92+ private_key_admin .public_key ().public_bytes .return_value = b'admin_public_key'
93+
6494 token_tx .sign (private_key )
95+ token_tx .sign (private_key_admin )
96+
97+ assert len (token_tx .signature_map .sigPair ) == 2
6598
66- assert len (token_tx .signature_map .sigPair ) == 1
6799 sig_pair = token_tx .signature_map .sigPair [0 ]
68100 assert sig_pair .pubKeyPrefix == b'public_key'
69101 assert sig_pair .ed25519 == b'signature'
70102
103+ sig_pair_admin = token_tx .signature_map .sigPair [1 ]
104+ assert sig_pair_admin .pubKeyPrefix == b'admin_public_key'
105+ assert sig_pair_admin .ed25519 == b'admin_signature'
71106
72- def test_to_proto (mock_account_ids ):
107+ def test_to_proto_without_admin_key (mock_account_ids ):
108+ """Test protobuf conversion when admin key is not set."""
109+ treasury_account , _ , node_account_id , _ , _ = mock_account_ids
110+
111+ token_tx = TokenCreateTransaction ()
112+ token_tx .set_token_name ("MyToken" )
113+ token_tx .set_token_symbol ("MTK" )
114+ token_tx .set_decimals (2 )
115+ token_tx .set_initial_supply (1000 )
116+ token_tx .set_treasury_account_id (treasury_account )
117+ token_tx .transaction_id = generate_transaction_id (treasury_account )
118+ token_tx .node_account_id = node_account_id
119+
120+ private_key = MagicMock ()
121+ private_key .sign .return_value = b'signature'
122+ private_key .public_key ().public_bytes .return_value = b'public_key'
123+
124+ token_tx .sign (private_key )
125+ proto = token_tx .to_proto ()
126+
127+ assert len (proto .signedTransactionBytes ) > 0
128+
129+ transaction = transaction_pb2 .Transaction .FromString (proto .signedTransactionBytes )
130+ transaction_body = transaction_body_pb2 .TransactionBody .FromString (transaction .bodyBytes )
131+
132+ assert not transaction_body .tokenCreation .HasField ("adminKey" )
133+
134+ def test_to_proto (mock_account_ids , admin_key ):
73135 """Test converting the token creation transaction to protobuf format after signing."""
74136 treasury_account , _ , node_account_id , _ , _ = mock_account_ids
137+
75138 token_tx = TokenCreateTransaction ()
76139 token_tx .set_token_name ("MyToken" )
77140 token_tx .set_token_symbol ("MTK" )
78141 token_tx .set_decimals (2 )
79142 token_tx .set_initial_supply (1000 )
80143 token_tx .set_treasury_account_id (treasury_account )
144+ token_tx .set_admin_key (admin_key [0 ])
81145 token_tx .transaction_id = generate_transaction_id (treasury_account )
82146 token_tx .node_account_id = node_account_id
83147
84148 private_key = MagicMock ()
85149 private_key .sign .return_value = b'signature'
86150 private_key .public_key ().public_bytes .return_value = b'public_key'
87151
152+ private_key_admin = MagicMock ()
153+ private_key_admin .sign .return_value = b'admin_signature'
154+ private_key_admin .public_key ().public_bytes .return_value = b'admin_public_key'
155+
88156 token_tx .sign (private_key )
157+ token_tx .sign (private_key_admin )
89158 proto = token_tx .to_proto ()
90159
91- assert proto .signedTransactionBytes
92160 assert len (proto .signedTransactionBytes ) > 0
161+
162+ transaction = transaction_pb2 .Transaction .FromString (proto .signedTransactionBytes )
163+ transaction_body = transaction_body_pb2 .TransactionBody .FromString (transaction .bodyBytes )
164+
165+ assert transaction_body .tokenCreation .adminKey .ed25519 == admin_key [1 ]
0 commit comments