forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_create.py
More file actions
64 lines (52 loc) · 1.74 KB
/
file_create.py
File metadata and controls
64 lines (52 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Run with:
uv run examples/file_create.py
python examples/file_create.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import (
Client,
AccountId,
PrivateKey,
Network,
)
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction
from hiero_sdk_python.response_code import ResponseCode
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
network = Network(os.getenv('NETWORK'))
client = Client(network)
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
client.set_operator(operator_id, operator_key)
return client
def file_create():
"""
Demonstrates creating a file on the network by:
1. Setting up client with operator account
2. Creating a file with a private key
3. Creating a new file
"""
client = setup_client()
file_private_key = PrivateKey.generate_ed25519()
# Create file
receipt = (
FileCreateTransaction()
.set_keys(file_private_key.public_key()) # Set the keys required to sign any modifications to this file
.set_contents(b"Hello, this is the content of my file on Hedera!")
.set_file_memo("My first file on Hedera")
.freeze_with(client)
.sign(file_private_key)
.execute(client)
)
# Check if the transaction was successful
if receipt.status != ResponseCode.SUCCESS:
print(f"File creation failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
file_id = receipt.file_id
print(f"File created successfully with ID: {file_id}")
if __name__ == "__main__":
file_create()