33{-# LANGUAGE DerivingStrategies #-}
44{-# LANGUAGE FlexibleContexts #-}
55{-# LANGUAGE GADTs #-}
6+ {-# LANGUAGE InstanceSigs #-}
67{-# LANGUAGE LambdaCase #-}
78{-# LANGUAGE NamedFieldPuns #-}
89{-# LANGUAGE ScopedTypeVariables #-}
@@ -56,6 +57,10 @@ module Cardano.Api.Certificate
5657 , Ledger. MIRPot (.. )
5758 , selectStakeCredentialWitness
5859
60+ -- * Anchor data
61+ , AnchorDataFromCertificateException (.. )
62+ , getAnchorDataFromCertificate
63+
5964 -- * Internal conversion functions
6065 , toShelleyCertificate
6166 , fromShelleyCertificate
@@ -90,9 +95,12 @@ import Cardano.Api.StakePoolMetadata
9095import Cardano.Api.Utils (noInlineMaybeToStrictMaybe )
9196import Cardano.Api.Value
9297
98+ import Cardano.Ledger.BaseTypes (strictMaybe )
9399import qualified Cardano.Ledger.Coin as L
94100import qualified Cardano.Ledger.Keys as Ledger
95101
102+ import Control.Exception (Exception )
103+ import Control.Monad.Except (MonadError (.. ))
96104import Data.ByteString (ByteString )
97105import qualified Data.ByteString as BS
98106import Data.IP (IPv4 , IPv6 )
@@ -101,6 +109,7 @@ import Data.Text (Text)
101109import qualified Data.Text as Text
102110import qualified Data.Text.Encoding as Text
103111import Data.Typeable
112+ import GHC.Exception.Type (Exception (.. ))
104113import GHC.Exts (IsList (.. ))
105114import Network.Socket (PortNumber )
106115
@@ -724,3 +733,63 @@ fromShelleyPoolParams
724733 fromShelleyDnsName =
725734 Text. encodeUtf8
726735 . Ledger. dnsToText
736+
737+ data AnchorDataFromCertificateException
738+ = InvalidPoolMetadataHash Ledger. Url ByteString
739+ deriving (Eq , Show )
740+
741+ instance Exception AnchorDataFromCertificateException where
742+ displayException :: AnchorDataFromCertificateException -> String
743+ displayException (InvalidPoolMetadataHash url hash) =
744+ " Invalid pool metadata hash for URL " <> show url <> " : " <> show hash
745+
746+ -- | Get anchor data hash from a certificate
747+ getAnchorDataFromCertificate
748+ :: MonadError AnchorDataFromCertificateException m
749+ => Certificate era
750+ -> m (Maybe (Ledger. Anchor StandardCrypto ))
751+ getAnchorDataFromCertificate =
752+ \ case
753+ ShelleyRelatedCertificate _ shelleyCert ->
754+ case shelleyCert of
755+ Ledger. ShelleyTxCertDelegCert shelleyDelegCert ->
756+ case shelleyDelegCert of
757+ Ledger. ShelleyRegCert _ -> return Nothing
758+ Ledger. ShelleyUnRegCert _ -> return Nothing
759+ Ledger. ShelleyDelegCert _ _ -> return Nothing
760+ Ledger. ShelleyTxCertPool shelleyPoolCert ->
761+ case shelleyPoolCert of
762+ Ledger. RegPool poolParams -> strictMaybe (return Nothing ) anchorDataFromPoolMetadata $ Ledger. ppMetadata poolParams
763+ Ledger. RetirePool _ _ -> return Nothing
764+ Ledger. ShelleyTxCertGenesisDeleg _ -> return Nothing
765+ Ledger. ShelleyTxCertMir _ -> return Nothing
766+ ConwayCertificate ceo conwayCert ->
767+ conwayEraOnwardsConstraints ceo $
768+ case conwayCert of
769+ Ledger. ConwayTxCertDeleg _ -> return Nothing
770+ Ledger. ConwayTxCertPool conwayPoolCert ->
771+ case conwayPoolCert of
772+ Ledger. RegPool poolParams -> strictMaybe (return Nothing ) anchorDataFromPoolMetadata $ Ledger. ppMetadata poolParams
773+ Ledger. RetirePool _ _ -> return Nothing
774+ Ledger. ConwayTxCertGov govCert ->
775+ case govCert of
776+ Ledger. ConwayRegDRep _ _ mAnchor -> return $ Ledger. strictMaybeToMaybe mAnchor
777+ Ledger. ConwayUnRegDRep _ _ -> return Nothing
778+ Ledger. ConwayUpdateDRep _ mAnchor -> return $ Ledger. strictMaybeToMaybe mAnchor
779+ Ledger. ConwayAuthCommitteeHotKey _ _ -> return Nothing
780+ Ledger. ConwayResignCommitteeColdKey _ mAnchor -> return $ Ledger. strictMaybeToMaybe mAnchor
781+ where
782+ anchorDataFromPoolMetadata
783+ :: MonadError AnchorDataFromCertificateException m
784+ => Ledger. PoolMetadata
785+ -> m (Maybe (Ledger. Anchor StandardCrypto ))
786+ anchorDataFromPoolMetadata (Ledger. PoolMetadata {Ledger. pmUrl = url, Ledger. pmHash = hashBytes}) = do
787+ hash <-
788+ maybe (throwError $ InvalidPoolMetadataHash url hashBytes) return $ Ledger. hashFromBytes hashBytes
789+ return $
790+ Just
791+ ( Ledger. Anchor
792+ { Ledger. anchorUrl = url
793+ , Ledger. anchorDataHash = Ledger. unsafeMakeSafeHash hash
794+ }
795+ )
0 commit comments