|
1 | | -""" |
2 | | -Run: |
3 | | -uv run examples/account_create.py |
4 | | -python examples/account_create.py |
5 | | -""" |
6 | | -import os |
7 | | -import sys |
8 | | -from dotenv import load_dotenv |
9 | | - |
10 | | -from hiero_sdk_python import ( |
11 | | - Client, |
12 | | - Network, |
13 | | - AccountId, |
14 | | - PrivateKey, |
15 | | - AccountCreateTransaction, |
16 | | - ResponseCode, |
17 | | -) |
18 | | - |
19 | | -load_dotenv() |
20 | | - |
21 | | -def setup_client(): |
22 | | - network = Network(network='testnet') |
23 | | - client = Client(network) |
24 | | - |
25 | | - operator_id = AccountId.from_string(os.getenv('OPERATOR_ID')) |
26 | | - operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY')) |
27 | | - client.set_operator(operator_id, operator_key) |
28 | | - |
29 | | - return client, operator_key |
30 | | - |
31 | | -def create_new_account(client, operator_key): |
32 | | - new_account_private_key = PrivateKey.generate("ed25519") |
33 | | - new_account_public_key = new_account_private_key.public_key() |
34 | | - |
35 | | - transaction = ( |
36 | | - AccountCreateTransaction() |
37 | | - .set_key(new_account_public_key) |
38 | | - .set_initial_balance(100000000) # 1 HBAR in tinybars |
39 | | - .set_account_memo("My new account") |
40 | | - .freeze_with(client) |
41 | | - ) |
42 | | - |
43 | | - transaction.sign(operator_key) |
44 | | - |
45 | | - try: |
46 | | - receipt = transaction.execute(client) |
47 | | - print(f"Transaction status: {receipt.status}") |
48 | | - |
49 | | - if receipt.status != ResponseCode.SUCCESS: |
50 | | - status_message = ResponseCode(receipt.status).name |
51 | | - raise Exception(f"Transaction failed with status: {status_message}") |
52 | | - |
53 | | - new_account_id = receipt.account_id |
54 | | - if new_account_id is not None: |
55 | | - print(f"Account creation successful. New Account ID: {new_account_id}") |
56 | | - print(f"New Account Private Key: {new_account_private_key.to_string()}") |
57 | | - print(f"New Account Public Key: {new_account_public_key.to_string()}") |
58 | | - else: |
59 | | - raise Exception("AccountID not found in receipt. Account may not have been created.") |
60 | | - |
61 | | - except Exception as e: |
62 | | - print(f"Account creation failed: {str(e)}") |
63 | | - sys.exit(1) |
64 | | - |
65 | | -if __name__ == "__main__": |
66 | | - client, operator_key = setup_client() |
67 | | - create_new_account(client, operator_key) |
| 1 | +""" |
| 2 | +Account Creation Example. |
| 3 | +
|
| 4 | +This module demonstrates how to create a new Hedera account using the Hiero Python SDK. |
| 5 | +It shows the complete workflow from setting up a client with operator credentials |
| 6 | +to creating a new account and handling the transaction response. |
| 7 | +
|
| 8 | +The example creates an account with: |
| 9 | +- A generated Ed25519 key pair |
| 10 | +- An initial balance of 1 HBAR (100,000,000 tinybars) |
| 11 | +- A custom account memo |
| 12 | +
|
| 13 | +Usage: |
| 14 | + Run this script directly: |
| 15 | + python examples/account_create.py |
| 16 | + |
| 17 | + Or using uv: |
| 18 | + uv run examples/account_create.py |
| 19 | +
|
| 20 | +Requirements: |
| 21 | + - Environment variables OPERATOR_ID and OPERATOR_KEY must be set |
| 22 | + - A .env file with the operator credentials (recommended) |
| 23 | + - Sufficient HBAR balance in the operator account to pay for account creation |
| 24 | +""" |
| 25 | +import os |
| 26 | +import sys |
| 27 | +from dotenv import load_dotenv |
| 28 | + |
| 29 | +from hiero_sdk_python import ( |
| 30 | + Client, |
| 31 | + Network, |
| 32 | + AccountId, |
| 33 | + PrivateKey, |
| 34 | + AccountCreateTransaction, |
| 35 | + ResponseCode, |
| 36 | +) |
| 37 | + |
| 38 | +load_dotenv() |
| 39 | + |
| 40 | +def setup_client(): |
| 41 | + """ |
| 42 | + Set up and configure a Hedera client for testnet operations. |
| 43 | + |
| 44 | + Creates a client instance connected to the Hedera testnet and configures it |
| 45 | + with operator credentials from environment variables. The operator account |
| 46 | + is used to pay for transactions and sign them. |
| 47 | + |
| 48 | + Returns: |
| 49 | + tuple: A tuple containing: |
| 50 | + - Client: Configured Hedera client instance |
| 51 | + - PrivateKey: The operator's private key for signing transactions |
| 52 | + |
| 53 | + Raises: |
| 54 | + ValueError: If OPERATOR_ID or OPERATOR_KEY environment variables are not set |
| 55 | + Exception: If there's an error parsing the operator credentials |
| 56 | + |
| 57 | + Environment Variables: |
| 58 | + OPERATOR_ID (str): The account ID of the operator (format: "0.0.xxxxx") |
| 59 | + OPERATOR_KEY (str): The private key of the operator account |
| 60 | + """ |
| 61 | + network = Network(network='testnet') |
| 62 | + client = Client(network) |
| 63 | + |
| 64 | + operator_id = AccountId.from_string(os.getenv('OPERATOR_ID')) |
| 65 | + operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY')) |
| 66 | + client.set_operator(operator_id, operator_key) |
| 67 | + |
| 68 | + return client, operator_key |
| 69 | + |
| 70 | +def create_new_account(client, operator_key): |
| 71 | + """ |
| 72 | + Create a new Hedera account with generated keys and initial balance. |
| 73 | + |
| 74 | + This function generates a new Ed25519 key pair, creates an account creation |
| 75 | + transaction, signs it with the operator key, and executes it on the network. |
| 76 | + The new account is created with an initial balance and a custom memo. |
| 77 | + |
| 78 | + Args: |
| 79 | + client (Client): Configured Hedera client instance for network communication |
| 80 | + operator_key (PrivateKey): The operator's private key for signing the transaction |
| 81 | + |
| 82 | + Returns: |
| 83 | + None: This function doesn't return a value but prints the results |
| 84 | + |
| 85 | + Raises: |
| 86 | + Exception: If the transaction fails or the account ID is not found in the receipt |
| 87 | + SystemExit: Calls sys.exit(1) if account creation fails |
| 88 | + |
| 89 | + Side Effects: |
| 90 | + - Prints transaction status and account details to stdout |
| 91 | + - Creates a new account on the Hedera network |
| 92 | + - Deducts transaction fees from the operator account |
| 93 | + - Exits the program with code 1 if creation fails |
| 94 | + |
| 95 | + Example Output: |
| 96 | + Transaction status: ResponseCode.SUCCESS |
| 97 | + Account creation successful. New Account ID: 0.0.123456 |
| 98 | + New Account Private Key: 302e020100300506032b657004220420... |
| 99 | + New Account Public Key: 302a300506032b6570032100... |
| 100 | + """ |
| 101 | + new_account_private_key = PrivateKey.generate("ed25519") |
| 102 | + new_account_public_key = new_account_private_key.public_key() |
| 103 | + |
| 104 | + transaction = ( |
| 105 | + AccountCreateTransaction() |
| 106 | + .set_key(new_account_public_key) |
| 107 | + .set_initial_balance(100000000) # 1 HBAR in tinybars |
| 108 | + .set_account_memo("My new account") |
| 109 | + .freeze_with(client) |
| 110 | + ) |
| 111 | + |
| 112 | + transaction.sign(operator_key) |
| 113 | + |
| 114 | + try: |
| 115 | + receipt = transaction.execute(client) |
| 116 | + print(f"Transaction status: {receipt.status}") |
| 117 | + |
| 118 | + if receipt.status != ResponseCode.SUCCESS: |
| 119 | + status_message = ResponseCode(receipt.status).name |
| 120 | + raise Exception(f"Transaction failed with status: {status_message}") |
| 121 | + |
| 122 | + new_account_id = receipt.account_id |
| 123 | + if new_account_id is not None: |
| 124 | + print(f"Account creation successful. New Account ID: {new_account_id}") |
| 125 | + print(f"New Account Private Key: {new_account_private_key.to_string()}") |
| 126 | + print(f"New Account Public Key: {new_account_public_key.to_string()}") |
| 127 | + else: |
| 128 | + raise Exception("AccountID not found in receipt. Account may not have been created.") |
| 129 | + |
| 130 | + except Exception as e: |
| 131 | + print(f"Account creation failed: {str(e)}") |
| 132 | + sys.exit(1) |
| 133 | + |
| 134 | +if __name__ == "__main__": |
| 135 | + client, operator_key = setup_client() |
| 136 | + create_new_account(client, operator_key) |
0 commit comments