@tristeroresearch/mach-sdk
Version:
A TypeScript SDK for integrating with Mach's API.
41 lines (40 loc) • 1.62 kB
JavaScript
/**
* This helper function initializes wallet and public clients for a
* specified blockchain network using a provided private key.
* @param chain - The chain to create the wallet client for
* @param privateKey - The private key of the account (optional)
* @returns The wallet client
*/
import { createPublicClient, createWalletClient, http, } from 'viem';
import { privateKeyToAccount } from 'viem/accounts';
import { getChainFromName } from './getChainFromName.util';
import { attemptToLoadPrivateKeyFromEnv } from './attemptToLoadPrivateKeyFromEnv.util';
import { ErrorMessage } from '../errors/constants';
export const createWalletClients = async (chain, privateKey) => {
try {
// Throws an error if the private key is not found in the environment
if (!privateKey)
privateKey = attemptToLoadPrivateKeyFromEnv(privateKey);
const selectedChain = await getChainFromName(chain);
// Clean and format the private key to ensure it's properly formatted
const cleanKey = privateKey?.replace('0x', '').padStart(64, '0');
const account = privateKeyToAccount(`0x${cleanKey}`);
const publicClient = createPublicClient({
chain: selectedChain,
transport: http(),
});
const walletClient = createWalletClient({
account,
chain: selectedChain,
transport: http(),
});
return {
publicClient,
walletClient,
account,
};
}
catch (error) {
throw new Error(ErrorMessage.FailedToCreateWalletClients);
}
};