-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
277 lines (247 loc) · 8.27 KB
/
utils.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
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
const axios = require('axios');
const compare = require('hamming-distance');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const chalk = require('chalk');
const { BLAZO_PATH } = require('./constant');
const inquirer = require('inquirer');
async function deployContract(bytecode, port) {
const deployData = JSON.stringify({
id: 1,
jsonrpc: '2.0',
method: 'eth_sendTransaction',
params: [
{
data: `0x${bytecode}`,
from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
gas: '0x9716c0',
gasPrice: '0x9184e72a000',
value: '0x0',
},
],
});
try {
const deployResponse = await axios.post(
`http://localhost:${port}`,
deployData,
{
headers: {
'content-type': 'application/json',
},
}
);
if (!deployResponse.data || !deployResponse.data.result) {
console.log('Failed to deploy contract');
return [];
}
const txHash = deployResponse.data.result;
const traceData = JSON.stringify({
id: 2,
jsonrpc: '2.0',
method: 'trace_transaction',
params: [txHash],
});
const traceResponse = await axios.post(
`http://localhost:${port}`,
traceData,
{
headers: {
'content-type': 'application/json',
},
}
);
if (
!traceResponse.data ||
!traceResponse.data.result ||
traceResponse.data.result.length === 0
) {
console.log('Failed to trace transaction');
return [];
}
return traceResponse.data.result;
} catch (e) {
console.log(e);
return [];
}
}
async function deploy(data, offchainConfig) {
const port = 8545;
let bytecodeToAddress = [];
for (var i = 0; i < data.length; i++) {
const contracts = data[i].bytecode;
for (const fileName in contracts) {
for (const contractName in contracts[fileName]) {
if (
offchainConfig.hasOwnProperty(fileName) &&
offchainConfig[fileName].hasOwnProperty(contractName)
) {
const bytecode = contracts[fileName][contractName];
const results = await deployContract(bytecode, port);
if (!results || results.length === 0) {
continue;
}
for (const result of results) {
bytecodeToAddress.push({
code: result.result.code,
address: result.result.address,
});
}
}
}
}
}
for (var i = 0; i < data.length; i++) {
const contracts = data[i].bytecode;
for (const fileName in contracts) {
for (const contractName in contracts[fileName]) {
let bytecode = contracts[fileName][contractName];
if (bytecode.startsWith('0x')) {
bytecode = bytecode.slice(2);
}
let minDistance = Infinity;
let closestBytecodeAddress = null;
for (const bytecodeAddress of bytecodeToAddress) {
let code = bytecodeAddress.code;
if (code.startsWith('0x')) {
code = code.slice(2);
}
const lengthDifference = Math.abs(
bytecode.length - code.length
);
const maxLength = Math.max(bytecode.length, code.length);
if (lengthDifference / maxLength <= 0.1) {
const bytecodeBuffer = Buffer.from(bytecode, 'hex');
const codeBuffer = Buffer.from(code, 'hex');
const targetLength = Math.max(
bytecodeBuffer.length,
codeBuffer.length
);
let paddedBytecodeBuffer = bytecodeBuffer;
let paddedCodeBuffer = codeBuffer;
if (bytecodeBuffer.length < targetLength) {
paddedBytecodeBuffer = Buffer.concat([
bytecodeBuffer,
Buffer.alloc(
targetLength - bytecodeBuffer.length
),
]);
}
if (codeBuffer.length < targetLength) {
paddedCodeBuffer = Buffer.concat([
codeBuffer,
Buffer.alloc(targetLength - codeBuffer.length),
]);
}
const distance = compare(
paddedBytecodeBuffer,
paddedCodeBuffer
);
if (distance !== null && distance < minDistance) {
minDistance = distance;
closestBytecodeAddress = bytecodeAddress;
}
}
}
if (closestBytecodeAddress) {
if (!data[i].hasOwnProperty('address')) {
data[i]['address'] = {};
}
if (!data[i]['address'].hasOwnProperty(fileName)) {
data[i]['address'][fileName] = {};
}
data[i]['address'][fileName][contractName] =
closestBytecodeAddress.address;
}
}
}
}
return data;
}
function randomAddress() {
let address = '0x';
for (let i = 0; i < 40; i++) {
address += Math.floor(Math.random() * 16).toString(16);
}
return address;
}
function hasYarn(cwd = process.cwd()) {
return fs.existsSync(path.resolve(cwd, 'yarn.lock'));
}
function hasPnpm(cwd = process.cwd()) {
return fs.existsSync(path.resolve(cwd, 'pnpm-lock.yaml'));
}
function checkSolcSelectInstalled() {
try {
execSync('solc-select versions');
return true;
} catch (error) {
console.log(error.message);
return false;
}
}
const logger = {
error(...args) {
console.log(chalk.red(...args));
},
warn(...args) {
console.log(chalk.yellow(...args));
},
info(...args) {
console.log(chalk.cyan(...args));
},
success(...args) {
console.log(chalk.green(...args));
},
break() {
console.log('');
},
};
async function getAPIKey(isReset = false) {
if (!isReset) {
if (fs.existsSync(BLAZO_PATH)) {
const token = fs.readFileSync(BLAZO_PATH, 'utf-8').trim();
if (token) {
return token;
}
}
}
logger.info('===============================================');
logger.info(
"You're not logged in. To interact with the Blaz service, you need to be authenticated."
);
logger.info('Please provide your login credentials below.');
logger.info(
"If you don't have an account, you can register at https://blaz.ai, and get the API key at https://blaz.ai/account/apikeys."
);
logger.info('===============================================');
const data = await inquirer.prompt([
{
type: 'input',
name: 'API_KEY',
message: 'Input API Key:',
},
]);
const API_KEY = data['API_KEY'];
fs.writeFileSync(BLAZO_PATH, API_KEY);
return API_KEY;
}
function checkFileExists(filePath) {
const exists = fs.existsSync(filePath);
if (exists) {
return true;
} else {
logger.error(`${filePath} does not exist`);
return false;
}
}
module.exports = {
randomAddress,
deploy,
hasYarn,
hasPnpm,
checkSolcSelectInstalled,
logger,
getAPIKey,
checkFileExists,
};