forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_update.py
More file actions
107 lines (80 loc) · 2.88 KB
/
file_update.py
File metadata and controls
107 lines (80 loc) · 2.88 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
106
107
"""
Run:
uv run examples/file_update.py
python examples/file_update.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_info_query import FileInfoQuery
from hiero_sdk_python.file.file_update_transaction import FileUpdateTransaction
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):
"""Create a test file"""
file_private_key = PrivateKey.generate_ed25519()
receipt = (
FileCreateTransaction()
.set_keys(file_private_key.public_key())
.set_contents(b"Hello, this is a test file for querying!")
.set_file_memo("Test file for query")
.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"\nFile created with ID: {file_id}")
return file_id, file_private_key
def query_file_info(client, file_id):
info = FileInfoQuery().set_file_id(file_id).execute(client)
print(info)
def file_update():
"""
Demonstrates querying file info by:
1. Setting up client with operator account
2. Creating a test file
3. Querying the file info
4. Updating the file info
5. Querying the file info again
"""
client = setup_client()
# Create a test file first
file_id, file_private_key = create_file(client)
print("File info before update:")
# Query the file info
query_file_info(client, file_id)
# Generate a new private key that will add to the existing key
new_private_key = PrivateKey.generate_ed25519()
# Update the file
receipt = (
FileUpdateTransaction()
.set_file_id(file_id)
.set_keys([file_private_key.public_key(), new_private_key.public_key()])
.set_contents(b"Updated contents!")
.set_file_memo("Updated memo!")
.freeze_with(client)
.sign(new_private_key)
.sign(file_private_key)
.execute(client)
)
if receipt.status != ResponseCode.SUCCESS:
print(f"File update failed with status: {ResponseCode(receipt.status).name}")
sys.exit(1)
print("File info after update:")
# Query the file info again
query_file_info(client, file_id)
if __name__ == "__main__":
file_update()