1+ import time
2+ from typing import Optional
3+ from hiero_sdk_python .crypto .public_key import PublicKey
4+ from hiero_sdk_python .hapi .services import file_create_pb2
5+ from hiero_sdk_python .hapi .services .basic_types_pb2 import KeyList as KeyListProto
6+ from hiero_sdk_python .hbar import Hbar
7+ from hiero_sdk_python .timestamp import Timestamp
8+ from hiero_sdk_python .transaction .transaction import Transaction
9+ from hiero_sdk_python .channels import _Channel
10+ from hiero_sdk_python .executable import _Method
11+
12+ class FileCreateTransaction (Transaction ):
13+ """
14+ Represents a file create transaction on the network.
15+
16+ This transaction creates a new file on the network with the specified keys, contents,
17+ expiration time and memo.
18+
19+ Inherits from the base Transaction class and implements the required methods
20+ to build and execute a file create transaction.
21+ """
22+
23+ # 90 days in seconds is the default expiration time
24+ DEFAULT_EXPIRY_SECONDS = 90 * 24 * 60 * 60 # 7776000
25+
26+ def __init__ (self , keys : Optional [list [PublicKey ]] = None , contents : Optional [str | bytes ] = None , expiration_time : Optional [Timestamp ] = None , file_memo : Optional [str ] = None ):
27+ """
28+ Initializes a new FileCreateTransaction instance with the specified parameters.
29+
30+ Args:
31+ keys (Optional[list[PublicKey]], optional): The keys that are allowed to update/delete the file.
32+ contents (Optional[str | bytes], optional): The contents of the file to create. Strings will be automatically encoded as UTF-8 bytes.
33+ expiration_time (Optional[Timestamp], optional): The time at which the file should expire.
34+ file_memo (Optional[str], optional): A memo to include with the file.
35+ """
36+ super ().__init__ ()
37+ self .keys : Optional [list [PublicKey ]] = keys or []
38+ self .contents : Optional [bytes ] = self ._encode_contents (contents )
39+ self .expiration_time : Optional [Timestamp ] = expiration_time if expiration_time else Timestamp (int (time .time ()) + self .DEFAULT_EXPIRY_SECONDS , 0 )
40+ self .file_memo : Optional [str ] = file_memo
41+ self ._default_transaction_fee = Hbar (5 ).to_tinybars ()
42+
43+ def _encode_contents (self , contents : Optional [str | bytes ]) -> Optional [bytes ]:
44+ """
45+ Helper method to encode string contents to UTF-8 bytes.
46+
47+ Args:
48+ contents (Optional[str | bytes]): The contents to encode.
49+
50+ Returns:
51+ Optional[bytes]: The encoded contents or None if input is None.
52+ """
53+ if contents is None :
54+ return None
55+ if isinstance (contents , str ):
56+ return contents .encode ('utf-8' )
57+ return contents
58+
59+ def set_keys (self , keys : Optional [list [PublicKey ]] | PublicKey ) -> 'FileCreateTransaction' :
60+ """
61+ Sets the keys for this file create transaction.
62+
63+ Args:
64+ keys (Optional[list[PublicKey]] | PublicKey): The keys to set for the file. Can be a list of PublicKey objects or None.
65+
66+ Returns:
67+ FileCreateTransaction: This transaction instance.
68+ """
69+ self ._require_not_frozen ()
70+ if isinstance (keys , PublicKey ):
71+ self .keys = [keys ]
72+ else :
73+ self .keys = keys or []
74+ return self
75+
76+ def set_contents (self , contents : Optional [str | bytes ]) -> 'FileCreateTransaction' :
77+ """
78+ Sets the contents for this file create transaction.
79+
80+ Args:
81+ contents (Optional[str | bytes]): The contents of the file to create. Strings will be automatically encoded as UTF-8 bytes.
82+
83+ Returns:
84+ FileCreateTransaction: This transaction instance.
85+ """
86+ self ._require_not_frozen ()
87+ self .contents = self ._encode_contents (contents )
88+ return self
89+
90+ def set_expiration_time (self , expiration_time : Optional [Timestamp ]) -> 'FileCreateTransaction' :
91+ """
92+ Sets the expiration time for this file create transaction.
93+
94+ Args:
95+ expiration_time (Optional[Timestamp]): The expiration time for the file.
96+
97+ Returns:
98+ FileCreateTransaction: This transaction instance.
99+ """
100+ self ._require_not_frozen ()
101+ self .expiration_time = expiration_time
102+ return self
103+
104+ def set_file_memo (self , file_memo : Optional [str ]) -> 'FileCreateTransaction' :
105+ """
106+ Sets the memo for this file create transaction.
107+
108+ Args:
109+ file_memo (Optional[str]): The memo to set for the file.
110+
111+ Returns:
112+ FileCreateTransaction: This transaction instance.
113+ """
114+ self ._require_not_frozen ()
115+ self .file_memo = file_memo
116+ return self
117+
118+ def build_transaction_body (self ):
119+ """
120+ Builds the transaction body for this file create transaction.
121+
122+ Returns:
123+ TransactionBody: The built transaction body.
124+ """
125+ file_create_body = file_create_pb2 .FileCreateTransactionBody (
126+ keys = KeyListProto (keys = [key ._to_proto () for key in self .keys or []]),
127+ contents = self .contents if self .contents is not None else b'' ,
128+ expirationTime = self .expiration_time ._to_protobuf () if self .expiration_time else None ,
129+ memo = self .file_memo if self .file_memo is not None else ''
130+ )
131+ transaction_body = self .build_base_transaction_body ()
132+ transaction_body .fileCreate .CopyFrom (file_create_body )
133+ return transaction_body
134+
135+ def _get_method (self , channel : _Channel ) -> _Method :
136+ """
137+ Gets the method to execute the file create transaction.
138+
139+ This internal method returns a _Method object containing the appropriate gRPC
140+ function to call when executing this transaction on the Hedera network.
141+
142+ Args:
143+ channel (_Channel): The channel containing service stubs
144+
145+ Returns:
146+ _Method: An object containing the transaction function to create a file.
147+ """
148+ return _Method (
149+ transaction_func = channel .file .createFile ,
150+ query_func = None
151+ )
152+
153+ def _from_proto (self , proto : file_create_pb2 .FileCreateTransactionBody ) -> 'FileCreateTransaction' :
154+ """
155+ Initializes a new FileCreateTransaction instance from a protobuf object.
156+
157+ Args:
158+ proto (FileCreateTransactionBody): The protobuf object to initialize from.
159+
160+ Returns:
161+ FileCreateTransaction: This transaction instance.
162+ """
163+ self .keys = [PublicKey ._from_proto (key ) for key in proto .keys .keys ] if proto .keys .keys else []
164+ self .contents = proto .contents
165+ self .expiration_time = Timestamp ._from_protobuf (proto .expirationTime ) if proto .expirationTime else None
166+ self .file_memo = proto .memo
167+ return self
0 commit comments