-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.js
211 lines (174 loc) · 6.32 KB
/
sign.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//填入你的地址和私钥即可 address privateKey
import { ethers } from "ethers";
import fetch from 'node-fetch';
import randomUserAgent from 'random-useragent';
import { promises as fsPromises } from 'fs';
let address = '';
let privateKey = '';
async function getDataInfo() {
try {
const data = await fsPromises.readFile('mydata.json', 'utf8');
const jsonData = JSON.parse(data);
// 访问变量 a 和 b
address = jsonData.address;
privateKey = jsonData.privateKey;
console.log('地址:', address);
console.log('私钥:', privateKey);
} catch (parseError) {
console.error('解析 JSON 时发生错误:', parseError);
}
}
function getHeaders() {
const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
'Origin': 'https://skygate.skyarkchronicles.com',
'Referer': 'https://skygate.skyarkchronicles.com/',
'User-Agent': randomUserAgent.getRandom(), // 假设有一个名为 randomUA 的函数来生成随机的 User-Agent
'Sec-Ch-Ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'cross-site'
};
return headers;
}
let signPromise = '';
let jwt = '';
class YourLoginClass {
constructor() {
this.jwt = null;
// 其他属性初始化
}
async login() {
// 获取登录态jwt
if (this.jwt) {
return this.jwt;
}
const host = 'https://apisky.ntoken.bwtechnology.net/api/{}';
const url = host.replace('{}', 'wallet_signin.php');
const payload = {
api_id: 'skyark_react_api',
api_token: '3C2D36F79AFB3D5374A49BE767A17C6A3AEF91635BF7A3FB25CEA8D4DD',
uWalletAddr: address,
sign: signPromise
};
if (this.invite_code) {
payload.inviter = this.invite_code;
}
try {
const res = await fetch(url, {
method: 'POST',
body: new URLSearchParams(payload),
timeout: 30000, // 超时设置为30秒
// proxies: this.proxy, // 如果需要代理的话,添加代理配置
headers: getHeaders() // 假设有一个用于获取请求头的函数 getHeaders
});
if (res.status !== 200) {
throw new Error(`login error, status code ${res.status}`);
}
const jsonRes = await res.json();
if (jsonRes.err !== 0) {
throw new Error(`login error, error code ${jsonRes.err}, error message ${jsonRes[0]}`);
}
// {"err":0,"msg":"verify_success","jwt":"...","uWalletAddr":"..."}
this.jwt = jsonRes.jwt;
return this.jwt;
} catch (error) {
throw new Error(`login error: ${error.message}`);
}
}
// 其他辅助函数和属性定义
}
async function getJWT() {
try {
const ALCHEMY_GOERLI_URL = 'https://opbnb.publicnode.com';
const provider = new ethers.JsonRpcProvider(ALCHEMY_GOERLI_URL);
const wallet = new ethers.Wallet(privateKey, provider)
const result = await wallet.signMessage("skygate");
console.log(result);
signPromise = result;
} catch (error) {
console.error(error);
// 在这里可以处理错误
}
}
async function startLoginWithRetry(maxRetries = 3) {
let retries = 0;
while (retries < maxRetries) {
try {
const yourLoginInstance = new YourLoginClass();
jwt = await yourLoginInstance.login();
console.log(`JWT Token: ${jwt}`);
return; // 成功登录,退出循环
} catch (error) {
console.error(`Error: ${error.message}`);
retries++;
if (retries < maxRetries) {
console.log(`Retrying... (attempt ${retries} of ${maxRetries})`);
await sleep(1000); // 可以添加等待时间,以避免过于频繁的重试
} else {
console.error("Max retries reached. Unable to start login.");
throw error; // 如果达到最大重试次数仍然失败,抛出错误
}
}
}
}
async function checkinWithRetry(maxRetries = 3) {
let retries = 0;
const retryInterval = 1000; // 1秒间隔
async function doCheckin() {
try {
const result = await checkin();
return result;
} catch (error) {
if (retries < maxRetries) {
console.error(`Error during checkin, retrying in ${retryInterval / 1000} seconds...`);
await new Promise((resolve) => setTimeout(resolve, retryInterval));
retries++;
return doCheckin(); // 递归调用重试
} else {
throw new Error(`Maximum retry limit (${maxRetries}) reached. Error: ${error.message}`);
}
}
}
return doCheckin();
}
async function checkin() {
const host = 'https://apisky.ntoken.bwtechnology.net/api/{}';
const url = host.replace('{}', 'checkIn_skyGate_member.php');
const payload = {
api_id: 'skyark_react_api',
api_token: '3C2D36F79AFB3D5374A49BE767A17C6A3AEF91635BF7A3FB25CEA8D4DD',
jwt: jwt,
};
const response = await fetch(url, {
method: 'POST',
body: new URLSearchParams(payload),
timeout: 30000,
headers: getHeaders(),
});
if (response.status !== 200) {
throw new Error(`checkin error, status code: ${response.status}`);
}
const result = await response.json();
if (result.err !== 0) {
throw new Error(`checkin error, error code ${result.err}, error message ${result['0']}`);
}
return result;
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
try {
await getDataInfo();
await getJWT(); // 等待getJWT完成
await startLoginWithRetry(); // 等待startLogin完成
await checkinWithRetry();
} catch (error) {
console.error(error);
}
}
main(); // 调用主函数开始执行操作