2323from hiero_sdk_python .account .account_id import AccountId
2424from hiero_sdk_python .crypto .private_key import PrivateKey
2525from hiero_sdk_python .tokens .custom_fee import CustomFee
26+ from hiero_sdk_python .crypto .public_key import PublicKey
2627
2728DEFAULT_TRANSACTION_FEE = 3_000_000_000
2829
@@ -401,29 +402,21 @@ def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
401402 # Validate freeze status
402403 TokenCreateValidator ._validate_token_freeze_status (self ._keys , self ._token_params )
403404
404- admin_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .admin_key )
405- supply_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .supply_key )
406- freeze_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .freeze_key )
407- wipe_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .wipe_key )
408- metadata_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .metadata_key )
409- pause_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .pause_key )
410- kyc_key_proto : Optional [basic_types_pb2 .Key ] = self ._to_proto_key (self ._keys .kyc_key )
411-
412- # Ensure token type is correctly set with default to fungible
413- if self ._token_params .token_type is None :
414- token_type_value = 0 # default FUNGIBLE_COMMON
415- elif isinstance (self ._token_params .token_type , TokenType ):
416- token_type_value = self ._token_params .token_type .value
417- else :
418- token_type_value = self ._token_params .token_type
419-
420- # Ensure supply type is correctly set with default to infinite
421- if self ._token_params .supply_type is None :
422- supply_type_value = 0 # default INFINITE
423- elif isinstance (self ._token_params .supply_type , SupplyType ):
424- supply_type_value = self ._token_params .supply_type .value
425- else :
426- supply_type_value = self ._token_params .supply_type
405+ # --- small helpers to keep this method simple ---
406+ def _enum_value (val , enum_cls , default_int : int ) -> int :
407+ """Return enum int for None/enum/int input, with a default."""
408+ if val is None :
409+ return default_int
410+ if isinstance (val , enum_cls ):
411+ return val .value
412+ return int (val )
413+
414+ def _key_proto (pk : Optional [PublicKey ]) -> Optional [basic_types_pb2 .Key ]:
415+ return self ._to_proto_key (pk )
416+
417+ # Resolve enum-like params with defaults (FUNGIBLE_COMMON=0, INFINITE=0)
418+ token_type_value = _enum_value (self ._token_params .token_type , TokenType , 0 )
419+ supply_type_value = _enum_value (self ._token_params .supply_type , SupplyType , 0 )
427420
428421 # Construct the TokenCreateTransactionBody
429422 token_create_body = token_create_pb2 .TokenCreateTransactionBody (
@@ -438,29 +431,27 @@ def build_transaction_body(self) -> transaction_body_pb2.TransactionBody:
438431 treasury = self ._token_params .treasury_account_id ._to_proto (),
439432 )
440433
441- # Conditionally attach each optional sub-message
442- if admin_key_proto is not None :
443- token_create_body . adminKey . CopyFrom ( admin_key_proto )
444- if supply_key_proto is not None :
445- token_create_body . supplyKey . CopyFrom ( supply_key_proto )
446- if freeze_key_proto is not None :
447- token_create_body . freezeKey . CopyFrom ( freeze_key_proto )
448- if wipe_key_proto is not None :
449- token_create_body . wipeKey . CopyFrom ( wipe_key_proto )
450- if metadata_key_proto is not None :
451- token_create_body . metadata_key . CopyFrom ( metadata_key_proto )
452- if pause_key_proto is not None :
453- token_create_body . pause_key . CopyFrom (pause_key_proto )
454- if kyc_key_proto is not None :
455- token_create_body . kycKey . CopyFrom ( kyc_key_proto )
434+ # Conditionally attach each optional sub-message (data-driven)
435+ key_map = (
436+ ( _key_proto ( self . _keys . admin_key ), ( "adminKey" ,)),
437+ ( _key_proto ( self . _keys . supply_key ), ( "supplyKey" ,)),
438+ ( _key_proto ( self . _keys . freeze_key ), ( "freezeKey" ,)),
439+ ( _key_proto ( self . _keys . wipe_key ), ( "wipeKey" ,)),
440+ ( _key_proto ( self . _keys . metadata_key ), ( "metadata_key" ,)), # snake_case in recent protos
441+ ( _key_proto ( self . _keys . pause_key ), ( "pause_key" ,)), # snake_case
442+ ( _key_proto ( self . _keys . kyc_key ), ( "kycKey" ,)),
443+ )
444+ for key_proto , path in key_map :
445+ if key_proto is not None :
446+ getattr ( token_create_body , path [ 0 ]). CopyFrom (key_proto )
447+
448+ # Custom fees (append if present )
456449 if self ._token_params .custom_fees :
457- for fee in self ._token_params .custom_fees :
458- token_create_body .custom_fees .append (fee ._to_proto ())
450+ token_create_body .custom_fees .extend (f ._to_proto () for f in self ._token_params .custom_fees )
459451
460452 # Build the base transaction body and attach the token creation details
461453 transaction_body : transaction_body_pb2 .TransactionBody = self .build_base_transaction_body ()
462454 transaction_body .tokenCreation .CopyFrom (token_create_body )
463-
464455 return transaction_body
465456
466457 def _get_method (self , channel : _Channel ) -> _Method :
0 commit comments