@@ -61,6 +61,63 @@ def generate_transaction_id(account_id_proto):
6161
6262########### Basic Tests for Building Transactions ###########
6363
64+ def test_to_proto_key_with_public_key ():
65+ """
66+ Tests the _to_proto_key 'airlock' with a PublicKey.
67+ This is the "new" happy path.
68+ """
69+ tx = TokenCreateTransaction ()
70+ private_key = PrivateKey .generate_ed25519 ()
71+ public_key = private_key .public_key ()
72+
73+ # This is the "proto" object we expect to get back
74+ expected_proto = public_key ._to_proto ()
75+
76+ # Call the function directly
77+ result_proto = tx ._to_proto_key (public_key )
78+
79+ # Assert the result is correct
80+ assert result_proto == expected_proto
81+ assert isinstance (result_proto , basic_types_pb2 .Key )
82+
83+ def test_to_proto_key_with_private_key ():
84+ """
85+ Tests the _to_proto_key 'airlock' with a PrivateKey.
86+ This proves backward compatibility.
87+ """
88+ tx = TokenCreateTransaction ()
89+ private_key = PrivateKey .generate_ed25519 ()
90+ public_key = private_key .public_key ()
91+
92+ # We expect the *public key's* proto, even though we passed a private key
93+ expected_proto = public_key ._to_proto ()
94+
95+ # Call the function with the PrivateKey
96+ result_proto = tx ._to_proto_key (private_key )
97+
98+ # Assert it correctly converted it to the public key proto
99+ assert result_proto == expected_proto
100+ assert isinstance (result_proto , basic_types_pb2 .Key )
101+
102+ def test_to_proto_key_with_none ():
103+ """
104+ Tests the _to_proto_key 'airlock' with None (a non-happy path).
105+ """
106+ tx = TokenCreateTransaction ()
107+ result = tx ._to_proto_key (None )
108+ assert result is None
109+
110+ def test_to_proto_key_with_invalid_string_raises_error ():
111+ """
112+ Tests the _to_proto_key 'airlock' safety net with a string (a non-happy path).
113+ """
114+ tx = TokenCreateTransaction ()
115+
116+ with pytest .raises (TypeError ) as e :
117+ tx ._to_proto_key ("this is not a key" )
118+
119+ assert "Key must be of type PrivateKey or PublicKey" in str (e .value )
120+
64121# This test uses fixture mock_account_ids as parameter
65122def test_build_transaction_body_without_key (mock_account_ids ):
66123 """Test building a token creation transaction body without an admin, supply or freeze key."""
@@ -260,36 +317,36 @@ def test_sign_transaction(mock_account_ids, mock_client):
260317 private_key .sign .return_value = b"signature"
261318 private_key .public_key ().to_bytes_raw .return_value = b"public_key"
262319
263- private_key_admin = MagicMock ()
320+ private_key_admin = MagicMock (spec = PrivateKey )
264321 private_key_admin .sign .return_value = b"admin_signature"
265322 private_key_admin .public_key ().to_bytes_raw .return_value = b"admin_public_key"
266323 private_key_admin .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"admin_public_key" )
267324
268- private_key_supply = MagicMock ()
325+ private_key_supply = MagicMock (spec = PrivateKey )
269326 private_key_supply .sign .return_value = b"supply_signature"
270327 private_key_supply .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"supply_public_key" )
271328
272- private_key_freeze = MagicMock ()
329+ private_key_freeze = MagicMock (spec = PrivateKey )
273330 private_key_freeze .sign .return_value = b"freeze_signature"
274331 private_key_freeze .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"freeze_public_key" )
275332
276- private_key_wipe = MagicMock ()
333+ private_key_wipe = MagicMock (spec = PrivateKey )
277334 private_key_wipe .sign .return_value = b"wipe_signature"
278335 private_key_wipe .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"wipe_public_key" )
279336
280- private_key_metadata = MagicMock ()
337+ private_key_metadata = MagicMock (spec = PrivateKey )
281338 private_key_metadata .sign .return_value = b"metadata_signature"
282339 private_key_metadata .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"metadata_public_key" )
283340
284- private_key_pause = MagicMock ()
341+ private_key_pause = MagicMock (spec = PrivateKey )
285342 private_key_pause .sign .return_value = b"pause_signature"
286343 private_key_pause .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"pause_public_key" )
287344
288- private_key_kyc = MagicMock ()
345+ private_key_kyc = MagicMock (spec = PrivateKey )
289346 private_key_kyc .sign .return_value = b"kyc_signature"
290347 private_key_kyc .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"kyc_public_key" )
291348
292- private_key_fee_schedule = MagicMock ()
349+ private_key_fee_schedule = MagicMock (spec = PrivateKey )
293350 private_key_fee_schedule .sign .return_value = b"fee_schedule_signature"
294351 private_key_fee_schedule .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"fee_schedule_public_key" )
295352
@@ -722,36 +779,36 @@ def test_build_and_sign_nft_transaction_to_proto(mock_account_ids, mock_client):
722779 private_key_private .sign .return_value = b"private_signature"
723780 private_key_private .public_key ().to_bytes_raw .return_value = b"private_public_key"
724781
725- private_key_admin = MagicMock ()
782+ private_key_admin = MagicMock (spec = PrivateKey )
726783 private_key_admin .sign .return_value = b"admin_signature"
727784 private_key_admin .public_key ().to_bytes_raw .return_value = b"admin_public_key"
728785 private_key_admin .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"admin_public_key" )
729786
730- private_key_supply = MagicMock ()
787+ private_key_supply = MagicMock (spec = PrivateKey )
731788 private_key_supply .sign .return_value = b"supply_signature"
732789 private_key_supply .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"supply_public_key" )
733790
734- private_key_freeze = MagicMock ()
791+ private_key_freeze = MagicMock (spec = PrivateKey )
735792 private_key_freeze .sign .return_value = b"freeze_signature"
736793 private_key_freeze .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"freeze_public_key" )
737794
738- private_key_wipe = MagicMock ()
795+ private_key_wipe = MagicMock (spec = PrivateKey )
739796 private_key_wipe .sign .return_value = b"wipe_signature"
740797 private_key_wipe .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"wipe_public_key" )
741798
742- private_key_metadata = MagicMock ()
799+ private_key_metadata = MagicMock (spec = PrivateKey )
743800 private_key_metadata .sign .return_value = b"metadata_signature"
744801 private_key_metadata .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"metadata_public_key" )
745802
746- private_key_pause = MagicMock ()
803+ private_key_pause = MagicMock (spec = PrivateKey )
747804 private_key_pause .sign .return_value = b"pause_signature"
748805 private_key_pause .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"pause_public_key" )
749806
750- private_key_kyc = MagicMock ()
807+ private_key_kyc = MagicMock (spec = PrivateKey )
751808 private_key_kyc .sign .return_value = b"kyc_signature"
752809 private_key_kyc .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"kyc_public_key" )
753810
754- private_key_fee_schedule = MagicMock ()
811+ private_key_fee_schedule = MagicMock (spec = PrivateKey )
755812 private_key_fee_schedule .sign .return_value = b"fee_schedule_signature"
756813 private_key_fee_schedule .public_key ()._to_proto .return_value = basic_types_pb2 .Key (ed25519 = b"fee_schedule_public_key" )
757814
0 commit comments