|
| 1 | +""" |
| 2 | +**INTERNAL DEVELOPER REFERENCE** |
| 3 | +
|
| 4 | +This example is primarily for internal SDK developers and contributors. |
| 5 | +
|
| 6 | +--- |
| 7 | +
|
| 8 | +This example demonstrates how to work with the TopicInfo class. |
| 9 | +
|
| 10 | +TopicInfo represents consensus topic information on the Hedera network. |
| 11 | +It exposes attributes such as memo, running hash, sequence number, expiration time, |
| 12 | +admin key, submit key, auto-renewal configuration, and ledger ID. |
| 13 | +
|
| 14 | +This example shows: |
| 15 | +- How to manually construct a TopicInfo instance with mock data |
| 16 | +- How to populate key fields |
| 17 | +- How to inspect or pretty-print the data with __str__ / __repr__ |
| 18 | +- How to use _from_proto() with a mocked protobuf message |
| 19 | +
|
| 20 | +No network calls or topic creation are performed. |
| 21 | +
|
| 22 | +Run with: |
| 23 | + uv run examples/topic_info.py |
| 24 | + python examples/topic_info.py |
| 25 | +""" |
| 26 | + |
| 27 | +from hiero_sdk_python.consensus.topic_info import TopicInfo |
| 28 | +from hiero_sdk_python.account.account_id import AccountId |
| 29 | +from hiero_sdk_python.crypto.private_key import PrivateKey |
| 30 | +from hiero_sdk_python.Duration import Duration |
| 31 | +from hiero_sdk_python.hapi.services import consensus_topic_info_pb2 |
| 32 | +from hiero_sdk_python.hapi.services.basic_types_pb2 import AccountID, Key |
| 33 | +from hiero_sdk_python.hapi.services.timestamp_pb2 import Timestamp |
| 34 | +from hiero_sdk_python.tokens.custom_fixed_fee import CustomFixedFee |
| 35 | + |
| 36 | +def mock_running_hash() -> bytes: |
| 37 | + """Generate a mock 48-byte running hash.""" |
| 38 | + return bytes.fromhex("00" * 48) |
| 39 | + |
| 40 | +def mock_admin_key() -> Key: |
| 41 | + """Create a mock admin key.""" |
| 42 | + public_key = PrivateKey.generate_ed25519().public_key() |
| 43 | + key = Key() |
| 44 | + key.ed25519 = public_key.to_bytes_raw() |
| 45 | + return key |
| 46 | + |
| 47 | +def mock_submit_key() -> Key: |
| 48 | + """Create a mock submit key.""" |
| 49 | + public_key = PrivateKey.generate_ecdsa().public_key() |
| 50 | + key = Key() |
| 51 | + key.ECDSA_secp256k1 = public_key.to_bytes_raw() |
| 52 | + return key |
| 53 | + |
| 54 | +def mock_custom_fee() -> CustomFixedFee: |
| 55 | + """Create a mock custom fee.""" |
| 56 | + return CustomFixedFee( |
| 57 | + amount=100, |
| 58 | + denominating_token_id=None, |
| 59 | + fee_collector_account_id=AccountId(0, 0, 456), |
| 60 | + all_collectors_are_exempt=False, |
| 61 | + ) |
| 62 | + |
| 63 | +def mock_expiration_time() -> Timestamp: |
| 64 | + """Create a mock expiration timestamp.""" |
| 65 | + timestamp = Timestamp() |
| 66 | + timestamp.seconds = 1767225600 |
| 67 | + timestamp.nanos = 0 |
| 68 | + return timestamp |
| 69 | + |
| 70 | +def mock_auto_renew_account() -> AccountID: |
| 71 | + """Create a mock auto-renew account ID.""" |
| 72 | + account_id = AccountID() |
| 73 | + account_id.shardNum = 0 |
| 74 | + account_id.realmNum = 0 |
| 75 | + account_id.accountNum = 100 |
| 76 | + return account_id |
| 77 | + |
| 78 | +def mock_ledger_id() -> bytes: |
| 79 | + """Create a mock ledger ID.""" |
| 80 | + return bytes.fromhex("01") |
| 81 | + |
| 82 | +def build_mock_topic_info() -> TopicInfo: |
| 83 | + """Manually construct a TopicInfo instance with mock data.""" |
| 84 | + topic_info = TopicInfo( |
| 85 | + memo="Example topic memo", |
| 86 | + running_hash=mock_running_hash(), |
| 87 | + sequence_number=42, |
| 88 | + expiration_time=mock_expiration_time(), |
| 89 | + admin_key=mock_admin_key(), |
| 90 | + submit_key=mock_submit_key(), |
| 91 | + auto_renew_period=Duration(7776000), |
| 92 | + auto_renew_account=mock_auto_renew_account(), |
| 93 | + ledger_id=mock_ledger_id(), |
| 94 | + fee_schedule_key=None, |
| 95 | + fee_exempt_keys=None, |
| 96 | + custom_fees=[mock_custom_fee()], |
| 97 | + ) |
| 98 | + return topic_info |
| 99 | + |
| 100 | +def build_topic_info_from_proto() -> TopicInfo: |
| 101 | + """Build a TopicInfo from a mocked protobuf message using _from_proto().""" |
| 102 | + proto = consensus_topic_info_pb2.ConsensusTopicInfo() |
| 103 | + proto.memo = "Topic from protobuf" |
| 104 | + proto.runningHash = mock_running_hash() |
| 105 | + proto.sequenceNumber = 100 |
| 106 | + proto.expirationTime.CopyFrom(mock_expiration_time()) |
| 107 | + proto.adminKey.CopyFrom(mock_admin_key()) |
| 108 | + proto.submitKey.CopyFrom(mock_submit_key()) |
| 109 | + proto.autoRenewPeriod.seconds = 7776000 |
| 110 | + proto.autoRenewAccount.CopyFrom(mock_auto_renew_account()) |
| 111 | + proto.ledger_id = mock_ledger_id() |
| 112 | + proto.custom_fees.append(mock_custom_fee()._to_topic_fee_proto()) |
| 113 | + |
| 114 | + topic_info = TopicInfo._from_proto(proto) |
| 115 | + return topic_info |
| 116 | + |
| 117 | +def print_topic_info(topic: TopicInfo) -> None: |
| 118 | + """Display the key attributes of a TopicInfo instance.""" |
| 119 | + print("\nTopicInfo Details:") |
| 120 | + print(f" Memo: {topic.memo}") |
| 121 | + print(f" Sequence Number: {topic.sequence_number}") |
| 122 | + print(f" Running Hash: {topic.running_hash.hex()}") |
| 123 | + |
| 124 | + if topic.auto_renew_period: |
| 125 | + print(f" Auto-Renew Period: {topic.auto_renew_period.seconds} seconds") |
| 126 | + |
| 127 | + if topic.ledger_id: |
| 128 | + print(f" Ledger ID: {topic.ledger_id.hex()}") |
| 129 | + |
| 130 | + print(f" Custom Fees: {len(topic.custom_fees)}") |
| 131 | + |
| 132 | + # Pretty-print using __str__ |
| 133 | + print("\nUsing __str__:") |
| 134 | + print(topic) |
| 135 | + |
| 136 | + # Pretty-print using __repr__ |
| 137 | + print("\nUsing __repr__:") |
| 138 | + print(repr(topic)) |
| 139 | + |
| 140 | +def main(): |
| 141 | + """Demonstrate TopicInfo functionality.""" |
| 142 | + print("TopicInfo Example - Demonstrating topic info construction and inspection") |
| 143 | + print("=" * 70) |
| 144 | + |
| 145 | + # Build TopicInfo directly |
| 146 | + print("\n1. Building TopicInfo directly:") |
| 147 | + topic1 = build_mock_topic_info() |
| 148 | + print_topic_info(topic1) |
| 149 | + |
| 150 | + # Build TopicInfo from protobuf |
| 151 | + print("\n" + "=" * 70) |
| 152 | + print("\n2. Building TopicInfo from protobuf:") |
| 153 | + topic2 = build_topic_info_from_proto() |
| 154 | + print_topic_info(topic2) |
| 155 | + |
| 156 | + print("\n" + "=" * 70) |
| 157 | + print("Example completed successfully!") |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments