recaptcha.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import axios from 'axios'
  2. import dotenv from 'dotenv'
  3. import polly from 'polly-js'
  4. dotenv.config()
  5. const googlekey = '6LdLhtYpAAAAAOe1xmceNR-i6MTtzq7N6AYztoVI'
  6. const captchaApiKey = process.env.CAPTCHA_API_KEY
  7. export async function getCaptcha(address: string) {
  8. return await polly()
  9. .waitAndRetry(2)
  10. .executeForPromise(async () => {
  11. const res = await axios.post('https://2captcha.com/in.php', {
  12. method: 'userrecaptcha',
  13. key: captchaApiKey,
  14. googlekey,
  15. pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
  16. json: 1,
  17. action: 'verify',
  18. })
  19. const request = res.data.request
  20. return await polly()
  21. .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
  22. .executeForPromise(async () => {
  23. const captchaRes = await axios.get(
  24. `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
  25. )
  26. const token = captchaRes.data.request
  27. console.log(token)
  28. if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
  29. if (!token) throw new Error('no result')
  30. return token
  31. })
  32. })
  33. }