-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
298 lines (256 loc) · 8.84 KB
/
main.ts
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const { getOctokit, context } = require("@actions/github");
const actions = require("@actions/core");
const { cryptoWaitReady, decodeAddress, signatureVerify } = require("@polkadot/util-crypto");
const { u8aToHex } = require("@polkadot/util");
const nodeFetch = require("node-fetch");
const path = require("path");
const fs = require("fs");
const categories: string[] = require("../../../categories.json");
const networks: string[] = require("../../../networks.json");
const headers = {
"Content-Type": "application/json",
"X-Api-Key": process.env.INPUT_APIKEY,
};
const getPRContentBySha = async (token: string, sha: string) => {
const octKit = getOctokit(token);
const result = await octKit.rest.repos.listPullRequestsAssociatedWithCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: sha,
});
const prs = result.data.filter((pr) => pr.state === "open");
if (prs.length) {
let pullRequest = prs[0];
for (const pr of prs) {
if (pr.head.sha.startsWith(sha)) {
pullRequest = pr;
}
}
return pullRequest;
}
return null;
};
const getPRContentByNumber = async (token: string, num: number) => {
const octKit = getOctokit(token);
const { data: pullRequest } = await octKit.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: num,
});
return pullRequest;
};
const isValidSignature = (signedMessage, signature, address) => {
const publicKey = decodeAddress(address);
const hexPublicKey = u8aToHex(publicKey);
return signatureVerify(signedMessage, signature, hexPublicKey).isValid;
};
const tryIsValidSignatures = (signedMessages, signature, address) => {
for (const signedMessage of signedMessages) {
if (isValidSignature(signedMessage, signature, address)) {
return true;
}
}
return false;
};
const isValidAsset = async (id, symbol, network, owner, file) => {
const apiUrl = `https://${network}.api.subscan.io/api/scan/assets/asset`;
const body = JSON.stringify({ asset_id: id.toString() });
const data = await nodeFetch(apiUrl, {
method: "post",
headers,
body,
})
.then((resp) => resp.json())
.then((data) => {
if (data?.code === 0) {
return data;
}
throw new Error(`response code ${data?.code} failed: ${data?.message}`);
})
.then((data) => data.data)
.catch(console.error);
if (data?.admin && data?.owner && data?.metadata) {
if (owner !== data.owner?.address || owner !== data.admin.address) {
actions.setFailed("wrong Asset Owner & Signature Account");
return false;
}
if (symbol && symbol !== data.metadata?.symbol) {
actions.setFailed(`wrong TokenSymbol in ${file}`);
return false;
}
return true;
}
actions.setFailed("get asset failed");
return false;
};
const isValidSystemCustom = async (symbol, category, network, file) => {
const apiUrl = `https://${network}.api.subscan.io/api/v2/scan/tokens`;
const body = JSON.stringify({
include_extends: true,
page: 0,
provider: category === "system" ? "system" : "asset_registry",
row: 100,
});
const tokens = await nodeFetch(apiUrl, {
method: "post",
headers,
body,
})
.then((resp) => resp.json())
.then((data) => {
if (data?.code === 0) {
return data;
}
throw new Error(`response code ${data?.code} failed: ${data?.message}`);
})
.then((data) => data.data.tokens)
.catch(console.error);
if (tokens?.length) {
const found = tokens.filter((token) => token.symbol === symbol);
if (found.length) {
if (found.length > 1) {
actions.warning(`found ${found.length} ${symbol} tokens`);
}
return true;
}
actions.setFailed(`TokenSymbol is invalid in ${file}`);
return false;
}
actions.setFailed(`"get ${category} asset failed"`);
return false;
};
const isValidERC20ERC721 = async (id, symbol, category, network, file) => {
const apiUrl = `https://${network}.api.subscan.io/api/scan/evm/tokens`;
const body = JSON.stringify({
contracts: [id.toString()],
});
const list = await nodeFetch(apiUrl, {
method: "post",
headers,
body,
})
.then((resp) => resp.json())
.then((data) => {
if (data?.code === 0) {
return data;
}
throw new Error(`response code ${data?.code} failed: ${data?.message}`);
})
.then((data) => data.data?.list)
.catch(console.error);
if (list?.length) {
if (symbol && symbol !== list[0].symbol) {
actions.setFailed(`wrong TokenSymbol in ${file}`);
return false;
}
return true;
}
actions.setFailed(`get ${category} asset failed`);
return false;
};
const assetRegex =
/Asset Owner & Signature Account:[\n\s]+`([a-zA-Z\d ]+)`[\n\s]+Data that needs to be signed:[\n\s]+`file`[\n\s]+Signature Hash: `([0xa-zA-Z\d ]+)`/;
const othersRegex = /Your Identity:[\n\s]+`(?:Team Member|Community Member|Other)`/;
const main = async () => {
const changes: string[] = process.env.INPUT_CHANGED_FILES.split(" ");
if (changes.length === 0) {
console.log("changes not found");
return;
}
const prSha = process.env.INPUT_SHA;
const prNum = process.env.INPUT_NUM ? Number(process.env.INPUT_NUM) : 0;
const prContent = prNum
? await getPRContentByNumber(process.env.INPUT_TOKEN, prNum)
: await getPRContentBySha(process.env.INPUT_TOKEN, prSha);
console.log("pr", `#${prNum}`, prSha);
if (!prContent) {
actions.setFailed("get PR content failed");
return;
}
const extractedAsset = assetRegex.exec(prContent.body);
const extractedOthers = othersRegex.exec(prContent.body);
if (extractedAsset?.length !== 3 && extractedOthers?.length !== 1) {
actions.setFailed("the PR content is not expected");
return;
}
const owner = extractedAsset?.[1].trim();
const signature = extractedAsset?.[2].trim();
console.log("extracted", owner, signature);
let verified = true;
let logoVerified = true;
await cryptoWaitReady();
for (const file of changes) {
if (!file.startsWith("assets/") || !file.endsWith(".json")) {
continue;
}
const body: string = fs.readFileSync(__dirname + "/../../../" + file, "utf8").toString();
if (body.length === 0) {
continue;
}
const detail = JSON.parse(body);
const {
TokenID: tokenId,
Category: category,
Network: Network,
Logo: logo,
} = detail;
let tokenSymbol = detail.TokenSymbol;
if (!networks.includes(Network)) {
actions.setFailed(`Network is invalid in ${file}`);
verified = false;
}
if (!categories.includes(category)) {
actions.setFailed(`Category is invalid in ${file}`);
verified = false;
}
if (logo) {
const extension = logo.split('.').slice(-1)[0];
const logoPath = path.join(__dirname, "/../../", logo);
const logoFileName = logo.split('/').slice(-1)[0].split('.')[0].split('_');
if (!fs.existsSync(logoPath)) {
actions.setFailed(`${logoPath} does not exists`);
verified = false;
logoVerified = false;
} else if (extension !== 'png' && extension !== 'svg') {
actions.setFailed(`Expect logo in png or svg format`);
verified = false;
logoVerified = false;
} else if (fs.lstatSync(logoPath).size / 1024 > 30) {
actions.setFailed(`The logo file should not be larger than 30KB`);
verified = false;
logoVerified = false;
} else if (logoFileName.length !== 3 || logoFileName[0] !== Network || logoFileName[1] !== category || (tokenSymbol && logoFileName[2] !== tokenSymbol)) {
actions.setFailed(`Please name the logo file according to the network_category_symbol.<png/svg> format`);
verified = false;
logoVerified = false;
}
if (!tokenSymbol && logoFileName.length === 3) {
tokenSymbol = logoFileName[2];
}
}
if (category === "asset") {
if (!owner || !signature) {
actions.setFailed("get owner or signature failed");
verified = false;
continue;
}
verified = !verified || (await isValidAsset(tokenId, tokenSymbol, Network, owner, file));
const secondBody = body.replace(/\n/g, " ");
const thirdBody = secondBody.trim();
if (!tryIsValidSignatures([body, secondBody, thirdBody], signature, owner)) {
actions.setFailed("The signature is invalid");
verified = false;
}
} else if (category === "system" || category === "custom") {
verified = !verified || (await isValidSystemCustom(tokenId || tokenSymbol, category, Network, file));
} else if (category === "erc20" || category === "erc721") {
verified = !verified || (await isValidERC20ERC721(tokenId, tokenSymbol, category, Network, file));
}
if (!verified) {
break;
}
}
actions.setOutput("logoVerified", logoVerified ? "true" : "false");
actions.setOutput("verified", verified ? "true" : "false");
};
main().then();