derive-keys.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Derive L2 API keys from your Polymarket wallet.
  3. *
  4. * Usage:
  5. * pnpm derive-keys
  6. *
  7. * Prerequisites:
  8. * - Set PRIVATE_KEY in .env (your Polymarket signer key)
  9. * - Set FUNDER_ADDRESS in .env (your Polymarket proxy wallet)
  10. * - Set SIGNATURE_TYPE in .env (1 for POLY_PROXY)
  11. *
  12. * This script will:
  13. * 1. Create a ClobClient with your wallet
  14. * 2. Call /auth/derive-api-key to get L2 API credentials
  15. * 3. Print the credentials so you can add them to .env
  16. */
  17. import { ClobClient } from '@polymarket/clob-client';
  18. import { Wallet } from '@ethersproject/wallet';
  19. const CLOB_HOST = 'https://clob.polymarket.com';
  20. const CHAIN_ID = 137;
  21. async function main() {
  22. const privateKey = process.env['PRIVATE_KEY'];
  23. const funderAddress = process.env['FUNDER_ADDRESS'];
  24. const signatureType = parseInt(process.env['SIGNATURE_TYPE'] || '1', 10);
  25. if (!privateKey) {
  26. console.error('❌ PRIVATE_KEY not set in .env');
  27. console.error(' Export your key from Polymarket: Settings > Export Private Key');
  28. process.exit(1);
  29. }
  30. if (!funderAddress) {
  31. console.error('❌ FUNDER_ADDRESS not set in .env');
  32. console.error(' This is your Polymarket deposit/proxy wallet address');
  33. process.exit(1);
  34. }
  35. const wallet = new Wallet(privateKey);
  36. console.log('🔑 Deriving L2 API keys from your Polymarket wallet...\n');
  37. console.log(` Signer: ${wallet.address}`);
  38. console.log(` Funder: ${funderAddress}`);
  39. console.log(` Signature Type: ${signatureType} (${signatureType === 1 ? 'POLY_PROXY' : 'EOA'})`);
  40. console.log();
  41. try {
  42. const client = new ClobClient(
  43. CLOB_HOST,
  44. CHAIN_ID,
  45. wallet,
  46. undefined, // creds — will be derived
  47. signatureType,
  48. funderAddress,
  49. );
  50. const creds = await client.createOrDeriveApiKey();
  51. console.log('✅ L2 API keys derived successfully!\n');
  52. console.log('Add these to your .env file:\n');
  53. console.log(`L2_API_KEY=${creds.key}`);
  54. console.log(`L2_API_SECRET=${creds.secret}`);
  55. console.log(`L2_API_PASSPHRASE=${creds.passphrase}`);
  56. console.log();
  57. console.log('⚠️ Keep these secret! Do not commit .env to git.');
  58. } catch (err) {
  59. console.error('❌ Failed to derive API keys:', err);
  60. process.exit(1);
  61. }
  62. }
  63. main();