forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_delete.py
More file actions
105 lines (77 loc) · 2.91 KB
/
file_delete.py
File metadata and controls
105 lines (77 loc) · 2.91 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
This example demonstrates how to delete a file using the Python SDK.
Run with:
uv run examples/file_delete.py
python examples/file_delete.py
"""
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python import AccountId, Client, Network, PrivateKey
from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction
from hiero_sdk_python.file.file_delete_transaction import FileDeleteTransaction
from hiero_sdk_python.file.file_id import FileId
from hiero_sdk_python.file.file_info_query import FileInfoQuery
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 create_file(client: Client):
"""Create a test file and return its ID along with the private key"""
file_private_key = PrivateKey.generate_ed25519()
receipt = (
FileCreateTransaction()
.set_keys(file_private_key.public_key())
.set_contents(b"Hello, this is a test file that will be deleted!")
.set_file_memo("Test file for deletion example")
.freeze_with(client)
.sign(file_private_key)
.execute(client)
)
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}")
return file_id, file_private_key
def query_file_info(client: Client, file_id: FileId):
"""Query file info and display the results"""
info = FileInfoQuery().set_file_id(file_id).execute(client)
print(info)
def file_delete():
"""
Demonstrates the complete file lifecycle by:
1. Creating a file
2. Querying file info (before deletion)
3. Deleting the file
4. Querying file info (after deletion)
"""
# Setup client
client = setup_client()
# Create file
file_id, file_private_key = create_file(client)
# Query file info before deletion
query_file_info(client, file_id)
# Delete the file
receipt = (
FileDeleteTransaction()
.set_file_id(file_id)
.freeze_with(client)
.sign(file_private_key) # File key is required to delete
.execute(client)
)
if receipt.status != ResponseCode.SUCCESS:
print(f"File deletion failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
print(f"\nFile deleted successfully with ID: {file_id}\n")
print("Check if file is deleted by querying info:")
# Query file info after deletion
query_file_info(client, file_id)
if __name__ == "__main__":
file_delete()