Skip to content

Medium QoL changes #825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cardano-api/gen/Test/Hedgehog/Roundtrip/Bech32.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Hedgehog qualified as H

roundtrip_Bech32
:: (SerialiseAsBech32 a, Eq a, Show a)
=> AsType a -> Gen a -> Property
roundtrip_Bech32 typeProxy gen =
=> Gen a -> Property
roundtrip_Bech32 gen =
H.property $ do
val <- H.forAll gen
H.tripping val serialiseToBech32 (deserialiseFromBech32 typeProxy)
H.tripping val serialiseToBech32 deserialiseFromBech32
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ module Cardano.Api
-- * Type tags
, HasTypeProxy (..)
, AsType (..)
, asType

-- * Cryptographic key interface
-- $keys
Expand Down
35 changes: 19 additions & 16 deletions cardano-api/src/Cardano/Api/Internal/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ instance SerialiseAddress (Address ShelleyAddr) where

deserialiseAddress (AsAddress AsShelleyAddr) t =
either (const Nothing) Just $
deserialiseFromBech32 (AsAddress AsShelleyAddr) t
deserialiseFromBech32 t

instance ToJSON (Address ShelleyAddr) where
toJSON = Aeson.String . serialiseAddress
Expand Down Expand Up @@ -384,10 +384,10 @@ instance IsShelleyBasedEra era => FromJSON (AddressInEra era) where
addressAny <- runParsecParser parseAddressAny txt
pure $ anyAddressInShelleyBasedEra sbe addressAny

parseAddressAny :: Parsec.Parser AddressAny
parseAddressAny :: SerialiseAddress addr => Parsec.Parser addr
parseAddressAny = do
str <- lexPlausibleAddressString
case deserialiseAddress AsAddressAny str of
case deserialiseAddress asType str of
Nothing -> fail $ "invalid address: " <> Text.unpack str
Just addr -> pure addr

Expand Down Expand Up @@ -478,7 +478,8 @@ shelleyAddressInEra
-> Address ShelleyAddr
-> AddressInEra era
shelleyAddressInEra sbe =
AddressInEra (ShelleyAddressInEra sbe)
shelleyBasedEraConstraints sbe $
AddressInEra (ShelleyAddressInEra sbe)

anyAddressInShelleyBasedEra
:: ()
Expand All @@ -495,12 +496,12 @@ anyAddressInEra
-> Either String (AddressInEra era)
anyAddressInEra era = \case
AddressByron addr ->
Right (AddressInEra ByronAddressInAnyEra addr)
AddressShelley addr ->
forEraInEon
era
(Left "Expected Byron based era address")
(\sbe -> Right (AddressInEra (ShelleyAddressInEra sbe) addr))
pure $ AddressInEra ByronAddressInAnyEra addr
AddressShelley addr -> do
sbe <- forEraMaybeEon era ?! "Expected Byron based era address"
shelleyBasedEraConstraints sbe $
pure $
AddressInEra (ShelleyAddressInEra sbe) addr

toAddressAny :: Address addr -> AddressAny
toAddressAny a@ShelleyAddress{} = AddressShelley a
Expand Down Expand Up @@ -589,7 +590,7 @@ instance SerialiseAddress StakeAddress where

deserialiseAddress AsStakeAddress t =
either (const Nothing) Just $
deserialiseFromBech32 AsStakeAddress t
deserialiseFromBech32 t

instance ToJSON StakeAddress where
toJSON s = Aeson.String $ serialiseAddress s
Expand Down Expand Up @@ -685,18 +686,20 @@ fromShelleyAddrIsSbe sbe = \case
Shelley.AddrBootstrap (Shelley.BootstrapAddress addr) ->
AddressInEra ByronAddressInAnyEra (ByronAddress addr)
Shelley.Addr nw pc scr ->
AddressInEra (ShelleyAddressInEra sbe) (ShelleyAddress nw pc scr)
shelleyBasedEraConstraints sbe $
AddressInEra (ShelleyAddressInEra sbe) (ShelleyAddress nw pc scr)

