-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
129 lines (112 loc) · 3.82 KB
/
index.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
import axios from "axios";
const checkFunctions = [
"safeTransferFrom",
"_isApprovedOrOwner",
"isApprovedForAll",
"getApproved",
"_requireMinted",
"_safeTransfer",
"_transfer",
"_beforeTokenTransfer",
"_afterTokenTransfer",
];
const allowedFunctionsRepetead = [
'safeTransferFrom(from,to,tokenId,"");',
'asd(from,to,tokenId,"");',
];
(async () => {
const endpoint = `https://api.polygonscan.com/api?module=contract&action=getsourcecode&address=0x3062850D424e4efD276ccF8eE32f8a9Bf919624a&apikey=PB1JK4DAVCBIRME5A9V7535TMMNDNASG3J`;
//const endpoint = `https://api.bscscan.com/api?module=contract&action=getsourcecode&address=0xB08c4427b5Eb8D51e1D091def1AefE69BA3A6285&apikey=Z2RTQ26BTN38KB8Z5P2W23XJC4CACT52S3`;
try {
const data = await axios.get(endpoint);
// if(data.status !== 200) return false;
let isNotVerified = false;
let sourceCode = data.data.result[0].SourceCode;
let contractName = data.data.result[0].ContractName;
console.log("Contract Name = ", contractName);
if (sourceCode === "") {
console.log(isNotVerified);
return isNotVerified;
}
let sources: any;
let functions: string[] = [];
let fileCodes: string[] = [];
if (sourceCode.substring(0, 2) === "{{") {
sourceCode = sourceCode.slice(1, sourceCode.length - 1);
sources = JSON.parse(sourceCode).sources;
Object.keys(sources).forEach((key) => {
if (!key.includes("@openzeppelin")) {
let func = sources[key].content.split("function ");
fileCodes.push(
sources[key].content
.replaceAll(/(\r\n|\n|\r)/gm, "")
.replaceAll(" ", "")
);
func.forEach((e: string, index: number) => {
if (index > 0) {
let str = e.replace(/\n/g, "");
str = str.slice(0, str.indexOf("{"));
functions.push(str);
}
});
} else {
if (key.includes("ERC721Upgradeable.sol")) {
isNotVerified = true;
}
}
});
} else {
sources = sourceCode;
let fromIndex = sources.indexOf(`contract ${contractName}`);
let mainContractFile = sources.substring(fromIndex);
let isIndex = mainContractFile.indexOf("is") + 2;
let leftCurlyBracketIndex = mainContractFile.indexOf("{");
let checkInheritance = mainContractFile
.substring(isIndex, leftCurlyBracketIndex)
.replaceAll(" ", "")
.split(",");
console.log("Inheritance: ", checkInheritance);
if (checkInheritance.indexOf("ERC721Upgradeable") > -1)
return isNotVerified;
let func = mainContractFile.split("function ");
fileCodes.push(
mainContractFile.replaceAll(/(\r\n|\n|\r)/gm, "").replaceAll(" ", "")
);
func.forEach((e: string, index: number) => {
if (index > 0) {
let str = e.replace(/\n/g, "");
str = str.slice(0, str.indexOf("{"));
functions.push(str);
}
});
}
let isSafeTransfer = false;
if (!isNotVerified) {
functions.forEach((func: any) => {
if (func.includes("safeTransferFrom")) {
isSafeTransfer = true;
}
isNotVerified = func.match(new RegExp(checkFunctions.join("|"), "g"))
? true
: isNotVerified;
});
}
console.log("first condition", isNotVerified);
if (isNotVerified && isSafeTransfer) {
fileCodes.forEach((code: any) => {
for (const allow of allowedFunctionsRepetead) {
isNotVerified = code.includes(allow) ? false : isNotVerified;
if (!isNotVerified) {
break;
}
}
});
}
console.log("second condition", isNotVerified);
console.log(!isNotVerified);
return !isNotVerified;
} catch (ex) {
console.log("ERROR", ex);
return false;
}
})();