Skip to content

Commit

Permalink
Merge pull request #5011 from input-output-hk/mafo/cli-wrappers
Browse files Browse the repository at this point in the history
Use Haskell variables for passing values.
  • Loading branch information
MarcFontaine committed Mar 29, 2023
2 parents 0ac69d4 + bc210d5 commit 7b249f6
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 20 deletions.
1 change: 1 addition & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ library
Testnet.Byron
Testnet.Util.Assert
Testnet.Util.Base
Testnet.Util.Cli
Testnet.Util.Ignore
Testnet.Util.Process
Testnet.Util.Runtime
Expand Down
28 changes: 8 additions & 20 deletions cardano-testnet/src/Testnet/Shelley.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import qualified System.Info as OS
import Testnet.Commands.Genesis
import qualified Testnet.Conf as H
import qualified Testnet.Util.Base as H
import Testnet.Util.Cli
import qualified Testnet.Util.Process as H
import Testnet.Util.Process (execCli_)
import Testnet.Util.Runtime hiding (allNodes)
Expand Down Expand Up @@ -227,11 +228,7 @@ shelleyTestnet testnetOptions H.Conf {..} = do
, "--operational-certificate-issue-counter-file", tempAbsPath </> n </> "operator.counter"
]

execCli_
[ "node", "key-gen-VRF"
, "--verification-key-file", tempAbsPath </> n </> "vrf.vkey"
, "--signing-key-file", tempAbsPath </> n </> "vrf.skey"
]
cliNodeKeyGenVrf tempAbsPath $ KeyNames (n </> "vrf.vkey") (n </> "vrf.skey")
-- Symlink the BFT operator keys from the genesis delegates, for uniformity
forM_ praosNodesN $ \n -> do
H.createFileLink (tempAbsPath </> "delegate-keys/delegate" <> n <> ".skey") (tempAbsPath </> "node-praos" <> n </> "operator.skey")
Expand All @@ -242,11 +239,7 @@ shelleyTestnet testnetOptions H.Conf {..} = do

-- Make hot keys and for all nodes
forM_ allNodes $ \node -> do
execCli_
[ "node", "key-gen-KES"
, "--verification-key-file", tempAbsPath </> node </> "kes.vkey"
, "--signing-key-file", tempAbsPath </> node </> "kes.skey"
]
_keys <- cliNodeKeyGenKes tempAbsPath $ KeyNames (node </> "key.vkey") (node </> "key.skey")

