1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- import axios, { AxiosInstance } from 'axios'
- import dotenv from 'dotenv'
- import polly from 'polly-js'
- import { solveChallenge } from 'shawnlu96-altcha-lib'
- dotenv.config()
- const googlekey = '6LdLhtYpAAAAAOe1xmceNR-i6MTtzq7N6AYztoVI'
- const hCaptchaKey = '04d28d90-d5b9-4a90-94e5-a12c595bd4e2'
- const captchaApiKey = process.env.CAPTCHA_API_KEY
- export async function getRecaptcha(address: string) {
- return await polly()
- .waitAndRetry(2)
- .executeForPromise(async () => {
- const res = await axios.post('https://2captcha.com/in.php', {
- method: 'userrecaptcha',
- key: captchaApiKey,
- googlekey,
- pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
- json: 1,
- action: 'verify',
- })
- const request = res.data.request
- return await polly()
- .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
- .executeForPromise(async () => {
- const captchaRes = await axios.get(
- `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
- )
- const token = captchaRes.data.request
- console.log(token)
- if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
- if (!token) throw new Error('no result')
- return token
- })
- })
- }
- export async function getAltchaPayload(client: AxiosInstance) {
- return await polly()
- .waitAndRetry(5)
- .executeForPromise(async () => {
- // console.log('retry:', info.count)
- const challengeResp = await client.get(
- 'https://faucet-api.initiation-1.initia.xyz/create_challenge',
- )
- const challenge = challengeResp.data
- // console.log(challenge)
- const { promise } = solveChallenge(
- challenge.challenge,
- challenge.salt,
- challenge.max,
- challenge.start,
- )
- const solution = await promise
- const payload = {
- algorithm: 'SHA-256',
- challenge: challenge.challenge,
- number: solution.number,
- salt: challenge.salt,
- signature: challenge.signature,
- took: solution.took,
- }
- console.log('challenge resolved')
- return btoa(JSON.stringify(payload))
- })
- }
- export async function solveHCaptcha(address: string) {
- return await polly()
- .waitAndRetry(2)
- .executeForPromise(async () => {
- const res = await axios.post('https://2captcha.com/in.php', {
- method: 'hcaptcha',
- sitekey: hCaptchaKey,
- key: captchaApiKey,
- pageUrl: `https://faucet.testnet.initia.xyz/?address=${address}`,
- json: 1,
- })
- const request = res.data.request
- return await polly()
- .waitAndRetry([10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000])
- .executeForPromise(async () => {
- const captchaRes = await axios.get(
- `https://2captcha.com/res.php?key=${captchaApiKey}&action=get&id=${request}&json=1`,
- )
- const token = captchaRes.data.request
- // console.log(token)
- if (token === 'CAPCHA_NOT_READY') throw new Error('CAPCHA_NOT_READY')
- if (!token) throw new Error('no result')
- console.log('hCaptcha solved')
- return token
- })
- })
- }
|