captcha.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import axios, { AxiosInstance } from 'axios'
  2. import dotenv from 'dotenv'
  3. import polly from 'polly-js'
  4. import { solveChallenge } from 'shawnlu96-altcha-lib'
  5. dotenv.config()
  6. const googlekey = '6LdLhtYpAAAAAOe1xmceNR-i6MTtzq7N6AYztoVI'
  7. const hCaptchaKey = '04d28d90-d5b9-4a90-94e5-a12c595bd4e2'
  8. const captchaApiKey = process.env.CAPTCHA_API_KEY
  9. export async function getRecaptcha(address: string) {
  10. return await polly()
  11. .waitAndRetry(2)
  12. .executeForPromise(async () => {
  13. const res = await axios.post('https://2captcha.com/in.php', {
  14. method: 'userrecaptcha',
  15. key: captchaApiKey,
  16. googlekey,
  17. pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
  18. json: 1,
  19. action: 'verify',
  20. })
  21. const request = res.data.request
  22. return await polly()
  23. .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
  24. .executeForPromise(async () => {
  25. const captchaRes = await axios.get(
  26. `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
  27. )
  28. const token = captchaRes.data.request
  29. console.log(token)
  30. if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
  31. if (!token) throw new Error('no result')
  32. return token
  33. })
  34. })
  35. }
  36. export async function getAltchaPayload(client: AxiosInstance) {
  37. return await polly()
  38. .waitAndRetry(5)
  39. .executeForPromise(async () => {
  40. // console.log('retry:', info.count)
  41. const challengeResp = await client.get(
  42. 'https://faucet-api.initiation-1.initia.xyz/create_challenge',
  43. )
  44. const challenge = challengeResp.data
  45. // console.log(challenge)
  46. const { promise } = solveChallenge(
  47. challenge.challenge,
  48. challenge.salt,
  49. challenge.max,
  50. challenge.start,
  51. )
  52. const solution = await promise
  53. const payload = {
  54. algorithm: 'SHA-256',
  55. challenge: challenge.challenge,
  56. number: solution.number,
  57. salt: challenge.salt,
  58. signature: challenge.signature,
  59. took: solution.took,
  60. }
  61. console.log('challenge resolved')
  62. return btoa(JSON.stringify(payload))
  63. })
  64. }
  65. export async function solveHCaptcha(address: string) {
  66. return await polly()
  67. .waitAndRetry(2)
  68. .executeForPromise(async () => {
  69. const res = await axios.post('https://2captcha.com/in.php', {
  70. method: 'hcaptcha',
  71. sitekey: hCaptchaKey,
  72. key: captchaApiKey,
  73. pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
  74. json: 1,
  75. })
  76. const request = res.data.request
  77. return await polly()
  78. .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
  79. .executeForPromise(async () => {
  80. const captchaRes = await axios.get(
  81. `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
  82. )
  83. const token = captchaRes.data.request
  84. // console.log(token)
  85. if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
  86. if (!token) throw new Error('no result')
  87. console.log('hCaptcha solved')
  88. return token
  89. })
  90. })
  91. }