execCli_
[ "node", "issue-op-cert"
Expand Down Expand Up @@ -277,18 +270,13 @@ shelleyTestnet testnetOptions H.Conf {..} = do

forM_ addrs $ \addr -> do
-- Payment address keys
execCli_
[ "address", "key-gen"
, "--verification-key-file", tempAbsPath </> "addresses/" <> addr <> ".vkey"
, "--signing-key-file", tempAbsPath </> "addresses/" <> addr <> ".skey"
]
_address <- cliAddressKeyGen tempAbsPath $ KeyNames ("addresses" </> addr <> ".vkey") ("addresses" </> addr <> ".skey")

-- Stake address keys
execCli_
[ "stake-address", "key-gen"
, "--verification-key-file", tempAbsPath </> "addresses/" <> addr <> "-stake.vkey"
, "--signing-key-file", tempAbsPath </> "addresses/" <> addr <> "-stake.skey"
]
_stakeAddress <- cliStakeAddressKeyGen tempAbsPath
$ KeyNames
("addresses" </> addr <> "-stake.vkey")
("addresses" </> addr <> "-stake.skey")

-- Payment addresses
execCli_
Expand Down
138 changes: 138 additions & 0 deletions cardano-testnet/src/Testnet/Util/Cli.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
module Testnet.Util.Cli
( cliAddressKeyGen
, cliNodeKeyGen
, cliNodeKeyGenVrf
, cliNodeKeyGenKes
, cliStakeAddressKeyGen
, KeyGen
, KeyNames (..)

, File (..)

, VKey
, SKey

, OperatorCounter

, ByronDelegationKey
, ByronDelegationCert

, getVKeyPath
, getSKeyPath

, cliKeyGen

, cliByronSigningKeyAddress
) where

import System.FilePath.Posix

import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H (writeFile)

import Cardano.Api (ByronAddr, ByronKeyLegacy, PaymentKey, StakeKey)
import Cardano.Api.Shelley (KesKey, StakePoolKey, VrfKey)

import Testnet.Util.Process

data KeyNames = KeyNames
{ verificationKeyFile :: FilePath
, signingKeyFile :: FilePath
}

type KeyGen a = (File (VKey a), File (SKey a))

cliAddressKeyGen :: TmpDir -> KeyNames -> H.Integration (KeyGen PaymentKey)
cliAddressKeyGen = shelleyKeyGen "address" "key-gen"

cliStakeAddressKeyGen :: TmpDir -> KeyNames -> H.Integration (KeyGen StakeKey)
cliStakeAddressKeyGen = shelleyKeyGen "stake-address" "key-gen"

cliNodeKeyGenVrf :: TmpDir -> KeyNames -> H.Integration (KeyGen VrfKey)
cliNodeKeyGenVrf = shelleyKeyGen "node" "key-gen-VRF"

cliNodeKeyGenKes :: TmpDir -> KeyNames -> H.Integration (KeyGen KesKey)
cliNodeKeyGenKes = shelleyKeyGen "node" "key-gen-KES"

shelleyKeyGen :: String -> String -> TmpDir -> KeyNames -> H.Integration (KeyGen x)
shelleyKeyGen command subCommand tmpDir keyNames = do
let
vKeyPath = tmpDir </> verificationKeyFile keyNames
sKeyPath = tmpDir </> signingKeyFile keyNames
execCli_
[ command, subCommand
, "--verification-key-file", vKeyPath
, "--signing-key-file", sKeyPath
]
return (File vKeyPath, File sKeyPath)

cliNodeKeyGen
:: TmpDir
-> FilePath
-> FilePath
-> FilePath
-> H.Integration (File (VKey StakePoolKey), File (SKey StakePoolKey), File OperatorCounter)
cliNodeKeyGen tmpDir vkey skey counter = do
let
vkPath = tmpDir </> vkey
skPath = tmpDir </> skey
counterPath = tmpDir </> counter
execCli_
[ "node", "key-gen"
, "--cold-verification-key-file", vkPath
, "--cold-signing-key-file", skPath
, "--operational-certificate-issue-counter-file", counterPath
]
return (File vkPath, File skPath, File counterPath)

-- | Verification keys
data VKey a

-- | Signing keys
data SKey a

-- | The 'OperatorCounter'
data OperatorCounter

-- | Tag a 'File' that holds a 'ByronDelegationKey'.
data ByronDelegationKey

-- | Tag a 'File' that holds a 'ByronDelegationKey'.
data ByronDelegationCert

type TmpDir = FilePath

newtype File a = File {unFile :: FilePath}
deriving (Show, Eq)

getVKeyPath :: (File (VKey a), File (SKey a)) -> FilePath
getVKeyPath = unFile . fst

getSKeyPath :: (File (VKey a), File (SKey a)) -> FilePath
getSKeyPath = unFile . snd

-- Byron
cliKeyGen :: TmpDir -> FilePath -> H.Integration (File ByronKeyLegacy)
cliKeyGen tmp key = do
let keyPath = tmp </> key
execCli_
[ "keygen"
, "--secret", keyPath
]
return $ File keyPath

cliByronSigningKeyAddress
:: TmpDir
-> Int
-> File ByronKeyLegacy
-> FilePath
-> H.Integration (File ByronAddr)
cliByronSigningKeyAddress tmp testnetMagic (File key) destPath = do
let addrPath = tmp </> destPath
addr <- execCli
[ "signing-key-address"
, "--testnet-magic", show testnetMagic
, "--secret", tmp </> key
]
H.writeFile addrPath addr
return $ File addrPath

0 comments on commit 7b249f6

Please sign in to comment.