@hashgraphonline/standards-agent-kit
Version:
A modular SDK for building on-chain autonomous agents using Hashgraph Online Standards, including HCS-10 for agent discovery and communication. https://hol.org
220 lines (219 loc) • 7.12 kB
JavaScript
import { BaseServiceBuilder } from "hedera-agent-kit";
import { HCS6Client, HederaMirrorNode } from "@hashgraphonline/standards-sdk";
import { SignerProviderRegistry } from "./standards-agent-kit.es3.js";
import { CodedError } from "./standards-agent-kit.es49.js";
class HCS6Builder extends BaseServiceBuilder {
constructor(hederaKit) {
super(hederaKit);
}
/**
* Get or create HCS-6 client
*/
async getHCS6Client() {
if (!this.hcs6Client) {
const operatorId = this.hederaKit.signer.getAccountId().toString();
const operatorPrivateKey = this.hederaKit.signer?.getOperatorPrivateKey() ? this.hederaKit.signer.getOperatorPrivateKey().toString() : "";
const network = this.hederaKit.client.network;
const networkType = network.toString().includes("mainnet") ? "mainnet" : "testnet";
const config = {
network: networkType,
operatorId,
operatorKey: operatorPrivateKey
};
this.hcs6Client = new HCS6Client(config);
}
return this.hcs6Client;
}
/**
* Create a new HCS-6 dynamic registry
* Note: This executes the transaction directly via HCS6Client
*/
async createRegistry(options = {}) {
const exec = SignerProviderRegistry.walletExecutor;
const preferWallet = SignerProviderRegistry.preferWalletOnly;
const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet";
if (exec) {
const start = SignerProviderRegistry.startHCSDelegate;
if (start) {
try {
const request = { options };
const { transactionBytes } = await start(
"hcs6.createRegistry",
request,
network
);
if (transactionBytes) {
return { success: true, transactionBytes };
}
} catch (err) {
if (preferWallet) {
throw new CodedError(
"wallet_submit_failed",
`wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`
);
}
}
}
if (preferWallet) {
throw new CodedError(
"wallet_unavailable",
"WalletExecutor not configured for hcs6.createRegistry"
);
}
}
const client = await this.getHCS6Client();
const sanitized = { ...options };
if ("adminKey" in sanitized) {
delete sanitized.adminKey;
}
return await client.createRegistry(sanitized);
}
/**
* Register a new dynamic hashinal entry in an HCS-6 registry
*/
async registerEntry(registryTopicId, options) {
const exec = SignerProviderRegistry.walletExecutor;
const preferWallet = SignerProviderRegistry.preferWalletOnly;
const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet";
if (exec) {
const start = SignerProviderRegistry.startHCSDelegate;
if (start) {
try {
const request = { registryTopicId, options };
const { transactionBytes } = await start(
"hcs6.registerEntry",
request,
network
);
if (transactionBytes) {
return { success: true, transactionBytes };
}
} catch (err) {
if (preferWallet) {
throw new CodedError(
"wallet_submit_failed",
`wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`
);
}
}
}
if (preferWallet) {
throw new CodedError(
"wallet_unavailable",
"WalletExecutor not configured for hcs6.registerEntry"
);
}
}
const client = await this.getHCS6Client();
return await client.registerEntry(registryTopicId, options);
}
/**
* Query entries from an HCS-6 registry
*/
async getRegistry(topicId, options = {}) {
const client = await this.getHCS6Client();
return await client.getRegistry(topicId, options);
}
/**
* Create a complete dynamic hashinal with inscription and registry
*/
async createHashinal(options) {
const client = await this.getHCS6Client();
const metadata = {
name: options.metadata?.name || "Dynamic Hashinal",
creator: options.metadata?.creator || this.hederaKit.signer.getAccountId().toString(),
description: options.metadata?.description || "Dynamic hashinal metadata",
type: options.metadata?.type || "json",
...options.metadata
};
return await client.createHashinal({
...options,
metadata
});
}
/**
* Register a dynamic hashinal with combined inscription and registry creation
* This is the main method for creating and updating dynamic hashinals
*/
async register(options) {
const client = await this.getHCS6Client();
const metadata = {
name: options.metadata?.name || "Dynamic Hashinal",
creator: options.metadata?.creator || this.hederaKit.signer.getAccountId().toString(),
description: options.metadata?.description || "Dynamic hashinal registration",
type: options.metadata?.type || "json",
...options.metadata
};
return await client.register({
...options,
metadata
});
}
/**
* Submit a raw message to an HCS-6 topic
*/
async submitMessage(topicId, payload) {
const exec = SignerProviderRegistry.walletExecutor;
const preferWallet = SignerProviderRegistry.preferWalletOnly;
const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet";
if (exec) {
const start = SignerProviderRegistry.startHCSDelegate;
if (start) {
try {
const request = { topicId, payload };
const { transactionBytes } = await start(
"hcs6.submitMessage",
request,
network
);
if (transactionBytes) {
return {
success: true,
transactionBytes
};
}
} catch (err) {
if (preferWallet) {
throw new CodedError(
"wallet_submit_failed",
`wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`
);
}
}
}
if (preferWallet) {
throw new CodedError(
"wallet_unavailable",
"WalletExecutor not configured for hcs6.submitMessage"
);
}
}
const client = await this.getHCS6Client();
return await client.submitMessage(
topicId,
payload
);
}
/**
* Get topic info from mirror node
*/
async getTopicInfo(topicId) {
const network = this.hederaKit.client.network;
const networkType = network.toString().includes("mainnet") ? "mainnet" : "testnet";
const client = new HederaMirrorNode(networkType);
return await client.getTopicInfo(topicId);
}
/**
* Close the HCS-6 client
*/
async close() {
if (this.hcs6Client) {
this.hcs6Client.close();
this.hcs6Client = void 0;
}
}
}
export {
HCS6Builder
};
//# sourceMappingURL=standards-agent-kit.es7.js.map