forked from vechain/token-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validates.js
97 lines (85 loc) · 2.48 KB
/
validates.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
const path = require('path')
const axios = require('axios')
const file = require('file-system')
const sharp = require('sharp')
const { getTokens } = require('./utils')
const { NETS: NET_FOLDERS, NODES } = require('./const')
function checkFolderName(folderName) {
if (!/^0x[a-f0-9]{40}$/.test(folderName)) {
throw new Error('folder name should be a valid address with lower case')
}
}
function checkInfo(info) {
if (!checkName(info.name)) {
throw new Error('name should be string')
}
if (!checkSymbol(info.symbol)) {
throw new Error('symbol should be string')
}
if (!checkDecimals(info.decimals)) {
throw new Error('decimals should be integer')
}
if (!checkDesc(info.desc)) {
throw new Error('desc should be string')
}
}
async function checkAddressHasCode(net, address) {
const resp = await axios.get(`${NODES[net]}/accounts/${address}`)
if (!resp.data.hasCode) {
throw new Error('invalid contract address')
}
}
function checkName(name) {
return !(name === null || name === undefined || name.length === 0)
}
function checkSymbol(symbol) {
return !(symbol === null || symbol === undefined || symbol.length === 0)
}
function checkDesc(desc) {
return !(desc === null || desc === undefined || desc.length === 0)
}
function checkDecimals(decimals) {
return !(
decimals === null ||
decimals === undefined ||
typeof decimals !== 'number'
)
}
async function checkImg(path) {
const info = await sharp(path).metadata()
if (info.width !== 256 || info.height !== 256) {
throw new Error('image should be 256px * 256px')
}
}
async function validate(net, address) {
const tokenFolder = path.join(
__dirname,
`./tokens/${NET_FOLDERS[net]}/${address}`
)
const info = require(path.join(tokenFolder, 'info.json'))
try {
checkFolderName(address)
const files = file.readdirSync(tokenFolder)
if (!files.includes('info.json') || !files.includes('token.png')) {
throw new Error('missing info.json or token.png')
}
await checkAddressHasCode(net, address)
checkInfo(info)
await checkImg(path.join(tokenFolder, 'token.png'))
} catch (error) {
throw new Error(
`token: ${info.name}\npath: ${tokenFolder}\nmessage: "${error.message}"`
)
}
}
module.exports = {
lint: async function (net) {
const tokens = getTokens(
path.join(__dirname, `./tokens/${NET_FOLDERS[net]}`)
)
for (let i = 0; i < tokens.length; i++) {
const item = tokens[i]
await validate(net, item)
}
}
}