Skip to content

Commit

Permalink
Add function to extract anchor data from certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Oct 25, 2024
1 parent fcde6bc commit a243e63
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
68 changes: 68 additions & 0 deletions cardano-api/internal/Cardano/Api/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
Expand Down Expand Up @@ -56,6 +57,9 @@ module Cardano.Api.Certificate
, Ledger.MIRPot (..)
, selectStakeCredentialWitness

-- * Anchor data
, getAnchorDataFromCertificate

-- * Internal conversion functions
, toShelleyCertificate
, fromShelleyCertificate
Expand Down Expand Up @@ -90,9 +94,12 @@ import Cardano.Api.StakePoolMetadata
import Cardano.Api.Utils (noInlineMaybeToStrictMaybe)
import Cardano.Api.Value

import Cardano.Ledger.BaseTypes (strictMaybe)
import qualified Cardano.Ledger.Coin as L
import qualified Cardano.Ledger.Keys as Ledger

import Control.Exception (Exception)
import Control.Monad.Except (MonadError (..))
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.IP (IPv4, IPv6)
Expand All @@ -101,6 +108,7 @@ import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Typeable
import GHC.Exception.Type (Exception (..))
import GHC.Exts (IsList (..))
import Network.Socket (PortNumber)

Expand Down Expand Up @@ -724,3 +732,63 @@ fromShelleyPoolParams
fromShelleyDnsName =
Text.encodeUtf8
. Ledger.dnsToText

data AnchorDataFromCertificateException
= InvalidPoolMetadataHash Ledger.Url ByteString
deriving (Eq, Show)

instance Exception AnchorDataFromCertificateException where
displayException :: AnchorDataFromCertificateException -> String
displayException (InvalidPoolMetadataHash url hash) =
"Invalid pool metadata hash for URL " <> show url <> ": " <> show hash

-- | Get anchor data hash from a certificate
getAnchorDataFromCertificate
:: MonadError AnchorDataFromCertificateException m
=> Certificate era
-> m (Maybe (Ledger.Anchor StandardCrypto))
getAnchorDataFromCertificate =
\case
ShelleyRelatedCertificate _ shelleyCert ->
case shelleyCert of
Ledger.ShelleyTxCertDelegCert shelleyDelegCert ->
case shelleyDelegCert of
Ledger.ShelleyRegCert _ -> return Nothing
Ledger.ShelleyUnRegCert _ -> return Nothing
Ledger.ShelleyDelegCert _ _ -> return Nothing
Ledger.ShelleyTxCertPool shelleyPoolCert ->
case shelleyPoolCert of
Ledger.RegPool poolParams -> strictMaybe (return Nothing) anchorDataFromPoolMetadata $ Ledger.ppMetadata poolParams
Ledger.RetirePool _ _ -> return Nothing
Ledger.ShelleyTxCertGenesisDeleg _ -> return Nothing
Ledger.ShelleyTxCertMir _ -> return Nothing
ConwayCertificate ceo conwayCert ->
conwayEraOnwardsConstraints ceo $
case conwayCert of
Ledger.ConwayTxCertDeleg _ -> return Nothing
Ledger.ConwayTxCertPool conwayPoolCert ->
case conwayPoolCert of
Ledger.RegPool poolParams -> strictMaybe (return Nothing) anchorDataFromPoolMetadata $ Ledger.ppMetadata poolParams
Ledger.RetirePool _ _ -> return Nothing
Ledger.ConwayTxCertGov govCert ->
case govCert of
Ledger.ConwayRegDRep _ _ mAnchor -> return $ Ledger.strictMaybeToMaybe mAnchor
Ledger.ConwayUnRegDRep _ _ -> return Nothing
Ledger.ConwayUpdateDRep _ mAnchor -> return $ Ledger.strictMaybeToMaybe mAnchor
Ledger.ConwayAuthCommitteeHotKey _ _ -> return Nothing
Ledger.ConwayResignCommitteeColdKey _ mAnchor -> return $ Ledger.strictMaybeToMaybe mAnchor
where
anchorDataFromPoolMetadata
:: MonadError AnchorDataFromCertificateException m
=> Ledger.PoolMetadata
-> m (Maybe (Ledger.Anchor StandardCrypto))
anchorDataFromPoolMetadata (Ledger.PoolMetadata{Ledger.pmUrl = url, Ledger.pmHash = hashBytes}) = do
hash <-
maybe (throwError $ InvalidPoolMetadataHash url hashBytes) return $ Ledger.hashFromBytes hashBytes
return $
Just
( Ledger.Anchor
{ Ledger.anchorUrl = url
, Ledger.anchorDataHash = Ledger.unsafeMakeSafeHash hash
}
)
3 changes: 3 additions & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,9 @@ module Cardano.Api
, StakePoolRelay
, StakePoolMetadataReference

-- ** Anchor data
, getAnchorDataFromCertificate

-- * Rewards
, DelegationsAndRewards (..)
, mergeDelegsAndRewards
Expand Down

0 comments on commit a243e63

Please sign in to comment.