Repository navigation
Expand file tree
/
Copy pathvalidate_contract.py
More file actions
252 lines (203 loc) Β· 9.64 KB
/
Copy pathvalidate_contract.py
File metadata and controls
252 lines (203 loc) Β· 9.64 KB
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
#!/usr/bin/env python3
"""
validate_contract.py
Static validation script for the FinchippayContract Soroban contract.
Checks that all required structs, functions, auth patterns, and tests are
present in `contracts/finchippay-contract/src/lib.rs` without requiring
a Rust toolchain.
Usage:
python3 validate_contract.py
"""
import re
import os
import sys
CONTRACT_PATH = "contracts/finchippay-contract/src/lib.rs"
CARGO_PATH = "Cargo.toml"
CONTRACT_CARGO_PATH = "contracts/finchippay-contract/Cargo.toml"
PASS = "β
"
FAIL = "β"
# βββ Helpers ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def read_file(path: str) -> str | None:
if not os.path.exists(path):
print(f"{FAIL} File not found: {path}")
return None
with open(path, "r") as f:
return f.read()
def check(label: str, pattern: str, content: str, flags: int = 0) -> bool:
if re.search(pattern, content, flags):
print(f" {PASS} {label}")
return True
print(f" {FAIL} {label}")
return False
# βββ Contract validation ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def validate_contract(content: str) -> bool:
ok = True
print("\nπ Contract name and structs")
ok &= check("FinchippayContract struct", r"pub struct FinchippayContract", content)
ok &= check("Stream struct", r"pub struct Stream\s*\{", content)
ok &= check("Escrow struct", r"pub struct Escrow\s*\{", content)
ok &= check("MultiSigProposal struct", r"pub struct MultiSigProposal\s*\{", content)
ok &= check("TipRecord struct", r"pub struct TipRecord\s*\{", content)
ok &= check("ReceiptMetadata struct", r"pub struct ReceiptMetadata\s*\{", content)
print("\nπ Admin functions")
ok &= check("initialize", r"pub fn initialize\s*\(", content)
ok &= check("transfer_admin", r"pub fn transfer_admin\s*\(", content)
ok &= check("get_admin", r"pub fn get_admin\s*\(", content)
print("\nπ Tip functions")
for fn in ["send_tip", "get_tip_total", "get_tip_count", "get_tip_record"]:
ok &= check(fn, rf"pub fn {fn}\s*\(", content)
print("\nπ Receipt functions")
for fn in ["mint_receipt", "get_receipt", "get_receipt_count"]:
ok &= check(fn, rf"pub fn {fn}\s*\(", content)
print("\nπ Escrow functions")
for fn in ["create_escrow", "claim_escrow", "cancel_escrow", "get_escrow", "get_escrow_count"]:
ok &= check(fn, rf"pub fn {fn}\s*\(", content)
print("\nπ Streaming payment functions")
for fn in ["open_stream", "claim_stream", "top_up_stream", "close_stream", "get_stream", "get_claimable"]:
ok &= check(fn, rf"pub fn {fn}\s*\(", content)
print("\nπ Multi-sig functions")
for fn in ["create_multisig", "approve_multisig", "cancel_multisig", "get_multisig"]:
ok &= check(fn, rf"pub fn {fn}\s*\(", content)
print("\nπ Batch send")
ok &= check("batch_send", r"pub fn batch_send\s*\(", content)
print("\nπ Security: require_auth calls")
for label, pattern in [
("payer.require_auth()", r"payer\.require_auth\(\)"),
("from.require_auth()", r"from\.require_auth\(\)"),
("recipient.require_auth()", r"recipient\.require_auth\(\)"),
("signer.require_auth()", r"signer\.require_auth\(\)"),
("proposer.require_auth()", r"proposer\.require_auth\(\)"),
]:
ok &= check(label, pattern, content)
print("\nπ Security: checked arithmetic")
for label, pattern in [
("checked_add", r"checked_add\("),
("checked_sub", r"checked_sub\("),
("checked_mul", r"checked_mul\("),
]:
ok &= check(label, pattern, content)
print("\nπ Streaming maths")
ok &= check("saturating_sub for elapsed", r"saturating_sub\(stream\.start_ledger\)", content)
ok &= check("min() cap on deposited", r"\.min\(stream\.deposited\)", content)
print("\nπ Error enum")
ok &= check("ContractError enum", r"pub enum ContractError\s*\{", content)
ok &= check("AlreadyInitialized", r"AlreadyInitialized\s*=\s*1", content)
print("\nπ Test suite")
tests = [
"test_initialize_sets_admin",
"test_double_initialize_returns_error",
"test_send_tip_stores_record_and_totals",
"test_mint_receipt_and_retrieve",
"test_escrow_full_lifecycle",
"test_cancel_escrow_refunds_payer",
"test_stream_claim_correct_at_various_ledgers",
"test_stream_claimable_capped_at_deposit",
"test_stream_topup_increases_claimable_ceiling",
"test_stream_close_refunds_payer_and_pays_recipient",
"test_multisig_executes_on_threshold",
"test_multisig_cancel_refunds_proposer",
"test_multisig_double_approve_panics",
]
for test in tests:
ok &= check(test, rf"fn {test}\s*\(", content)
return ok
# βββ Cargo.toml validation ββββββββββββββββββββββββββββββββββββββββββββββββββββ
def validate_cargo(workspace: str, contract: str) -> bool:
ok = True
print("\nπ Workspace Cargo.toml")
ok &= check("workspace table", r"\[workspace\]", workspace)
ok &= check("finchippay-contract in members", r"finchippay-contract", workspace)
print("\nπ Contract Cargo.toml")
ok &= check("package name = finchippay-contract", r'name\s*=\s*"finchippay-contract"', contract)
ok &= check("soroban-sdk dependency", r"soroban-sdk", contract)
ok &= check("cdylib crate type", r"cdylib", contract)
return ok
# βββ Main βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def validate_fuzz() -> bool:
"""Validate that fuzz targets and configuration exist."""
ok = True
print("\nπ Fuzz testing infrastructure")
fuzz_dir = "contracts/finchippay-contract/fuzz"
fuzz_cargo = os.path.join(fuzz_dir, "Cargo.toml")
fuzz_targets_dir = os.path.join(fuzz_dir, "fuzz_targets")
if os.path.exists(fuzz_cargo):
print(f" {PASS} {fuzz_cargo} exists")
fuzz_cargo_content = read_file(fuzz_cargo)
if fuzz_cargo_content:
ok &= check("cargo-fuzz metadata", r"cargo-fuzz\s*=\s*true", fuzz_cargo_content)
ok &= check("libfuzzer-sys dependency", r"libfuzzer-sys", fuzz_cargo_content)
ok &= check("testutils feature", r"testutils", fuzz_cargo_content)
else:
ok = False
print(f" {FAIL} {fuzz_cargo} not found")
targets = [
"fuzz_target_1_parse_payment.rs",
"fuzz_target_2_escrow.rs",
"fuzz_target_3_multisig.rs",
"fuzz_target_4_batch_send.rs",
]
for target in targets:
path = os.path.join(fuzz_targets_dir, target)
if os.path.exists(path):
content = read_file(path)
if content:
ok &= check(f"{target} β uses libfuzzer_sys", r"libfuzzer_sys", content)
ok &= check(f"{target} β uses finchippay_contract", r"finchippay_contract", content)
else:
ok = False
print(f" {FAIL} {path} not found")
# Validate fuzz script
script_path = "scripts/fuzz-contract.sh"
if os.path.exists(script_path):
script_content = read_file(script_path)
if script_content:
if os.access(script_path, os.X_OK):
print(f" {PASS} fuzz-contract.sh β executable")
else:
ok = False
print(f" {FAIL} fuzz-contract.sh is not executable")
ok &= check("fuzz-contract.sh β contains fuzz_target names", r"fuzz_target_1_parse_payment", script_content)
else:
ok = False
print(f" {FAIL} {script_path} not found")
# Validate FUZZING.md
fuzzing_doc = "FUZZING.md"
if os.path.exists(fuzzing_doc):
doc_content = read_file(fuzzing_doc)
if doc_content:
ok &= check("FUZZING.md β contains usage instructions", r"cargo-fuzz", doc_content)
else:
ok = False
print(f" {FAIL} {fuzzing_doc} not found")
return ok
def main() -> int:
print("π Finchippay-Solution β Static Contract Validator")
print("=" * 52)
contract = read_file(CONTRACT_PATH)
workspace_cargo = read_file(CARGO_PATH)
contract_cargo = read_file(CONTRACT_CARGO_PATH)
if not all([contract, workspace_cargo, contract_cargo]):
print(f"\n{FAIL} One or more required files not found. Aborting.")
return 1
contract_ok = validate_contract(contract)
cargo_ok = validate_cargo(workspace_cargo, contract_cargo)
fuzz_ok = validate_fuzz()
print("\n" + "=" * 52)
if contract_ok and cargo_ok and fuzz_ok:
print("π All validation checks passed!")
print("\nThe FinchippayContract is structurally complete with:")
for feat in [
"Tips, receipts, escrow, streaming, multi-sig, batch-send",
"require_auth() on every mutating entry-point",
"Checked arithmetic (no silent overflows)",
"Comprehensive test suite (13+ tests)",
"Coverage-guided fuzz testing (4 targets)",
]:
print(f" {PASS} {feat}")
print("\nπ Ready to build: cargo build --target wasm32-unknown-unknown --release")
return 0
else:
print(f"{FAIL} Validation failed β see issues above.")
return 1
if __name__ == "__main__":
sys.exit(main())