Skip to content

Commit b00053d

Browse files
committed
Add function to extract anchor data from certificate
1 parent fcde6bc commit b00053d

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

cardano-api/internal/Cardano/Api/Certificate.hs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
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
9095
import Cardano.Api.Utils (noInlineMaybeToStrictMaybe)
9196
import Cardano.Api.Value
9297

98+
import Cardano.Ledger.BaseTypes (strictMaybe)
9399
import qualified Cardano.Ledger.Coin as L
94100
import qualified Cardano.Ledger.Keys as Ledger
95101

102+
import Control.Exception (Exception)
103+
import Control.Monad.Except (MonadError (..))
96104
import Data.ByteString (ByteString)
97105
import qualified Data.ByteString as BS
98106
import Data.IP (IPv4, IPv6)
@@ -101,6 +109,7 @@ import Data.Text (Text)
101109
import qualified Data.Text as Text
102110
import qualified Data.Text.Encoding as Text
103111
import Data.Typeable
112+
import GHC.Exception.Type (Exception (..))
104113
import GHC.Exts (IsList (..))
105114
import 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+
)

cardano-api/src/Cardano/Api.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ module Cardano.Api
483483
, StakePoolRelay
484484
, StakePoolMetadataReference
485485

486+
-- ** Anchor data
487+
, AnchorDataFromCertificateException (..)
488+
, getAnchorDataFromCertificate
489+
486490
-- * Rewards
487491
, DelegationsAndRewards (..)
488492
, mergeDelegsAndRewards

0 commit comments

Comments
 (0)