@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
117 lines (105 loc) • 3.8 kB
text/typescript
import {
RegistryBrokerClient,
type RegistryBrokerClientOptions,
type LedgerCredentialAuthOptions,
} from '@hashgraphonline/standards-sdk/services/registry-broker';
import type { GenericPluginContext } from '../PluginInterface';
export interface RegistryBrokerLedgerOptions
extends LedgerCredentialAuthOptions {
autoAuthenticate?: boolean;
}
export interface RegistryBrokerPluginConfiguration {
client?: RegistryBrokerClientOptions;
ledger?: RegistryBrokerLedgerOptions;
}
type ClientFactory = (
options: RegistryBrokerClientOptions,
) => RegistryBrokerClient;
const env = (key: string): string | undefined =>
process.env[key]?.trim() || undefined;
export class RegistryBrokerClientProvider {
private clientPromise: Promise<RegistryBrokerClient> | null = null;
constructor(
private readonly config: RegistryBrokerPluginConfiguration | undefined,
private readonly logger: GenericPluginContext['logger'],
private readonly createClient: ClientFactory = options =>
new RegistryBrokerClient(options),
) {}
async getClient(): Promise<RegistryBrokerClient> {
if (!this.clientPromise) {
this.clientPromise = this.initialiseClient();
}
return this.clientPromise;
}
private async initialiseClient(): Promise<RegistryBrokerClient> {
const options = this.buildClientOptions();
const client = this.createClient(options);
const ledgerConfig = this.resolveLedgerOptions();
if (ledgerConfig) {
const { autoAuthenticate, ...authOptions } = ledgerConfig;
if (autoAuthenticate === false) {
this.logger.info?.(
'[RegistryBrokerPlugin] Ledger authentication disabled via configuration.',
);
} else {
await client.authenticateWithLedgerCredentials(authOptions);
this.logger.info?.(
`[RegistryBrokerPlugin] Authenticated with ledger account ${authOptions.accountId}.`,
);
}
} else if (!options.apiKey) {
this.logger.warn?.(
'[RegistryBrokerPlugin] Neither REGISTRY_BROKER_API_KEY nor ledger credentials were provided. Paid endpoints will fail.',
);
}
return client;
}
private buildClientOptions(): RegistryBrokerClientOptions {
const merged: RegistryBrokerClientOptions = {
...this.config?.client,
};
merged.baseUrl =
merged.baseUrl ?? env('REGISTRY_BROKER_BASE_URL') ?? merged.baseUrl;
merged.apiKey =
this.normalizeSecret(this.config?.client?.apiKey) ??
this.normalizeSecret(env('REGISTRY_BROKER_API_KEY')) ??
this.normalizeSecret(env('RB_API_KEY')) ??
merged.apiKey;
merged.ledgerApiKey =
this.normalizeSecret(this.config?.client?.ledgerApiKey) ??
this.normalizeSecret(env('REGISTRY_BROKER_LEDGER_KEY')) ??
merged.ledgerApiKey;
return merged;
}
private normalizeSecret(value?: string): string | undefined {
if (!value) {
return undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}
private resolveLedgerOptions(): RegistryBrokerLedgerOptions | undefined {
if (this.config?.ledger) {
return this.config.ledger;
}
const accountId =
env('REGISTRY_BROKER_LEDGER_ACCOUNT_ID') ?? env('HEDERA_ACCOUNT_ID');
const hederaPrivateKey =
env('REGISTRY_BROKER_LEDGER_PRIVATE_KEY') ?? env('HEDERA_PRIVATE_KEY');
const evmPrivateKey =
env('REGISTRY_BROKER_LEDGER_EVM_PRIVATE_KEY') ?? env('ETH_PK');
const network =
env('REGISTRY_BROKER_LEDGER_NETWORK') ??
env('HEDERA_NETWORK') ??
'hedera:testnet';
if (accountId && network && (hederaPrivateKey || evmPrivateKey)) {
return {
accountId,
network,
hederaPrivateKey,
evmPrivateKey,
};
}
return undefined;
}
}