Skip to content

Commit

Permalink
Fix script file option name
Browse files Browse the repository at this point in the history
New --stake-script-file option
  • Loading branch information
newhoggy committed Apr 8, 2021
1 parent 3c556c5 commit 1c8bad8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 48 deletions.
6 changes: 3 additions & 3 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Expand Up @@ -54,7 +54,7 @@ import Cardano.Api.Shelley hiding (PoolId)

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

import Cardano.CLI.Shelley.Key (PaymentSource, VerificationKeyOrFile,
import Cardano.CLI.Shelley.Key (PaymentVerifier, StakeVerifier, VerificationKeyOrFile,
VerificationKeyOrHashOrFile, VerificationKeyTextOrFile)
import Cardano.CLI.Types

Expand Down Expand Up @@ -96,8 +96,8 @@ data AddressCmd
= AddressKeyGen AddressKeyType VerificationKeyFile SigningKeyFile
| AddressKeyHash VerificationKeyTextOrFile (Maybe OutputFile)
| AddressBuild
PaymentSource
(Maybe (VerificationKeyOrFile StakeKey))
PaymentVerifier
(Maybe StakeVerifier)
NetworkId
(Maybe OutputFile)
| AddressBuildMultiSig ScriptFile NetworkId (Maybe OutputFile)
Expand Down
14 changes: 10 additions & 4 deletions cardano-cli/src/Cardano/CLI/Shelley/Key.hs
Expand Up @@ -32,7 +32,8 @@ module Cardano.CLI.Shelley.Key
, readVerificationKeyOrHashOrFile
, readVerificationKeyOrHashOrTextEnvFile

, PaymentSource(..)
, PaymentVerifier(..)
, StakeVerifier(..)
) where

import Cardano.Prelude
Expand Down Expand Up @@ -383,9 +384,14 @@ readVerificationKeyOrTextEnvFile asType verKeyOrFile =
VerificationKeyFilePath (VerificationKeyFile fp) ->
readKeyFileTextEnvelope (AsVerificationKey asType) fp

data PaymentSource
= SourcePaymentKey VerificationKeyTextOrFile
| SourcePaymentScript ScriptFile
data PaymentVerifier
= PaymentVerifierKey VerificationKeyTextOrFile
| PaymentVerifierScriptFile ScriptFile
deriving (Eq, Show)

data StakeVerifier
= StakeVerifierKey (VerificationKeyOrFile StakeKey)
| StakeVerifierScriptFile ScriptFile
deriving (Eq, Show)

-- | Either an unvalidated text representation of a verification key or a path
Expand Down
34 changes: 21 additions & 13 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Expand Up @@ -22,16 +22,16 @@ import Cardano.Api.Shelley
import Cardano.CLI.Mary.TxOutParser (parseTxOutAnyEra)
import Cardano.CLI.Mary.ValueParser (parseValue)
import Cardano.CLI.Shelley.Commands
import Cardano.CLI.Shelley.Key (InputFormat (..), PaymentSource (..),
VerificationKeyOrFile (..), VerificationKeyOrHashOrFile (..),
import Cardano.CLI.Shelley.Key (InputFormat (..), PaymentVerifier (..),
StakeVerifier (..), VerificationKeyOrFile (..), VerificationKeyOrHashOrFile (..),
VerificationKeyTextOrFile (..), deserialiseInput, renderInputDecodeError)
import Cardano.CLI.Types
import Control.Monad.Fail (fail)
import Data.Attoparsec.Combinator ((<?>))
import Data.Time.Clock (UTCTime)
import Data.Time.Format (defaultTimeLocale, iso8601DateFormat, parseTimeOrError)
import Network.Socket (PortNumber)
import Options.Applicative hiding (str)
import Options.Applicative hiding (help, str)
import Ouroboros.Consensus.BlockchainTime (SystemStart (..))

import qualified Data.ByteString.Base16 as B16
Expand Down Expand Up @@ -128,7 +128,7 @@ pAddressCmd =
, subParser "build"
(Opt.info pAddressBuild $ Opt.progDesc "Build a Shelley payment address, with optional delegation to a stake address.")
, subParser "build-script"
(Opt.info pAddressBuildScript $ Opt.progDesc "Build a Shelley script address. (deprecated; use 'build' instead with '--script-file')")
(Opt.info pAddressBuildScript $ Opt.progDesc "Build a Shelley script address. (deprecated; use 'build' instead with '--payment-script-file')")
, subParser "info"
(Opt.info pAddressInfo $ Opt.progDesc "Print information about an address.")
]
Expand All @@ -146,8 +146,8 @@ pAddressCmd =

pAddressBuild :: Parser AddressCmd
pAddressBuild = AddressBuild
<$> pPaymentSource
<*> Opt.optional pStakeVerificationKeyOrFile
<$> pPaymentVerifier
<*> Opt.optional pStakeVerifier
<*> pNetworkId
<*> pMaybeOutputFile

