|
| 1 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 2 | + |
| 3 | +module Cardano.CLI.EraBased.HashChecking |
| 4 | + ( checkCertificateHashes |
| 5 | + , checkVotingProcedureHashes |
| 6 | + , checkProposalHashes |
| 7 | + ) |
| 8 | +where |
| 9 | + |
| 10 | +import Cardano.Api (Certificate (..), ExceptT, firstExceptT) |
| 11 | +import qualified Cardano.Api.Ledger as L |
| 12 | +import qualified Cardano.Api.Shelley as Shelley |
| 13 | + |
| 14 | +import Cardano.CLI.Run.Hash (carryHashChecks) |
| 15 | +import Cardano.CLI.Types.Common (MustCheckHash (..), PotentiallyCheckedAnchor (..)) |
| 16 | +import Cardano.CLI.Types.Errors.TxCmdError (TxCmdError (..)) |
| 17 | +import qualified Cardano.Ledger.Api.Governance as L |
| 18 | + |
| 19 | +import Control.Monad (forM_) |
| 20 | +import Control.Monad.Trans.Except.Extra (left) |
| 21 | + |
| 22 | +-- | Check the hash of the anchor data against the hash in the anchor |
| 23 | +checkAnchorMetadataHash :: L.Anchor L.StandardCrypto -> ExceptT TxCmdError IO () |
| 24 | +checkAnchorMetadataHash anchor = |
| 25 | + firstExceptT (TxCmdHashCheckError $ L.anchorUrl anchor) $ |
| 26 | + carryHashChecks |
| 27 | + ( PotentiallyCheckedAnchor |
| 28 | + { pcaMustCheck = CheckHash |
| 29 | + , pcaAnchor = anchor |
| 30 | + } |
| 31 | + ) |
| 32 | + |
| 33 | +-- | Find references to anchor data and check the hashes are valid |
| 34 | +-- and they match the linked data. |
| 35 | +checkCertificateHashes :: Certificate era -> ExceptT TxCmdError IO () |
| 36 | +checkCertificateHashes c = |
| 37 | + case c of |
| 38 | + ShelleyRelatedCertificate _ shelleyCert -> |
| 39 | + case shelleyCert of |
| 40 | + L.ShelleyTxCertDelegCert shelleyDelegCert -> |
| 41 | + case shelleyDelegCert of |
| 42 | + L.ShelleyRegCert _ -> return () |
| 43 | + L.ShelleyUnRegCert _ -> return () |
| 44 | + L.ShelleyDelegCert _ _ -> return () |
| 45 | + L.ShelleyTxCertPool shelleyPoolCert -> |
| 46 | + case shelleyPoolCert of |
| 47 | + L.RegPool poolParams -> forM_ (L.ppMetadata poolParams) checkPoolMetadataHash |
| 48 | + L.RetirePool _ _ -> return () |
| 49 | + L.ShelleyTxCertGenesisDeleg _ -> return () |
| 50 | + L.ShelleyTxCertMir _ -> return () |
| 51 | + ConwayCertificate ceo conwayCert -> |
| 52 | + Shelley.conwayEraOnwardsConstraints ceo $ |
| 53 | + case conwayCert of |
| 54 | + L.ConwayTxCertDeleg _ -> return () |
| 55 | + L.ConwayTxCertPool conwayPoolCert -> |
| 56 | + case conwayPoolCert of |
| 57 | + L.RegPool poolParams -> forM_ (L.ppMetadata poolParams) checkPoolMetadataHash |
| 58 | + L.RetirePool _ _ -> return () |
| 59 | + L.ConwayTxCertGov govCert -> |
| 60 | + case govCert of |
| 61 | + L.ConwayRegDRep _ _ mAnchor -> forM_ mAnchor checkAnchorMetadataHash |
| 62 | + L.ConwayUnRegDRep _ _ -> return () |
| 63 | + L.ConwayUpdateDRep _ mAnchor -> forM_ mAnchor checkAnchorMetadataHash |
| 64 | + L.ConwayAuthCommitteeHotKey _ _ -> return () |
| 65 | + L.ConwayResignCommitteeColdKey _ mAnchor -> forM_ mAnchor checkAnchorMetadataHash |
| 66 | + where |
| 67 | + checkPoolMetadataHash :: L.PoolMetadata -> ExceptT TxCmdError IO () |
| 68 | + checkPoolMetadataHash (L.PoolMetadata{L.pmUrl = url, L.pmHash = hashBytes}) = do |
| 69 | + let mHash = L.hashFromBytes hashBytes |
| 70 | + hash <- maybe (left $ TxCmdPoolMetadataHashError url) return mHash |
| 71 | + let safeHash = L.unsafeMakeSafeHash hash |
| 72 | + checkAnchorMetadataHash |
| 73 | + ( L.Anchor |
| 74 | + { L.anchorUrl = url |
| 75 | + , L.anchorDataHash = safeHash |
| 76 | + } |
| 77 | + ) |
| 78 | + |
| 79 | +-- | Find references to anchor data in voting procedures and check the hashes are valid |
| 80 | +-- and they match the linked data. |
| 81 | +checkVotingProcedureHashes |
| 82 | + :: Shelley.ShelleyBasedEra era -> Shelley.VotingProcedures era -> ExceptT TxCmdError IO () |
| 83 | +checkVotingProcedureHashes eon (Shelley.VotingProcedures (L.VotingProcedures voterMap)) = |
| 84 | + Shelley.shelleyBasedEraConstraints eon $ |
| 85 | + forM_ |
| 86 | + voterMap |
| 87 | + ( \vpMap -> |
| 88 | + forM_ |
| 89 | + vpMap |
| 90 | + ( \(L.VotingProcedure _ mAnchor) -> |
| 91 | + forM_ mAnchor checkAnchorMetadataHash |
| 92 | + ) |
| 93 | + ) |
| 94 | + |
| 95 | +-- | Find references to anchor data in proposals and check the hashes are valid |
| 96 | +-- and they match the linked data. |
| 97 | +checkProposalHashes |
| 98 | + :: forall era. Shelley.ShelleyBasedEra era -> Shelley.Proposal era -> ExceptT TxCmdError IO () |
| 99 | +checkProposalHashes |
| 100 | + eon |
| 101 | + ( Shelley.Proposal |
| 102 | + ( L.ProposalProcedure |
| 103 | + { L.pProcGovAction = govAction |
| 104 | + , L.pProcAnchor = anchor |
| 105 | + } |
| 106 | + ) |
| 107 | + ) = |
| 108 | + Shelley.shelleyBasedEraConstraints eon $ do |
| 109 | + checkAnchorMetadataHash anchor |
| 110 | + checkGovActionHashes govAction |
| 111 | + where |
| 112 | + checkGovActionHashes |
| 113 | + :: L.GovAction (Shelley.ShelleyLedgerEra era) -> ExceptT TxCmdError IO () |
| 114 | + checkGovActionHashes govAction' = |
| 115 | + Shelley.shelleyBasedEraConstraints eon $ |
| 116 | + case govAction' of |
| 117 | + L.ParameterChange{} -> return () |
| 118 | + L.HardForkInitiation _ _ -> return () |
| 119 | + L.TreasuryWithdrawals _ _ -> return () |
| 120 | + L.NoConfidence _ -> return () |
| 121 | + L.UpdateCommittee{} -> return () |
| 122 | + L.NewConstitution _ constitution -> checkAnchorMetadataHash $ L.constitutionAnchor constitution |
| 123 | + L.InfoAction -> return () |
0 commit comments