Skip to content

Commit

Permalink
Complete the era renaming in the cardano-api
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoutts committed Nov 19, 2020
1 parent da29342 commit 2208573
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 159 deletions.
26 changes: 13 additions & 13 deletions cardano-api/src/Cardano/Api/Address.hs
Expand Up @@ -105,23 +105,23 @@ data Address era where
:: Shelley.Network
-> Shelley.PaymentCredential StandardShelley
-> Shelley.StakeReference StandardShelley
-> Address Shelley
-> Address ShelleyEra

deriving instance Eq (Address era)
deriving instance Ord (Address era)
deriving instance Show (Address era)


instance HasTypeProxy (Address Byron) where
data AsType (Address Byron) = AsByronAddress
instance HasTypeProxy (Address ByronEra) where
data AsType (Address ByronEra) = AsByronAddress
proxyToAsType _ = AsByronAddress

instance HasTypeProxy (Address Shelley) where
data AsType (Address Shelley) = AsShelleyAddress
instance HasTypeProxy (Address ShelleyEra) where
data AsType (Address ShelleyEra) = AsShelleyAddress
proxyToAsType _ = AsShelleyAddress


instance SerialiseAsRawBytes (Address Byron) where
instance SerialiseAsRawBytes (Address ByronEra) where
serialiseToRawBytes (ByronAddress addr) = CBOR.serialize' addr

deserialiseFromRawBytes AsByronAddress bs =
Expand All @@ -130,7 +130,7 @@ instance SerialiseAsRawBytes (Address Byron) where
Right addr -> Just (ByronAddress addr)


instance SerialiseAsRawBytes (Address Shelley) where
instance SerialiseAsRawBytes (Address ShelleyEra) where
serialiseToRawBytes (ByronAddress addr) =
Shelley.serialiseAddr
. Shelley.AddrBootstrap
Expand All @@ -149,15 +149,15 @@ instance SerialiseAsRawBytes (Address Shelley) where
Just (Shelley.AddrBootstrap (Shelley.BootstrapAddress addr)) ->
Just (ByronAddress addr)

instance SerialiseAsBech32 (Address Shelley) where
instance SerialiseAsBech32 (Address ShelleyEra) where
bech32PrefixFor (ShelleyAddress Shelley.Mainnet _ _) = "addr"
bech32PrefixFor (ShelleyAddress Shelley.Testnet _ _) = "addr_test"
bech32PrefixFor (ByronAddress _) = "addr"

bech32PrefixesPermitted AsShelleyAddress = ["addr", "addr_test"]


instance SerialiseAddress (Address Byron) where
instance SerialiseAddress (Address ByronEra) where
serialiseAddress addr@ByronAddress{} =
Text.decodeLatin1
. Base58.encodeBase58 Base58.bitcoinAlphabet
Expand All @@ -168,9 +168,9 @@ instance SerialiseAddress (Address Byron) where
bs <- Base58.decodeBase58 Base58.bitcoinAlphabet (Text.encodeUtf8 txt)
deserialiseFromRawBytes AsByronAddress bs

instance SerialiseAddress (Address Shelley) where
instance SerialiseAddress (Address ShelleyEra) where
serialiseAddress (ByronAddress addr) =
serialiseAddress (ByronAddress addr :: Address Byron)
serialiseAddress (ByronAddress addr :: Address ByronEra)

serialiseAddress addr@ShelleyAddress{} =
serialiseToBech32 addr
Expand All @@ -187,7 +187,7 @@ instance SerialiseAddress (Address Shelley) where
castByronToShelleyAddress <$>
deserialiseAddress AsByronAddress t

castByronToShelleyAddress :: Address Byron -> Address Shelley
castByronToShelleyAddress :: Address ByronEra -> Address ShelleyEra
castByronToShelleyAddress (ByronAddress addr) = ByronAddress addr


Expand All @@ -204,7 +204,7 @@ makeByronAddress nw (ByronVerificationKey vk) =
makeShelleyAddress :: NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address Shelley
-> Address ShelleyEra
makeShelleyAddress nw pc scr =
ShelleyAddress
(toShelleyNetwork nw)
Expand Down
4 changes: 2 additions & 2 deletions cardano-api/src/Cardano/Api/Fees.hs
Expand Up @@ -36,7 +36,7 @@ import Cardano.Api.Value
--
transactionFee :: Natural -- ^ The fixed tx fee
-> Natural -- ^ The tx fee per byte
-> Tx Shelley
-> Tx ShelleyEra
-> Lovelace
transactionFee txFeeFixed txFeePerByte (ShelleyTx tx) =
Lovelace (a * x + b)
Expand All @@ -59,7 +59,7 @@ transactionFee txFeeFixed txFeePerByte (ShelleyTx tx) =
estimateTransactionFee :: NetworkId
-> Natural -- ^ The fixed tx fee
-> Natural -- ^ The tx fee per byte
-> Tx Shelley
-> Tx ShelleyEra
-> Int -- ^ The number of extra UTxO transaction inputs
-> Int -- ^ The number of extra transaction outputs
-> Int -- ^ The number of extra Shelley key witnesses
Expand Down
54 changes: 26 additions & 28 deletions cardano-api/src/Cardano/Api/Script.hs
Expand Up @@ -76,8 +76,6 @@ import qualified Shelley.Spec.Ledger.Scripts as Shelley
import qualified Shelley.Spec.Ledger.Tx as Shelley

