/
Node.hs
246 lines (207 loc) · 9.37 KB
/
Node.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
module Cardano.CLI.Shelley.Run.Node
( ShelleyNodeCmdError
, renderShelleyNodeCmdError
, runNodeCmd
) where
import Cardano.Api.TextView (TextViewDescription (..))
import Cardano.Api.Typed
import Cardano.CLI.Shelley.Commands
import Cardano.CLI.Shelley.Key (KeyDecodeError, VerificationKeyOrFile,
readSigningKeyFileAnyOf, readVerificationKeyOrFile)
import Cardano.CLI.Types (SigningKeyFile (..), VerificationKeyFile (..))
import Cardano.Prelude
import Control.Monad.Trans.Except (ExceptT)
import Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, newExceptT)
import Prelude (id)
import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as Text
{- HLINT ignore "Reduce duplication" -}
data ShelleyNodeCmdError
= ShelleyNodeCmdReadFileError !(FileError TextEnvelopeError)
| ShelleyNodeCmdReadKeyFileError !(FileError KeyDecodeError)
| ShelleyNodeCmdWriteFileError !(FileError ())
| ShelleyNodeCmdOperationalCertificateIssueError !OperationalCertIssueError
deriving Show
renderShelleyNodeCmdError :: ShelleyNodeCmdError -> Text
renderShelleyNodeCmdError err =
case err of
ShelleyNodeCmdReadFileError fileErr -> Text.pack (displayError fileErr)
ShelleyNodeCmdReadKeyFileError fileErr -> Text.pack (displayError fileErr)
ShelleyNodeCmdWriteFileError fileErr -> Text.pack (displayError fileErr)
ShelleyNodeCmdOperationalCertificateIssueError issueErr ->
Text.pack (displayError issueErr)
runNodeCmd :: NodeCmd -> ExceptT ShelleyNodeCmdError IO ()
runNodeCmd (NodeKeyGenCold vk sk ctr) = runNodeKeyGenCold vk sk ctr
runNodeCmd (NodeKeyGenKES vk sk) = runNodeKeyGenKES vk sk
runNodeCmd (NodeKeyGenVRF vk sk) = runNodeKeyGenVRF vk sk
runNodeCmd (NodeKeyHashVRF vk mOutFp) = runNodeKeyHashVRF vk mOutFp
runNodeCmd (NodeNewCounter vk ctr out) = runNodeNewCounter vk ctr out
runNodeCmd (NodeIssueOpCert vk sk ctr p out) =
runNodeIssueOpCert vk sk ctr p out
--
-- Node command implementations
--
runNodeKeyGenCold :: VerificationKeyFile
-> SigningKeyFile
-> OpCertCounterFile
-> ExceptT ShelleyNodeCmdError IO ()
runNodeKeyGenCold (VerificationKeyFile vkeyPath) (SigningKeyFile skeyPath)
(OpCertCounterFile ocertCtrPath) = do
skey <- liftIO $ generateSigningKey AsStakePoolKey
let vkey = getVerificationKey skey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope skeyPath (Just skeyDesc) skey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope vkeyPath (Just vkeyDesc) vkey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope ocertCtrPath (Just ocertCtrDesc)
$ OperationalCertificateIssueCounter initialCounter vkey
where
skeyDesc, vkeyDesc, ocertCtrDesc :: TextViewDescription
skeyDesc = TextViewDescription "Stake Pool Operator Signing Key"
vkeyDesc = TextViewDescription "Stake Pool Operator Verification Key"
ocertCtrDesc = TextViewDescription $ "Next certificate issue number: " <> BS.pack (show initialCounter)
initialCounter :: Word64
initialCounter = 0
runNodeKeyGenKES :: VerificationKeyFile
-> SigningKeyFile
-> ExceptT ShelleyNodeCmdError IO ()
runNodeKeyGenKES (VerificationKeyFile vkeyPath) (SigningKeyFile skeyPath) = do
skey <- liftIO $ generateSigningKey AsKesKey
let vkey = getVerificationKey skey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope skeyPath (Just skeyDesc) skey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope vkeyPath (Just vkeyDesc) vkey
where
skeyDesc, vkeyDesc :: TextViewDescription
skeyDesc = TextViewDescription "KES Signing Key"
vkeyDesc = TextViewDescription "KES Verification Key"
runNodeKeyGenVRF :: VerificationKeyFile -> SigningKeyFile
-> ExceptT ShelleyNodeCmdError IO ()
runNodeKeyGenVRF (VerificationKeyFile vkeyPath) (SigningKeyFile skeyPath) = do
skey <- liftIO $ generateSigningKey AsVrfKey
let vkey = getVerificationKey skey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope skeyPath (Just skeyDesc) skey
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope vkeyPath (Just vkeyDesc) vkey
where
skeyDesc, vkeyDesc :: TextViewDescription
skeyDesc = TextViewDescription "VRF Signing Key"
vkeyDesc = TextViewDescription "VRF Verification Key"
runNodeKeyHashVRF :: VerificationKeyOrFile VrfKey
-> Maybe OutputFile
-> ExceptT ShelleyNodeCmdError IO ()
runNodeKeyHashVRF verKeyOrFile mOutputFp = do
vkey <- firstExceptT ShelleyNodeCmdReadKeyFileError
. newExceptT
$ readVerificationKeyOrFile AsVrfKey verKeyOrFile
let hexKeyHash = serialiseToRawBytesHex (verificationKeyHash vkey)
case mOutputFp of
Just (OutputFile fpath) -> liftIO $ BS.writeFile fpath hexKeyHash
Nothing -> liftIO $ BS.putStrLn hexKeyHash
runNodeNewCounter :: ColdVerificationKeyOrFile
-> Word
-> OpCertCounterFile
-> ExceptT ShelleyNodeCmdError IO ()
runNodeNewCounter coldVerKeyOrFile counter
(OpCertCounterFile ocertCtrPath) = do
vkey <- firstExceptT ShelleyNodeCmdReadFileError . newExceptT $
readColdVerificationKeyOrFile coldVerKeyOrFile
let ocertIssueCounter =
OperationalCertificateIssueCounter (fromIntegral counter) vkey
firstExceptT ShelleyNodeCmdWriteFileError . newExceptT $
writeFileTextEnvelope ocertCtrPath Nothing ocertIssueCounter
runNodeIssueOpCert :: VerificationKeyOrFile KesKey
-- ^ This is the hot KES verification key.
-> SigningKeyFile
-- ^ This is the cold signing key.
-> OpCertCounterFile
-- ^ Counter that establishes the precedence
-- of the operational certificate.
-> KESPeriod
-- ^ Start of the validity period for this certificate.
-> OutputFile
-> ExceptT ShelleyNodeCmdError IO ()
runNodeIssueOpCert kesVerKeyOrFile
stakePoolSKeyFile
(OpCertCounterFile ocertCtrPath)
kesPeriod
(OutputFile certFile) = do
ocertIssueCounter <- firstExceptT ShelleyNodeCmdReadFileError
. newExceptT
$ readFileTextEnvelope AsOperationalCertificateIssueCounter ocertCtrPath
verKeyKes <- firstExceptT ShelleyNodeCmdReadKeyFileError
. newExceptT
$ readVerificationKeyOrFile AsKesKey kesVerKeyOrFile
signKey <- firstExceptT ShelleyNodeCmdReadKeyFileError
. newExceptT
$ readSigningKeyFileAnyOf
bech32PossibleBlockIssuers
textEnvPossibleBlockIssuers
stakePoolSKeyFile
(ocert, nextOcertCtr) <-
firstExceptT ShelleyNodeCmdOperationalCertificateIssueError
. hoistEither
$ issueOperationalCertificate
verKeyKes
signKey
kesPeriod
ocertIssueCounter
-- Write the counter first, to reduce the chance of ending up with
-- a new cert but without updating the counter.
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope
ocertCtrPath
(Just $ ocertCtrDesc $ getCounter nextOcertCtr)
nextOcertCtr
firstExceptT ShelleyNodeCmdWriteFileError
. newExceptT
$ writeFileTextEnvelope certFile Nothing ocert
where
getCounter :: OperationalCertificateIssueCounter -> Word64
getCounter (OperationalCertificateIssueCounter n _) = n
ocertCtrDesc :: Word64 -> TextViewDescription
ocertCtrDesc n = TextViewDescription $ "Next certificate issue number: " <> BS.pack (show n)
textEnvPossibleBlockIssuers
:: [FromSomeType HasTextEnvelope
(Either (SigningKey StakePoolKey)
(SigningKey GenesisDelegateExtendedKey))]
textEnvPossibleBlockIssuers =
[ FromSomeType (AsSigningKey AsStakePoolKey) Left
, FromSomeType (AsSigningKey AsGenesisDelegateKey) (Left . castSigningKey)
, FromSomeType (AsSigningKey AsGenesisDelegateExtendedKey) Right
]
bech32PossibleBlockIssuers
:: [FromSomeType SerialiseAsBech32
(Either (SigningKey StakePoolKey)
(SigningKey GenesisDelegateExtendedKey))]
bech32PossibleBlockIssuers =
[FromSomeType (AsSigningKey AsStakePoolKey) Left]
-- | Read a cold verification key or file.
--
-- If a filepath is provided, it will be interpreted as a text envelope
-- formatted file.
readColdVerificationKeyOrFile
:: ColdVerificationKeyOrFile
-> IO (Either (FileError TextEnvelopeError) (VerificationKey StakePoolKey))
readColdVerificationKeyOrFile coldVerKeyOrFile =
case coldVerKeyOrFile of
ColdStakePoolVerificationKey vk -> pure (Right vk)
ColdGenesisDelegateVerificationKey vk ->
pure $ Right (castVerificationKey vk)
ColdVerificationKeyFile (VerificationKeyFile fp) ->
readFileTextEnvelopeAnyOf
[ FromSomeType (AsVerificationKey AsStakePoolKey) id
, FromSomeType (AsVerificationKey AsGenesisDelegateKey) castVerificationKey
]
fp