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

Fix key non extended key for StakeExtendedVerificationKeyShelley_ed25519_bip32 envelope #4918

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
1 change: 1 addition & 0 deletions cardano-api/src/Cardano/Api/DeserialiseAnyOf.hs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ deserialiseAnyVerificationKeyTextEnvelope bs =
[ FromSomeType (AsVerificationKey AsByronKey) AByronVerificationKey
, FromSomeType (AsVerificationKey AsPaymentKey) APaymentVerificationKey
, FromSomeType (AsVerificationKey AsPaymentExtendedKey) APaymentExtendedVerificationKey
, FromSomeType (AsVerificationKey AsStakeExtendedKey) AStakeExtendedVerificationKey
, FromSomeType (AsVerificationKey AsGenesisUTxOKey) AGenesisUTxOVerificationKey
, FromSomeType (AsVerificationKey AsGenesisExtendedKey) AGenesisExtendedVerificationKey
]
Expand Down
7 changes: 6 additions & 1 deletion cardano-cli/test/Test/Golden/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ keyTests :: IO Bool
keyTests =
H.checkSequential
$ H.Group "Key command group"
[ ("golden_KeyNonExtendedKey", Test.Golden.Key.NonExtendedKey.golden_KeyNonExtendedKey)
[ ( "golden_KeyNonExtendedKey_GenesisExtendedVerificationKey"
, Test.Golden.Key.NonExtendedKey.golden_KeyNonExtendedKey_GenesisExtendedVerificationKey
)
, ( "golden_KeyNonExtendedKey_StakeExtendedVerificationKeyShelley"
, Test.Golden.Key.NonExtendedKey.golden_KeyNonExtendedKey_StakeExtendedVerificationKeyShelley
)
]
41 changes: 32 additions & 9 deletions cardano-cli/test/Test/Golden/Key/NonExtendedKey.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{-# LANGUAGE OverloadedStrings #-}

module Test.Golden.Key.NonExtendedKey
( golden_KeyNonExtendedKey
( golden_KeyNonExtendedKey_GenesisExtendedVerificationKey
, golden_KeyNonExtendedKey_StakeExtendedVerificationKeyShelley
) where

import Control.Monad (void)
Expand All @@ -10,30 +11,52 @@ import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import System.FilePath ((</>))
import Test.OptParse (execCardanoCLI, propertyOnce)
import Test.Utilities (diffVsGoldenFile)
import Test.Utilities (diffFileVsGoldenFile)

{- HLINT ignore "Use camelCase" -}

-- | Test that converting a @cardano-address@ Byron signing key yields the
-- expected result.
golden_KeyNonExtendedKey :: Property
golden_KeyNonExtendedKey =
golden_KeyNonExtendedKey_GenesisExtendedVerificationKey :: Property
golden_KeyNonExtendedKey_GenesisExtendedVerificationKey =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
genesisVKeyFp <- H.note "test/data/golden/key/non-extended-keys/shelley.000.vkey"
nonExtendedGenesisVKeyFp <- H.note $ tempDir </> "non-extended-shelley.000.vkey"
nonExtendedFp <- H.note "test/data/golden/key/non-extended-keys/non-extended-shelley.000.vkey"
outFp <- H.note $ tempDir </> "non-extended-shelley.000.vkey"

H.assertFilesExist [genesisVKeyFp]

-- Convert the `cardano-address` signing key
void $ execCardanoCLI
[ "key", "non-extended-key"
, "--extended-verification-key-file", genesisVKeyFp
, "--verification-key-file", nonExtendedGenesisVKeyFp
, "--verification-key-file", outFp
]

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

contents <- H.readFile nonExtendedGenesisVKeyFp
diffFileVsGoldenFile outFp nonExtendedFp

diffVsGoldenFile contents nonExtendedGenesisVKeyFp
-- | Test that converting a @cardano-address@ Byron signing key yields the
-- expected result.
golden_KeyNonExtendedKey_StakeExtendedVerificationKeyShelley :: Property
golden_KeyNonExtendedKey_StakeExtendedVerificationKeyShelley =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
genesisVKeyFp <- H.note "test/data/golden/key/non-extended-keys/stake.000.vkey"
nonExtendedFp <- H.note "test/data/golden/key/non-extended-keys/non-extended-stake.000.vkey"
outFp <- H.note $ tempDir </> "non-extended-stake.000.vkey"

H.assertFilesExist [genesisVKeyFp]

-- Convert the `cardano-address` signing key
void $ execCardanoCLI
[ "key", "non-extended-key"
, "--extended-verification-key-file", genesisVKeyFp
, "--verification-key-file", outFp
]

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

diffFileVsGoldenFile outFp nonExtendedFp
84 changes: 72 additions & 12 deletions cardano-cli/test/Test/Utilities.hs
Original file line number Diff line number Diff line change
@@ -1,27 +1,87 @@
module Test.Utilities (diffVsGoldenFile) where
module Test.Utilities
( diffVsGoldenFile,
diffFileVsGoldenFile,
) where

import Cardano.Prelude (ConvertText (..))
import Cardano.Prelude (ConvertText (..), HasCallStack)

import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Algorithm.Diff (PolyDiff (Both), getGroupedDiff)
import Data.Algorithm.DiffOutput (ppDiff)
import GHC.Stack (callStack)
import qualified GHC.Stack as GHC
import Hedgehog (MonadTest)
import qualified Hedgehog.Extras.Test as H
import Hedgehog.Extras.Test.Base (failMessage)
import qualified Hedgehog.Internal.Property as H
import qualified System.Directory as IO
import qualified System.Environment as IO
import qualified System.IO.Unsafe as IO

-- | Whether the test should create the golden files if the file does ont exist.
Jimbo4350 marked this conversation as resolved.
Show resolved Hide resolved
createFiles :: Bool
createFiles = IO.unsafePerformIO $ do
value <- IO.lookupEnv "CREATE_GOLDEN_FILES"
return $ value == Just "1"

-- | Diff contents against the golden file. If CREATE_GOLDEN_FILES environment is
-- set to "1", then should the gold file not exist it would be created.
--
-- Set the environment variable when you intend to generate or re-generate the golden
-- file for example when running the test for the first time or if the golden file
-- genuinely needs to change.
--
-- To re-generate a golden file you must also delete the golden file because golden
-- files are never overwritten.
--
-- TODO: Improve the help output by saying the difference of
-- each input.
diffVsGoldenFile
:: (MonadIO m, MonadTest m)
=> String -- ^ actual content
-> FilePath -- ^ reference file
:: HasCallStack
=> (MonadIO m, MonadTest m)
=> String -- ^ Actual content
-> FilePath -- ^ Reference file
-> m ()
diffVsGoldenFile actualContent referenceFile =
do
referenceLines <- map toS . lines <$> liftIO (readFile referenceFile)
let difference = getGroupedDiff actualLines referenceLines
case difference of
[Both{}] -> pure ()
_ -> failMessage callStack $ ppDiff difference
diffVsGoldenFile actualContent referenceFile = GHC.withFrozenCallStack $ do
fileExists <- liftIO $ IO.doesFileExist referenceFile

if fileExists
then do
referenceLines <- map toS . lines <$> H.readFile referenceFile
let difference = getGroupedDiff actualLines referenceLines
case difference of
[Both{}] -> pure ()
_ -> failMessage callStack $ ppDiff difference
else if createFiles
then do
-- CREATE_GOLDEN_FILES is set, so we create any golden files that don't
-- already exist.
H.note_ $ "Creating golden file " <> referenceFile
H.writeFile referenceFile actualContent
newhoggy marked this conversation as resolved.
Show resolved Hide resolved
else do
H.note_ $ mconcat
[ "Golden file " <> referenceFile
, " does not exist. To create, run with CREATE_GOLDEN_FILES=1"
]
H.failure
where
actualLines = Prelude.lines actualContent

-- | Diff file against the golden file. If CREATE_GOLDEN_FILES environment is
-- set to "1", then should the gold file not exist it would be created.
--
-- Set the environment variable when you intend to generate or re-generate the golden
-- file for example when running the test for the first time or if the golden file
-- genuinely needs to change.
--
-- To re-generate a golden file you must also delete the golden file because golden
-- files are never overwritten.
diffFileVsGoldenFile
:: HasCallStack
=> (MonadIO m, MonadTest m)
=> FilePath -- ^ Actual file
-> FilePath -- ^ Reference file
-> m ()
diffFileVsGoldenFile actualFile referenceFile = GHC.withFrozenCallStack $ do
contents <- H.readFile actualFile
diffVsGoldenFile contents referenceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "GenesisVerificationKey_ed25519",
"description": "",
"cborHex": "58200834b58f4bdda9522bb202af1f546db4cbbd94b068ae72c9fd96d9b55279edf0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "StakeVerificationKeyShelley_ed25519",
"description": "",
"cborHex": "58200f205175c0a47cba409c328f066e31ea4e81ef211f539c12b64b4b14e1d87188"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "StakeExtendedVerificationKeyShelley_ed25519_bip32",
"description": "",
"cborHex": "58400f205175c0a47cba409c328f066e31ea4e81ef211f539c12b64b4b14e1d87188a54f03c3edad073428f37dbdad714b7c07371ca19fe66c72d41fda23a81d8309"
}