import Cardano.Api.Eras
(Shelley, Allegra, Mary,
AsType (AsByron, AsShelley, AsAllegra, AsMary))
import Cardano.Api.Hash
import Cardano.Api.HasTypeProxy
import Cardano.Api.KeysShelley
Expand All @@ -97,9 +95,9 @@ import qualified Cardano.Api.Shelley.Serialisation.Legacy as Legacy

data Script era where

ShelleyScript :: Shelley.Script StandardShelley -> Script Shelley
AllegraScript :: Timelock.Timelock StandardAllegra -> Script Allegra
MaryScript :: Timelock.Timelock StandardMary -> Script Mary
ShelleyScript :: Shelley.Script StandardShelley -> Script ShelleyEra
AllegraScript :: Timelock.Timelock StandardAllegra -> Script AllegraEra
MaryScript :: Timelock.Timelock StandardMary -> Script MaryEra

deriving stock instance (Eq (Script era))
deriving stock instance (Show (Script era))
Expand All @@ -115,7 +113,7 @@ instance HasTypeProxy era => HasTypeProxy (Script era) where
data AsType (Script era) = AsScript (AsType era)
proxyToAsType _ = AsScript (proxyToAsType (Proxy :: Proxy era))

instance SerialiseAsCBOR (Script Shelley) where
instance SerialiseAsCBOR (Script ShelleyEra) where
serialiseToCBOR (ShelleyScript s) =
-- We use 'WrappedMultiSig' here to support the legacy binary
-- serialisation format for the @Script@ type from
Expand All @@ -124,7 +122,7 @@ instance SerialiseAsCBOR (Script Shelley) where
-- See the documentation of 'WrappedMultiSig' for more information.
CBOR.serialize' (Legacy.WrappedMultiSig s)

deserialiseFromCBOR (AsScript AsShelley) bs =
deserialiseFromCBOR (AsScript AsShelleyEra) bs =
-- We use 'WrappedMultiSig' here to support the legacy binary
-- serialisation format for the @Script@ type from
-- @cardano-ledger-specs@.
Expand All @@ -133,25 +131,25 @@ instance SerialiseAsCBOR (Script Shelley) where
ShelleyScript . Legacy.unWrappedMultiSig <$>
CBOR.decodeAnnotator "Script" fromCBOR (LBS.fromStrict bs)

instance HasTextEnvelope (Script Shelley) where
instance HasTextEnvelope (Script ShelleyEra) where
textEnvelopeType _ = "Script"
textEnvelopeDefaultDescr ShelleyScript{} = "Multi-signature script"

instance SerialiseAsCBOR (Script Allegra) where
instance SerialiseAsCBOR (Script AllegraEra) where
serialiseToCBOR (AllegraScript s) = CBOR.serialize' s
deserialiseFromCBOR (AsScript AsAllegra) bs =
deserialiseFromCBOR (AsScript AsAllegraEra) bs =
AllegraScript <$> CBOR.decodeAnnotator "Script" fromCBOR (LBS.fromStrict bs)

instance HasTextEnvelope (Script Allegra) where
instance HasTextEnvelope (Script AllegraEra) where
textEnvelopeType _ = "Script"
textEnvelopeDefaultDescr AllegraScript{} = "Simple script"

instance SerialiseAsCBOR (Script Mary) where
instance SerialiseAsCBOR (Script MaryEra) where
serialiseToCBOR (MaryScript s) = CBOR.serialize' s
deserialiseFromCBOR (AsScript AsMary) bs =
deserialiseFromCBOR (AsScript AsMaryEra) bs =
MaryScript <$> CBOR.decodeAnnotator "Script" fromCBOR (LBS.fromStrict bs)

instance HasTextEnvelope (Script Mary) where
instance HasTextEnvelope (Script MaryEra) where
textEnvelopeType _ = "Script"
textEnvelopeDefaultDescr MaryScript{} = "Simple script"

