// scripts/export-cdp-account.ts
import { CdpClient } from "@coinbase/cdp-sdk";
import dotenv from "dotenv";
dotenv.config();
async function exportAccount() {
try {
console.log('š§ Initializing CDP client...');
const cdp = new CdpClient();
const targetAddress = process.env.CDP_AGENT_ADDRESS! as `0x${string}`;
console.log('š Target Account Address:', targetAddress);
// First, load the account to verify it exists
console.log('š Loading account by address...');
const account = await cdp.evm.getAccount({
address: targetAddress
});
console.log(`ā
Found account: ${account.address}`);
// Export the private key using the address
console.log('š Exporting private key...');
const privateKey = await cdp.evm.exportAccount({
address: targetAddress
});
console.log('ā
Account exported successfully!');
console.log('š Private Key:', privateKey);
console.log('š Address:', targetAddress);
console.log('\nš” You can now use this private key with viem or other libraries');
console.log('ā ļø Keep this private key secure and never share it!');
} catch (error) {
console.error('ā Error exporting account:', error);
if (error instanceof Error) {
console.error('Error details:', error.message);
}
console.log('\nš” Troubleshooting tips:');
console.log('1. Make sure CDP_API_KEY_ID and CDP_API_KEY_SECRET are set in .env');
console.log('2. Ensure CDP_AGENT_ADDRESS is correct');
console.log('3. Verify the account exists and you have access to it');
}
}
// Run the export
exportAccount();