@hashgraphonline/hedera-agent-kit
Version:
Build LLM-powered applications that interact with the Hedera Network. Create conversational agents that can understand user requests in natural language and execute Hedera transactions, or build backend systems that leverage AI for on-chain operations.
135 lines (134 loc) • 4.53 kB
JavaScript
import { FileCreateTransaction, PublicKey, FileAppendTransaction, FileUpdateTransaction, FileDeleteTransaction } from "@hashgraph/sdk";
import { Buffer } from "buffer";
import { detectKeyTypeFromString } from "@hashgraphonline/standards-sdk";
import { BaseServiceBuilder } from "./index11.js";
const MAX_FILE_APPEND_BYTES = 6e3;
class FileBuilder extends BaseServiceBuilder {
constructor(hederaKit) {
super(hederaKit);
}
/**
* @param {CreateFileParams} params
* @returns {this}
*/
createFile(params) {
this.clearNotes();
const transaction = new FileCreateTransaction();
if (params.contents) {
if (typeof params.contents === "string") {
transaction.setContents(Buffer.from(params.contents, "utf8"));
} else {
transaction.setContents(params.contents);
}
}
if (params.keys) {
const publicKeys = params.keys.map((keyInput) => {
if (typeof keyInput === "string") {
const keyDetection = detectKeyTypeFromString(keyInput);
return keyDetection.privateKey.publicKey;
} else if (keyInput instanceof PublicKey) {
return keyInput;
}
this.logger.warn(
"FileBuilder: createFile expects keys to be string or PublicKey. Other types are ignored."
);
return void 0;
}).filter((key) => key instanceof PublicKey);
if (publicKeys.length > 0) {
transaction.setKeys(publicKeys);
}
}
if (params.memo) {
transaction.setFileMemo(params.memo);
}
this.setCurrentTransaction(transaction);
return this;
}
/**
* @param {AppendFileParams} params
* @returns {this}
* @throws {Error}
*/
appendFile(params) {
this.clearNotes();
if (!params.fileId) {
throw new Error("File ID is required to append to a file.");
}
const transaction = new FileAppendTransaction().setFileId(params.fileId);
if (params.contents) {
const contentsBytes = typeof params.contents === "string" ? Buffer.from(params.contents, "utf8") : params.contents;
if (contentsBytes.length > MAX_FILE_APPEND_BYTES) {
console.warn(
`FileBuilder: Content size (${contentsBytes.length} bytes) for appendFile exceeds single transaction limit (${MAX_FILE_APPEND_BYTES} bytes). Only the first chunk will be prepared. Implement multi-transaction append for larger files.`
);
this.addNote(`Content for file append was truncated to ${MAX_FILE_APPEND_BYTES} bytes due to single transaction limit.`);
transaction.setContents(
contentsBytes.subarray(0, MAX_FILE_APPEND_BYTES)
);
} else {
transaction.setContents(contentsBytes);
}
}
this.setCurrentTransaction(transaction);
return this;
}
/**
* @param {UpdateFileParams} params
* @returns {this}
* @throws {Error}
*/
updateFile(params) {
this.clearNotes();
if (!params.fileId) {
throw new Error("File ID is required to update a file.");
}
const transaction = new FileUpdateTransaction().setFileId(params.fileId);
if (params.contents) {
if (typeof params.contents === "string") {
transaction.setContents(Buffer.from(params.contents, "utf8"));
} else {
transaction.setContents(params.contents);
}
}
if (params.keys) {
const publicKeys = params.keys.map((keyInput) => {
if (typeof keyInput === "string") {
const keyDetection = detectKeyTypeFromString(keyInput);
return keyDetection.privateKey.publicKey;
} else if (keyInput instanceof PublicKey) {
return keyInput;
}
this.logger.warn(
"FileBuilder: updateFile expects keys to be string or PublicKey. Other types are ignored."
);
return void 0;
}).filter((key) => key instanceof PublicKey);
if (publicKeys.length > 0) {
transaction.setKeys(publicKeys);
}
}
if (params.memo) {
transaction.setFileMemo(params.memo);
}
this.setCurrentTransaction(transaction);
return this;
}
/**
* @param {DeleteFileParams} params
* @returns {this}
* @throws {Error}
*/
deleteFile(params) {
this.clearNotes();
if (!params.fileId) {
throw new Error("File ID is required to delete a file.");
}
const transaction = new FileDeleteTransaction().setFileId(params.fileId);
this.setCurrentTransaction(transaction);
return this;
}
}
export {
FileBuilder
};
//# sourceMappingURL=index15.js.map