Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
0xalpharush committed Nov 6, 2023
1 parent 1f834c0 commit 85faa6d
Show file tree
Hide file tree
Showing 9 changed files with 831 additions and 0 deletions.
Empty file added slither/detectors/_cli.py
Empty file.
Empty file added slither/printers/_cli.py
Empty file.
Empty file added slither/tools/flat/__init__.py
Empty file.
53 changes: 53 additions & 0 deletions slither/tools/flat/__main__.py
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()

Check failure on line 21 in slither/tools/flat/__main__.py

View workflow job for this annotation

GitHub Actions / Lint Code Base

E0602: Undefined variable 'parse_args' (undefined-variable)

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()
83 changes: 83 additions & 0 deletions slither/tools/flat/_cli.py
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.
57 changes: 57 additions & 0 deletions slither/tools/flat/export/export.py
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)
Loading

0 comments on commit 85faa6d

Please sign in to comment.