Skip to content

Commit b112599

Browse files
author
Arnaud Bailly
authored
Merge pull request #1139 from input-output-hk/gen-hydra-key-should-not-overwrite
gen-hydra-key should not overwrite key if it exists
2 parents 544a71c + 5395e13 commit b112599

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ changes.
1010

1111
## [0.14.0] - UNRELEASED
1212

13+
- Improved `gen-hydra-keys` command to not overwrite keys if they are present
14+
already.
15+
1316
- Hydra node API `submit-transaction` endpoint now accepts three types of
1417
encoding: Base16 encoded CBOR string, TextEnvelope type and JSON.
1518

hydra-node/src/Hydra/Utils.hs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
module Hydra.Utils where
22

33
import Crypto.Random (getRandomBytes)
4-
import Hydra.Cardano.Api (File (..), FileError, Key (SigningKey), getVerificationKey, writeFileTextEnvelope)
4+
import GHC.IO.Exception (userError)
5+
import Hydra.Cardano.Api (File (..), FileError (FileIOError), Key (SigningKey), getVerificationKey, writeFileTextEnvelope)
56
import Hydra.Crypto (HydraKey, generateSigningKey)
67
import Hydra.Options (GenerateKeyPair (..))
78
import Hydra.Prelude
9+
import System.Directory (doesFileExist)
810
import System.FilePath ((<.>))
911

1012
genHydraKeys :: GenerateKeyPair -> IO (Either (FileError ()) ())
1113
genHydraKeys GenerateKeyPair{outputFile} = do
12-
sk :: SigningKey HydraKey <- generateSigningKey <$> getRandomBytes 16
13-
runExceptT $ do
14-
ExceptT $ writeFileTextEnvelope (File (outputFile <.> "sk")) Nothing sk
15-
ExceptT $ writeFileTextEnvelope (File (outputFile <.> "vk")) Nothing (getVerificationKey sk)
14+
fileExists <- doesFileExist outputFile
15+
if fileExists
16+
then
17+
pure $
18+
Left $
19+
FileIOError
20+
outputFile
21+
(userError "File already exists! Please remove it in order to generate new hydra keys.")
22+
else do
23+
sk :: SigningKey HydraKey <- generateSigningKey <$> getRandomBytes 16
24+
runExceptT $ do
25+
ExceptT $ writeFileTextEnvelope (File (outputFile <.> "sk")) Nothing sk
26+
ExceptT $ writeFileTextEnvelope (File (outputFile <.> "vk")) Nothing (getVerificationKey sk)

hydra-node/test/Hydra/UtilsSpec.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Hydra.Cardano.Api (FileError)
44
import Hydra.Options (GenerateKeyPair (GenerateKeyPair))
55
import Hydra.Prelude
66
import Hydra.Utils (genHydraKeys)
7+
import System.FilePath ((</>))
78
import Test.Hydra.Prelude
89

910
spec :: Spec
@@ -13,3 +14,11 @@ spec = do
1314
case result of
1415
Left (_ :: FileError e) -> pure ()
1516
Right _ -> expectationFailure "getHydraKeys should have failed with FileError"
17+
18+
it "Should throw if the file already exists" $
19+
withTempDir "gen-hydra-keys" $ \tmp -> do
20+
writeFile (tmp </> "hydra") "hydra key"
21+
result <- genHydraKeys (GenerateKeyPair $ tmp </> "hydra")
22+
case result of
23+
Left (_ :: FileError e) -> pure ()
24+
Right _ -> expectationFailure "getHydraKeys should have failed with FileError"

0 commit comments

Comments
 (0)