fromShelleyAddr
:: ShelleyBasedEra era
-> Shelley.Addr
-> AddressInEra era
fromShelleyAddr _ (Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)) =
AddressInEra ByronAddressInAnyEra (ByronAddress addr)
fromShelleyAddr sBasedEra (Shelley.Addr nw pc scr) =
AddressInEra
(ShelleyAddressInEra sBasedEra)
(ShelleyAddress nw pc scr)
fromShelleyAddr sbe (Shelley.Addr nw pc scr) =
shelleyBasedEraConstraints sbe $
AddressInEra
(ShelleyAddressInEra sbe)
(ShelleyAddress nw pc scr)

fromShelleyStakeAddr :: Shelley.RewardAccount -> StakeAddress
fromShelleyStakeAddr (Shelley.RewardAccount nw sc) = StakeAddress nw sc
Expand Down
14 changes: 8 additions & 6 deletions cardano-api/src/Cardano/Api/Internal/CIP/Cip129.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Api.Internal.CIP.Cip129
( Cip129 (..)
Expand Down Expand Up @@ -98,15 +98,17 @@ serialiseToBech32Cip129 a =
humanReadablePart = cip129Bech32PrefixFor (proxyToAsType (Proxy :: Proxy a))

deserialiseFromBech32Cip129
:: Cip129 a
=> AsType a -> Text -> Either Bech32DecodeError a
deserialiseFromBech32Cip129 asType bech32Str = do
:: forall a
. Cip129 a
=> Text
-> Either Bech32DecodeError a
deserialiseFromBech32Cip129 bech32Str = do
(prefix, dataPart) <-
Bech32.decodeLenient bech32Str
?!. Bech32DecodingError

let actualPrefix = Bech32.humanReadablePartToText prefix
permittedPrefixes = cip129Bech32PrefixesPermitted asType
permittedPrefixes = cip129Bech32PrefixesPermitted (asType @a)
guard (actualPrefix `elem` permittedPrefixes)
?! Bech32UnexpectedPrefix actualPrefix (fromList permittedPrefixes)

Expand All @@ -128,7 +130,7 @@ deserialiseFromBech32Cip129 asType bech32Str = do
guard (header == expectedHeader)
?! Bech32UnexpectedHeader (toBase16Text expectedHeader) (toBase16Text header)

let expectedPrefix = Bech32.humanReadablePartToText $ cip129Bech32PrefixFor asType
let expectedPrefix = Bech32.humanReadablePartToText $ cip129Bech32PrefixFor (asType @a)
guard (actualPrefix == expectedPrefix)
?! Bech32WrongPrefix actualPrefix expectedPrefix

Expand Down
12 changes: 6 additions & 6 deletions cardano-api/src/Cardano/Api/Internal/DeserialiseAnyOf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ where

import Cardano.Api.Internal.Address
import Cardano.Api.Internal.Error
import Cardano.Api.Internal.HasTypeProxy
import Cardano.Api.Internal.Keys.Byron
import Cardano.Api.Internal.Keys.Class
import Cardano.Api.Internal.Keys.Praos
Expand Down Expand Up @@ -108,11 +109,10 @@ data DeserialiseInputResult a
-- | Deserialise an input of some type that is formatted in some way.
deserialiseInput
:: forall a
. AsType a
-> NonEmpty (InputFormat a)
. NonEmpty (InputFormat a)
-> ByteString
-> Either InputDecodeError a
deserialiseInput asType acceptedFormats inputBs =
deserialiseInput acceptedFormats inputBs =
go (toList acceptedFormats)
where
inputText :: Text
Expand All @@ -135,7 +135,7 @@ deserialiseInput asType acceptedFormats inputBs =
deserialiseTextEnvelope = do
let textEnvRes :: Either TextEnvelopeError a
textEnvRes =
deserialiseFromTextEnvelope asType
deserialiseFromTextEnvelope
=<< first TextEnvelopeAesonDecodeError (Aeson.eitherDecodeStrict' inputBs)
case textEnvRes of
Right res -> DeserialiseInputSuccess res
Expand All @@ -148,7 +148,7 @@ deserialiseInput asType acceptedFormats inputBs =

deserialiseBech32 :: SerialiseAsBech32 a => DeserialiseInputResult a
deserialiseBech32 =
case deserialiseFromBech32 asType inputText of
case deserialiseFromBech32 inputText of
Right res -> DeserialiseInputSuccess res
-- The input was not valid Bech32.
Left (Bech32DecodingError _) -> DeserialiseInputErrorFormatMismatch
Expand All @@ -158,7 +158,7 @@ deserialiseInput asType acceptedFormats inputBs =
deserialiseHex :: SerialiseAsRawBytes a => DeserialiseInputResult a
deserialiseHex
| isValidHex inputBs =
case deserialiseFromRawBytesHex asType inputBs of
case deserialiseFromRawBytesHex inputBs of
Left _ -> DeserialiseInputError InputInvalidError
Right x -> DeserialiseInputSuccess x
| otherwise = DeserialiseInputErrorFormatMismatch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data AllegraEraOnwards era where
Expand Down Expand Up @@ -101,6 +102,7 @@ type AllegraEraOnwardsConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

allegraEraOnwardsConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data AlonzoEraOnwards era where
Expand Down Expand Up @@ -115,6 +116,7 @@ type AlonzoEraOnwardsConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

alonzoEraOnwardsConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data BabbageEraOnwards era where
Expand Down Expand Up @@ -119,6 +120,7 @@ type BabbageEraOnwardsConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

babbageEraOnwardsConstraints
Expand Down
4 changes: 4 additions & 0 deletions cardano-api/src/Cardano/Api/Internal/Eon/Convert.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
Expand All @@ -14,3 +15,6 @@ import Data.Kind (Type)
-- relationship between types.
class Convert (f :: a -> Type) (g :: a -> Type) where
convert :: forall era. f era -> g era

instance Convert a a where
convert = id
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data ConwayEraOnwards era where
Expand Down Expand Up @@ -122,6 +123,7 @@ type ConwayEraOnwardsConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

conwayEraOnwardsConstraints
Expand Down
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api/Internal/Eon/MaryEraOnwards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data MaryEraOnwards era where
Expand Down Expand Up @@ -103,6 +104,7 @@ type MaryEraOnwardsConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

maryEraOnwardsConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus
import Control.DeepSeq
import Data.Aeson (FromJSON (..), ToJSON, toJSON, withText)
import Data.Text qualified as Text
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
import Data.Type.Equality (TestEquality (..), (:~:) (Refl), type (==))
import Data.Typeable (Typeable)
import Text.Pretty (Pretty (..))

Expand Down Expand Up @@ -230,6 +230,7 @@ type ShelleyBasedEraConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (L.PredicateFailure (L.EraRule "LEDGER" (ShelleyLedgerEra era)))
, Typeable era
, (era == ByronEra) ~ False
)

shelleyBasedEraConstraints
Expand Down
2 changes: 2 additions & 0 deletions cardano-api/src/Cardano/Api/Internal/Eon/ShelleyEraOnly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data ShelleyEraOnly era where
Expand Down Expand Up @@ -97,6 +98,7 @@ type ShelleyEraOnlyConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

shelleyEraOnlyConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data ShelleyToAllegraEra era where
Expand Down Expand Up @@ -100,6 +101,7 @@ type ShelleyToAllegraEraConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

shelleyToAllegraEraConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data ShelleyToAlonzoEra era where
Expand Down Expand Up @@ -101,6 +102,7 @@ type ShelleyToAlonzoEraConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

shelleyToAlonzoEraConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data ShelleyToBabbageEra era where
Expand Down Expand Up @@ -105,6 +106,7 @@ type ShelleyToBabbageEraConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

shelleyToBabbageEraConstraints
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Ouroboros.Consensus.Protocol.Praos.Common qualified as Consensus
import Ouroboros.Consensus.Shelley.Ledger qualified as Consensus

import Data.Aeson
import Data.Type.Equality
import Data.Typeable (Typeable)

data ShelleyToMaryEra era where
Expand Down Expand Up @@ -100,6 +101,7 @@ type ShelleyToMaryEraConstraints era =
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
, (era == ByronEra) ~ False
)

shelleyToMaryEraConstraints
Expand Down
Loading
Loading