import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { SigningCyberClient } from '@cybercongress/cyber-js';
import { CybMcpConfig, BECH32_PREFIX } from './config.js';
export class CyberWallet {
private wallet: DirectSecp256k1HdWallet | null = null;
private signingClient: SigningCyberClient | null = null;
private address: string | null = null;
constructor(private config: CybMcpConfig) {}
async initialize(): Promise<void> {
if (!this.config.mnemonic) {
// No mnemonic provided - wallet will not be available for signing operations
return;
}
// Create wallet from mnemonic
this.wallet = await DirectSecp256k1HdWallet.fromMnemonic(
this.config.mnemonic,
{
prefix: BECH32_PREFIX,
}
);
// Get the first account address
const accounts = await this.wallet.getAccounts();
if (accounts.length === 0) {
throw new Error('No accounts found in wallet');
}
this.address = accounts[0].address;
// Create signing client
this.signingClient = await SigningCyberClient.connectWithSigner(
this.config.rpcUrl,
this.wallet
);
}
getAddress(): string {
if (!this.address) {
throw new Error('Wallet not initialized or no mnemonic provided');
}
return this.address;
}
getSigningClient(): SigningCyberClient {
if (!this.signingClient) {
throw new Error('Wallet not initialized or no mnemonic provided');
}
return this.signingClient;
}
isInitialized(): boolean {
return this.wallet !== null && this.signingClient !== null && this.address !== null;
}
}