Expand All @@ -160,10 +160,15 @@ pAddressCmd =
pAddressInfo :: Parser AddressCmd
pAddressInfo = AddressInfo <$> pAddress <*> pMaybeOutputFile

pPaymentSource :: Parser PaymentSource
pPaymentSource =
SourcePaymentKey <$> pPaymentVerificationKeyTextOrFile
<|> SourcePaymentScript <$> pScript
pPaymentVerifier :: Parser PaymentVerifier
pPaymentVerifier =
PaymentVerifierKey <$> pPaymentVerificationKeyTextOrFile
<|> PaymentVerifierScriptFile <$> pScriptFor "payment-script-file" "Filepath of the payment script."

pStakeVerifier :: Parser StakeVerifier
pStakeVerifier =
StakeVerifierKey <$> pStakeVerificationKeyOrFile
<|> StakeVerifierScriptFile <$> pScriptFor "stake-script-file" "Filepath of the staking script."

pPaymentVerificationKeyTextOrFile :: Parser VerificationKeyTextOrFile
pPaymentVerificationKeyTextOrFile =
Expand Down Expand Up @@ -196,10 +201,13 @@ pPaymentVerificationKeyFile =
)

pScript :: Parser ScriptFile
pScript = ScriptFile <$> Opt.strOption
( Opt.long "script-file"
pScript = pScriptFor "script-file" "Filepath of the script."

pScriptFor :: String -> String -> Parser ScriptFile
pScriptFor name help = ScriptFile <$> Opt.strOption
( Opt.long name
<> Opt.metavar "FILE"
<> Opt.help "Filepath of the script."
<> Opt.help help
<> Opt.completer (Opt.bashCompleter "file")
)

Expand Down
65 changes: 38 additions & 27 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs
Expand Up @@ -29,8 +29,8 @@ import Control.Monad.Trans.Except.Extra (firstExceptT, handleIOExceptT
import Cardano.Api
import Cardano.Api.Shelley

import Cardano.CLI.Shelley.Key (InputDecodeError, PaymentSource (..),
VerificationKeyOrFile, VerificationKeyTextOrFile,
import Cardano.CLI.Shelley.Key (InputDecodeError, PaymentVerifier (..),
StakeVerifier (..), VerificationKeyTextOrFile,
VerificationKeyTextOrFileError (..), readVerificationKeyOrFile,
readVerificationKeyTextOrFileAnyOf, renderVerificationKeyTextOrFileError)
import Cardano.CLI.Shelley.Parsers (AddressCmd (..), AddressKeyType (..), OutputFile (..))
Expand Down Expand Up @@ -65,7 +65,7 @@ runAddressCmd cmd =
case cmd of
AddressKeyGen kt vkf skf -> runAddressKeyGen kt vkf skf
AddressKeyHash vkf mOFp -> runAddressKeyHash vkf mOFp
AddressBuild paymentSource stkVk nw mOutFp -> runAddressBuild paymentSource stkVk nw mOutFp
AddressBuild paymentVerifier mbStakeVerifier nw mOutFp -> runAddressBuild paymentVerifier mbStakeVerifier nw mOutFp
AddressBuildMultiSig sFp nId mOutFp -> runAddressBuildScript sFp nId mOutFp
AddressInfo txt mOFp -> firstExceptT ShelleyAddressCmdAddressInfoError $ runAddressInfo txt mOFp

Expand Down Expand Up @@ -109,14 +109,14 @@ runAddressKeyHash vkeyTextOrFile mOutputFp = do
Nothing -> liftIO $ BS.putStrLn hexKeyHash


runAddressBuild :: PaymentSource
-> Maybe (VerificationKeyOrFile StakeKey)
runAddressBuild :: PaymentVerifier
-> Maybe StakeVerifier
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild paymentSource mbStkVkeyOrFile nw mOutFp = do
outText <- case paymentSource of
SourcePaymentKey payVkeyTextOrFile -> do
runAddressBuild paymentVerifier mbStakeVerifier nw mOutFp = do
outText <- case paymentVerifier of
PaymentVerifierKey payVkeyTextOrFile -> do
payVKey <- firstExceptT ShelleyAddressCmdVerificationKeyTextOrFileError $
readAddressVerificationKeyTextOrFile payVkeyTextOrFile

Expand All @@ -125,50 +125,61 @@ runAddressBuild paymentSource mbStkVkeyOrFile nw mOutFp = do
return (AddressByron (makeByronAddress nw vk))

APaymentVerificationKey vk ->
AddressShelley <$> buildShelleyAddress vk mbStkVkeyOrFile nw
AddressShelley <$> buildShelleyAddress vk mbStakeVerifier nw

APaymentExtendedVerificationKey vk ->
AddressShelley <$> buildShelleyAddress (castVerificationKey vk) mbStkVkeyOrFile nw
AddressShelley <$> buildShelleyAddress (castVerificationKey vk) mbStakeVerifier nw

AGenesisUTxOVerificationKey vk ->
AddressShelley <$> buildShelleyAddress (castVerificationKey vk) mbStkVkeyOrFile nw
AddressShelley <$> buildShelleyAddress (castVerificationKey vk) mbStakeVerifier nw

return $ serialiseAddress (addr :: AddressAny)

SourcePaymentScript (ScriptFile fp) -> do
PaymentVerifierScriptFile (ScriptFile fp) -> do
scriptBytes <- handleIOExceptT (ShelleyAddressCmdReadFileException . FileIOError fp) $ LBS.readFile fp
ScriptInAnyLang _lang script <-
firstExceptT (ShelleyAddressCmdAesonDecodeError fp . Text.pack) $
hoistEither $ Aeson.eitherDecode scriptBytes

let payCred = PaymentCredentialByScript (hashScript script)

serialiseAddress . makeShelleyAddress nw payCred <$> makeStakeAddressRef mbStkVkeyOrFile
serialiseAddress . makeShelleyAddress nw payCred <$> makeStakeAddressRef mbStakeVerifier

case mOutFp of
Just (OutputFile fpath) -> liftIO $ Text.writeFile fpath outText
Nothing -> liftIO $ Text.putStr outText

makeStakeAddressRef
:: Maybe (VerificationKeyOrFile StakeKey)
:: Maybe StakeVerifier
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
makeStakeAddressRef mbStkVkeyOrFile = do
mstakeVKey <- case mbStkVkeyOrFile of
Nothing -> pure Nothing
Just stkVkeyOrFile -> firstExceptT ShelleyAddressCmdReadKeyFileError $
fmap Just $ newExceptT $ readVerificationKeyOrFile AsStakeKey stkVkeyOrFile

return $ maybe NoStakeAddress
(StakeAddressByValue . StakeCredentialByKey . verificationKeyHash)
mstakeVKey
makeStakeAddressRef mbStakeVerifier = do
case mbStakeVerifier of
Nothing -> pure NoStakeAddress
Just stakeVerifier -> case stakeVerifier of
StakeVerifierKey stkVkeyOrFile -> do
mstakeVKey <- firstExceptT ShelleyAddressCmdReadKeyFileError $
fmap Just $ newExceptT $ readVerificationKeyOrFile AsStakeKey stkVkeyOrFile

return $ maybe NoStakeAddress
(StakeAddressByValue . StakeCredentialByKey . verificationKeyHash)
mstakeVKey

StakeVerifierScriptFile (ScriptFile fp) -> do
scriptBytes <- handleIOExceptT (ShelleyAddressCmdReadFileException . FileIOError fp) $ LBS.readFile fp
ScriptInAnyLang _lang script <-
firstExceptT (ShelleyAddressCmdAesonDecodeError fp . Text.pack) $
hoistEither $ Aeson.eitherDecode scriptBytes

let stakeCred = StakeCredentialByScript (hashScript script)
return (StakeAddressByValue stakeCred)

buildShelleyAddress
:: VerificationKey PaymentKey
-> Maybe (VerificationKeyOrFile StakeKey)
-> Maybe StakeVerifier
-> NetworkId
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
buildShelleyAddress vkey mbStkVkeyOrFile nw =
makeShelleyAddress nw (PaymentCredentialByKey (verificationKeyHash vkey)) <$> makeStakeAddressRef mbStkVkeyOrFile
buildShelleyAddress vkey mbStakeVerifier nw =
makeShelleyAddress nw (PaymentCredentialByKey (verificationKeyHash vkey)) <$> makeStakeAddressRef mbStakeVerifier


--
Expand Down Expand Up @@ -230,7 +241,7 @@ runAddressBuildScript
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuildScript scriptFile networkId mOutputFile = do
liftIO deprecationWarning
runAddressBuild (SourcePaymentScript scriptFile) Nothing networkId mOutputFile
runAddressBuild (PaymentVerifierScriptFile scriptFile) Nothing networkId mOutputFile

deprecationWarning :: IO ()
deprecationWarning = do
Expand Down
2 changes: 1 addition & 1 deletion cardano-cli/src/Cardano/CLI/Shelley/Run/Genesis.hs
Expand Up @@ -647,7 +647,7 @@ computeDelegation nw delegDir pool delegIx = do
initialUtxoAddr <- case paySVK of
APaymentVerificationKey payVK ->
firstExceptT ShelleyGenesisCmdAddressCmdError
$ buildShelleyAddress payVK (Just . VerificationKeyFilePath . VerificationKeyFile $ stakeVKF) nw
$ buildShelleyAddress payVK (Just . StakeVerifierKey . VerificationKeyFilePath . VerificationKeyFile $ stakeVKF) nw
_ -> left $ ShelleyGenesisCmdUnexpectedAddressVerificationKey payVKF "APaymentVerificationKey" paySVK

StakeVerificationKey stakeVK <- firstExceptT ShelleyGenesisCmdTextEnvReadFileError
Expand Down

0 comments on commit 1c8bad8

Please sign in to comment.