UNPKG

@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

372 lines (371 loc) 14.5 kB
import { BaseServiceBuilder } from "hedera-agent-kit"; import { HederaMirrorNode, HCS2Client } from "@hashgraphonline/standards-sdk"; import { SignerProviderRegistry } from "./standards-agent-kit.es3.js"; import { CodedError } from "./standards-agent-kit.es49.js"; class HCS2Builder extends BaseServiceBuilder { constructor(hederaKit) { super(hederaKit); } /** * Get or create HCS-2 client */ async getHCS2Client() { if (!this.hcs2Client) { 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"; let keyTypeHint; try { const mirror = new HederaMirrorNode(networkType); const info = await mirror.requestAccount(operatorId); const t = info?.key?._type; if (t) { const upper = t.toUpperCase(); if (upper.includes("ED25519")) keyTypeHint = "ed25519"; else if (upper.includes("ECDSA")) keyTypeHint = "ecdsa"; } } catch { } const config = { network: networkType, operatorId, operatorKey: operatorPrivateKey, ...keyTypeHint ? { keyType: keyTypeHint } : {} }; this.hcs2Client = new HCS2Client(config); } return this.hcs2Client; } /** * Create a new HCS-2 registry * Note: This executes the transaction directly via HCS2Client */ async createRegistry(options = {}) { const exec = SignerProviderRegistry.walletExecutor; const preferWallet = SignerProviderRegistry.preferWalletOnly; const hasPrivateKey = !!(this.hederaKit?.signer?.getOperatorPrivateKey && this.hederaKit.signer.getOperatorPrivateKey()); if (exec) { const start = SignerProviderRegistry.startHCSDelegate; if (start) { try { const request = { options }; const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet"; const { transactionBytes } = await start("hcs2.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 hcs2.createRegistry"); } } if (!hasPrivateKey) { throw new CodedError("wallet_unavailable", "No wallet executor and no operator private key available for server execution"); } const client = await this.getHCS2Client(); return await client.createRegistry(options); } /** * Register a new entry in an HCS-2 registry */ async registerEntry(registryTopicId, options) { const exec = SignerProviderRegistry.walletExecutor; const preferWallet = SignerProviderRegistry.preferWalletOnly; const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet"; const hasPrivateKey = !!(this.hederaKit?.signer?.getOperatorPrivateKey && this.hederaKit.signer.getOperatorPrivateKey()); try { const { ByteBuildRegistry } = await import("./standards-agent-kit.es4.js"); if (ByteBuildRegistry.has("hcs2.registerEntry")) { const built = await ByteBuildRegistry.build("hcs2.registerEntry", this.hederaKit, { registryTopicId, options }); if (built && built.transactionBytes) { if (exec) { return { success: true, transactionBytes: built.transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes: built.transactionBytes }; } } } } catch { } const start = SignerProviderRegistry.startHCSDelegate; if (start) { try { const request = { registryTopicId, options }; const { transactionBytes } = await start("hcs2.registerEntry", request, network); if (transactionBytes) { if (exec) { return { success: true, transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes }; } } } catch (err) { if (preferWallet) { const msg = `wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`; throw new CodedError("wallet_submit_failed", msg); } } } if (preferWallet) { throw new CodedError("wallet_unavailable", "WalletExecutor not configured for hcs2.registerEntry"); } if (!hasPrivateKey) { throw new CodedError("wallet_unavailable", "No wallet executor and no operator private key available for server execution"); } const client = await this.getHCS2Client(); return await client.registerEntry(registryTopicId, options); } /** * Update an existing entry in an HCS-2 registry */ async updateEntry(registryTopicId, options) { const exec = SignerProviderRegistry.walletExecutor; const preferWallet = SignerProviderRegistry.preferWalletOnly; const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet"; const hasPrivateKey = !!(this.hederaKit?.signer?.getOperatorPrivateKey && this.hederaKit.signer.getOperatorPrivateKey()); try { const { ByteBuildRegistry } = await import("./standards-agent-kit.es4.js"); if (ByteBuildRegistry.has("hcs2.updateEntry")) { const built = await ByteBuildRegistry.build("hcs2.updateEntry", this.hederaKit, { registryTopicId, options }); if (built && built.transactionBytes) { if (exec) { return { success: true, transactionBytes: built.transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes: built.transactionBytes }; } } } } catch { } const start = SignerProviderRegistry.startHCSDelegate; if (start) { try { const request = { registryTopicId, options }; const { transactionBytes } = await start("hcs2.updateEntry", request, network); if (transactionBytes) { if (exec) { return { success: true, transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes }; } } } catch (err) { if (preferWallet) { const msg = `wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`; throw new CodedError("wallet_submit_failed", msg); } } } if (preferWallet) { throw new CodedError("wallet_unavailable", "WalletExecutor not configured for hcs2.updateEntry"); } if (!hasPrivateKey) { throw new CodedError("wallet_unavailable", "No wallet executor and no operator private key available for server execution"); } const client = await this.getHCS2Client(); return await client.updateEntry(registryTopicId, options); } /** * Delete an entry from an HCS-2 registry */ async deleteEntry(registryTopicId, options) { const exec = SignerProviderRegistry.walletExecutor; const preferWallet = SignerProviderRegistry.preferWalletOnly; const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet"; const hasPrivateKey = !!(this.hederaKit?.signer?.getOperatorPrivateKey && this.hederaKit.signer.getOperatorPrivateKey()); try { const { ByteBuildRegistry } = await import("./standards-agent-kit.es4.js"); if (ByteBuildRegistry.has("hcs2.deleteEntry")) { const built = await ByteBuildRegistry.build("hcs2.deleteEntry", this.hederaKit, { registryTopicId, options }); if (built && built.transactionBytes) { if (exec) { return { success: true, transactionBytes: built.transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes: built.transactionBytes }; } } } } catch { } const start = SignerProviderRegistry.startHCSDelegate; if (start) { try { const request = { registryTopicId, options }; const { transactionBytes } = await start("hcs2.deleteEntry", request, network); if (transactionBytes) { if (exec) { return { success: true, transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes }; } } } catch (err) { if (preferWallet) { const msg = `wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`; throw new CodedError("wallet_submit_failed", msg); } } } if (preferWallet) { throw new CodedError("wallet_unavailable", "WalletExecutor not configured for hcs2.deleteEntry"); } if (!hasPrivateKey) { throw new CodedError("wallet_unavailable", "No wallet executor and no operator private key available for server execution"); } const client = await this.getHCS2Client(); return await client.deleteEntry(registryTopicId, options); } /** * Migrate an HCS-2 registry to a new topic */ async migrateRegistry(registryTopicId, options) { const exec = SignerProviderRegistry.walletExecutor; const preferWallet = SignerProviderRegistry.preferWalletOnly; const hasPrivateKey = !!(this.hederaKit?.signer?.getOperatorPrivateKey && this.hederaKit.signer.getOperatorPrivateKey()); try { const { ByteBuildRegistry } = await import("./standards-agent-kit.es4.js"); if (ByteBuildRegistry.has("hcs2.migrateRegistry")) { const built = await ByteBuildRegistry.build("hcs2.migrateRegistry", this.hederaKit, { registryTopicId, options }); if (built && built.transactionBytes) { if (exec) { return { success: true, transactionBytes: built.transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes: built.transactionBytes }; } } } } catch { } const start = SignerProviderRegistry.startHCSDelegate; if (start) { try { const request = { registryTopicId, options }; const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet"; const { transactionBytes } = await start("hcs2.migrateRegistry", request, network); if (transactionBytes) { if (exec) { return { success: true, transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes }; } } } catch (err) { if (preferWallet) { const msg = `wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`; throw new CodedError("wallet_submit_failed", msg); } } } if (preferWallet) { throw new CodedError("wallet_unavailable", "WalletExecutor not configured for hcs2.migrateRegistry"); } if (!hasPrivateKey) { throw new CodedError("wallet_unavailable", "No wallet executor and no operator private key available for server execution"); } const client = await this.getHCS2Client(); return await client.migrateRegistry(registryTopicId, options); } /** * Query entries from an HCS-2 registry */ async getRegistry(topicId, options = {}) { const client = await this.getHCS2Client(); return await client.getRegistry(topicId, options); } /** * Submit a raw message to an HCS-2 topic */ async submitMessage(topicId, payload) { const exec = SignerProviderRegistry.walletExecutor; const preferWallet = SignerProviderRegistry.preferWalletOnly; const network = this.hederaKit.client.network.toString().includes("mainnet") ? "mainnet" : "testnet"; const hasPrivateKey = !!(this.hederaKit?.signer?.getOperatorPrivateKey && this.hederaKit.signer.getOperatorPrivateKey()); try { const { ByteBuildRegistry } = await import("./standards-agent-kit.es4.js"); if (ByteBuildRegistry.has("hcs2.submitMessage")) { const built = await ByteBuildRegistry.build("hcs2.submitMessage", this.hederaKit, { topicId, payload }); if (built && built.transactionBytes) { if (exec) { return { success: true, transactionBytes: built.transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes: built.transactionBytes }; } } } } catch { } const start = SignerProviderRegistry.startHCSDelegate; if (start) { try { const request = { topicId, payload }; const { transactionBytes } = await start("hcs2.submitMessage", request, network); if (transactionBytes) { if (exec) { return { success: true, transactionBytes }; } if (!hasPrivateKey) { return { success: true, transactionBytes }; } } } catch (err) { if (preferWallet) { const msg = `wallet_submit_failed: ${err instanceof Error ? err.message : String(err)}`; throw new CodedError("wallet_submit_failed", msg); } } } if (preferWallet) { throw new CodedError("wallet_unavailable", "WalletExecutor not configured for hcs2.submitMessage"); } if (!hasPrivateKey) { throw new CodedError("wallet_unavailable", "No wallet executor and no operator private key available for server execution"); } const client = await this.getHCS2Client(); await client.submitMessage(topicId, payload); return { success: true }; } /** * Get topic info from mirror node */ async getTopicInfo(topicId) { const client = await this.getHCS2Client(); return await client.getTopicInfo(topicId); } /** * Close the HCS-2 client */ async close() { if (this.hcs2Client) { this.hcs2Client.close(); this.hcs2Client = void 0; } } } export { HCS2Builder }; //# sourceMappingURL=standards-agent-kit.es6.js.map