[? ]
那麼您有一組智能合約,並且想要查找具有名為withdrawEth
的public
方法的所有合約,但詞法 grep 會產生大量誤報?這就是 solgrep 可以提供幫助的地方! ?
? npm install -g solgrep
Solgrep 遞歸地在目標目錄中尋找智慧合約,解析來源單元以理解語言語義,並使這些資訊可供強大的基於 javascript 的過濾功能使用。這樣,您可以:
AST_EXACT
和AST_FUZZY
匹配)使用此工具的最常見方法可能是使用--find=<js-filter-statement>
選項來執行它,其中js-filter-statement
是一個 javascript 單行程式碼,它告訴引擎您感興趣的內容。 boolean
的語句(“查找模式”)或傳回要提取的資訊(“提取模式”)。
--find=<js-filter-statement>
的可信輸入,因為此參數被評估為 javascript!
--find
、 --rule
時檢查參數的正確順序。請參閱用法。
運行預設規則並顯示一些統計資料?
⇒ solgrep < folder >
執行特定或多個內建規則( solgrep -l
列出可用規則)?
⇒ solgrep < folder > -- rule = IsInitializable -- rule = Stats
您想尋找具有名為withdrawEth
的函數的合約的所有來源單元嗎?
⇒ solgrep < folder > -- find = "function.name=='withdrawEth'"
做同樣的事情但不區分大小寫?
⇒ solgrep < folder > -- find = "function.name.toLowerCase()=='withdraweth'"
找所有呼叫selfdestruct
的函式?
⇒ solgrep < folder > -- find = "function.callsTo('selfdestruct')"
從所有合約中提取所有函數名稱?
⇒ solgrep < folder > -- find = "function.name"
取得所有external
函數的列表?
⇒ solgrep < folder > -- find = "function.visibility.includes('external')"
找ERC777
合約?
⇒ solgrep < folder > -- find = "contract.name=='ERC777'"
提取所有合約名稱?
⇒ solgrep < folder > -- find = "contract.name"
提取所有介面名稱?
⇒ solgrep < folder > -- find = "contract.name && contract.kind=='interface'"
與AST
中的某些內容相符?
⇒ solgrep < folder > -- find = "Object.keys(function.modifiers).includes('nonReentrant')"
詞法匹配函數原始碼?
⇒ solgrep < folder > -- find = "function.getSource().includes('hi')"
找到重複的合約? (AST精確匹配和模糊匹配)
⇒ solgrep < folder > -- rule DupeFinder
使用選項--output=<output.json>
將所有結果寫入檔案。
--find
的內建關鍵字sourceUnit
物件與引擎目前正在處理的來源檔案相符。contract
對象與引擎目前正在處理的合約相符。function
物件與引擎目前正在處理的函數相符。 可以使用以下方法:
<sourceUnit|contract|function>.getSource()
- 提供對單位原始碼的訪問<sourceUnit|contract|function>.ast
- 提供對 Solidity-parser AST 的直接訪問function.callsTo('withdrawEth')
- 尋找所有合約中呼叫名為withdrawEth
的方法的所有函數特殊合約函數可以參考:
function.name == '__fallback__'
function.name == '__receiveEther__'
function.name == '__constructor__'
Stats
- 收集有關已處理的文件、合約、唯一名稱等數量的統計信息GenericGrep
- 是--find
功能背後的引擎分享即關懷- 提交您的規則!
⇒ solgrep -- help
Usage: solgrep < folder | ... > [ options ]
Options:
- r , -- rule Enable rules [ array ] [ default : [ ] ]
- l , -- list - rules List available rules [ boolean ] [ default : false ]
- f , -- find Find / Extract information using custom pattern
[ array ] [ default : [ ] ]
- o , -- output Write "results" as JSON to output file path . [ string ]
- h , -- help Show help [ boolean ]
- v , -- version Show version number [ boolean ]
--find
、 --rule
)時,請確保使用下列格式:
⇒ solgrep <folder|...> [options]
或者
⇒ solgrep [options] -- <folder|...>
否則附加選項可能會被解釋為附加--find
選項!
const solgrep = require ( 'solgrep' ) ;
let sg = new SolGrep ( '::memory::' , rules , callbacks ) ;
sg . analyzeDir ( "/path/to/smart/contracts" ) . then ( ( ) => {
console . log ( " ──────────────────────────── Results" )
console . log ( sg . results )
console . log ( " ────────────────────────────" )
sgrep . close ( ) ;
} )
下面的範例說明如何從所有sourceUnits
和contracts
中提取所有函數名稱。
⇒ solgrep . . / d0 -- find = 'function.name'
? SolGrep v0 .0 .3 ready !
Enabled Modules :
✔️ GenericGrep _function . name
[ ████████████████████████████████████████ ] 100 % | ? ETA : 0 s | 5 / 5 | ? 389 | 0 | . . / d0
────────────────────────────
{
'../d0/D0788Af7a613b81F437a51b96594A6387c7329b1_PendleLiquidityMiningBaseV2Multi.sol' : [
{
rule : 'GenericGrep' ,
tag : 'match-function: PendleLiquidityMiningBaseV2Multi.__constructor__' ,
info : '__constructor__' ,
loc : [ Object ]
} ,
{
rule : 'GenericGrep' ,
tag : 'match-function: PendleLiquidityMiningBaseV2Multi.setUpEmergencyMode' ,
info : 'setUpEmergencyMode' ,
loc : [ Object ]
} ,
...
如果您沒有提供設定選項,它將採用預設的內建 grep 規則並使用stats
模組來處理數字。
⇒ solgrep . . / d6
? SolGrep v0 .0 .1 starting . . .
? . . / d6
████████████████████████████████████████ 100 % | ETA : 0 s | 5 / 5
──────────────────────────── Results
{
'../d6/d60a598998ed27a7C54315F2675908B628E434B1_LiquidityPool.sol' : [
{
rule : 'IsInitializable' ,
tag : 'INITIALIZEABLE' ,
info : 'initialize - public initialize function; likely proxy'
} ,
{
rule : 'IsInitializable' ,
tag : 'INITIALIZEABLE' ,
info : 'initialize - public initialize function; likely proxy'
}
] ,
'../d6/d64E77C7C6A1dcC7e302F8fe31A22745e223c39c_MyStrategy.sol' : [
{
rule : 'IsInitializable' ,
tag : 'INITIALIZEABLE' ,
info : 'initialize - public initialize function; likely proxy'
}
] ,
undefined : [ { rule : 'Stats' , tag : 'STATS' , info : [ Object ] } ]
}
────────────────────────────
{
sourceUnits : 4 ,
contracts : {
total : 13 ,
names : {
ERC20 : 1 ,
StrategySushiEthMimLp : 1 ,
LiquidityPool : 1 ,
Getter : 1 ,
Governance : 1 ,
LibraryEvents : 1 ,
Perpetual : 1 ,
Storage : 1 ,
ArbiBoneman : 1 ,
ERC721 : 1 ,
MyStrategy : 1 ,
PausableUpgradeable : 1 ,
SettAccessControl : 1
}
} ,
interfaces : {
total : 39 ,
names : {
IERC20 : 1 ,
IJar : 1 ,
IStakingRewards : 1 ,
IStakingRewardsFactory : 1 ,
IMasterchef : 1 ,
UniswapRouterV2 : 1 ,
IUniswapV2Pair : 1 ,
IUniswapV2Factory : 1 ,
IRewarder : 1 ,
IMiniChefV2 : 1 ,
ILiquidityPool : 1 ,
IOracle : 1 ,
IAccessControl : 1 ,
IGovernor : 1 ,
IPoolCreatorFull : 1 ,
ISymbolService : 1 ,
IPoolCreator : 1 ,
ITracer : 1 ,
IVersionControl : 1 ,
IVariables : 1 ,
IKeeperWhitelist : 1 ,
IProxyAdmin : 1 ,
IDecimals : 1 ,
ILiquidityPoolGetter : 1 ,
ILiquidityPoolGovernance : 1 ,
IPerpetual : 1 ,
IERC721Enumerable : 1 ,
IERC721 : 1 ,
IERC721Receiver : 1 ,
IERC721Metadata : 1 ,
IERC165 : 1 ,
ICurveGauge : 1 ,
ICurveStableSwapREN : 1 ,
IUniswapRouterV2 : 1 ,
IStrategy : 1 ,
IController : 2 ,
IERC20Upgradeable : 2
}
} ,
libraries : {
total : 31 ,
names : {
SafeMath : 1 ,
SafeERC20 : 1 ,
BoringERC20 : 1 ,
EnumerableSetUpgradeable : 1 ,
SafeCastUpgradeable : 1 ,
AMMModule : 1 ,
LiquidityPoolModule : 1 ,
PerpetualModule : 1 ,
SignedSafeMathUpgradeable : 1 ,
Constant : 1 ,
Math : 1 ,
SafeMathExt : 1 ,
Utils : 1 ,
MarginAccountModule : 1 ,
EnumerableSet : 1 ,
OrderData : 1 ,
CollateralModule : 1 ,
TradeModule : 1 ,
OrderModule : 1 ,
Signature : 1 ,
ECDSAUpgradeable : 1 ,
Strings : 1 ,
MathUpgradeable : 1 ,
Address : 2 ,
SafeMathUpgradeable : 2 ,
AddressUpgradeable : 2 ,
SafeERC20Upgradeable : 2
}
} ,
abstract : {
total : 13 ,
names : {
StrategyBase : 1 ,
StrategySushiFarmBase : 1 ,
ReentrancyGuardUpgradeable : 1 ,
ERC721Enumerable : 1 ,
Ownable : 1 ,
ERC165 : 1 ,
BaseStrategy : 1 ,
Context : 2 ,
ContextUpgradeable : 2 ,
Initializable : 2
}
}
}
TOTAL FILES : 5
ERRORS: 1
────────────────────────────
cheers ?
@ tintinweb
ConsenSys Diligence @ https : //consensys.net/diligence/
https : //github.com/tintinweb/solgrep/