-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtruffle.py
executable file
·469 lines (390 loc) · 17.8 KB
/
truffle.py
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
"""
Truffle platform
"""
import glob
import json
import logging
import os
import platform
import re
import shutil
import subprocess
import uuid
from pathlib import Path
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple
from crytic_compile.compilation_unit import CompilationUnit
from crytic_compile.compiler.compiler import CompilerVersion
from crytic_compile.platform import solc
from crytic_compile.platform.abstract_platform import AbstractPlatform
from crytic_compile.platform.exceptions import InvalidCompilation
from crytic_compile.platform.types import Type
from crytic_compile.utils.naming import convert_filename
from crytic_compile.utils.natspec import Natspec
# Handle cycle
if TYPE_CHECKING:
from crytic_compile import CryticCompile
LOGGER = logging.getLogger("CryticCompile")
def export_to_truffle(crytic_compile: "CryticCompile", **kwargs: str) -> List[str]:
"""Export to the truffle format
Args:
crytic_compile (CryticCompile): CryticCompile object to export
**kwargs: optional arguments. Used: "export_dir"
Raises:
InvalidCompilation: If there are more than 1 compilation unit
Returns:
List[str]: Singleton with the generated directory
"""
# Get our export directory, if it's set, we create the path.
export_dir = kwargs.get("export_dir", "crytic-export")
if export_dir and not os.path.exists(export_dir):
os.makedirs(export_dir)
compilation_units = list(crytic_compile.compilation_units.values())
if len(compilation_units) != 1:
raise InvalidCompilation("Truffle export require 1 compilation unit")
compilation_unit = compilation_units[0]
# Loop for each contract filename.
results: List[Dict] = []
for contract_name in compilation_unit.contracts_names:
# Create the informational object to output for this contract
filename = compilation_unit.contracts_filenames[contract_name]
output = {
"contractName": contract_name,
"abi": compilation_unit.abi(contract_name),
"bytecode": "0x" + compilation_unit.bytecode_init(contract_name),
"deployedBytecode": "0x" + compilation_unit.bytecode_runtime(contract_name),
"ast": compilation_unit.ast(filename.absolute),
"userdoc": compilation_unit.natspec[contract_name].userdoc.export(),
"devdoc": compilation_unit.natspec[contract_name].devdoc.export(),
}
results.append(output)
# If we have an export directory, export it.
path = os.path.join(export_dir, contract_name + ".json")
with open(path, "w", encoding="utf8") as file_desc:
json.dump(output, file_desc)
return [export_dir]
class Truffle(AbstractPlatform):
"""
Truffle platform
"""
NAME = "Truffle"
PROJECT_URL = "https://github.com/trufflesuite/truffle"
TYPE = Type.TRUFFLE
# pylint: disable=too-many-locals,too-many-statements,too-many-branches
def compile(self, crytic_compile: "CryticCompile", **kwargs: str) -> None:
"""Compile
Args:
crytic_compile (CryticCompile): CryticCompile object to populate
**kwargs: optional arguments. Used "truffle_build_directory", "truffle_ignore_compile", "ignore_compile",
"truffle_version", "npx_disable"
Raises:
InvalidCompilation: If truffle failed to run
"""
build_directory = kwargs.get("truffle_build_directory", os.path.join("build", "contracts"))
truffle_ignore_compile = kwargs.get("truffle_ignore_compile", False) or kwargs.get(
"ignore_compile", False
)
truffle_version = kwargs.get("truffle_version", None)
# crytic_compile.type = Type.TRUFFLE
# Truffle on windows has naming conflicts where it will invoke truffle.js directly instead
# of truffle.cmd (unless in powershell or git bash).
# The cleanest solution is to explicitly call
# truffle.cmd. Reference:
# https://truffleframework.com/docs/truffle/reference/configuration#resolving-naming-conflicts-on-windows
truffle_overwrite_config = kwargs.get("truffle_overwrite_config", False)
if platform.system() == "Windows":
base_cmd = ["truffle.cmd"]
elif kwargs.get("npx_disable", False):
base_cmd = ["truffle"]
else:
base_cmd = ["npx", "truffle"]
if truffle_version:
if truffle_version.startswith("truffle"):
base_cmd = ["npx", truffle_version]
else:
base_cmd = ["npx", f"truffle@{truffle_version}"]
elif os.path.isfile(os.path.join(self._target, "package.json")):
with open(os.path.join(self._target, "package.json"), encoding="utf8") as file_desc:
package = json.load(file_desc)
if "devDependencies" in package:
if "truffle" in package["devDependencies"]:
version = package["devDependencies"]["truffle"]
if version.startswith("^"):
version = version[1:]
truffle_version = "truffle@{}".format(version)
base_cmd = ["npx", truffle_version]
if "dependencies" in package:
if "truffle" in package["dependencies"]:
version = package["dependencies"]["truffle"]
if version.startswith("^"):
version = version[1:]
truffle_version = "truffle@{}".format(version)
base_cmd = ["npx", truffle_version]
if not truffle_ignore_compile:
cmd = base_cmd + ["compile", "--all"]
LOGGER.info(
"'%s' running (use --truffle-version [email protected] to use specific version)",
" ".join(cmd),
)
config_used = None
config_saved = None
if truffle_overwrite_config:
overwritten_version = kwargs.get("truffle_overwrite_version", None)
# If the version is not provided, we try to guess it with the config file
if overwritten_version is None:
version_from_config = _get_version_from_config(self._target)
if version_from_config:
overwritten_version, _ = version_from_config
# Save the config file, and write our temporary config
config_used, config_saved = _save_config(Path(self._target))
if config_used is None:
config_used = Path("truffle-config.js")
_write_config(Path(self._target), config_used, overwritten_version)
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self._target
) as process:
stdout_bytes, stderr_bytes = process.communicate()
stdout, stderr = (
stdout_bytes.decode(),
stderr_bytes.decode(),
) # convert bytestrings to unicode strings
if truffle_overwrite_config:
assert config_used
_reload_config(Path(self._target), config_saved, config_used)
LOGGER.info(stdout)
if stderr:
LOGGER.error(stderr)
if not os.path.isdir(os.path.join(self._target, build_directory)):
if not os.path.isdir(os.path.join(self._target, "node_modules")):
raise InvalidCompilation(
"External dependencies not found, please install them. (npm install)"
)
raise InvalidCompilation(f"`truffle compile` failed. Can you run It? Output:\n{stdout}")
filenames = glob.glob(os.path.join(self._target, build_directory, "*.json"))
optimized = None
version = None
compiler = None
compilation_unit = CompilationUnit(crytic_compile, str(self._target))
for filename_txt in filenames:
with open(filename_txt, encoding="utf8") as file_desc:
target_loaded = json.load(file_desc)
# pylint: disable=too-many-nested-blocks
if optimized is None:
if "metadata" in target_loaded:
metadata = target_loaded["metadata"]
try:
metadata = json.loads(metadata)
if "settings" in metadata:
if "optimizer" in metadata["settings"]:
if "enabled" in metadata["settings"]["optimizer"]:
optimized = metadata["settings"]["optimizer"]["enabled"]
except json.decoder.JSONDecodeError:
pass
userdoc = target_loaded.get("userdoc", {})
devdoc = target_loaded.get("devdoc", {})
natspec = Natspec(userdoc, devdoc)
if not "ast" in target_loaded:
continue
filename = target_loaded["ast"]["absolutePath"]
# Since truffle 5.3.14, the filenames start with "project:"
# See https://github.com/crytic/crytic-compile/issues/199
if filename.startswith("project:"):
filename = "." + filename[len("project:") :]
try:
filename = convert_filename(
filename, _relative_to_short, crytic_compile, working_dir=self._target
)
except InvalidCompilation as i:
txt = str(i)
txt += "\nConsider removing the build/contracts content (rm build/contracts/*)"
# pylint: disable=raise-missing-from
raise InvalidCompilation(txt)
compilation_unit.asts[filename.absolute] = target_loaded["ast"]
crytic_compile.filenames.add(filename)
compilation_unit.filenames.add(filename)
contract_name = target_loaded["contractName"]
compilation_unit.natspec[contract_name] = natspec
compilation_unit.contracts_filenames[contract_name] = filename
compilation_unit.contracts_names.add(contract_name)
compilation_unit.abis[contract_name] = target_loaded["abi"]
compilation_unit.bytecodes_init[contract_name] = target_loaded["bytecode"].replace(
"0x", ""
)
compilation_unit.bytecodes_runtime[contract_name] = target_loaded[
"deployedBytecode"
].replace("0x", "")
compilation_unit.srcmaps_init[contract_name] = target_loaded["sourceMap"].split(";")
compilation_unit.srcmaps_runtime[contract_name] = target_loaded[
"deployedSourceMap"
].split(";")
if compiler is None:
compiler = target_loaded.get("compiler", {}).get("name", None)
if version is None:
version = target_loaded.get("compiler", {}).get("version", None)
if "+" in version:
version = version[0 : version.find("+")]
if version is None or compiler is None:
version_from_config = _get_version_from_config(self._target)
if version_from_config:
version, compiler = version_from_config
else:
version, compiler = _get_version(base_cmd, cwd=self._target)
compilation_unit.compiler_version = CompilerVersion(
compiler=compiler, version=version, optimized=optimized
)
@staticmethod
def is_supported(target: str, **kwargs: str) -> bool:
"""Check if the target is a truffle project
Args:
target (str): path to the target
**kwargs: optional arguments. Used: "truffle_ignore"
Returns:
bool: True if the target is a truffle project
"""
truffle_ignore = kwargs.get("truffle_ignore", False)
if truffle_ignore:
return False
# Avoid conflicts with hardhat
if os.path.isfile(os.path.join(target, "hardhat.config.js")) | os.path.isfile(
os.path.join(target, "hardhat.config.ts")
):
return False
return os.path.isfile(os.path.join(target, "truffle.js")) or os.path.isfile(
os.path.join(target, "truffle-config.js")
)
# pylint: disable=no-self-use
def is_dependency(self, path: str) -> bool:
"""Check if the path is a dependency
Args:
path (str): path to the target
Returns:
bool: True if the target is a dependency
"""
if path in self._cached_dependencies:
return self._cached_dependencies[path]
ret = "node_modules" in Path(path).parts
self._cached_dependencies[path] = ret
return ret
# pylint: disable=no-self-use
def _guessed_tests(self) -> List[str]:
"""Guess the potential unit tests commands
Returns:
List[str]: The guessed unit tests commands
"""
return ["truffle test"]
def _get_version_from_config(target: str) -> Optional[Tuple[str, str]]:
"""Naive check on the truffleconfig file to get the version
Args:
target (str): path to the project directory
Returns:
Optional[Tuple[str, str]]: (compiler version, compiler name)
"""
config = Path(target, "truffle-config.js")
if not config.exists():
config = Path(target, "truffle.js")
if not config.exists():
return None
with open(config) as config_f:
config_buffer = config_f.read()
# The config is a javascript file
# Use a naive regex to match the solc version
match = re.search(r'solc: {[ ]*\n[ ]*version: "([0-9\.]*)', config_buffer)
if match:
if match.groups():
version = match.groups()[0]
return version, "solc-js"
return None
def _get_version(truffle_call: List[str], cwd: str) -> Tuple[str, str]:
"""Get the compiler version
Args:
truffle_call (List[str]): Command to run truffle
cwd (str): Working directory to run truffle
Raises:
InvalidCompilation: If truffle failed, or the solidity version was not found
Returns:
Tuple[str, str]: (compiler version, compiler name)
"""
cmd = truffle_call + ["version"]
try:
with subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd
) as process:
sstdout, _ = process.communicate()
ssstdout = sstdout.decode() # convert bytestrings to unicode strings
if not ssstdout:
raise InvalidCompilation("Truffle failed to run: 'truffle version'")
stdout = ssstdout.split("\n")
for line in stdout:
if "Solidity" in line:
if "native" in line:
return solc.get_version("solc", dict()), "solc-native"
version = re.findall(r"\d+\.\d+\.\d+", line)[0]
compiler = re.findall(r"(solc[a-z\-]*)", line)
if len(compiler) > 0:
return version, compiler[0]
raise InvalidCompilation(f"Solidity version not found {stdout}")
except OSError as error:
# pylint: disable=raise-missing-from
raise InvalidCompilation(f"Truffle failed: {error}")
def _save_config(cwd: Path) -> Tuple[Optional[Path], Optional[Path]]:
"""Save truffle-config.js / truffle.js to a temporary file.
Args:
cwd (Path): Working directory
Returns:
Tuple[Optional[Path], Optional[Path]]: (original_config_name, temporary_file). None if there was no config file
"""
unique_filename = str(uuid.uuid4())
while Path(cwd, unique_filename).exists():
unique_filename = str(uuid.uuid4())
if Path(cwd, "truffle-config.js").exists():
shutil.move(str(Path(cwd, "truffle-config.js")), str(Path(cwd, unique_filename)))
return Path("truffle-config.js"), Path(unique_filename)
if Path(cwd, "truffle.js").exists():
shutil.move(str(Path(cwd, "truffle.js")), str(Path(cwd, unique_filename)))
return Path("truffle.js"), Path(unique_filename)
return None, None
def _reload_config(cwd: Path, original_config: Optional[Path], tmp_config: Path) -> None:
"""Restore the original config
Args:
cwd (Path): Working directory
original_config (Optional[Path]): Original config saved
tmp_config (Path): Temporary config
"""
os.remove(Path(cwd, tmp_config))
if original_config is not None:
shutil.move(str(Path(cwd, original_config)), str(Path(cwd, tmp_config)))
def _write_config(cwd: Path, original_config: Path, version: Optional[str]) -> None:
"""Write the config file
Args:
cwd (Path): Working directory
original_config (Path): Original config saved
version (Optional[str]): Solc version
"""
txt = ""
if version:
txt = f"""
module.exports = {{
compilers: {{
solc: {{
version: "{version}"
}}
}}
}}
"""
with open(Path(cwd, original_config), "w") as f:
f.write(txt)
def _relative_to_short(relative: Path) -> Path:
"""Convert the relative path to its short version
Args:
relative (Path): Path to convert
Returns:
Path: Converted path
"""
short = relative
try:
short = short.relative_to(Path("contracts"))
except ValueError:
try:
short = short.relative_to("node_modules")
except ValueError:
pass
return short