@hashgraphonline/conversational-agent
Version:
Hashgraph Online conversational AI agent implementing HCS-10 communication, HCS-2 registries, and content inscription on Hedera. https://hol.org
70 lines (69 loc) • 2.29 kB
JavaScript
import { AccountId, Client, PrivateKey, TransactionReceipt, PublicKey } from "@hashgraph/sdk";
import { AbstractSigner } from "hedera-agent-kit";
import { HederaMirrorNode, Logger } from "@hashgraphonline/standards-sdk";
class BrowserSigner extends AbstractSigner {
getAccountId() {
return this.account;
}
getNetwork() {
return this.network;
}
constructor(accountId, network, executor) {
super();
this.account = AccountId.fromString(accountId);
this.network = network;
this.client = network === "mainnet" ? Client.forMainnet() : Client.forTestnet();
this.exec = executor ?? null;
this.ephemeralKey = PrivateKey.generateED25519();
}
/**
* Returns an auto-generated ED25519 key for client wiring only.
* Do not use for signing; wallet performs signing in renderer.
*/
getOperatorPrivateKey() {
return this.ephemeralKey;
}
getClient() {
return this.client;
}
async signAndExecuteTransaction(tx) {
if (!this.exec) {
throw new Error("BrowserSigner executor not available");
}
if (!tx.isFrozen()) {
await tx.freezeWith(this.client);
}
const base64 = Buffer.from(tx.toBytes()).toString("base64");
const { transactionId } = await this.exec(base64, this.network);
const mirror = new HederaMirrorNode(this.network);
const deadline = Date.now() + 6e4;
while (Date.now() < deadline) {
try {
const details = await mirror.getTransaction(transactionId);
if (details && details.result) {
return TransactionReceipt.fromBytes(
Buffer.from(details.result, "base64")
);
}
} catch {
}
await new Promise((r) => setTimeout(r, 1200));
}
return TransactionReceipt.fromBytes(Buffer.from(""));
}
async getPublicKey() {
const network = this.network === "mainnet" ? "mainnet" : "testnet";
const mirror = new HederaMirrorNode(
network,
new Logger({ module: "BrowserSigner" })
);
const anyKey = await mirror.getPublicKey(this.account.toString());
const keyStr = typeof anyKey?.toString === "function" ? anyKey.toString() : String(anyKey);
return PublicKey.fromString(keyStr);
}
}
export {
BrowserSigner,
BrowserSigner as default
};
//# sourceMappingURL=index40.js.map