Skip to content

Commit

Permalink
Merge pull request #1031 from input-output-hk/move-gen-hydra-key-to-h…
Browse files Browse the repository at this point in the history
…ydra-node

Move gen-hydra-key to hydra node
  • Loading branch information
v0d1ch authored Aug 21, 2023
2 parents 065c35b + 1a94dcd commit 9754dcc
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 112 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
As a minor extension, we also keep a semantic version for the `UNRELEASED`
changes.

## [0.13.0] - UNRELEASED

- Remove hydra-tools package. Move functionality to generate hydra keys to the
hydra-node executable.

## [0.12.0] - 2023-08-18

- **BREAKING** Support new cardano-node version 8.1.2
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/getting-started/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ docker run --rm ghcr.io/input-output-hk/hydra-node --help

## Using prebuilt static binaries

We provide builds for `hydra-node`, `hydra-tools` and `hydra-tui` as statically
We provide builds for `hydra-node` and `hydra-tui` as statically
linked `x64_86` binaries. These binaries can be found in our CI or attached to
[github releases](https://github.com/input-output-hk/hydra/releases)
starting with version `0.8.1`.
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/getting-started/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ The second set of keys are the so-called Hydra keys, which are used for multi-si

These are similar to cardano keys but are used only in the layer 2. We provide demo key pairs as `alice.{vk,sk}`, `bob.{vk,sk}` and `carol.{vk,sk}` in our [demo folder](https://github.com/input-output-hk/hydra/tree/master/demo).

Alternatively, unique keys can be generated using `hydra-tools`, a command-line utility that's provided as part of Hydra:
Alternatively, unique keys can be generated using `hydra-node`:

```mdx-code-block
<TerminalWindow>
hydra-tools gen-hydra-key --output-file my-key
hydra-node gen-hydra-key --output-file my-key
</TerminalWindow>
```

Expand Down
21 changes: 19 additions & 2 deletions hydra-node/exe/hydra-node/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@ module Main where

import Hydra.Prelude

import Crypto.Random (getRandomBytes)
import Hydra.API.Server (Server (Server, sendOutput), withAPIServer)
import Hydra.API.ServerOutput (ServerOutput (PeerConnected, PeerDisconnected))
import Hydra.Cardano.Api (serialiseToRawBytesHex)
import Hydra.Cardano.Api (
File (..),
Key (SigningKey),
getVerificationKey,
serialiseToRawBytesHex,
writeFileTextEnvelope,
)
import Hydra.Chain.CardanoClient (QueryPoint (..), queryGenesisParameters)
import Hydra.Chain.Direct (initialChainState, loadChainContext, mkTinyWallet, withDirectChain)
import Hydra.Chain.Direct.ScriptRegistry (publishHydraScripts)
import Hydra.Chain.Direct.Util (readKeyPair)
import Hydra.Crypto (HydraKey, generateSigningKey)
import Hydra.HeadLogic (
Environment (..),
Event (..),
Expand Down Expand Up @@ -43,7 +51,8 @@ import Hydra.Node (
import Hydra.Node.EventQueue (EventQueue (..), createEventQueue)
import Hydra.Options (
ChainConfig (..),
Command (Publish, Run),
Command (GenHydraKey, Publish, Run),
GenerateKeyPair (GenerateKeyPair, outputFile),
LedgerConfig (..),
PublishOptions (..),
RunOptions (..),
Expand All @@ -52,6 +61,7 @@ import Hydra.Options (
validateRunOptions,
)
import Hydra.Persistence (createPersistenceIncremental)
import System.FilePath ((<.>))

main :: IO ()
main = do
Expand All @@ -62,6 +72,8 @@ main = do
run (identifyNode options)
Publish options ->
publish options
GenHydraKey outputFile ->
genHydraKeys outputFile
where
run opts = do
let RunOptions{verbosity, monitoringPort, persistenceDir} = opts
Expand Down Expand Up @@ -108,6 +120,11 @@ main = do
txId <- publishHydraScripts networkId publishNodeSocket sk
putStr (decodeUtf8 (serialiseToRawBytesHex txId))

genHydraKeys GenerateKeyPair{outputFile} = do
sk :: SigningKey HydraKey <- generateSigningKey <$> getRandomBytes 16
void $ writeFileTextEnvelope (File (outputFile <.> "sk")) Nothing sk
void $ writeFileTextEnvelope (File (outputFile <.> "vk")) Nothing (getVerificationKey sk)

withNetwork tracer Server{sendOutput} signingKey parties host port peers nodeId =
let localhost = Host{hostname = show host, port}
connectionMessages = \case
Expand Down
72 changes: 0 additions & 72 deletions hydra-node/exe/hydra-tools/Main.hs

This file was deleted.

16 changes: 2 additions & 14 deletions hydra-node/hydra-node.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: hydra-node
version: 0.12.0
version: 0.13.0
synopsis: The Hydra node
author: IOG
copyright: 2022 IOG
Expand Down Expand Up @@ -197,26 +197,14 @@ executable hydra-node
import: project-config
hs-source-dirs: exe/hydra-node
main-is: Main.hs
build-depends:
, hydra-cardano-api
, hydra-node
, hydra-prelude

ghc-options: -threaded -rtsopts -with-rtsopts=-N4

executable hydra-tools
import: project-config
hs-source-dirs: exe/hydra-tools
main-is: Main.hs
build-depends:
, cryptonite
, filepath
, hydra-cardano-api
, hydra-node
, hydra-prelude
, optparse-applicative

ghc-options: -threaded -rtsopts
ghc-options: -threaded -rtsopts -with-rtsopts=-N4

executable hydra-net
import: project-config
Expand Down
28 changes: 28 additions & 0 deletions hydra-node/src/Hydra/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import Options.Applicative (
header,
help,
helper,
hsubparser,
info,
infoOption,
listCompleter,
Expand Down Expand Up @@ -89,13 +90,15 @@ instance Arbitrary ParamMismatch where
data Command
= Run RunOptions
| Publish PublishOptions
| GenHydraKey GenerateKeyPair
deriving (Show, Eq)

commandParser :: Parser Command
commandParser =
asum
[ Run <$> runOptionsParser
, Publish <$> publishScriptsParser
, GenHydraKey <$> genHydraKeyParser
]
where
publishScriptsParser :: Parser PublishOptions
Expand Down Expand Up @@ -213,6 +216,31 @@ runOptionsParser =
<*> chainConfigParser
<*> ledgerConfigParser

newtype GenerateKeyPair = GenerateKeyPair
{ outputFile :: FilePath
}
deriving (Eq, Show)

genHydraKeyParser :: Parser GenerateKeyPair
genHydraKeyParser =
hsubparser
( command
"gen-hydra-key"
( info
(helper <*> (GenerateKeyPair <$> outputFileParser))
(progDesc "Generate a pair of Hydra signing/verification keys (off-chain keys).")
)
)

outputFileParser :: Parser FilePath
outputFileParser =
strOption
( long "output-file"
<> metavar "FILE"
<> value "hydra-key"
<> help "Basename of files to generate key-pair into. Signing key will be suffixed '.sk' and verification key '.vk'"
)

newtype LedgerConfig = CardanoLedgerConfig
{ cardanoLedgerProtocolParametersFile :: FilePath
}
Expand Down
12 changes: 12 additions & 0 deletions hydra-node/test/Hydra/OptionsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import Hydra.Network (Host (Host), NodeId (NodeId))
import Hydra.Options (
ChainConfig (..),
Command (..),
GenerateKeyPair (GenerateKeyPair),
InvalidOptions (..),
LedgerConfig (..),
ParserResult (..),
PublishOptions (..),
RunOptions (..),
defaultChainConfig,
defaultLedgerConfig,
outputFile,
parseHydraCommandFromArgs,
renderFailure,
toArgs,
Expand Down Expand Up @@ -314,6 +316,16 @@ spec = parallel $
, publishNetworkId = Mainnet
, publishSigningKey = "crux"
}
describe "gen-hydra-keys sub-command" $ do
it "should be able to parse gen-hydra-keys sub-command" $
mconcat
[ ["gen-hydra-key"]
, ["--output-file", "foo"]
]
`shouldParse` GenHydraKey GenerateKeyPair{outputFile = "foo"}

it "should parse gen-hydra-keys without the output-file flag using default file name" $
["gen-hydra-key"] `shouldParse` GenHydraKey GenerateKeyPair{outputFile = "hydra-key"}

canRoundtripRunOptionsAndPrettyPrinting :: RunOptions -> Property
canRoundtripRunOptionsAndPrettyPrinting opts =
Expand Down
9 changes: 0 additions & 9 deletions nix/hydra/docker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ in
};
};

hydra-tools = pkgs.dockerTools.buildImage {
name = "hydra-tools";
tag = "latest";
created = "now";
config = {
Entrypoint = [ "${hydraPackages.hydra-tools-static}/bin/hydra-tools" ];
};
};

hydra-tui = pkgs.dockerTools.buildImage {
name = "hydra-tui";
tag = "latest";
Expand Down
2 changes: 0 additions & 2 deletions nix/hydra/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ rec {
"hydra-node"
paddedRevision;

hydra-tools-static = musl64Pkgs.hydra-node.components.exes.hydra-tools;

hydra-tui =
embedRevision
nativePkgs.hydra-tui.components.exes.hydra-tui
Expand Down
10 changes: 5 additions & 5 deletions sample-node-config/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This directory contains some [Terraform](https://www.hashicorp.com/products/terr

### Pre-requisites
- you have access to an aws account with root priviledges.
- you have configured your local aws credentials.
- you have configured your local aws credentials.
for this example in `~/.aws/credentials` we have:
```
[personal]
Expand Down Expand Up @@ -99,11 +99,11 @@ prepare the funds to be marked as fuel.
To get some funds from the preview faucet to your address, you can either:
+ claim them from site:
https://faucet.preview.world.dev.cardano.org/basic-faucet
+ or request them via http:
+ or request them via http:
```sh
$ curl -X POST -s "https://faucet.preview.world.dev.cardano.org/send-money/$(cat credentials/cardano.addr)?api_key="
```

> to request via http you must first obtain your api_key.

Note: From now on we are assuming that the following commands will be executed inside of `sample-node-config/aws`
Expand Down Expand Up @@ -139,7 +139,7 @@ $ fuel
## Configuring Hydra Node
We need to make sure some files, which are not provided by default and which are required for starting the `hydra-node`, are in place at the home folder of your VM:
* A Hydra signing key file `hydra-key.sk` which will be used in the Head to sign snapshots.
This can be generated using [hydra-tools](https://hydra.family/head-protocol/docs/getting-started/quickstart#hydra-keys),
This can be generated using [hydra-node](https://hydra.family/head-protocol/docs/getting-started/quickstart#hydra-keys),
* A cardano signing key file `cardano-key.sk` which is required to identify the parties on-chain and sign transactions.
This is a standard Cardano key so one can reuse an existing key or [generate a new one](https://hydra.family/head-protocol/docs/getting-started/quickstart#cardano-keys),
* 0 or more hydra verification keys and cardano verification keys for the other Head parties,
Expand Down Expand Up @@ -174,7 +174,7 @@ Finally, execute the hydra-tui and open the head:
$ tui
```

> If you take down your hydra-node instance once the head is open. you will loose access to your funds commited to the head.
> If you take down your hydra-node instance once the head is open. you will loose access to your funds commited to the head.
To get them back, currently you need to start the head from a point time in the past.
For that you must run hydra-node the using parameter `--start-chain-from`.
i.e.: --start-chain-from 2730515.c7a3629911ef004c873ef07313842df5d1331f61e0eb632432ac8c0636dfd391
Expand Down
8 changes: 4 additions & 4 deletions sample-node-config/aws/setup/credentials.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ test -d $DIR || mkdir $DIR

echo "building personal keys"

test -f $DIR/hydra-key.sk ||
cabal run hydra-tools gen-hydra-key
test -f $DIR/hydra-key.sk ||
cabal run hydra-node gen-hydra-key

test -f hydra-key.sk &&
mv hydra-key.sk $DIR

test -f hydra-key.vk &&
mv hydra-key.vk $DIR

test -f $DIR/cardano-key.sk ||
test -f $DIR/cardano-key.sk ||
cardano-cli address key-gen \
--signing-key-file "$DIR/cardano-key.sk" \
--verification-key-file "$DIR/cardano-key.vk"
Expand All @@ -36,7 +36,7 @@ MEMBERS=$(echo $TEAM_JSON | jq keys | jq -r '.[]')
for member in $MEMBERS; do
echo "Processing peer: $member"
MEMBER_JSON=$(echo $TEAM_JSON | jq ".$member")

cardanoVK=$(echo $MEMBER_JSON | jq '.["cardano-vk"]')
cat << EOF >./$DIR/$member.cardano.vk
{
Expand Down
2 changes: 1 addition & 1 deletion sample-node-config/gcp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ $ terraform plan -out vm.plan

The configuration process expects to find some files which are not provided by default and which are required for starting the Hydra node:
* A Hydra signing key file `keys/arnaud-hydra.sk` which will be used in the Head to sign snapshots.
This can be generated using [hydra-tools](https://hydra.family/head-protocol/docs/getting-started/quickstart#hydra-keys),
This can be generated using [hydra-node](https://hydra.family/head-protocol/docs/getting-started/quickstart#hydra-keys),
* A cardano signing key file `keys/arnaud.sk` which is required to identify the parties on-chain and sign transactions.
This is a standard Cardano key so one can reuse an existing key or [generate a new one](https://hydra.family/head-protocol/docs/getting-started/quickstart#cardano-keys),
* 0 or more hydra verification keys and cardano verification keys for the other Head parties,
Expand Down

0 comments on commit 9754dcc

Please sign in to comment.