forked from hedera-dev/hedera-code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_file_onchain.py
42 lines (33 loc) · 1.3 KB
/
store_file_onchain.py
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
#Import library and modules
import os
from hedera import (
Client,
Hbar,
PrivateKey,
AccountId,
FileCreateTransaction,
FileContentsQuery,
Client
)
#create function to store and retrieve file
def createFile():
#Get operator AccountID and PvtKey
OPERATOR_ID = AccountId.fromString('Get yours on Hedera Portal')
OPERATOR_KEY = PrivateKey.fromString('Get yours on Hedera Portal')
#Create client class using Operator credentials
client = Client.forTestnet()
client.setOperator(OPERATOR_ID, OPERATOR_KEY)
#Create a string that contains data to store on Hedera Testnet
f_content = 'This is my test for new file'
#create Transaction to store on Hedera the file and execute it
tnx = FileCreateTransaction()
resp = tnx.setKeys(OPERATOR_KEY.getPublicKey()).setContents(f_content).setMaxTransactionFee(Hbar(2)).execute(client)
#Get receipt of transaction executed
receipt = resp.getReceipt(client)
#Get transaction ID
tx_id = receipt.transactionId.toString()
#Get file ID from receipt and call the class to query the content of the file
fileId = receipt.fileId
query = FileContentsQuery()
#Retreive file content through its ID using the Query class
fileContent = query.setFileId(fileId).execute(client).toStringUtf8()