Skip to content

Commit

Permalink
Update cardano-cli to use withCardanoEra
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 authored and intricate committed Nov 23, 2020
1 parent 844dde7 commit d666c2b
Show file tree
Hide file tree
Showing 15 changed files with 177 additions and 76 deletions.
8 changes: 4 additions & 4 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ module Cardano.Api.Script (
, ScriptFeatureInEra(..)
, SignatureFeature
, TimeLocksFeature
, HasScriptFeatures

-- * Deprecated aliases
, MultiSigScript
Expand Down Expand Up @@ -69,12 +70,11 @@ import qualified Cardano.Crypto.Hash.Class as Crypto
import Cardano.Slotting.Slot (SlotNo)

import qualified Cardano.Ledger.Core as Shelley
import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Era as Ledger

import qualified Cardano.Ledger.ShelleyMA.Timelocks as Timelock
import Ouroboros.Consensus.Shelley.Eras
(StandardAllegra, StandardMary, StandardShelley,
StandardCrypto)
import Ouroboros.Consensus.Shelley.Eras (StandardAllegra, StandardCrypto, StandardMary,
StandardShelley)
import qualified Shelley.Spec.Ledger.Keys as Shelley
import qualified Shelley.Spec.Ledger.Scripts as Shelley
import qualified Shelley.Spec.Ledger.Tx as Shelley
Expand Down
21 changes: 8 additions & 13 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
Expand Down Expand Up @@ -44,29 +44,28 @@ module Cardano.Api.TxBody (

import Prelude

import qualified Data.List.NonEmpty as NonEmpty
import Data.String (IsString)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Short as SBS
import qualified Data.List.NonEmpty as NonEmpty
import qualified Data.Map.Strict as Map
import qualified Data.Sequence.Strict as Seq
import qualified Data.Set as Set
import Data.String (IsString)

import Cardano.Binary (Annotated (..), reAnnotate, recoverBytes)
import qualified Cardano.Binary as CBOR
import qualified Shelley.Spec.Ledger.Serialization as CBOR
(decodeNullMaybe, encodeNullMaybe)
import qualified Shelley.Spec.Ledger.Serialization as CBOR (decodeNullMaybe, encodeNullMaybe)

import Cardano.Slotting.Slot (SlotNo (..))
import qualified Cardano.Crypto.Hash.Class as Crypto
import Cardano.Slotting.Slot (SlotNo (..))

import qualified Cardano.Crypto.Hashing as Byron
import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Chain.UTxO as Byron
import qualified Cardano.Crypto.Hashing as Byron

import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Era as Ledger
import qualified Cardano.Ledger.Shelley as Ledger
import qualified Cardano.Ledger.ShelleyMA.TxBody as Allegra
import Ouroboros.Consensus.Shelley.Eras (StandardShelley)
Expand Down Expand Up @@ -212,9 +211,6 @@ toShelleyTxOut (TxOut addr (TxOutAdaOnly AdaOnlyInAllegraEra value)) =
toShelleyTxOut (TxOut addr (TxOutValue MultiAssetInMaryEra value)) =
Shelley.TxOut (toShelleyAddr addr) (toMaryValue value)




-- ----------------------------------------------------------------------------
-- Transaction bodies
--
Expand Down Expand Up @@ -511,4 +507,3 @@ genesisUTxOPseudoTxIn nw (GenesisUTxOKeyHash kh) =
fromShelleyTxId :: Shelley.TxId StandardShelley -> TxId
fromShelleyTxId (Shelley.TxId h) =
TxId (Crypto.castHash h)

3 changes: 2 additions & 1 deletion cardano-api/src/Cardano/Api/Typed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ module Cardano.Api.Typed (
ScriptFeatureInEra(..),
SignatureFeature,
TimeLocksFeature,
HasScriptFeatures,
-- *** Deprecated aliases
MultiSigScript,
makeMultiSigScript,
Expand Down Expand Up @@ -467,7 +468,7 @@ import qualified Cardano.Chain.Slotting as Byron
--
-- Shelley imports
--
import Ouroboros.Consensus.Shelley.Eras (StandardAllegra, StandardShelley, StandardMary)
import Ouroboros.Consensus.Shelley.Eras (StandardAllegra, StandardMary, StandardShelley)
import Ouroboros.Consensus.Shelley.Protocol.Crypto (StandardCrypto)

import qualified Shelley.Spec.Ledger.Address as Shelley
Expand Down
9 changes: 5 additions & 4 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ module Cardano.CLI.Shelley.Commands
import Data.Text (Text)
import Prelude

import Cardano.Api.Typed hiding (PoolId)
import Cardano.Api.Protocol (Protocol)
import Cardano.Api.Typed hiding (PoolId)

import Ouroboros.Consensus.BlockchainTime (SystemStart (..))

Expand Down Expand Up @@ -100,7 +100,7 @@ data AddressCmd
(Maybe (VerificationKeyOrFile StakeKey))
NetworkId
(Maybe OutputFile)
| AddressBuildMultiSig ScriptFile NetworkId (Maybe OutputFile)
| AddressBuildMultiSig UseCardanoEra ScriptFile NetworkId (Maybe OutputFile)
| AddressInfo Text (Maybe OutputFile)
deriving (Eq, Show)

Expand Down Expand Up @@ -161,6 +161,7 @@ renderKeyCmd cmd =

data TransactionCmd
= TxBuildRaw
UseCardanoEra
[TxIn]
[TxOut ShelleyEra]
(Maybe String) -- Placeholder for multi asset Values
Expand All @@ -172,8 +173,8 @@ data TransactionCmd
[MetaDataFile]
(Maybe UpdateProposalFile)
TxBodyFile
| TxSign TxBodyFile [WitnessSigningData] (Maybe NetworkId) TxFile
| TxCreateWitness TxBodyFile WitnessSigningData (Maybe NetworkId) OutputFile
| TxSign UseCardanoEra TxBodyFile [WitnessSigningData] (Maybe NetworkId) TxFile
| TxCreateWitness UseCardanoEra TxBodyFile WitnessSigningData (Maybe NetworkId) OutputFile
| TxAssembleTxBodyWitness TxBodyFile [WitnessFile] OutputFile
| TxSubmit Protocol NetworkId FilePath
| TxMintedPolicyId ScriptFile
Expand Down
34 changes: 29 additions & 5 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ module Cardano.CLI.Shelley.Parsers
import Cardano.Prelude hiding (All, Any, option)
import Prelude (String)

import Cardano.Api.Typed hiding (PoolId)
import Cardano.Api.Protocol (Protocol (..))
import Cardano.Api.Typed hiding (PoolId)

import Cardano.Chain.Slotting (EpochSlots (..))
import Cardano.CLI.Shelley.Commands
Expand Down Expand Up @@ -147,7 +147,8 @@ pAddressCmd =

pAddressBuildScript :: Parser AddressCmd
pAddressBuildScript = AddressBuildMultiSig
<$> pScript
<$> pUseCardanoEra
<*> pScript
<*> pNetworkId
<*> pMaybeOutputFile

Expand Down Expand Up @@ -492,7 +493,8 @@ pTransaction =
$ Opt.command "sign-witness" assembleInfo <> Opt.internal

pTransactionBuild :: Parser TransactionCmd
pTransactionBuild = TxBuildRaw <$> some pTxIn
pTransactionBuild = TxBuildRaw <$> pUseCardanoEra
<*> some pTxIn
<*> some pTxOut
<*> optional pMint
<*> pTxTTL
Expand All @@ -505,14 +507,16 @@ pTransaction =
<*> pTxBodyFile Output

pTransactionSign :: Parser TransactionCmd
pTransactionSign = TxSign <$> pTxBodyFile Input
pTransactionSign = TxSign <$> pUseCardanoEra
<*> pTxBodyFile Input
<*> pSomeWitnessSigningData
<*> optional pNetworkId
<*> pTxFile Output

pTransactionCreateWitness :: Parser TransactionCmd
pTransactionCreateWitness = TxCreateWitness
<$> pTxBodyFile Input
<$> pUseCardanoEra
<*> pTxBodyFile Input
<*> pWitnessSigningData
<*> optional pNetworkId
<*> pOutputFile
Expand Down Expand Up @@ -1417,6 +1421,26 @@ pTxSubmitFile =
<> Opt.completer (Opt.bashCompleter "file")
)

pUseCardanoEra :: Parser UseCardanoEra
pUseCardanoEra = asum
[ Opt.flag' UseByronEra
( Opt.long "byron-era"
<> Opt.help "Specify the Byron era"
)
, Opt.flag' UseShelleyEra
( Opt.long "shelley-era"
<> Opt.help "Specify the Shelley era"
)
, Opt.flag' UseAllegraEra
( Opt.long "allegra-era"
<> Opt.help "Specify the Allegra era"
)
, Opt.flag' UseMaryEra
( Opt.long "mary-era"
<> Opt.help "Specify the Mary era"
)
]

pTxIn :: Parser TxIn
pTxIn =
Opt.option (readerFromAttoParser parseTxIn)
Expand Down
49 changes: 37 additions & 12 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Shelley.Run.Address
( ShelleyAddressCmdError
Expand All @@ -15,7 +16,8 @@ import qualified Data.ByteString.Lazy as LB
import qualified Data.Text as Text
import qualified Data.Text.IO as Text

import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT, left, newExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT, hoistEither,
newExceptT)

import Cardano.Api.Typed

Expand Down Expand Up @@ -56,7 +58,7 @@ runAddressCmd cmd =
AddressKeyGen kt vkf skf -> runAddressKeyGen kt vkf skf
AddressKeyHash vkf mOFp -> runAddressKeyHash vkf mOFp
AddressBuild payVk stkVk nw mOutFp -> runAddressBuild payVk stkVk nw mOutFp
AddressBuildMultiSig sFp nId mOutFp -> runAddressBuildScript sFp nId mOutFp
AddressBuildMultiSig useEra sFp nId mOutFp -> runAddressBuildScript useEra sFp nId mOutFp
AddressInfo txt mOFp -> firstExceptT ShelleyAddressCmdAddressInfoError $ runAddressInfo txt mOFp

runAddressKeyGen :: AddressKeyType
Expand Down Expand Up @@ -201,18 +203,41 @@ readAddressVerificationKeyTextOrFile vkTextOrFile =
--

runAddressBuildScript
:: ScriptFile
:: UseCardanoEra
-> ScriptFile
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuildScript (ScriptFile fp) nId mOutFp = do
runAddressBuildScript useEra (ScriptFile fp) nId mOutFp = do
scriptLB <- handleIOExceptT (ShelleyAddressCmdReadFileException . FileIOError fp)
$ LB.readFile fp
script <- case eitherDecode scriptLB :: Either String (MultiSigScript ShelleyEra) of
Right mss -> return $ makeMultiSigScript mss
Left err -> left . ShelleyAddressCmdAesonDecodeError fp $ Text.pack err
let payCred = PaymentCredentialByScript $ scriptHash script
scriptAddr = serialiseAddress $ makeShelleyAddress nId payCred NoStakeAddress
case mOutFp of
Just (OutputFile oFp) -> liftIO $ Text.writeFile oFp scriptAddr
Nothing -> liftIO $ Text.putStr scriptAddr
withCardanoEra useEra $ \_era _eraStyle ->
case useEra of
UseByronEra -> liftIO $ putTextLn "Not implemented yet"
UseShelleyEra -> do
aScript :: SimpleScript ShelleyEra <-
firstExceptT (ShelleyAddressCmdAesonDecodeError fp . Text.pack) . hoistEither $ decodeScript scriptLB
mOutput mOutFp $ serialiseScriptAddress nId aScript
UseAllegraEra -> do
aScript :: SimpleScript AllegraEra <-
firstExceptT (ShelleyAddressCmdAesonDecodeError fp . Text.pack) . hoistEither $ decodeScript scriptLB
mOutput mOutFp $ serialiseScriptAddress nId aScript
UseMaryEra -> do
aScript :: SimpleScript MaryEra <-
firstExceptT (ShelleyAddressCmdAesonDecodeError fp . Text.pack) . hoistEither $ decodeScript scriptLB
mOutput mOutFp $ serialiseScriptAddress nId aScript

serialiseScriptAddress :: HasScriptFeatures era => NetworkId -> SimpleScript era -> Text
serialiseScriptAddress nId s =
let payCred = makePaymentCredential s
in serialiseAddress $ makeShelleyAddress nId payCred NoStakeAddress

decodeScript :: HasScriptFeatures era => LB.ByteString -> Either String (SimpleScript era)
decodeScript bs = eitherDecode bs

makePaymentCredential :: HasScriptFeatures era => SimpleScript era -> PaymentCredential
makePaymentCredential s = PaymentCredentialByScript . scriptHash $ SimpleScript s

mOutput :: Maybe OutputFile -> Text -> ExceptT a IO ()
mOutput (Just (OutputFile oFp)) output = liftIO $ Text.writeFile oFp output
mOutput Nothing output = liftIO $ Text.putStr output
Loading

0 comments on commit d666c2b

Please sign in to comment.