import axios, { AxiosInstance } from 'axios' import dotenv from 'dotenv' import polly from 'polly-js' import { solveChallenge, solveChallengeWorkers } from 'shawnlu96-altcha-lib' import * as os from 'node:os' dotenv.config() const cpus = os.cpus() console.log('current cores', cpus.length) 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 solution = await solveChallengeWorkers( './build/src/faucet/worker.js', cpus.length / 2, challenge.challenge, challenge.salt, challenge.max, challenge.start, ) 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 }) }) }