Skip to content

Commit a1b5e89

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

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ module Cardano.Api.Certificate
5656
, Ledger.MIRPot (..)
5757
, selectStakeCredentialWitness
5858

59+
-- * Anchor data
60+
, getAnchorDataFromCertificate
61+
5962
-- * Internal conversion functions
6063
, toShelleyCertificate
6164
, fromShelleyCertificate
@@ -90,6 +93,7 @@ import Cardano.Api.StakePoolMetadata
9093
import Cardano.Api.Utils (noInlineMaybeToStrictMaybe)
9194
import Cardano.Api.Value
9295

96+
import Cardano.Ledger.BaseTypes (strictMaybe)
9397
import qualified Cardano.Ledger.Coin as L
9498
import qualified Cardano.Ledger.Keys as Ledger
9599

@@ -724,3 +728,46 @@ fromShelleyPoolParams
724728
fromShelleyDnsName =
725729
Text.encodeUtf8
726730
. Ledger.dnsToText
731+
732+
-- | Get anchor data hash from a certificate
733+
getAnchorDataFromCertificate :: Certificate era -> Maybe (Ledger.Anchor StandardCrypto)
734+
getAnchorDataFromCertificate =
735+
\case
736+
ShelleyRelatedCertificate _ shelleyCert ->
737+
case shelleyCert of
738+
Ledger.ShelleyTxCertDelegCert shelleyDelegCert ->
739+
case shelleyDelegCert of
740+
Ledger.ShelleyRegCert _ -> Nothing
741+
Ledger.ShelleyUnRegCert _ -> Nothing
742+
Ledger.ShelleyDelegCert _ _ -> Nothing
743+
Ledger.ShelleyTxCertPool shelleyPoolCert ->
744+
case shelleyPoolCert of
745+
Ledger.RegPool poolParams -> strictMaybe Nothing anchorDataFromPoolMetadata $ Ledger.ppMetadata poolParams
746+
Ledger.RetirePool _ _ -> Nothing
747+
Ledger.ShelleyTxCertGenesisDeleg _ -> Nothing
748+
Ledger.ShelleyTxCertMir _ -> Nothing
749+
ConwayCertificate ceo conwayCert ->
750+
conwayEraOnwardsConstraints ceo $
751+
case conwayCert of
752+
Ledger.ConwayTxCertDeleg _ -> Nothing
753+
Ledger.ConwayTxCertPool conwayPoolCert ->
754+
case conwayPoolCert of
755+
Ledger.RegPool poolParams -> strictMaybe Nothing anchorDataFromPoolMetadata $ Ledger.ppMetadata poolParams
756+
Ledger.RetirePool _ _ -> Nothing
757+
Ledger.ConwayTxCertGov govCert ->
758+
case govCert of
759+
Ledger.ConwayRegDRep _ _ mAnchor -> Ledger.strictMaybeToMaybe mAnchor
760+
Ledger.ConwayUnRegDRep _ _ -> Nothing
761+
Ledger.ConwayUpdateDRep _ mAnchor -> Ledger.strictMaybeToMaybe mAnchor
762+
Ledger.ConwayAuthCommitteeHotKey _ _ -> Nothing
763+
Ledger.ConwayResignCommitteeColdKey _ mAnchor -> Ledger.strictMaybeToMaybe mAnchor
764+
where
765+
anchorDataFromPoolMetadata :: Ledger.PoolMetadata -> Maybe (Ledger.Anchor StandardCrypto)
766+
anchorDataFromPoolMetadata (Ledger.PoolMetadata{Ledger.pmUrl = url, Ledger.pmHash = hashBytes}) = do
767+
hash <- Ledger.hashFromBytes hashBytes
768+
return
769+
( Ledger.Anchor
770+
{ Ledger.anchorUrl = url
771+
, Ledger.anchorDataHash = Ledger.unsafeMakeSafeHash hash
772+
}
773+
)

cardano-api/src/Cardano/Api.hs

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

486+
-- ** Anchor data
487+
, getAnchorDataFromCertificate
488+
486489
-- * Rewards
487490
, DelegationsAndRewards (..)
488491
, mergeDelegsAndRewards

0 commit comments

Comments
 (0)