11{-# LANGUAGE CPP #-}
2+ {-# LANGUAGE OverloadedStrings #-}
23
34{- |
45Copyright: (c) 2020 Kowainik
@@ -50,6 +51,7 @@ module Stan.Inspection.AntiPattern
5051 , plustan01
5152 , plustan02
5253 , plustan03
54+ , plustan04
5355 -- * All inspections
5456 , antiPatternInspectionsMap
5557 ) where
@@ -60,7 +62,7 @@ import Relude.Extra.Tuple (fmapToFst)
6062import Stan.Core.Id (Id (.. ))
6163import Stan.Inspection (Inspection (.. ), InspectionAnalysis (.. ), InspectionsMap , categoryL ,
6264 descriptionL , severityL , solutionL )
63- import Stan.NameMeta (NameMeta (.. ), baseNameFrom , mkBaseFoldableMeta , mkBaseOldListMeta ,
65+ import Stan.NameMeta (ghcPrimNameFrom , NameMeta (.. ), baseNameFrom , mkBaseFoldableMeta , mkBaseOldListMeta ,
6466 primTypeMeta , textNameFrom , unorderedNameFrom , _nameFrom , plutusTxNameFrom )
6567import Stan.Pattern.Ast (Literal (.. ), PatternAst (.. ), anyNamesToPatternAst , app ,
6668 namesToPatternAst , opApp , range )
@@ -69,6 +71,8 @@ import Stan.Pattern.Type (PatternType, charPattern, foldableMethodsPatterns, fol
6971 listPattern , stringPattern , textPattern , (|->) , (|::) )
7072import Stan.Severity (Severity (.. ))
7173
74+ import Stan.Core.ModuleName
75+
7276import qualified Data.List.NonEmpty as NE
7377import qualified Stan.Category as Category
7478
@@ -95,6 +99,7 @@ antiPatternInspectionsMap = fromList $ fmapToFst inspectionId
9599 , plustan01
96100 , plustan02
97101 , plustan03
102+ , plustan04
98103 ]
99104
100105-- | Smart constructor to create anti-pattern 'Inspection'.
@@ -452,3 +457,40 @@ plustan03 = mkAntiPatternInspection (Id "PLU-STAN-03") "No usage of Optional typ
452457 where
453458 useOfFromMaybe :: NameMeta
454459 useOfFromMaybe = " fromMaybe" `plutusTxNameFrom` " PlutusTx.Maybe"
460+
461+ plustan04 :: Inspection
462+ plustan04 = mkAntiPatternInspection (Id " PLU-STAN-04" ) " Usage of eq instance of ScriptHash/PublicKeyHash/Credential"
463+ (FindAst pat)
464+ & descriptionL .~ " Usage of eq instance of script-hash / pubkeyhash / payment credential "
465+ & solutionL .~
466+ [ " Potential staking value theft might want to prefer eq comparison of address" ]
467+ & severityL .~ Warning
468+ where
469+
470+ opNames :: [NameMeta ]
471+ opNames = map opName [" <" , " <=" , " ==" , " >" , " >=" ]
472+
473+ pat = foldl' (\ acc x -> acc ||| PatternAstName x fun)
474+ (PatternAstNeg PatternAstAnything ) opNames
475+
476+ fun :: PatternType
477+ fun = (publicKeyHashPattern ||| scriptHashPattern ||| credentialPattern)
478+ |-> (?) |-> (?)
479+
480+ opName :: Text -> NameMeta
481+ opName = (`ghcPrimNameFrom` " GHC.Classes" )
482+
483+ publicKeyHashPattern :: PatternType
484+ publicKeyHashPattern = ledgerApiTypePattern " PubKeyHash" " Crypto"
485+
486+ scriptHashPattern :: PatternType
487+ scriptHashPattern = ledgerApiTypePattern " ScriptHash" " Scripts"
488+
489+ credentialPattern :: PatternType
490+ credentialPattern = ledgerApiTypePattern " Credential" " Credential"
491+
492+ ledgerApiTypePattern name moduleSuffix = NameMeta
493+ { nameMetaName = name
494+ , nameMetaModuleName = ModuleName $ " PlutusLedgerApi.V1." <> moduleSuffix
495+ , nameMetaPackage = " plutus-ledger-api"
496+ } |:: []
0 commit comments