Skip to content

Commit cd1bd3f

Browse files
authored
docs: add examples/account_id.py demonstrating AccountId (#542)
Signed-off-by: Molly Montoya <[email protected]>
1 parent a7da788 commit cd1bd3f

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
1010
- Unified balance and transfer logging format — both now consistently display values in hbars for clarity.
1111

1212
### Added
13+
- Add `examples/account_id.py` demonstrating AccountId class usage including creating standard AccountIds, parsing from strings, comparing instances, and creating AccountIds with public key aliases
1314

1415
- Added Google-style docstrings to `CustomFractionalFee` class and its methods in `custom_fractional_fee.py`.
1516
- Added `dependabot.yaml` file to enable automated dependency management.

examples/account_id.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
"""
2+
uv run examples/account_id.py
3+
python examples/account_id.py
4+
5+
This example demonstrates various ways to use the AccountId class:
6+
1. Creating a standard AccountId
7+
2. Parsing AccountId from a string
8+
3. Comparing AccountId instances
9+
4. Creating an AccountId with a public key alias
10+
"""
11+
12+
from hiero_sdk_python import AccountId, PrivateKey
13+
14+
15+
def create_standard_account_id():
16+
"""
17+
Demonstrates creating a standard AccountId with shard, realm, and num.
18+
19+
The standard format is: shard.realm.num (e.g., 0.0.123)
20+
"""
21+
print("\n=== Example 1: Creating a Standard AccountId ===")
22+
23+
# create an AccountId with default shard and realm (0.0) and account number 123
24+
account_id = AccountId(shard=0, realm=0, num=123)
25+
26+
print(f"Created AccountId: {account_id}")
27+
print(f" Shard: {account_id.shard}")
28+
print(f" Realm: {account_id.realm}")
29+
print(f" Account Number: {account_id.num}")
30+
print(f" String representation: {str(account_id)}")
31+
32+
return account_id
33+
34+
35+
def parse_account_id_from_string():
36+
"""
37+
Demonstrates parsing an AccountId from a string.
38+
39+
The string format should be: 'shard.realm.num'
40+
"""
41+
print("\n=== Example 2: Parsing AccountId from String ===")
42+
43+
# Parse AccountId from a string
44+
account_id_str = "0.0.456789"
45+
account_id = AccountId.from_string(account_id_str)
46+
47+
print(f"Parsed AccountId from string: '{account_id_str}'")
48+
print(f" Shard: {account_id.shard}")
49+
print(f" Realm: {account_id.realm}")
50+
print(f" Account Number: {account_id.num}")
51+
print(f" String representation: {str(account_id)}")
52+
53+
return account_id
54+
55+
56+
def compare_account_ids():
57+
"""
58+
Demonstrates comparing AccountId instances for equality.
59+
60+
Two AccountIds are equal if they have the same shard, realm, num, and alias_key.
61+
"""
62+
print("\n=== Example 3: Comparing AccountId Instances ===")
63+
64+
# Create two identical AccountIds
65+
account_id1 = AccountId(shard=0, realm=0, num=100)
66+
account_id2 = AccountId(shard=0, realm=0, num=100)
67+
68+
# Create a different AccountId
69+
account_id3 = AccountId(shard=0, realm=0, num=200)
70+
71+
print(f"AccountId 1: {account_id1}")
72+
print(f"AccountId 2: {account_id2}")
73+
print(f"AccountId 3: {account_id3}")
74+
75+
print(f"\nAre AccountId 1 and 2 equal? {account_id1 == account_id2}")
76+
print(f"Are AccountId 1 and 3 equal? {account_id1 == account_id3}")
77+
78+
# Demonstrate hash equality (useful for sets and dictionaries)
79+
print(f"\nHash of AccountId 1: {hash(account_id1)}")
80+
print(f"Hash of AccountId 2: {hash(account_id2)}")
81+
print(f"Hash of AccountId 3: {hash(account_id3)}")
82+
print(f"Are hashes of AccountId 1 and 2 equal? {hash(account_id1) == hash(account_id2)}")
83+
84+
85+
def create_account_id_with_alias():
86+
"""
87+
Demonstrates creating an AccountId with a public key alias.
88+
89+
An alias is a public key (ED25519 or ECDSA) that can be used instead of
90+
an account number to identify an account.
91+
"""
92+
print("\n=== Example 4: Creating AccountId with Public Key Alias ===")
93+
94+
# Generate a new private key and get its public key
95+
private_key = PrivateKey.generate()
96+
public_key = private_key.public_key()
97+
98+
# Create an AccountId with a public key alias
99+
account_id_with_alias = AccountId(shard=0, realm=0, num=0, alias_key=public_key)
100+
101+
print(f"Created AccountId with alias: {account_id_with_alias}")
102+
print(f" Shard: {account_id_with_alias.shard}")
103+
print(f" Realm: {account_id_with_alias.realm}")
104+
print(f" Alias Key: {account_id_with_alias.alias_key.to_string()}")
105+
print(f" String representation: {str(account_id_with_alias)}")
106+
print(f" Repr representation: {repr(account_id_with_alias)}")
107+
108+
return account_id_with_alias
109+
110+
111+
def main():
112+
"""
113+
Main function that runs all AccountId examples.
114+
"""
115+
print("=" * 60)
116+
print("AccountId Examples")
117+
print("=" * 60)
118+
119+
# ex 1: Create a standard AccountId
120+
create_standard_account_id()
121+
122+
# ex 2: Parse AccountId from string
123+
parse_account_id_from_string()
124+
125+
# ex 3: Compare AccountId instances
126+
compare_account_ids()
127+
128+
# ex 4: Create AccountId with public key alias
129+
create_account_id_with_alias()
130+
131+
print("\n" + "=" * 60)
132+
print("All AccountId examples completed successfully!")
133+
print("=" * 60)
134+
135+
136+
if __name__ == "__main__":
137+
main()

0 commit comments

Comments
 (0)