|
| 1 | +{-# LANGUAGE DataKinds #-} |
| 2 | +{-# LANGUAGE ExistentialQuantification #-} |
| 3 | +{-# LANGUAGE FlexibleContexts #-} |
| 4 | +{-# LANGUAGE GADTs #-} |
| 5 | +{-# LANGUAGE RankNTypes #-} |
| 6 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 7 | + |
| 8 | +module Cardano.Wasm.Api.Certificate.StakeCertificate |
| 9 | + ( makeStakeAddressStakeDelegationCertificateImpl |
| 10 | + , makeStakeAddressStakeDelegationCertificateExperimentalEraImpl |
| 11 | + , makeStakeAddressRegistrationCertificateImpl |
| 12 | + , makeStakeAddressRegistrationCertificateExperimentalEraImpl |
| 13 | + , makeStakeAddressUnregistrationCertificateImpl |
| 14 | + , makeStakeAddressUnregistrationCertificateExperimentalEraImpl |
| 15 | + ) |
| 16 | +where |
| 17 | + |
| 18 | +import Cardano.Api |
| 19 | + ( Coin (..) |
| 20 | + , Hash |
| 21 | + , PoolId |
| 22 | + , StakeKey |
| 23 | + , serialiseToCBOR |
| 24 | + , unStakePoolKeyHash |
| 25 | + ) |
| 26 | +import Cardano.Api.Address (StakeCredential (..)) |
| 27 | +import Cardano.Api.Experimental (Era (..), obtainCommonConstraints) |
| 28 | +import Cardano.Api.Experimental qualified as Exp |
| 29 | +import Cardano.Api.Experimental.Certificate (Certificate (..)) |
| 30 | +import Cardano.Api.Serialise.Raw qualified as Api |
| 31 | + |
| 32 | +import Cardano.Ledger.Api (Delegatee (DelegStake)) |
| 33 | +import Cardano.Wasm.ExceptionHandling (rightOrError) |
| 34 | + |
| 35 | +import Control.Monad.Catch (MonadThrow) |
| 36 | +import Data.ByteString.Base16 qualified as Base16 |
| 37 | +import Data.Text qualified as Text |
| 38 | +import Data.Text.Encoding qualified as Text |
| 39 | + |
| 40 | +-- | Make a certificate that delegates a stake address to a stake pool in Conway era. |
| 41 | +makeStakeAddressStakeDelegationCertificateImpl :: MonadThrow m => String -> String -> m String |
| 42 | +makeStakeAddressStakeDelegationCertificateImpl skHashStr poolIdStr = do |
| 43 | + stakeCertHash <- readHash skHashStr |
| 44 | + poolId <- readPoolId poolIdStr |
| 45 | + makeStakeAddressStakeDelegationCertificate Exp.ConwayEra stakeCertHash poolId |
| 46 | + |
| 47 | +-- | Make a certificate that delegates a stake address to a stake pool in the current experimental era. |
| 48 | +makeStakeAddressStakeDelegationCertificateExperimentalEraImpl |
| 49 | + :: MonadThrow m => String -> String -> m String |
| 50 | +makeStakeAddressStakeDelegationCertificateExperimentalEraImpl skHashStr poolIdStr = do |
| 51 | + stakeCertHash <- readHash skHashStr |
| 52 | + poolId <- readPoolId poolIdStr |
| 53 | + makeStakeAddressStakeDelegationCertificate Exp.DijkstraEra stakeCertHash poolId |
| 54 | + |
| 55 | +makeStakeAddressStakeDelegationCertificate |
| 56 | + :: forall era m. MonadThrow m => Exp.Era era -> Hash StakeKey -> PoolId -> m String |
| 57 | +makeStakeAddressStakeDelegationCertificate era stakeCertHash poolId = |
| 58 | + obtainCommonConstraints era $ do |
| 59 | + let cert :: Certificate (Exp.LedgerEra era) = |
| 60 | + Exp.makeStakeAddressDelegationCertificate |
| 61 | + (StakeCredentialByKey stakeCertHash) |
| 62 | + ( case era of |
| 63 | + ConwayEra -> DelegStake $ unStakePoolKeyHash poolId |
| 64 | + DijkstraEra -> DelegStake $ unStakePoolKeyHash poolId |
| 65 | + ) |
| 66 | + return $ serialiseCertificateToCBOR era cert |
| 67 | + |
| 68 | +-- | Make a stake address registration certificate in Conway era. |
| 69 | +makeStakeAddressRegistrationCertificateImpl :: MonadThrow m => String -> Integer -> m String |
| 70 | +makeStakeAddressRegistrationCertificateImpl skHashStr deposit = do |
| 71 | + skHash <- readHash skHashStr |
| 72 | + makeStakeAddressRegistrationCertificateWrapper Exp.ConwayEra skHash deposit |
| 73 | + |
| 74 | +-- | Make a stake address registration certificate in the current experimental era. |
| 75 | +makeStakeAddressRegistrationCertificateExperimentalEraImpl |
| 76 | + :: MonadThrow m => String -> Integer -> m String |
| 77 | +makeStakeAddressRegistrationCertificateExperimentalEraImpl skHashStr deposit = do |
| 78 | + skHash <- readHash skHashStr |
| 79 | + makeStakeAddressRegistrationCertificateWrapper Exp.DijkstraEra skHash deposit |
| 80 | + |
| 81 | +makeStakeAddressRegistrationCertificateWrapper |
| 82 | + :: forall era m. MonadThrow m => Era era -> Hash StakeKey -> Integer -> m String |
| 83 | +makeStakeAddressRegistrationCertificateWrapper era skHash deposit = |
| 84 | + obtainCommonConstraints era $ do |
| 85 | + let cert :: Certificate (Exp.LedgerEra era) = |
| 86 | + Exp.makeStakeAddressRegistrationCertificate |
| 87 | + (StakeCredentialByKey skHash) |
| 88 | + (Coin deposit) |
| 89 | + return $ serialiseCertificateToCBOR era cert |
| 90 | + |
| 91 | +-- | Make a stake address unregistration certificate in Conway era. |
| 92 | +makeStakeAddressUnregistrationCertificateImpl :: MonadThrow m => String -> Integer -> m String |
| 93 | +makeStakeAddressUnregistrationCertificateImpl skHashStr deposit = do |
| 94 | + skHash <- readHash skHashStr |
| 95 | + makeStakeAddressUnregistrationCertificateWrapper Exp.ConwayEra skHash deposit |
| 96 | + |
| 97 | +-- | Make a stake address unregistration certificate in the current experimental era. |
| 98 | +makeStakeAddressUnregistrationCertificateExperimentalEraImpl |
| 99 | + :: MonadThrow m => String -> Integer -> m String |
| 100 | +makeStakeAddressUnregistrationCertificateExperimentalEraImpl skHashStr deposit = do |
| 101 | + skHash <- readHash skHashStr |
| 102 | + makeStakeAddressUnregistrationCertificateWrapper Exp.DijkstraEra skHash deposit |
| 103 | + |
| 104 | +makeStakeAddressUnregistrationCertificateWrapper |
| 105 | + :: forall era m. MonadThrow m => Era era -> Hash StakeKey -> Integer -> m String |
| 106 | +makeStakeAddressUnregistrationCertificateWrapper era skHash deposit = |
| 107 | + obtainCommonConstraints era $ do |
| 108 | + let cert :: Certificate (Exp.LedgerEra era) = |
| 109 | + Exp.makeStakeAddressUnregistrationCertificate |
| 110 | + (StakeCredentialByKey skHash) |
| 111 | + (Coin deposit) |
| 112 | + return $ serialiseCertificateToCBOR era cert |
| 113 | + |
| 114 | +serialiseCertificateToCBOR :: Exp.Era era -> Certificate (Exp.LedgerEra era) -> String |
| 115 | +serialiseCertificateToCBOR era cert = |
| 116 | + obtainCommonConstraints era $ do |
| 117 | + Text.unpack $ |
| 118 | + Text.decodeUtf8 $ |
| 119 | + Base16.encode $ |
| 120 | + serialiseToCBOR |
| 121 | + cert |
| 122 | + |
| 123 | +readHash :: MonadThrow m => String -> m (Hash StakeKey) |
| 124 | +readHash = rightOrError . Api.deserialiseFromRawBytesHex . Text.encodeUtf8 . Text.pack |
| 125 | + |
| 126 | +readPoolId :: MonadThrow m => String -> m PoolId |
| 127 | +readPoolId = rightOrError . Api.deserialiseFromRawBytesHex . Text.encodeUtf8 . Text.pack |
0 commit comments