Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
- 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)]

### Changed

- Refactored `account_create_transaction_without_alias.py` into smaller, modular functions.(#1321)
- Renamed bot-inactivity workflow files to remove "-phase" suffix since the process no longer uses phased execution (#1339)
- Renamed the GitHub notify team script to match its corresponding workflow filename for better maintainability (#1338)
- style: apply black formatting to examples (#1299)
Expand Down
124 changes: 61 additions & 63 deletions examples/account/account_create_transaction_without_alias.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,98 +6,96 @@
- The resulting `contract_account_id` being the zero-padded value

Usage:
- uv run -m examples.account.account_create_transaction_without_alias
- python -m examples.account.account_create_transaction_without_alias
(we use -m because we use the util `info_to_dict`)
- uv run python examples/account/account_create_transaction_without_alias.py
- python examples/account/account_create_transaction_without_alias.py
"""

import os
from typing import Tuple
import sys
import json
from dotenv import load_dotenv

from examples.utils import info_to_dict

from hiero_sdk_python import (
Client,
PrivateKey,
PublicKey,
AccountCreateTransaction,
AccountInfoQuery,
Network,
AccountId,
AccountInfo,
Hbar,
ResponseCode,
)

load_dotenv()
network_name = os.getenv("NETWORK", "testnet").lower()


def setup_client():
def setup_client() -> Client:
"""Setup Client."""
network = Network(network_name)
print(f"Connecting to Hedera {network_name} network!")
client = Client(network)

try:
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
client.set_operator(operator_id, operator_key)
print(f"Client set up with operator id {client.operator_account_id}")
return client
except Exception:
print("Error: Please check OPERATOR_ID and OPERATOR_KEY in your .env file.")
sys.exit(1)


def create_account_without_alias(client: Client) -> None:
"""Create an account explicitly without an alias."""
try:
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
account_private_key = PrivateKey.generate()
account_public_key = account_private_key.public_key()

print(f"✅ Account public key (no alias): {account_public_key}")

print("\nSTEP 2: Creating the account without setting any alias...")

transaction = AccountCreateTransaction(
client = Client.from_env()
print(f"Network: {client.network.network}")
print(f"Client set up with operator id {client.operator_account_id}")
return client

def generate_account_key() -> Tuple[PrivateKey, PublicKey]:
"""Generate a key pair for the account."""
print("\nSTEP 1: Generating a key pair for the account (no alias)...")
account_private_key = PrivateKey.generate()
account_public_key = account_private_key.public_key()
print(f"✅ Account public key (no alias): {account_public_key}")
return account_private_key, account_public_key

def create_account_without_alias(client: Client, account_public_key: PublicKey, account_private_key: PrivateKey) -> AccountId:
"""Create an account without setting any alias."""
print("\nSTEP 2: Creating the account without setting any alias...")

transaction = (
AccountCreateTransaction(
initial_balance=Hbar(5),
memo="Account created without alias",
).set_key_without_alias(account_private_key)
)
.set_key_without_alias(account_public_key)
.freeze_with(client)
.sign(account_private_key)
)

transaction = transaction.freeze_with(client).sign(account_private_key)
response = transaction.execute(client)

response = transaction.execute(client)
new_account_id = response.account_id
if response.status != ResponseCode.SUCCESS:
raise RuntimeError(
f"Transaction failed with status: {response.status.name}"
)

if new_account_id is None:
raise RuntimeError(
"AccountID not found in receipt. Account may not have been created."
)
new_account_id = response.account_id

print(f"✅ Account created with ID: {new_account_id}\n")
if new_account_id is None:
raise RuntimeError(
"AccountID not found in receipt. Account may not have been created."
)

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

out = info_to_dict(account_info)
print("Account Info:")
print(json.dumps(out, indent=2) + "\n")
def fetch_account_info(client: Client, account_id: AccountId) -> AccountInfo:
"""Fetch account information."""
account_info = (
AccountInfoQuery()
.set_account_id(account_id)
.execute(client)
)
return account_info

def main() -> None:
"""Main entry point."""
try:
client = setup_client()
account_private_key, account_public_key = generate_account_key()
new_account_id = create_account_without_alias(client, account_public_key, account_private_key)
account_info = fetch_account_info(client, new_account_id)
print("\nAccount Info:")
print(account_info)
print(
"✅ contract_account_id (no alias, zero-padded): "
"\n✅ contract_account_id (no alias, zero-padded): "
f"{account_info.contract_account_id}"
)

except Exception as error:
print(f"❌ Error: {error}")
sys.exit(1)


def main():
"""Main entry point."""
client = setup_client()
create_account_without_alias(client)


if __name__ == "__main__":
main()