Skip to content
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

convert-cardano-address-key: support DRep and CC keys #691

Merged
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
14 changes: 13 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Options/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,19 @@ pKeyConvertCardanoAddressKeyCmd =
pCardanoAddressKeyType :: Parser CardanoAddressKeyType
pCardanoAddressKeyType =
asum
[ Opt.flag' CardanoAddressShelleyPaymentKey $ mconcat
[ Opt.flag' CardanoAddressCommitteeColdKey $ mconcat
[ Opt.long "cc-cold-key"
, Opt.help "Use a committee cold key."
]
, Opt.flag' CardanoAddressCommitteeHotKey $ mconcat
[ Opt.long "cc-hot-key"
, Opt.help "Use a committee hot key."
]
, Opt.flag' CardanoAddressDRepKey $ mconcat
[ Opt.long "drep-key"
, Opt.help "Use a DRep key."
]
, Opt.flag' CardanoAddressShelleyPaymentKey $ mconcat
[ Opt.long "shelley-payment-key"
, Opt.help "Use a Shelley-era extended payment key."
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Commands.Governance.Committee
import qualified Cardano.CLI.EraBased.Commands.Governance.Committee as Cmd
import qualified Cardano.CLI.EraBased.Run.Key as Key
import Cardano.CLI.Read (readVerificationKeyOrHashOrFileOrScript)
import Cardano.CLI.Types.Errors.GovernanceCommitteeError
import Cardano.CLI.Types.Key
Expand Down Expand Up @@ -49,19 +50,12 @@ runGovernanceCommitteeKeyGenCold

let vkey = getVerificationKey skey

writeLazyByteStringFile skeyPath (textEnvelopeToJSON (Just skeyDesc) skey)
writeLazyByteStringFile skeyPath (textEnvelopeToJSON (Just Key.ccColdSkeyDesc) skey)
& onLeft (left . GovernanceCommitteeCmdWriteFileError)

writeLazyByteStringFile vkeyPath (textEnvelopeToJSON (Just vkeyDesc) vkey)
writeLazyByteStringFile vkeyPath (textEnvelopeToJSON (Just Key.ccColdVkeyDesc) vkey)
& onLeft (left . GovernanceCommitteeCmdWriteFileError)

where
skeyDesc :: TextEnvelopeDescr
skeyDesc = "Constitutional Committee Cold Signing Key"

vkeyDesc :: TextEnvelopeDescr
vkeyDesc = "Constitutional Committee Cold Verification Key"

runGovernanceCommitteeKeyGenHot :: ()
=> Cmd.GovernanceCommitteeKeyGenHotCmdArgs era
-> ExceptT GovernanceCommitteeError IO ()
Expand All @@ -78,19 +72,12 @@ runGovernanceCommitteeKeyGenHot
firstExceptT GovernanceCommitteeCmdWriteFileError
. newExceptT
$ writeLazyByteStringFile skeyPath
$ textEnvelopeToJSON (Just skeyDesc) skey
$ textEnvelopeToJSON (Just Key.ccHotSkeyDesc) skey

firstExceptT GovernanceCommitteeCmdWriteFileError
. newExceptT
$ writeLazyByteStringFile vkeyPath
$ textEnvelopeToJSON (Just vkeyDesc) vkey

where
skeyDesc :: TextEnvelopeDescr
skeyDesc = "Constitutional Committee Hot Signing Key"

vkeyDesc :: TextEnvelopeDescr
vkeyDesc = "Constitutional Committee Hot Verification Key"
$ textEnvelopeToJSON (Just Key.ccHotVkeyDesc) vkey

data SomeCommitteeKey f
= ACommitteeHotKey (f CommitteeHotKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,9 @@ runGovernanceDRepKeyGenCmd
, skeyFile
} = do
(vkey, skey) <- liftIO $ generateKeyPair AsDRepKey
newExceptT $ writeLazyByteStringFile skeyFile (textEnvelopeToJSON (Just skeyDesc) skey)
newExceptT $ writeLazyByteStringFile skeyFile (textEnvelopeToJSON (Just Key.drepSkeyDesc) skey)
newExceptT $ writeLazyByteStringFile vkeyFile (textEnvelopeToJSON (Just Key.drepVkeyDesc) vkey)
return (vkey, skey)
where
skeyDesc :: TextEnvelopeDescr
skeyDesc = "Delegate Representative Signing Key"

runGovernanceDRepIdCmd :: ()
=> Cmd.GovernanceDRepIdCmdArgs era
Expand Down
74 changes: 52 additions & 22 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ module Cardano.CLI.EraBased.Run.Key
, runNonExtendedKeyCmd
, runVerificationKeyCmd

, ccColdSkeyDesc
, ccColdVkeyDesc
, ccHotSkeyDesc
, ccHotVkeyDesc
, drepSkeyDesc
, drepVkeyDesc
, genesisVkeyDesc
, genesisVkeyDelegateDesc
Expand Down Expand Up @@ -58,7 +63,31 @@ import qualified Data.Text.Encoding as Text
import System.Exit (exitFailure)

-- Note on these constants:
-- https://github.com/input-output-hk/cardano-cli/pull/416#discussion_r1378789737
-- https://github.com/IntersectMBO/cardano-cli/pull/416#discussion_r1378789737

ccColdSkeyDesc :: TextEnvelopeDescr
ccColdSkeyDesc = "Constitutional Committee Cold Signing Key"

ccColdExtendedSkeyDesc :: TextEnvelopeDescr
ccColdExtendedSkeyDesc = "Constitutional Committee Cold Extended Signing Key"

ccColdVkeyDesc :: TextEnvelopeDescr
ccColdVkeyDesc = "Constitutional Committee Cold Verification Key"

ccHotExtendedSkeyDesc :: TextEnvelopeDescr
ccHotExtendedSkeyDesc = "Constitutional Committee Hot Extended Signing Key"

ccHotSkeyDesc :: TextEnvelopeDescr
ccHotSkeyDesc = "Constitutional Committee Hot Signing Key"

ccHotVkeyDesc :: TextEnvelopeDescr
ccHotVkeyDesc = "Constitutional Committee Hot Verification Key"

drepSkeyDesc :: TextEnvelopeDescr
drepSkeyDesc = "Delegate Representative Signing Key"

drepExtendedSkeyDesc :: TextEnvelopeDescr
drepExtendedSkeyDesc = "Delegate Representative Extended Signing Key"

drepVkeyDesc :: TextEnvelopeDescr
drepVkeyDesc = "Delegate Representative Verification Key"
Expand Down Expand Up @@ -450,8 +479,11 @@ runConvertCardanoAddressKeyCmd
-- signing key.
data SomeCardanoAddressSigningKey
= ACardanoAddrShelleyPaymentSigningKey !(SigningKey PaymentExtendedKey)
| ACardanoAddrShelleyStakeSigningKey !(SigningKey StakeExtendedKey)
| ACardanoAddrByronSigningKey !(SigningKey ByronKey)
| ACardanoAddrShelleyStakeSigningKey !(SigningKey StakeExtendedKey)
| ACardanoAddrByronSigningKey !(SigningKey ByronKey)
| ACardanoAddrCommitteeColdKey !(SigningKey CommitteeColdExtendedKey)
| ACardanoAddrCommitteeHotKey !(SigningKey CommitteeHotExtendedKey)
| ACardanoAddrDRepKey !(SigningKey DRepExtendedKey)

-- | Decode a Bech32-encoded string.
decodeBech32
Expand Down Expand Up @@ -509,29 +541,27 @@ readSomeCardanoAddressSigningKeyFile keyType skFile = do
toSomeCardanoAddressSigningKey :: Crypto.XPrv -> SomeCardanoAddressSigningKey
toSomeCardanoAddressSigningKey xPrv =
case keyType of
CardanoAddressShelleyPaymentKey ->
ACardanoAddrShelleyPaymentSigningKey
(PaymentExtendedSigningKey xPrv)
CardanoAddressShelleyStakeKey ->
ACardanoAddrShelleyStakeSigningKey (StakeExtendedSigningKey xPrv)
CardanoAddressIcarusPaymentKey ->
ACardanoAddrByronSigningKey $
ByronSigningKey (Byron.SigningKey xPrv)
CardanoAddressByronPaymentKey ->
ACardanoAddrByronSigningKey $
ByronSigningKey (Byron.SigningKey xPrv)
CardanoAddressShelleyPaymentKey -> ACardanoAddrShelleyPaymentSigningKey (PaymentExtendedSigningKey xPrv)
CardanoAddressShelleyStakeKey -> ACardanoAddrShelleyStakeSigningKey (StakeExtendedSigningKey xPrv)
CardanoAddressIcarusPaymentKey -> ACardanoAddrByronSigningKey $ ByronSigningKey (Byron.SigningKey xPrv)
CardanoAddressByronPaymentKey -> ACardanoAddrByronSigningKey $ ByronSigningKey (Byron.SigningKey xPrv)
CardanoAddressCommitteeColdKey -> ACardanoAddrCommitteeColdKey (CommitteeColdExtendedSigningKey xPrv)
CardanoAddressCommitteeHotKey -> ACardanoAddrCommitteeHotKey (CommitteeHotExtendedSigningKey xPrv)
CardanoAddressDRepKey -> ACardanoAddrDRepKey (DRepExtendedSigningKey xPrv)

-- | Write a text envelope formatted file containing a @cardano-address@
-- extended signing key, but converted to a format supported by @cardano-cli@.
writeSomeCardanoAddressSigningKeyFile
:: File direction Out
-> SomeCardanoAddressSigningKey
-> IO (Either (FileError ()) ())
writeSomeCardanoAddressSigningKeyFile outFile skey =
case skey of
ACardanoAddrShelleyPaymentSigningKey sk ->
writeLazyByteStringFile outFile $ textEnvelopeToJSON Nothing sk
ACardanoAddrShelleyStakeSigningKey sk ->
writeLazyByteStringFile outFile $ textEnvelopeToJSON Nothing sk
ACardanoAddrByronSigningKey sk ->
writeLazyByteStringFile outFile $ textEnvelopeToJSON Nothing sk
writeSomeCardanoAddressSigningKeyFile outFile =
\case
ACardanoAddrShelleyPaymentSigningKey sk -> go Nothing sk
ACardanoAddrShelleyStakeSigningKey sk -> go Nothing sk
ACardanoAddrByronSigningKey sk -> go Nothing sk
ACardanoAddrCommitteeColdKey sk -> go (Just ccColdExtendedSkeyDesc) sk
ACardanoAddrCommitteeHotKey sk -> go (Just ccHotExtendedSkeyDesc) sk
ACardanoAddrDRepKey sk -> go (Just drepExtendedSkeyDesc) sk
where
go envelope sk = writeLazyByteStringFile outFile $ textEnvelopeToJSON envelope sk
14 changes: 13 additions & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Options/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,19 @@ pKeyConvertCardanoAddressKeyCmd =
pCardanoAddressKeyType :: Parser CardanoAddressKeyType
pCardanoAddressKeyType =
asum
[ Opt.flag' CardanoAddressShelleyPaymentKey $ mconcat
[ Opt.flag' CardanoAddressCommitteeColdKey $ mconcat
[ Opt.long "cc-cold-key"
, Opt.help "Use a committee cold key."
]
, Opt.flag' CardanoAddressCommitteeHotKey $ mconcat
[ Opt.long "cc-hot-key"
, Opt.help "Use a committee hot key."
]
, Opt.flag' CardanoAddressDRepKey $ mconcat
[ Opt.long "drep-key"
, Opt.help "Use a DRep key."
]
, Opt.flag' CardanoAddressShelleyPaymentKey $ mconcat
[ Opt.long "shelley-payment-key"
, Opt.help "Use a Shelley-era extended payment key."
]
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ data CardanoAddressKeyType
| CardanoAddressShelleyStakeKey
| CardanoAddressIcarusPaymentKey
| CardanoAddressByronPaymentKey
| CardanoAddressCommitteeColdKey
| CardanoAddressCommitteeHotKey
| CardanoAddressDRepKey
deriving Show

type OpCertCounterFile = File OpCertCounter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
module Test.Golden.Shelley.Key.ConvertCardanoAddressKey where

import Control.Monad (void)
import Control.Monad.Extra (forM_)
import Data.Text (Text)
import System.FilePath.Posix ((</>))

import qualified Test.Cardano.CLI.Aeson as Aeson
import qualified Test.Cardano.CLI.Util as H
import Test.Cardano.CLI.Util

import Hedgehog (Property)
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.Base as H hiding (noteTempFile)
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Golden as H

Expand Down Expand Up @@ -171,3 +175,35 @@ hprop_golden_convertCardanoAddressShelleyStakeSigningKey =
-- the golden file.
H.diffFileVsGoldenFile convertedSigningKeyFp
"test/cardano-cli-golden/files/golden/shelley/keys/converted_cardano-address_keys/shelley_stake_signing_key"

-- | Test that converting a @cardano-address@ CC/DRep signing key
-- yields the expected result.
-- Execute me with:
-- @cabal test cardano-cli-golden --test-options '-p "/convert cardano address cc drep/"'@
hprop_golden_convert_cardano_address_cc_drep :: Property
hprop_golden_convert_cardano_address_cc_drep = do
let supplyValues =
[ ("cc_cold.key", "--cc-cold-key", "Constitutional Committee Cold")
, ("cc_hot.key", "--cc-hot-key", "Constitutional Committee Hot")
, ("drep.key", "--drep-key", "Delegate Representative")
]

propertyOnce $ forM_ supplyValues $ \(filename, flag, descPrefix) -> H.moduleWorkspace "tmp" $ \tempDir -> do

let outFile = tempDir </> "out.json"

-- `cardano-address` signing key filepath
signingKeyFp <- H.noteInputFile $ "test/cardano-cli-golden/files/input/shelley/convert-cardano-address/" <> filename

-- Convert the `cardano-address` signing key
H.noteShowM_ $ execCardanoCLI
[ "key", "convert-cardano-address-key"
, flag
, "--signing-key-file", signingKeyFp
, "--out-file", outFile
]

H.diffFileVsGoldenFile outFile
("test/cardano-cli-golden/files/golden/shelley/keys/converted_cardano-address_keys/" <> filename)

Aeson.assertHasMappings [("description", descPrefix <> " Extended Signing Key")] outFile
45 changes: 36 additions & 9 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ Usage: cardano-cli shelley key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli shelley key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -1311,7 +1314,10 @@ Usage: cardano-cli allegra key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli allegra key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -2484,7 +2490,10 @@ Usage: cardano-cli mary key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli mary key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -3640,7 +3649,10 @@ Usage: cardano-cli alonzo key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli alonzo key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -4817,7 +4829,10 @@ Usage: cardano-cli babbage key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli babbage key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -6016,7 +6031,10 @@ Usage: cardano-cli conway key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli conway key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -7661,7 +7679,10 @@ Usage: cardano-cli latest key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli latest key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -9853,7 +9874,10 @@ Usage: cardano-cli legacy key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli legacy key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down Expand Up @@ -11065,7 +11089,10 @@ Usage: cardano-cli key convert-itn-bip32-key --itn-signing-key-file FILE
corresponding Shelley stake signing key

Usage: cardano-cli key convert-cardano-address-key
( --shelley-payment-key
( --cc-cold-key
| --cc-hot-key
| --drep-key
| --shelley-payment-key
| --shelley-stake-key
| --icarus-payment-key
| --byron-payment-key
Expand Down
Loading
Loading