|
6 | 6 | - The resulting `contract_account_id` being the zero-padded value |
7 | 7 |
|
8 | 8 | 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 |
12 | 11 | """ |
13 | 12 |
|
14 | | -import os |
| 13 | +from typing import Tuple |
15 | 14 | import sys |
16 | | -import json |
17 | | -from dotenv import load_dotenv |
18 | | - |
19 | | -from examples.utils import info_to_dict |
20 | 15 |
|
21 | 16 | from hiero_sdk_python import ( |
22 | 17 | Client, |
23 | 18 | PrivateKey, |
| 19 | + PublicKey, |
24 | 20 | AccountCreateTransaction, |
25 | 21 | AccountInfoQuery, |
26 | | - Network, |
27 | 22 | AccountId, |
| 23 | + AccountInfo, |
28 | 24 | Hbar, |
| 25 | + ResponseCode, |
29 | 26 | ) |
30 | 27 |
|
31 | | -load_dotenv() |
32 | | -network_name = os.getenv("NETWORK", "testnet").lower() |
33 | | - |
34 | | - |
35 | | -def setup_client(): |
| 28 | +def setup_client() -> Client: |
36 | 29 | """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( |
64 | 49 | initial_balance=Hbar(5), |
65 | 50 | 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 | + ) |
67 | 56 |
|
68 | | - transaction = transaction.freeze_with(client).sign(account_private_key) |
| 57 | + response = transaction.execute(client) |
69 | 58 |
|
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 | + ) |
72 | 63 |
|
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 |
77 | 65 |
|
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 | + ) |
79 | 70 |
|
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 |
81 | 73 |
|
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 |
85 | 82 |
|
| 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) |
86 | 92 | print( |
87 | | - "✅ contract_account_id (no alias, zero-padded): " |
| 93 | + "\n✅ contract_account_id (no alias, zero-padded): " |
88 | 94 | f"{account_info.contract_account_id}" |
89 | 95 | ) |
90 | | - |
91 | 96 | except Exception as error: |
92 | 97 | print(f"❌ Error: {error}") |
93 | 98 | sys.exit(1) |
94 | 99 |
|
95 | | - |
96 | | -def main(): |
97 | | - """Main entry point.""" |
98 | | - client = setup_client() |
99 | | - create_account_without_alias(client) |
100 | | - |
101 | | - |
102 | 100 | if __name__ == "__main__": |
103 | 101 | main() |
0 commit comments