forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_append.py
More file actions
123 lines (97 loc) · 3.86 KB
/
file_append.py
File metadata and controls
123 lines (97 loc) · 3.86 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python3
"""
File Append Example
This example demonstrates how to append content to an existing file on the network.
It shows both single-chunk and multi-chunk append operations.
Run with:
uv run examples/file_append.py
python examples/file_append.py
"""
import sys
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
from hiero_sdk_python import (
Client, Network, PrivateKey, FileCreateTransaction, AccountId,
FileAppendTransaction, ResponseCode
)
def setup_client():
"""Initialize and set up the client with operator account"""
network = Network(os.getenv('NETWORK'))
client = Client(network)
print(os.getenv('OPERATOR_ID'))
print(os.getenv('OPERATOR_KEY'))
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, file_private_key):
"""Create a file with initial content"""
print("Creating file with initial content...")
create_receipt = (
FileCreateTransaction()
.set_keys(file_private_key.public_key())
.set_contents(b"Initial file content")
.set_file_memo("File for append example")
.freeze_with(client)
.sign(file_private_key)
.execute(client)
)
if create_receipt.status != ResponseCode.SUCCESS:
print(f"File creation failed with status: {ResponseCode(create_receipt.status).name}")
sys.exit(1)
file_id = create_receipt.file_id
print(f"File created successfully with ID: {file_id}")
return file_id
def append_file_single(client, file_id, file_private_key):
"""Append content to the file (single chunk)"""
print("\nAppending content to file (single chunk)...")
append_receipt = (
FileAppendTransaction()
.set_file_id(file_id)
.set_contents(b"\nThis is appended content!")
.freeze_with(client)
.sign(file_private_key)
.execute(client)
)
if append_receipt.status != ResponseCode.SUCCESS:
print(f"File append failed with status: {ResponseCode(append_receipt.status).name}")
sys.exit(1)
print("Content appended successfully!")
def append_file_large(client, file_id, file_private_key):
"""Append large content to the file (multi-chunk)"""
print("\nAppending large content (multi-chunk)...")
large_content = b"Large content that will be split into multiple chunks. " * 100
large_append_receipt = (
FileAppendTransaction()
.set_file_id(file_id)
.set_contents(large_content)
.set_chunk_size(1024) # 1KB chunks
.set_max_chunks(50) # Allow up to 50 chunks
.freeze_with(client)
.sign(file_private_key)
.execute(client)
)
if large_append_receipt.status != ResponseCode.SUCCESS:
print(f"Large file append failed with status: {ResponseCode(large_append_receipt.status).name}")
sys.exit(1)
print("Large content appended successfully!")
print(f"Total chunks used: {FileAppendTransaction().set_contents(large_content).get_required_chunks()}")
def main():
"""
Demonstrates appending content to a file on the network by:
1. Setting up client with operator account
2. Creating a file with initial content
3. Appending additional content to the file
"""
client = setup_client()
file_private_key = PrivateKey.generate_ed25519()
# Step 1: Create a file with initial content
file_id = create_file(client, file_private_key)
# Step 2: Append content to the file (single chunk)
append_file_single(client, file_id, file_private_key)
# Step 3: Append large content (multi-chunk)
append_file_large(client, file_id, file_private_key)
if __name__ == "__main__":
main()