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 a1b5e89
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cardano-api/internal/Cardano/Api/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ module Cardano.Api.Certificate
, Ledger.MIRPot (..)
, selectStakeCredentialWitness

-- * Anchor data
, getAnchorDataFromCertificate

-- * Internal conversion functions
, toShelleyCertificate
, fromShelleyCertificate
Expand Down Expand Up @@ -90,6 +93,7 @@ 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

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

-- | Get anchor data hash from a certificate
getAnchorDataFromCertificate :: Certificate era -> Maybe (Ledger.Anchor StandardCrypto)
getAnchorDataFromCertificate =
\case
ShelleyRelatedCertificate _ shelleyCert ->
case shelleyCert of
Ledger.ShelleyTxCertDelegCert shelleyDelegCert ->
case shelleyDelegCert of
Ledger.ShelleyRegCert _ -> Nothing
Ledger.ShelleyUnRegCert _ -> Nothing
Ledger.ShelleyDelegCert _ _ -> Nothing
Ledger.ShelleyTxCertPool shelleyPoolCert ->
case shelleyPoolCert of
Ledger.RegPool poolParams -> strictMaybe Nothing anchorDataFromPoolMetadata $ Ledger.ppMetadata poolParams
Ledger.RetirePool _ _ -> Nothing
Ledger.ShelleyTxCertGenesisDeleg _ -> Nothing
Ledger.ShelleyTxCertMir _ -> Nothing
ConwayCertificate ceo conwayCert ->
conwayEraOnwardsConstraints ceo $
case conwayCert of
Ledger.ConwayTxCertDeleg _ -> Nothing
Ledger.ConwayTxCertPool conwayPoolCert ->
case conwayPoolCert of
Ledger.RegPool poolParams -> strictMaybe Nothing anchorDataFromPoolMetadata $ Ledger.ppMetadata poolParams
Ledger.RetirePool _ _ -> Nothing
Ledger.ConwayTxCertGov govCert ->
case govCert of
Ledger.ConwayRegDRep _ _ mAnchor -> Ledger.strictMaybeToMaybe mAnchor
Ledger.ConwayUnRegDRep _ _ -> Nothing
Ledger.ConwayUpdateDRep _ mAnchor -> Ledger.strictMaybeToMaybe mAnchor
Ledger.ConwayAuthCommitteeHotKey _ _ -> Nothing
Ledger.ConwayResignCommitteeColdKey _ mAnchor -> Ledger.strictMaybeToMaybe mAnchor
where
anchorDataFromPoolMetadata :: Ledger.PoolMetadata -> Maybe (Ledger.Anchor StandardCrypto)
anchorDataFromPoolMetadata (Ledger.PoolMetadata{Ledger.pmUrl = url, Ledger.pmHash = hashBytes}) = do
hash <- Ledger.hashFromBytes hashBytes
return
( 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 a1b5e89

Please sign in to comment.