-
Notifications
You must be signed in to change notification settings - Fork 981
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f834c0
commit 85faa6d
Showing
9 changed files
with
831 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import argparse | ||
import logging | ||
import sys | ||
|
||
from crytic_compile import cryticparser | ||
|
||
from slither import Slither | ||
from slither.tools.flat.flattening import ( | ||
Flattening, | ||
Strategy, | ||
STRATEGIES_NAMES, | ||
DEFAULT_EXPORT_PATH, | ||
) | ||
|
||
logging.basicConfig() | ||
logger = logging.getLogger("Slither") | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
def main() -> None: | ||
args = parse_args() | ||
|
||
slither = Slither(args.filename, **vars(args)) | ||
|
||
for compilation_unit in slither.compilation_units: | ||
|
||
flat = Flattening( | ||
compilation_unit, | ||
external_to_public=args.convert_external, | ||
remove_assert=args.remove_assert, | ||
convert_library_to_internal=args.convert_library_to_internal, | ||
private_to_internal=args.convert_private, | ||
export_path=args.dir, | ||
pragma_solidity=args.pragma_solidity, | ||
) | ||
|
||
try: | ||
strategy = Strategy[args.strategy] | ||
except KeyError: | ||
to_log = f"{args.strategy} is not a valid strategy, use: {STRATEGIES_NAMES} (default MostDerived)" | ||
logger.error(to_log) | ||
return | ||
flat.export( | ||
strategy=strategy, | ||
target=args.contract, | ||
json=args.json, | ||
zip=args.zip, | ||
zip_type=args.zip_type, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from argparse import ArgumentParser | ||
from crytic_compile.utils.zip import ZIP_TYPES_ACCEPTED | ||
|
||
from slither.tools.flat.flattening import Strategy, STRATEGIES_NAMES, DEFAULT_EXPORT_PATH | ||
|
||
|
||
def init_parser(sub_parser: ArgumentParser) -> None: | ||
"""Parse the underlying arguments for the program. | ||
Returns: | ||
The arguments for the program. | ||
""" | ||
parser = sub_parser.add_parser( | ||
name="flat", | ||
help="Contracts flattening. See https://github.com/crytic/slither/wiki/Contract-Flattening", | ||
) | ||
|
||
# parser.add_argument("filename", help="The filename of the contract or project to analyze.") # TODO remove? | ||
|
||
parser.add_argument("--contract", help="Flatten one contract.", default=None) | ||
|
||
parser.add_argument( | ||
"--strategy", | ||
help=f"Flatenning strategy: {STRATEGIES_NAMES} (default: MostDerived).", | ||
default=Strategy.MostDerived.name, # pylint: disable=no-member | ||
) | ||
|
||
group_export = parser.add_argument_group("Export options") | ||
|
||
group_export.add_argument( | ||
"--dir", | ||
help=f"Export directory (default: {DEFAULT_EXPORT_PATH}).", | ||
default=None, | ||
) | ||
|
||
group_export.add_argument( | ||
"--json", | ||
help='Export the results as a JSON file ("--json -" to export to stdout)', | ||
action="store", | ||
default=None, | ||
) | ||
|
||
parser.add_argument( | ||
"--zip", | ||
help="Export all the files to a zip file", | ||
action="store", | ||
default=None, | ||
) | ||
|
||
parser.add_argument( | ||
"--zip-type", | ||
help=f"Zip compression type. One of {','.join(ZIP_TYPES_ACCEPTED.keys())}. Default lzma", | ||
action="store", | ||
default=None, | ||
) | ||
|
||
group_patching = parser.add_argument_group("Patching options") | ||
|
||
group_patching.add_argument( | ||
"--convert-external", help="Convert external to public.", action="store_true" | ||
) | ||
|
||
group_patching.add_argument( | ||
"--convert-private", | ||
help="Convert private variables to internal.", | ||
action="store_true", | ||
) | ||
|
||
group_patching.add_argument( | ||
"--convert-library-to-internal", | ||
help="Convert external or public functions to internal in library.", | ||
action="store_true", | ||
) | ||
|
||
group_patching.add_argument( | ||
"--remove-assert", help="Remove call to assert().", action="store_true" | ||
) | ||
|
||
group_patching.add_argument( | ||
"--pragma-solidity", | ||
help="Set the solidity pragma with a given version.", | ||
action="store", | ||
default=None, | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import json | ||
import logging | ||
|
||
# https://docs.python.org/3/library/zipfile.html#zipfile-objects | ||
import zipfile | ||
from collections import namedtuple | ||
from typing import List | ||
|
||
ZIP_TYPES_ACCEPTED = { | ||
"lzma": zipfile.ZIP_LZMA, | ||
"stored": zipfile.ZIP_STORED, | ||
"deflated": zipfile.ZIP_DEFLATED, | ||
"bzip2": zipfile.ZIP_BZIP2, | ||
} | ||
|
||
Export = namedtuple("Export", ["filename", "content"]) | ||
|
||
logger = logging.getLogger("Slither-flat") | ||
|
||
|
||
def save_to_zip(files: List[Export], zip_filename: str, zip_type: str = "lzma"): | ||
""" | ||
Save projects to a zip | ||
""" | ||
logger.info(f"Export {zip_filename}") | ||
with zipfile.ZipFile( | ||
zip_filename, | ||
"w", | ||
compression=ZIP_TYPES_ACCEPTED.get(zip_type, zipfile.ZIP_LZMA), | ||
) as file_desc: | ||
for f in files: | ||
file_desc.writestr(str(f.filename), f.content) | ||
|
||
|
||
def save_to_disk(files: List[Export]): | ||
""" | ||
Save projects to a zip | ||
""" | ||
for file in files: | ||
with open(file.filename, "w", encoding="utf8") as f: | ||
logger.info(f"Export {file.filename}") | ||
f.write(file.content) | ||
|
||
|
||
def export_as_json(files: List[Export], filename: str): | ||
""" | ||
Save projects to a zip | ||
""" | ||
|
||
files_as_dict = {str(f.filename): f.content for f in files} | ||
|
||
if filename == "-": | ||
print(json.dumps(files_as_dict)) | ||
else: | ||
logger.info(f"Export {filename}") | ||
with open(filename, "w", encoding="utf8") as f: | ||
json.dump(files_as_dict, f) |
Oops, something went wrong.