11"""
2- Contract ID class.
2+ Represents a Contract ID on the Hedera network.
3+
4+ Provides utilities for creating, parsing from strings, converting to protobuf
5+ format, and validating checksums associated with a contract.
36"""
47
58import re
1821
1922EVM_ADDRESS_REGEX = re .compile (r"^(0|[1-9]\d*)\.(0|[1-9]\d*)\.([a-fA-F0-9]{40}$)" )
2023
24+
2125@dataclass (frozen = True )
2226class ContractId :
2327 """
24- Represents a contract ID on the network.
28+ Represents a unique contract ID on the Hedera network.
2529
26- A contract ID consists of three components: shard, realm, and contract ( number).
27- These components uniquely identify a contract in the network .
30+ A contract ID can be represented by its shard, realm, and contract number,
31+ or by a 20-byte EVM address .
2832
2933 Attributes:
30- shard (int): The shard number. Defaults to 0.
31- realm (int): The realm number. Defaults to 0.
32- contract (int): The contract number. Defaults to 0.
33- evm_address (bytes): The EVM address of the contract. Defaults to None.
34+ shard (int): The shard number (non-negative). Defaults to 0.
35+ realm (int): The realm number (non-negative). Defaults to 0.
36+ contract (int): The contract number (non-negative). Defaults to 0.
37+ evm_address (Optional[bytes]): The 20-byte EVM address of the contract.
38+ Defaults to None.
39+ checksum (Optional[str]): A network-specific checksum computed from
40+ the shard, realm, and contract numbers. Not used if `evm_address`
41+ is set.
3442 """
3543
3644 shard : int = 0
@@ -43,6 +51,14 @@ class ContractId:
4351 def _from_proto (cls , contract_id_proto : basic_types_pb2 .ContractID ) -> "ContractId" :
4452 """
4553 Creates a ContractId instance from a protobuf ContractID object.
54+
55+ Args:
56+ contract_id_proto (basic_types_pb2.ContractID): The protobuf
57+ ContractID object to convert from.
58+
59+ Returns:
60+ ContractId: A new ContractId instance populated with data from
61+ the protobuf object.
4662 """
4763 return cls (
4864 shard = contract_id_proto .shardNum ,
@@ -53,6 +69,10 @@ def _from_proto(cls, contract_id_proto: basic_types_pb2.ContractID) -> "Contract
5369 def _to_proto (self ):
5470 """
5571 Converts the ContractId instance to a protobuf ContractID object.
72+
73+ Returns:
74+ basic_types_pb2.ContractID: The corresponding protobuf
75+ ContractID object.
5676 """
5777 return basic_types_pb2 .ContractID (
5878 shardNum = self .shard ,
@@ -64,7 +84,21 @@ def _to_proto(self):
6484 @classmethod
6585 def from_string (cls , contract_id_str : str ) -> "ContractId" :
6686 """
67- Parses a string in the format 'shard.realm.contract' to create a ContractId instance.
87+ Parses a string to create a ContractId instance.
88+
89+ The string can be in the format 'shard.realm.contract' (e.g., "0.0.123"),
90+ 'shard.realm.contract-checksum' (e.g., "0.0.123-vfmkw"),
91+ or 'shard.realm.evm_address' (e.g., "0.0.a...f").
92+
93+ Args:
94+ contract_id_str (str): The contract ID string to parse.
95+
96+ Returns:
97+ ContractId: A new ContractId instance.
98+
99+ Raises:
100+ ValueError: If the contract ID string is None, not a string,
101+ or in an invalid format.
68102 """
69103 if contract_id_str is None or not isinstance (contract_id_str , str ):
70104 raise ValueError (
@@ -86,7 +120,7 @@ def from_string(cls, contract_id_str: str) -> "ContractId":
86120 try :
87121 shard , realm , contract , checksum = parse_from_string (contract_id_str )
88122
89- contract_id : ContractId = cls (
123+ contract_id : ContractId = cls (
90124 shard = int (shard ),
91125 realm = int (realm ),
92126 contract = int (contract )
@@ -102,16 +136,29 @@ def from_string(cls, contract_id_str: str) -> "ContractId":
102136
103137 def __str__ (self ):
104138 """
105- Returns the string representation of the ContractId in the format 'shard.realm.contract'.
139+ Returns the string representation of the ContractId.
140+
141+ Format will be 'shard.realm.contract' or 'shard.realm.evm_address_hex'
142+ if evm_address is set. Does not include a checksum.
143+
144+ Returns:
145+ str: The string representation of the ContractId.
106146 """
107147 if self .evm_address is not None :
108148 return f"{ self .shard } .{ self .realm } .{ self .evm_address .hex ()} "
109-
149+
110150 return f"{ self .shard } .{ self .realm } .{ self .contract } "
111151
112152 def to_evm_address (self ) -> str :
113153 """
114- Converts the ContractId to an EVM address.
154+ Converts the ContractId to a 20-byte EVM address string (hex).
155+
156+ If the `evm_address` attribute is set, it returns that.
157+ Otherwise, it computes the 20-byte EVM address from the shard, realm,
158+ and contract numbers (e.g., [4-byte shard][8-byte realm][8-byte contract]).
159+
160+ Returns:
161+ str: The 20-byte EVM address as a hex-encoded string.
115162 """
116163 if self .evm_address is not None :
117164 return self .evm_address .hex ()
@@ -127,7 +174,20 @@ def to_evm_address(self) -> str:
127174 return evm_bytes .hex ()
128175
129176 def validate_checksum (self , client : "Client" ) -> None :
130- """Validate the checksum for the contractId"""
177+ """
178+ Validates the checksum (if present) against a client's network.
179+
180+ The checksum is validated against the ledger ID of the provided client.
181+ This method does nothing if no checksum is present on the ContractId.
182+
183+ Args:
184+ client (Client): The client instance, which contains the network
185+ ledger ID used for checksum validation.
186+
187+ Raises:
188+ ValueError: If the checksum is present but invalid or does not
189+ match the client's network.
190+ """
131191 validate_checksum (
132192 self .shard ,
133193 self .realm ,
@@ -138,8 +198,20 @@ def validate_checksum(self, client: "Client") -> None:
138198
139199 def to_string_with_checksum (self , client : "Client" ) -> str :
140200 """
141- Returns the string representation of the ContractId with checksum
142- in 'shard.realm.contract-checksum' format.
201+ Generates a string representation with a network-specific checksum.
202+
203+ Format: 'shard.realm.contract-checksum' (e.g., "0.0.123-vfmkw").
204+
205+ Args:
206+ client (Client): The client instance used to generate the
207+ network-specific checksum.
208+
209+ Returns:
210+ str: The string representation with checksum.
211+
212+ Raises:
213+ ValueError: If the ContractId has an `evm_address` set,
214+ as checksums cannot be applied to EVM addresses.
143215 """
144216 if self .evm_address is not None :
145217 raise ValueError ("to_string_with_checksum cannot be applied to ContractId with evm_address" )
@@ -149,4 +221,4 @@ def to_string_with_checksum(self, client: "Client") -> str:
149221 self .realm ,
150222 self .contract ,
151223 client
152- )
224+ )
0 commit comments