difcli
Version:
CLI tool for Diffuse Prime
84 lines • 3.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClientFactory = void 0;
const viem_1 = require("viem");
const accounts_1 = require("viem/accounts");
const config_1 = require("../config");
const chain_utils_1 = require("./chain-utils");
const secure_storage_1 = require("./secure-storage");
class ClientFactory {
/**
* Creates a public client for the specified chain
*/
static async createPublicClient(chainType) {
const chain = await (0, chain_utils_1.getChainForType)(chainType);
const chainConfig = config_1.config.chains[chainType];
return (0, viem_1.createPublicClient)({
chain,
transport: (0, viem_1.http)(chainConfig.rpc),
});
}
/**
* Creates a wallet client for the specified chain using stored private key
*/
static async createWalletClient(chainType) {
const chain = await (0, chain_utils_1.getChainForType)(chainType);
const chainConfig = config_1.config.chains[chainType];
const privateKey = await secure_storage_1.SecureStorage.getPrivateKey();
if (!privateKey) {
throw new Error('No private key configured');
}
const account = (0, accounts_1.privateKeyToAccount)(privateKey);
return (0, viem_1.createWalletClient)({
chain,
transport: (0, viem_1.http)(chainConfig.rpc),
account,
});
}
/**
* Creates both public and wallet clients for the specified chain
*/
static async createClientPair(chainType) {
const chain = await (0, chain_utils_1.getChainForType)(chainType);
const chainConfig = config_1.config.chains[chainType];
const privateKey = await secure_storage_1.SecureStorage.getPrivateKey();
if (!privateKey) {
throw new Error('No private key configured');
}
const account = (0, accounts_1.privateKeyToAccount)(privateKey);
const publicClient = (0, viem_1.createPublicClient)({
chain,
transport: (0, viem_1.http)(chainConfig.rpc),
});
const walletClient = (0, viem_1.createWalletClient)({
chain,
transport: (0, viem_1.http)(chainConfig.rpc),
account,
});
return { publicClient, walletClient };
}
/**
* Gets the account from stored private key
*/
static async getAccount() {
const privateKey = await secure_storage_1.SecureStorage.getPrivateKey();
if (!privateKey) {
throw new Error('No private key configured');
}
return (0, accounts_1.privateKeyToAccount)(privateKey);
}
/**
* Gets chain configuration for the specified chain type
*/
static getChainConfig(chainType) {
return config_1.config.chains[chainType];
}
/**
* Gets current chain from storage
*/
static async getCurrentChain() {
return await secure_storage_1.SecureStorage.getSelectedChain();
}
}
exports.ClientFactory = ClientFactory;
//# sourceMappingURL=client-utils.js.map