|
| 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