123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- import axios, { AxiosInstance } from 'axios'
- import { HttpsProxyAgent } from 'https-proxy-agent'
- import { generateRandomString, getProxyAgent } from './utils'
- import { DBClient } from './singletons'
- import { MnemonicKey } from '@initia/initia.js'
- import xrpl from 'xrpl'
- import { ethers } from 'ethers'
- import { v4 as uuidv4 } from 'uuid'
- import {
- formatDateTime,
- getAndroidModel,
- getAndroidOS,
- getDeviceId,
- getIOS,
- getRawMessage,
- } from './faucet/faucetLunch'
- import { Status } from './models/Status'
- export class LunchClient {
- mnemonic: string
- axiosClient: AxiosInstance
- proxyAgent: HttpsProxyAgent<any>
- key: MnemonicKey
- isLogin: boolean = false
- constructor(mnemonic: string, useProxy: boolean = true) {
- this.mnemonic = mnemonic
- this.proxyAgent = useProxy ? getProxyAgent() : undefined
- this.key = new MnemonicKey({ mnemonic: mnemonic })
- }
- async login() {
- const account = await DBClient.instance.account.findFirst({
- where: { address: this.key.accAddress },
- })
- if (!account.gmail) {
- account.gmail = `${generateRandomString(8)}@gmail.com`
- }
- if (!account.xrpPrivateKey) {
- account.xrpPrivateKey = xrpl.Wallet.generate().seed
- }
- const evmWallet = new ethers.Wallet(this.key.privateKey.toString('hex'))
- const message = JSON.stringify({
- signedAt: formatDateTime(new Date()),
- })
- const evmSignedMessage = await evmWallet.signMessage(message)
- const proxyAgent = undefined
- // let isAndroid = Math.random() > 0.5
- let isAndroid = false
- if (account.duid) {
- isAndroid = !account.duid.includes('-')
- }
- if (isAndroid && !account.deviceModel) {
- account.deviceModel = getAndroidModel()
- }
- if (!account.duid) {
- account.duid = isAndroid
- ? await getDeviceId(isAndroid, account.deviceModel, proxyAgent)
- : uuidv4().toUpperCase()
- }
- if (!account.deviceOS) {
- account.deviceOS = isAndroid ? getAndroidOS() : getIOS()
- }
- const rawMessage = await getRawMessage(
- evmWallet.address,
- message,
- account.duid,
- )
- this.axiosClient = isAndroid
- ? axios.create({
- headers: {
- 'Lunch-Language': 'EN',
- 'lunch-fiat-currency': 'USD',
- 'lunch-app-duid': account.duid,
- 'lunch-app-version': '0.17.3',
- 'lun-app-build': 46,
- 'Lunch-Device-Model': account.deviceModel,
- 'Lunch-Device-OS': account.deviceOS,
- 'Lunch-Platform': 'Android',
- 'user-agent': `lunch/0.17.3(46) (Linux; ${account.deviceOS}; ${account.deviceModel} Build/PQ3B.190801.05281822)`,
- },
- httpsAgent: proxyAgent,
- })
- : axios.create({
- headers: {
- 'lunch-language': 'en',
- 'lunch-app-platform': 'iOS',
- 'lunch-app-version': '45',
- 'lunch-fiat-currency': 'USD',
- 'lunch-app-duid': account.duid,
- 'user-agent': `Lunch/1.0 (xyz.lunchlunch.app; build:45; ${account.deviceOS}) Alamofire/5.8.0`,
- },
- })
- const resp = await this.axiosClient.post(
- 'https://api.lunchlunch.xyz/v1/auth/sign-in',
- {
- walletAddress: evmWallet.address,
- signedMessage: evmSignedMessage,
- rawMessage: rawMessage,
- },
- )
- console.log(`${account.address}: sign-in success.`)
- // register
- this.axiosClient.defaults.headers[
- 'authorization'
- ] = `Bearer ${resp.data.accessToken}`
- const xrplWallet = xrpl.Wallet.fromSeed(account.xrpPrivateKey)
- let needRegister = false
- // try {
- // await this.axiosClient.get('https://api.lunchlunch.xyz/v1/member')
- // } catch (e) {
- // if (e.response.status === 404) {
- // needRegister = true
- // }
- // }
- if (needRegister) {
- await this.axiosClient.post(
- 'https://api.lunchlunch.xyz/v1/member/register',
- {
- evmWalletAddress: evmWallet.address,
- initiaWalletAddress: this.key.accAddress,
- isImportedWallet: true,
- firstInitiaWalletAddress: this.key.accAddress,
- email: account.gmail,
- xrplWalletAddress: xrplWallet.address,
- },
- )
- console.log(`${this.key.accAddress}: register done`)
- } else {
- console.log(`${this.key.accAddress}: already registered`)
- }
- await DBClient.instance.account.update({
- where: { id: account.id },
- data: {
- status: Status.Fauceted,
- lastRun: new Date(),
- xrpPrivateKey: account.xrpPrivateKey,
- gmail: account.gmail,
- duid: account.duid,
- deviceModel: account.deviceModel,
- deviceOS: account.deviceOS,
- },
- })
- this.isLogin = true
- }
- async submitOnchain(txHash: string) {
- if (!this.isLogin) throw new Error('Please login first')
- return await this.axiosClient.post(
- `https://api.lunchlunch.xyz/v1/dish/submit-action/onchain`,
- {
- dishId: 7,
- txHash: txHash,
- },
- )
- }
- async claimEgg(txHash: string) {
- if (!this.isLogin) throw new Error('Please login first')
- await this.axiosClient.post(
- `https://api.lunchlunch.xyz/v1/dish/submit-claim/egg`,
- {
- dishId: 7,
- txHash: txHash,
- },
- )
- }
- async getEggBalance() {
- if (!this.isLogin) throw new Error('Please login first')
- const resp = await this.axiosClient.get(
- 'https://api.lunchlunch.xyz/v1/member/egg/balance',
- )
- return resp.data
- }
- }
|