Expand Down Expand Up @@ -229,12 +227,12 @@ deriving instance Show (SimpleScript era)
-- specify which script features are enabled in a given era.
--
data ScriptFeatureInEra feature era where
SignaturesInShelleyEra :: ScriptFeatureInEra SignatureFeature Shelley
SignaturesInAllegraEra :: ScriptFeatureInEra SignatureFeature Allegra
SignaturesInMaryEra :: ScriptFeatureInEra SignatureFeature Mary
SignaturesInShelleyEra :: ScriptFeatureInEra SignatureFeature ShelleyEra
SignaturesInAllegraEra :: ScriptFeatureInEra SignatureFeature AllegraEra
SignaturesInMaryEra :: ScriptFeatureInEra SignatureFeature MaryEra

TimeLocksInAllegraEra :: ScriptFeatureInEra TimeLocksFeature Allegra
TimeLocksInMaryEra :: ScriptFeatureInEra TimeLocksFeature Mary
TimeLocksInAllegraEra :: ScriptFeatureInEra TimeLocksFeature AllegraEra
TimeLocksInMaryEra :: ScriptFeatureInEra TimeLocksFeature MaryEra

deriving instance Eq (ScriptFeatureInEra feature era)
deriving instance Show (ScriptFeatureInEra feature era)
Expand All @@ -253,33 +251,33 @@ data TimeLocksFeature
-- | Is the 'SimpleScript' language supported at all in this era?
--
data SimpleScriptSupportedInEra era where
SimpleScriptInShelleyEra :: SimpleScriptSupportedInEra Shelley
SimpleScriptInAllegraEra :: SimpleScriptSupportedInEra Allegra
SimpleScriptInMaryEra :: SimpleScriptSupportedInEra Mary
SimpleScriptInShelleyEra :: SimpleScriptSupportedInEra ShelleyEra
SimpleScriptInAllegraEra :: SimpleScriptSupportedInEra AllegraEra
SimpleScriptInMaryEra :: SimpleScriptSupportedInEra MaryEra

class HasScriptFeatures era where
simpleScriptSupported :: SimpleScriptSupportedInEra era
hasSignatureFeature :: Maybe (ScriptFeatureInEra SignatureFeature era)
hasTimeLocksFeature :: Maybe (ScriptFeatureInEra TimeLocksFeature era)

instance HasScriptFeatures Shelley where
instance HasScriptFeatures ShelleyEra where
simpleScriptSupported = SimpleScriptInShelleyEra
hasSignatureFeature = Just SignaturesInShelleyEra
hasTimeLocksFeature = Nothing

instance HasScriptFeatures Allegra where
instance HasScriptFeatures AllegraEra where
simpleScriptSupported = SimpleScriptInAllegraEra
hasSignatureFeature = Just SignaturesInAllegraEra
hasTimeLocksFeature = Just TimeLocksInAllegraEra

instance HasScriptFeatures Mary where
instance HasScriptFeatures MaryEra where
simpleScriptSupported = SimpleScriptInMaryEra
hasSignatureFeature = Just SignaturesInMaryEra
hasTimeLocksFeature = Just TimeLocksInMaryEra


--TODO: add a deprecation pragma and switch to the SimpleScript constructor
makeMultiSigScript :: MultiSigScript Shelley -> Script Shelley
makeMultiSigScript :: MultiSigScript ShelleyEra -> Script ShelleyEra
makeMultiSigScript = simpleScriptToScript

simpleScriptToScript :: forall era. HasScriptFeatures era
Expand All @@ -288,7 +286,7 @@ simpleScriptToScript =
case simpleScriptSupported :: SimpleScriptSupportedInEra era of
SimpleScriptInShelleyEra -> ShelleyScript . go
where
go :: SimpleScript Shelley -> Shelley.MultiSig StandardShelley
go :: SimpleScript ShelleyEra -> Shelley.MultiSig StandardShelley
go (RequireSignature _ (PaymentKeyHash kh))
= Shelley.RequireSignature (Shelley.coerceKeyRole kh)
go (RequireAllOf s) = Shelley.RequireAllOf (map go s)
Expand Down Expand Up @@ -318,7 +316,7 @@ simpleScriptToTimelock = go
scriptToSimpleScript :: Script era -> SimpleScript era
scriptToSimpleScript (ShelleyScript s0) = go s0
where
go :: Shelley.MultiSig StandardShelley -> SimpleScript Shelley
go :: Shelley.MultiSig StandardShelley -> SimpleScript ShelleyEra
go (Shelley.RequireSignature kh)
= RequireSignature SignaturesInShelleyEra
(PaymentKeyHash (Shelley.coerceKeyRole kh))
Expand Down

0 comments on commit 2208573

Please sign in to comment.