1- """
2- Example: Create an account without using any alias.
1+ """Example: Create an account without using any alias.
32
43This demonstrates:
54- Using `set_key_without_alias` so that no EVM alias is set
65- The resulting `contract_account_id` being the zero-padded value
76
87Usage:
9- - uv run python examples/account/account_create_transaction_without_alias.py
10- - python examples/account/account_create_transaction_without_alias.py
8+ uv run examples/account/account_create_transaction_without_alias.py
9+ python examples/account/account_create_transaction_without_alias.py
1110"""
1211
13- from typing import Tuple
12+ import os
1413import sys
14+ from typing import Tuple
15+
16+ from dotenv import load_dotenv
1517
1618from hiero_sdk_python import (
17- Client ,
18- PrivateKey ,
19- PublicKey ,
2019 AccountCreateTransaction ,
21- AccountInfoQuery ,
2220 AccountId ,
2321 AccountInfo ,
22+ AccountInfoQuery ,
23+ Client ,
2424 Hbar ,
25- ResponseCode ,
25+ Network ,
26+ PrivateKey ,
27+ PublicKey ,
2628)
2729
30+ load_dotenv ()
31+ network_name = os .getenv ("NETWORK" , "testnet" ).lower ()
32+
33+
2834def setup_client () -> Client :
29- """Setup Client."""
30- client = Client .from_env ()
31- print (f"Network: { client .network .network } " )
32- print (f"Client set up with operator id { client .operator_account_id } " )
33- return client
35+ """Set up the Hedera client."""
36+ network = Network (network_name )
37+ print (f"Connecting to Hedera { network_name } network!" )
38+ client = Client (network )
39+
40+ try :
41+ operator_id = AccountId .from_string (os .getenv ("OPERATOR_ID" , "" ))
42+ operator_key = PrivateKey .from_string (os .getenv ("OPERATOR_KEY" , "" ))
43+ client .set_operator (operator_id , operator_key )
44+ print (f"Client set up with operator id { client .operator_account_id } " )
45+ return client
46+ except Exception :
47+ print ("Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file." )
48+ sys .exit (1 )
49+
3450
3551def generate_account_key () -> Tuple [PrivateKey , PublicKey ]:
3652 """Generate a key pair for the account."""
@@ -40,10 +56,13 @@ def generate_account_key() -> Tuple[PrivateKey, PublicKey]:
4056 print (f"✅ Account public key (no alias): { account_public_key } " )
4157 return account_private_key , account_public_key
4258
43- def create_account_without_alias (client : Client , account_public_key : PublicKey , account_private_key : PrivateKey ) -> AccountId :
59+
60+ def create_account_without_alias (
61+ client : Client , account_public_key : PublicKey , account_private_key : PrivateKey
62+ ) -> AccountId :
4463 """Create an account without setting any alias."""
4564 print ("\n STEP 2: Creating the account without setting any alias..." )
46-
65+
4766 transaction = (
4867 AccountCreateTransaction (
4968 initial_balance = Hbar (5 ),
@@ -55,47 +74,39 @@ def create_account_without_alias(client: Client, account_public_key: PublicKey,
5574 )
5675
5776 response = transaction .execute (client )
58-
59- if response .status != ResponseCode .SUCCESS :
60- raise RuntimeError (
61- f"Transaction failed with status: { response .status .name } "
62- )
63-
6477 new_account_id = response .account_id
6578
6679 if new_account_id is None :
67- raise RuntimeError (
68- "AccountID not found in receipt. Account may not have been created."
69- )
80+ raise RuntimeError ("AccountID not found in receipt. Account may not have been created." )
7081
7182 print (f"✅ Account created with ID: { new_account_id } \n " )
7283 return new_account_id
7384
85+
7486def fetch_account_info (client : Client , account_id : AccountId ) -> AccountInfo :
7587 """Fetch account information."""
76- account_info = (
77- AccountInfoQuery ()
78- .set_account_id (account_id )
79- .execute (client )
80- )
81- return account_info
88+ print ("\n STEP 3: Fetching account information..." )
89+ return AccountInfoQuery ().set_account_id (account_id ).execute (client )
90+
8291
8392def main () -> None :
84- """Main entry point ."""
93+ """Execute the example workflow ."""
8594 try :
8695 client = setup_client ()
8796 account_private_key , account_public_key = generate_account_key ()
97+
8898 new_account_id = create_account_without_alias (client , account_public_key , account_private_key )
99+
89100 account_info = fetch_account_info (client , new_account_id )
90- print ("\n Account Info:" )
101+
102+ print ("\n 🧾 Account Info:" )
91103 print (account_info )
92- print (
93- "\n ✅ contract_account_id (no alias, zero-padded): "
94- f"{ account_info .contract_account_id } "
95- )
104+ print (f"\n ✅ contract_account_id (no alias, zero-padded): { account_info .contract_account_id } " )
105+
96106 except Exception as error :
97107 print (f"❌ Error: { error } " )
98108 sys .exit (1 )
99109
110+
100111if __name__ == "__main__" :
101112 main ()
0 commit comments