export const sendEthFromImportedWallet = async ({
walletAddress,
toAddress,
amount,
password,
}: {
walletAddress: string;
toAddress: string;
amount: string; // Amount in ETH
password?: string;
}) => {
const evmClient = await authenticatedEvmClient();
// Get key shares for imported wallet
const keyShares = await evmClient.getExternalServerKeyShares({
accountAddress: walletAddress
});
// Create public client
const publicClient = evmClient.createViemPublicClient({
chain: base,
rpcUrl: 'https://mainnet.base.org',
});
// Check balance first
const balance = await publicClient.getBalance({
address: walletAddress as `0x${string}`,
});
const amountWei = parseEther(amount);
if (balance < amountWei) {
throw new Error('Insufficient balance');
}
// Prepare and sign transaction
const preparedTx = await publicClient.prepareTransactionRequest({
to: toAddress as `0x${string}`,
value: amountWei,
chain: base,
account: walletAddress as `0x${string}`,
});
const signedTx = await evmClient.signTransaction({
senderAddress: walletAddress as `0x${string}`,
externalServerKeyShares: keyShares,
transaction: preparedTx,
password, // Only if wallet was created with password
});
// Send transaction
const walletClient = createWalletClient({
chain: base,
transport: http('https://mainnet.base.org'),
account: walletAddress as `0x${string}`,
});
const txHash = await walletClient.sendRawTransaction({
serializedTransaction: signedTx,
});
return txHash;
};
// Usage
const txHash = await sendEthFromImportedWallet({
walletAddress: '0xYourImportedWalletAddress',
toAddress: '0xRecipientAddress',
amount: '0.1',
password: 'your-wallet-password',
});
console.log('Transaction sent:', txHash);