Skip to content

Commit 7cebde3

Browse files
authored
Merge branch 'main' into feat/beginner-issue-guard
2 parents 37b807b + 5d8c71c commit 7cebde3

18 files changed

+1418
-274
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
77
## [Unreleased]
88

99
### Tests
10+
- Format account test files with Black (#1519)
1011
- Improve unit test coverage for `Hbar`, including edge cases, validation, comparisons, and hashing. (#1483)
1112

1213
### Added
@@ -107,6 +108,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
107108
- Added technical docstrings and hardening (set -euo pipefail) to the pr-check-test-files.sh script (#1336)
108109
- Added prompt for coderabbit to review `Query` and it's sub-classes.
109110
- Add StakingInfo class ([1364](https://github.com/hiero-ledger/hiero-sdk-python/issues/1364))
111+
- Added first-class support for EVM address aliases in `AccountId`, including parsing, serialization, Mirror Node population helpers.
110112

111113
### Changed
112114

docs/sdk_developers/training/network_and_client.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Each network has its default mirror node:
9494
| mainnet | `https://mainnet-public.mirrornode.hedera.com` |
9595
| testnet | `https://testnet.mirrornode.hedera.com` |
9696
| previewnet | `https://previewnet.mirrornode.hedera.com` |
97-
| solo | `http://localhost:8080` |
97+
| solo | `http://localhost:5551` |
9898

9999
---
100100

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
"""
2+
uv run examples/account/account_id_populate_from_mirror.py
3+
python examples/account/account_id_populate_from_mirror.py
4+
5+
This example demonstrates how to populate AccountId fields
6+
using mirror node lookups:
7+
8+
1. Create an AccountId from an EVM address
9+
2. Trigger auto account creation via HBAR transfer
10+
3. Populate account number (num) from mirror node
11+
4. Populate EVM address from mirror node
12+
"""
13+
14+
import sys
15+
import time
16+
17+
from hiero_sdk_python import (
18+
Client,
19+
AccountId,
20+
PrivateKey,
21+
TransferTransaction,
22+
Hbar,
23+
TransactionGetReceiptQuery
24+
)
25+
26+
def generate_evm_address():
27+
"""
28+
Generates a new ECDSA key pair and returns its EVM address.
29+
"""
30+
private_key = PrivateKey.generate_ecdsa()
31+
return private_key.public_key().to_evm_address()
32+
33+
34+
def auto_create_account(client, evm_address):
35+
"""
36+
Triggers auto account creation by transferring HBAR
37+
to an EVM address.
38+
"""
39+
print("\nAuto Account Creation...")
40+
41+
try:
42+
evm_account_id = AccountId.from_evm_address(evm_address, 0, 0)
43+
44+
transfer_tx = (
45+
TransferTransaction()
46+
.add_hbar_transfer(evm_account_id, Hbar(1).to_tinybars())
47+
.add_hbar_transfer(client.operator_account_id, Hbar(-1).to_tinybars())
48+
.execute(client)
49+
)
50+
51+
receipt = (
52+
TransactionGetReceiptQuery()
53+
.set_transaction_id(transfer_tx.transaction_id)
54+
.set_include_children(True)
55+
.execute(client)
56+
)
57+
except Exception as e:
58+
print(f"Failed during auto account creation tx: {e}")
59+
sys.exit(1)
60+
61+
if not receipt.children:
62+
print("Auto account creation failed: no child receipts found")
63+
sys.exit(1)
64+
65+
account_id = receipt.children[0].account_id
66+
print(f"Auto-created account: {account_id}")
67+
return account_id
68+
69+
70+
def populate_account_num_example(client, evm_address, created_account_id):
71+
"""
72+
Demonstrates populating AccountId.num from the mirror node.
73+
"""
74+
print("\nExample 1: Populate Account Number from Mirror Node...")
75+
76+
mirror_account_id = AccountId.from_evm_address(evm_address, 0, 0)
77+
print(f"Before populate: num = {mirror_account_id.num}")
78+
79+
time.sleep(5)
80+
81+
try:
82+
mirror_account_id.populate_account_num(client)
83+
except Exception as e:
84+
print(f"Failed to populate account number: {e}")
85+
sys.exit(1)
86+
87+
print("After populate:")
88+
print(f" Shard: {mirror_account_id.shard}")
89+
print(f" Realm: {mirror_account_id.realm}")
90+
print(f" Num: {mirror_account_id.num}")
91+
92+
if mirror_account_id.num != created_account_id.num:
93+
print(
94+
"Account number mismatch:\n"
95+
f" Expected: {created_account_id.num}\n"
96+
f" Got: {mirror_account_id.num}"
97+
)
98+
sys.exit(1)
99+
100+
101+
def populate_evm_address_example(client, created_account_id, evm_address):
102+
"""
103+
Demonstrates populating AccountId.evm_address from the mirror node.
104+
"""
105+
print("\nExample 2: Populate EVM Address from Mirror Node")
106+
107+
print(f"Before populate: evm_address = {created_account_id.evm_address}")
108+
109+
time.sleep(5)
110+
111+
try:
112+
created_account_id.populate_evm_address(client)
113+
except Exception as e:
114+
print(f"Failed to populate EVM address: {e}")
115+
sys.exit(1)
116+
117+
print(f"After populate: evm_address = {created_account_id.evm_address}")
118+
119+
if created_account_id.evm_address != evm_address:
120+
print(
121+
"EVM address mismatch:\n"
122+
f" Expected: {evm_address}\n"
123+
f" Got: {created_account_id.evm_address}"
124+
)
125+
sys.exit(1)
126+
127+
128+
def main():
129+
client = Client.from_env()
130+
131+
print(f"Client set up with operator id {client.operator_account_id}")
132+
133+
evm_address = generate_evm_address()
134+
print(f"Generated EVM address: {evm_address}")
135+
136+
created_account_id = auto_create_account(client, evm_address)
137+
138+
populate_account_num_example(client, evm_address, created_account_id)
139+
populate_evm_address_example(client, created_account_id, evm_address)
140+
141+
142+
143+
if __name__ == "__main__":
144+
main()

0 commit comments

Comments
 (0)