Skip to content

Commit 04ee6c4

Browse files
authored
Merge pull request #145 from FuzzingLabs/feat/scarb-integration
Scarb integration
2 parents eef24b3 + abeaf52 commit 04ee6c4

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Learn more about Thoth internals here: [Demo video](https://www.youtube.com/watc
1818
- **[Sierra files analysis](/sierra/README.md)** : Thoth can analyze **Sierra** files
1919
- **[Sierra files symbolic execution](/doc/symbolic_execution.md)** : Thoth allows **symbolic execution** on sierra files
2020
- **[Symbolic bounded model checker](/doc/symbolic_bounded_model_checker_sierra.md)** : Thoth can be used as a **Symbolic bounded model checker**
21+
- **[Use it with a Scarb project](#use-it-with-a-scarb-project)** : Thoth can be used in a project created with the Scarb toolchain
2122

2223
## Installation
2324

@@ -180,6 +181,38 @@ You can generate inputs for the [Cairo fuzzer](https://github.com/FuzzingLabs/ca
180181
thoth local ./tests/json_files/cairo_0/cairo_test_symbolic_execution_2.json -a fuzzer
181182
```
182183

184+
## Use it with a Scarb project
185+
186+
Add these lines to your `Scarb.toml` :
187+
188+
```yaml
189+
[[target.starknet-contract]]
190+
sierra = true
191+
casm = true
192+
```
193+
194+
Then build the project using Scarb :
195+
196+
```sh
197+
scarb build
198+
```
199+
200+
You can now run Thoth with the `--scarb` flag :
201+
202+
```sh
203+
// Run the disassembler
204+
thoth local --scarb -b
205+
206+
// Run the analyzer
207+
thoth local --scarb -a
208+
209+
// Generate the control-flow graph
210+
thoth local --scarb --cfg
211+
212+
// Generate the callgraph
213+
thoth local --scarb --call
214+
```
215+
183216
# F.A.Q
184217

185218
## How to find a Cairo/Starknet compilation artifact (json file)?

thoth/app/arguments.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def parse_args() -> argparse.Namespace:
1515
thoth_examples = """
1616
Examples:
1717
18-
Disassemble the contract's compilation artifact from a JSON file:
18+
Disassemble the contract's compilation artifact from a JSON file:
1919
thoth local tests/json_files/cairo_array_sum.json -b
2020
2121
Disassemble the contract's compilation artifact from Starknet:
@@ -215,7 +215,10 @@ def parse_args() -> argparse.Namespace:
215215

216216
# Use a JSON file
217217
file = contract_subparser.add_parser("local", parents=[root_parser])
218-
file.add_argument("path", type=argparse.FileType("r"), help="Cairo compiled JSON file")
218+
file.add_argument("--scarb", action="store_true", help="Use the scarb build output")
219+
file.add_argument(
220+
"path", type=argparse.FileType("r"), help="Cairo compiled JSON file", nargs="?"
221+
)
219222

220223
# Download a contract from StarkNet mainnet/goerli
221224
contract = contract_subparser.add_parser("remote", parents=[root_parser])
@@ -234,4 +237,12 @@ def parse_args() -> argparse.Namespace:
234237
help="Network of the contract, mainnet or goerli",
235238
)
236239

237-
return parser.parse_args()
240+
args = parser.parse_args()
241+
242+
# Ensure that --path is not required when --scarb is provided
243+
if args.contract == "local" and args.scarb and args.path is None:
244+
args.path = None
245+
elif args.contract == "local" and not args.scarb and args.path is None:
246+
parser.error("The --path argument is required when --scarb is not provided")
247+
248+
return args

thoth/thoth.py

+31-14
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,38 @@ def main() -> int:
3434
analyzer._print_help()
3535
return 0
3636

37-
# Load compiled contract from a file
38-
if args.contract == "local":
39-
file = args.path.name
40-
filename = os.path.basename(args.path.name).split(".")[0]
41-
# Load compiled contract from starknet API
37+
# Handle the --scarb flag
38+
if args.scarb:
39+
target_dir = "./target/dev"
40+
file_pattern = "compiled_contract_class.json"
41+
file = None
42+
for root, _, files in os.walk(target_dir):
43+
for f in files:
44+
if f.endswith(file_pattern):
45+
file = os.path.join(root, f)
46+
break
47+
if file:
48+
break
49+
if not file:
50+
print('You need to run "scarb build" before using the --scarb flag')
51+
return 1
52+
filename = os.path.basename(file).split(".")[0]
4253
else:
43-
try:
44-
contract = StarkNet(args.network).get_full_contract(args.address)
45-
except Exception as e:
46-
print(e)
47-
exit()
48-
file = tempfile.NamedTemporaryFile().name
49-
with open(file, "w") as f:
50-
f.write(contract)
51-
filename = args.address
54+
# Load compiled contract from a file
55+
if args.contract == "local":
56+
file = args.path.name
57+
filename = os.path.basename(args.path.name).split(".")[0]
58+
# Load compiled contract from starknet API
59+
else:
60+
try:
61+
contract = StarkNet(args.network).get_full_contract(args.address)
62+
except Exception as e:
63+
print(e)
64+
exit()
65+
file = tempfile.NamedTemporaryFile().name
66+
with open(file, "w") as f:
67+
f.write(contract)
68+
filename = args.address
5269

5370
disassembler = Disassembler(file, color=args.color)
5471

thoth/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.11.0"
1+
__version__ = "0.12.0"

0 commit comments

Comments
 (0)