
स्मार्ट कॉन्ट्रैक्ट सुरक्षा ऑडिट के लिए Solidity स्निपेट्स और Foundry स्क्रिप्ट्स का संग्रह
evm-audit-helpers Solidity स्निपेट्स, Foundry स्क्रिप्ट्स और टेस्ट टेम्पलेट्स का एक चुनिंदा संग्रह है, जिसे EVM स्मार्ट‑कॉन्ट्रैक्ट सुरक्षा ऑडिट को तेज़ करने के लिए डिज़ाइन किया गया है। यह रिपॉजिटरी सामान्य ऑडिट चिंताओं जैसे कि रीएंट्रेंसी, स्टोरेज लेआउट बेमेल और गैस‑ऑप्टिमाइज़ेशन कमजोरियों के लिए पुन: प्रयोज्य बिल्डिंग ब्लॉक्स बंडल करता है, जिससे ऑडिटर और डेवलपर्स बॉयलर‑प्लेट के बजाय लॉजिक पर ध्यान केंद्रित कर सकते हैं।
मुख्य लक्ष्य:
सभी सहायक Solidity ^0.8.20 के लिए लिखे गए हैं और Foundry (forge, cast) के साथ संगत हैं।
# 1️⃣ रिपॉजिटरी क्लोन करें
git clone https://github.com/your-org/evm-audit-helpers.git
cd evm-audit-helpers
# 2️⃣ Foundry इंस्टॉल करें (यदि पहले से इंस्टॉल नहीं है)
curl -L https://foundry.paradigm.xyz | bash
foundryup
# 3️⃣ निर्भरताएँ इंस्टॉल करें
forge install
# 4️⃣ डिफ़ॉल्ट टेस्ट सूट चलाएँ (रीएंट्रेंसी, स्टोरेज और गैस जाँच शामिल हैं)
forge test
टिप: CI‑अनुकूलित कंपाइलर सेटिंग्स का उपयोग करने के लिए अपने शेल में
export FOUNDRY_PROFILE=ciजोड़ें।
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "evm-audit-helpers/src/helpers/Reentrancy.sol";
contract MyVault is ReentrancyGuard {
mapping(address => uint256) private balances;
function deposit() external payable nonReentrant {
balances[msg.sender] += msg.value;
}
function withdraw(uint256 amount) external nonReentrant {
require(balances[msg.sender] >= amount, "Insufficient");
balances[msg.sender] -= amount;
(bool ok,) = msg.sender.call{value: amount}("");
require(ok, "Transfer failed");
}
}
संकलित कॉन्ट्रैक्ट पर रनटाइम डिटेक्टर चलाएँ:
forge script scripts/reentrancy_check.sol:ReentrancyChecker --rpc-url $RPC_URL --broadcast
# संकलित कॉन्ट्रैक्ट के स्टोरेज लेआउट का JSON विवरण उत्पन्न करें
cast abi-storage --contract MyUpgradeable.sol:MyUpgradeable > storage.json
# संदर्भ लेआउट के विरुद्ध तुलना करें (जैसे, पिछले संस्करण से)
forge script scripts/storage_analyzer.sol:StorageComparator \
--sig "compare(string memory, string memory)" storage.json reference.json
स्क्रिप्ट बेमेल स्लॉट्स, संभावित शैडोइंग और @custom:oz-upgrades-unsafe-allow एनोटेशन के लिए सुझाव आउटपुट करेगी।
अपने ऑडिट रिपॉजिटरी में एक टेम्पलेट कॉपी करें:
cp -r test/templates/reentrancy/ my-audit/tests/
ऑडिट के अधीन कॉन्ट्रैक्ट को इम्पोर्ट करने के लिए MyContract.t.sol संपादित करें और चलाएँ:
forge test --match-contract MyContract
अपने रिपॉजिटरी में प्रदान किया गया वर्कफ़्लो जोड़ें:
# .github/workflows/audit.yml
name: EVM Audit
on:
push:
branches: [main]
pull_request:
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Foundry
run: curl -L https://foundry.paradigm.xyz | bash && foundryup
- name: Run Audit Suite
run: |
forge install
forge test -vv
forge script scripts/reentrancy_check.sol:ReentrancyChecker --rpc-url ${{ secrets.RPC_URL }}
forge script scripts/storage_analyzer.sol:StorageComparator --sig "compare(string,string)" storage.json reference.json
वर्कफ़्लो स्वचालित रूप से विफलताओं को फ्लैग करेगा और PR टिप्पणी के रूप में एक markdown रिपोर्ट प्रकाशित करेगा।
हम योगदान का स्वागत करते हैं! कृपया इन चरणों का पालन करें:
git checkout -b feat/<name>).forge coverage).npm run lint – solhint और prettier का उपयोग करता है).pragma solidity ^0.8.20;snake_case का उपयोग करें, सार्वजनिक/बाहरी के लिए camelCase.///) के साथ दस्तावेज़ित करें।# लिंटिंग टूल्स इंस्टॉल करें
npm install
# solhint चलाएँ
npx solhint 'src/**/*.sol' 'test/**/*.sol'
# ऑटो‑फ़ॉर्मेट
npx prettier --write '**/*.sol'
evm-audit-helpers MIT लाइसेंस के अंतर्गत लाइसेंस प्राप्त है। विवरण के लिए LICENSE फ़ाइल देखें।
शुभ ऑडिटिंग!
| श्रेणी | विवरण | प्राथमिक फ़ाइलें |
|---|
| रीएंट्रेंसी चेकर्स | सहायक मॉडिफायर, nonReentrant पैटर्न और रनटाइम डिटेक्शन स्क्रिप्ट्स। | src/helpers/Reentrancy.sol, scripts/reentrancy_check.sol |
| स्टोरेज लेआउट विश्लेषक | स्टोरेज स्लॉट्स की तुलना करने, लेआउट आरेख उत्पन्न करने और असुरक्षित अपग्रेड का पता लगाने के लिए उपयोगिताएँ। | src/helpers/StorageLayout.sol, scripts/storage_analyzer.sol |
| Foundry टेस्ट टेम्पलेट्स | सामान्य कमजोरियों (एक्सेस कंट्रोल, अंकगणित, delegatecall, आदि) को कवर करने वाले बॉयलरप्लेट टेस्ट सूट। | test/templates/* |
| गैस और ऑपकोड ऑडिट | गैस उपयोग को प्रोफाइल करने और महंगे ऑपकोड्स को फ्लैग करने के लिए स्क्रिप्ट्स। | scripts/gas_profiler.sol |
| रिपोर्टिंग सहायक | JSON/Markdown रिपोर्टर जिन्हें CI डैशबोर्ड में पाइप किया जा सकता है। | scripts/report_generator.sol |
| CI एकीकरण | स्वचालित ऑडिट रन के लिए उदाहरण GitHub Actions वर्कफ़्लो। | .github/workflows/ci.yml |