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

key non-extended-key: support drep extended keys #377

Merged
merged 3 commits into from
Oct 31, 2023
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
5 changes: 2 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Cardano.Api
import qualified Cardano.Api.Ledger as Ledger
import Cardano.Api.Shelley

import qualified Cardano.CLI.EraBased.Run.Key as Key
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.GovernanceCmdError
import qualified Cardano.Ledger.Shelley.TxBody as Shelley
Expand Down Expand Up @@ -90,10 +91,8 @@ runGovernanceDRepKeyGen _w vkeyPath skeyPath = firstExceptT GovernanceCmdWriteFi
skey <- liftIO $ generateSigningKey AsDRepKey
let vkey = getVerificationKey skey
newExceptT $ writeLazyByteStringFile skeyPath (textEnvelopeToJSON (Just skeyDesc) skey)
newExceptT $ writeLazyByteStringFile vkeyPath (textEnvelopeToJSON (Just vkeyDesc) vkey)
newExceptT $ writeLazyByteStringFile vkeyPath (textEnvelopeToJSON (Just Key.drepKeyEnvelopeDescr) vkey)
where
skeyDesc :: TextEnvelopeDescr
skeyDesc = "Delegate Representative Signing Key"
vkeyDesc :: TextEnvelopeDescr
vkeyDesc = "Delegate Representative Verification Key"

21 changes: 15 additions & 6 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,8 @@ module Cardano.CLI.EraBased.Run.Key
, runNonExtendedKeyCmd
, runVerificationKeyCmd

, drepKeyEnvelopeDescr

-- * Exports for testing
, decodeBech32
) where
Expand Down Expand Up @@ -54,6 +56,9 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import System.Exit (exitFailure)

drepKeyEnvelopeDescr :: TextEnvelopeDescr
drepKeyEnvelopeDescr = "Delegate Representative Verification Key"

runKeyCmds :: ()
=> Cmd.KeyCmds era
-> ExceptT KeyCmdError IO ()
Expand Down Expand Up @@ -110,24 +115,27 @@ runNonExtendedKeyCmd
writeVerificationKey ssk =
case ssk of
APaymentExtendedVerificationKey vk ->
writeToDisk vkf (castVerificationKey vk :: VerificationKey PaymentKey)
writeToDisk vkf Nothing (castVerificationKey vk :: VerificationKey PaymentKey)
ADRepExtendedVerificationKey vk ->
writeToDisk vkf (Just drepKeyEnvelopeDescr) (castVerificationKey vk :: VerificationKey DRepKey)
AStakeExtendedVerificationKey vk ->
writeToDisk vkf (castVerificationKey vk :: VerificationKey StakeKey)
writeToDisk vkf Nothing (castVerificationKey vk :: VerificationKey StakeKey)
AGenesisExtendedVerificationKey vk ->
writeToDisk vkf (castVerificationKey vk :: VerificationKey GenesisKey)
writeToDisk vkf Nothing (castVerificationKey vk :: VerificationKey GenesisKey)
AGenesisDelegateExtendedVerificationKey vk ->
writeToDisk vkf (castVerificationKey vk :: VerificationKey GenesisDelegateKey)
writeToDisk vkf Nothing (castVerificationKey vk :: VerificationKey GenesisDelegateKey)
nonExtendedKey -> left $ KeyCmdExpectedExtendedVerificationKey nonExtendedKey


writeToDisk
:: Key keyrole
=> File content Out
-> Maybe TextEnvelopeDescr
-> VerificationKey keyrole
-> ExceptT KeyCmdError IO ()
writeToDisk vkf' vk =
writeToDisk vkf' descr vk =
firstExceptT KeyCmdWriteFileError . newExceptT
$ writeLazyByteStringFile vkf' $ textEnvelopeToJSON Nothing vk
$ writeLazyByteStringFile vkf' $ textEnvelopeToJSON descr vk


readExtendedVerificationKeyFile
Expand All @@ -139,6 +147,7 @@ readExtendedVerificationKeyFile evkfile = do
$ VktofVerificationKeyFile evkfile
case vKey of
k@APaymentExtendedVerificationKey{} -> return k
k@ADRepExtendedVerificationKey{} -> return k
k@AStakeExtendedVerificationKey{} -> return k
k@AGenesisExtendedVerificationKey{} -> return k
k@AGenesisDelegateExtendedVerificationKey{} -> return k
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,25 @@ hprop_golden_KeyNonExtendedKey_StakeExtendedVerificationKeyShelley =
H.assertFilesExist [outFp]

H.diffFileVsGoldenFile outFp nonExtendedFp

-- | Test that converting a drep extended verification key yields the
-- expected result.
hprop_golden_KeyNonExtendedKey_DRepExtendedVerificationKey :: Property
hprop_golden_KeyNonExtendedKey_DRepExtendedVerificationKey =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
extendedKeyFile <- H.noteInputFile "test/cardano-cli-golden/files/input/key/non-extended-keys/extended-drep.vkey"
goldenFile <- H.note "test/cardano-cli-golden/files/golden/key/non-extended-keys/non-extended-drep.vkey"
outFp <- H.note $ tempDir </> "non-extended-drep.vkey"

H.assertFilesExist [extendedKeyFile]

void $ execCardanoCLI
[ "conway", "key", "non-extended-key"
, "--extended-verification-key-file", extendedKeyFile
, "--verification-key-file", outFp
]

-- Check for existence of the converted signing key file
H.assertFilesExist [outFp]

H.diffFileVsGoldenFile outFp goldenFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "DRepVerificationKey_ed25519",
"description": "Delegate Representative Verification Key",
"cborHex": "5820b0cd9e6e3e274f4f38f55ef274af123cf4600ac0c58294399b7e076175262553"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "DRepExtendedVerificationKey_ed25519_bip32",
"description": "Delegate Representative Verification Key",
"cborHex": "5840b0cd9e6e3e274f4f38f55ef274af123cf4600ac0c58294399b7e076175262553dde8b75f847f2b7e61a8748627292a06d739c8ba8e78ac83e666030d1093fb3e"
}