-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
71 lines (62 loc) · 2.5 KB
/
Copy pathcode.js
File metadata and controls
71 lines (62 loc) · 2.5 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
const {
Client,
FileCreateTransaction,
FileAppendTransaction,
Hbar
} = require("@hashgraph/sdk");
const fs = require('fs');
// Initialize the Hedera client
const client = Client.forTestnet(); // or Client.forMainnet()
client.setOperator("your-account-id", "your-private-key");
// Function to create a file on Hedera
async function createFile(textData) {
try {
// Create the file transaction
const transaction = new FileCreateTransaction()
.setContents(textData) // Set your text data here
.setMaxTransactionFee(new Hbar(2)) // Set maximum transaction fee
.freezeWith(client);
// Sign the transaction with your private key
const signTx = await transaction.sign(client.operatorPrivateKey);
// Execute the transaction
const txResponse = await signTx.execute(client);
// Get the receipt of the transaction
const receipt = await txResponse.getReceipt(client);
// Get the file ID from the receipt
const fileId = receipt.fileId;
console.log(`File created with ID: ${fileId}`);
return fileId;
} catch (error) {
console.error("Error creating file:", error);
}
}
// Function to append content to a file on Hedera
async function appendToFile(fileId, additionalContent) {
try {
// Create the file append transaction
const appendTransaction = new FileAppendTransaction()
.setFileId(fileId)
.setContents(additionalContent) // Set additional content
.setMaxTransactionFee(new Hbar(2)) // Set maximum transaction fee
.freezeWith(client);
// Sign the transaction with your private key
const signTx = await appendTransaction.sign(client.operatorPrivateKey);
// Execute the transaction
const txResponse = await signTx.execute(client);
// Get the receipt of the transaction
const receipt = await txResponse.getReceipt(client);
console.log(`File append status: ${receipt.status}`);
} catch (error) {
console.error("Error appending to file:", error);
}
}
// Example text data to store on the Hedera ledger
const textData = "This is some text data to store on the Hedera ledger.";
// Create the file and append additional content if needed
(async () => {
const fileId = await createFile(textData);
if (fileId) {
const additionalContent = "This is additional content for the file.";
await appendToFile(fileId, additionalContent);
}
})();