Skip to content

Commit 1c0f6dd

Browse files
refactor: create account transaction without alias (#1334)
Signed-off-by: tech0priyanshu <priyanshuyadv101106@gmail.com>
1 parent 40efa6c commit 1c0f6dd

File tree

2 files changed

+62
-64
lines changed

2 files changed

+62
-64
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
8686
- Added `Client.from_env()` and network-specific factory methods (e.g., `Client.for_testnet()`) to simplify client initialization and reduce boilerplate. [[#1251](https://github.com/hiero-ledger/hiero-sdk-python/issues/1251)]
8787

8888
### Changed
89-
89+
- Refactored `account_create_transaction_without_alias.py` into smaller, modular functions.(#1321)
9090
- Renamed bot-inactivity workflow files to remove "-phase" suffix since the process no longer uses phased execution (#1339)
9191
- Renamed the GitHub notify team script to match its corresponding workflow filename for better maintainability (#1338)
9292
- style: apply black formatting to examples (#1299)

examples/account/account_create_transaction_without_alias.py

Lines changed: 61 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,98 +6,96 @@
66
- The resulting `contract_account_id` being the zero-padded value
77
88
Usage:
9-
- uv run -m examples.account.account_create_transaction_without_alias
10-
- python -m examples.account.account_create_transaction_without_alias
11-
(we use -m because we use the util `info_to_dict`)
9+
- uv run python examples/account/account_create_transaction_without_alias.py
10+
- python examples/account/account_create_transaction_without_alias.py
1211
"""
1312

14-
import os
13+
from typing import Tuple
1514
import sys
16-
import json
17-
from dotenv import load_dotenv
18-
19-
from examples.utils import info_to_dict
2015

2116
from hiero_sdk_python import (
2217
Client,
2318
PrivateKey,
19+
PublicKey,
2420
AccountCreateTransaction,
2521
AccountInfoQuery,
26-
Network,
2722
AccountId,
23+
AccountInfo,
2824
Hbar,
25+
ResponseCode,
2926
)
3027

31-
load_dotenv()
32-
network_name = os.getenv("NETWORK", "testnet").lower()
33-
34-
35-
def setup_client():
28+
def setup_client() -> Client:
3629
"""Setup Client."""
37-
network = Network(network_name)
38-
print(f"Connecting to Hedera {network_name} network!")
39-
client = Client(network)
40-
41-
try:
42-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
43-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
44-
client.set_operator(operator_id, operator_key)
45-
print(f"Client set up with operator id {client.operator_account_id}")
46-
return client
47-
except Exception:
48-
print("Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
49-
sys.exit(1)
50-
51-
52-
def create_account_without_alias(client: Client) -> None:
53-
"""Create an account explicitly without an alias."""
54-
try:
55-
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
56-
account_private_key = PrivateKey.generate()
57-
account_public_key = account_private_key.public_key()
58-
59-
print(f"✅ Account public key (no alias): {account_public_key}")
60-
61-
print("\nSTEP 2: Creating the account without setting any alias...")
62-
63-
transaction = AccountCreateTransaction(
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
34+
35+
def generate_account_key() -> Tuple[PrivateKey, PublicKey]:
36+
"""Generate a key pair for the account."""
37+
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
38+
account_private_key = PrivateKey.generate()
39+
account_public_key = account_private_key.public_key()
40+
print(f"✅ Account public key (no alias): {account_public_key}")
41+
return account_private_key, account_public_key
42+
43+
def create_account_without_alias(client: Client, account_public_key: PublicKey, account_private_key: PrivateKey) -> AccountId:
44+
"""Create an account without setting any alias."""
45+
print("\nSTEP 2: Creating the account without setting any alias...")
46+
47+
transaction = (
48+
AccountCreateTransaction(
6449
initial_balance=Hbar(5),
6550
memo="Account created without alias",
66-
).set_key_without_alias(account_private_key)
51+
)
52+
.set_key_without_alias(account_public_key)
53+
.freeze_with(client)
54+
.sign(account_private_key)
55+
)
6756

68-
transaction = transaction.freeze_with(client).sign(account_private_key)
57+
response = transaction.execute(client)
6958

70-
response = transaction.execute(client)
71-
new_account_id = response.account_id
59+
if response.status != ResponseCode.SUCCESS:
60+
raise RuntimeError(
61+
f"Transaction failed with status: {response.status.name}"
62+
)
7263

73-
if new_account_id is None:
74-
raise RuntimeError(
75-
"AccountID not found in receipt. Account may not have been created."
76-
)
64+
new_account_id = response.account_id
7765

78-
print(f"✅ Account created with ID: {new_account_id}\n")
66+
if new_account_id is None:
67+
raise RuntimeError(
68+
"AccountID not found in receipt. Account may not have been created."
69+
)
7970

80-
account_info = AccountInfoQuery().set_account_id(new_account_id).execute(client)
71+
print(f"✅ Account created with ID: {new_account_id}\n")
72+
return new_account_id
8173

82-
out = info_to_dict(account_info)
83-
print("Account Info:")
84-
print(json.dumps(out, indent=2) + "\n")
74+
def fetch_account_info(client: Client, account_id: AccountId) -> AccountInfo:
75+
"""Fetch account information."""
76+
account_info = (
77+
AccountInfoQuery()
78+
.set_account_id(account_id)
79+
.execute(client)
80+
)
81+
return account_info
8582

83+
def main() -> None:
84+
"""Main entry point."""
85+
try:
86+
client = setup_client()
87+
account_private_key, account_public_key = generate_account_key()
88+
new_account_id = create_account_without_alias(client, account_public_key, account_private_key)
89+
account_info = fetch_account_info(client, new_account_id)
90+
print("\nAccount Info:")
91+
print(account_info)
8692
print(
87-
"✅ contract_account_id (no alias, zero-padded): "
93+
"\n✅ contract_account_id (no alias, zero-padded): "
8894
f"{account_info.contract_account_id}"
8995
)
90-
9196
except Exception as error:
9297
print(f"❌ Error: {error}")
9398
sys.exit(1)
9499

95-
96-
def main():
97-
"""Main entry point."""
98-
client = setup_client()
99-
create_account_without_alias(client)
100-
101-
102100
if __name__ == "__main__":
103101
main()

0 commit comments

Comments
 (0)