-
Notifications
You must be signed in to change notification settings - Fork 15
/
Common.hs
3706 lines (3350 loc) · 120 KB
/
Common.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{- HLINT ignore "Move brackets to avoid $" -}
{- HLINT ignore "Use <$>" -}
{- HLINT ignore "Move brackets to avoid $" -}
module Cardano.CLI.EraBased.Options.Common where
import Cardano.Api
import qualified Cardano.Api.Ledger as L
import Cardano.Api.Shelley
import Cardano.CLI.Environment (EnvCli (..), envCliAnyEon)
import Cardano.CLI.Parser
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Governance
import Cardano.CLI.Types.Key
import Cardano.CLI.Types.Key.VerificationKey
import qualified Ouroboros.Network.Protocol.LocalStateQuery.Type as Consensus
import Control.Monad (void, when)
import qualified Data.Aeson as Aeson
import Data.Bifunctor
import Data.Bits (Bits, toIntegralSized)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Base16 as B16
import qualified Data.ByteString.Char8 as BSC
import Data.Data (Proxy (..), Typeable, typeRep)
import Data.Foldable
import Data.Functor (($>))
import qualified Data.IP as IP
import Data.List.NonEmpty (NonEmpty)
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Time.Clock (UTCTime)
import Data.Time.Format (defaultTimeLocale, parseTimeOrError)
import Data.Word
import GHC.Exts (IsList (..))
import GHC.Natural (Natural)
import Network.Socket (PortNumber)
import Options.Applicative hiding (help, str)
import qualified Options.Applicative as Opt
import qualified Text.Parsec as Parsec
import Text.Parsec ((<?>))
import qualified Text.Parsec.Error as Parsec
import qualified Text.Parsec.Language as Parsec
import qualified Text.Parsec.String as Parsec
import qualified Text.Parsec.Token as Parsec
import qualified Text.Read as Read
import Text.Read (readEither, readMaybe)
command' :: String -> String -> Parser a -> Mod CommandFields a
command' c descr p =
mconcat
[ command c (info (p <**> helper) $ mconcat [progDesc descr])
, metavar c
]
-- | @prefixFlag Nothing bar@ is @bar@, while @prefixFlag (Just "foo") bar@ is @foo-bar@.
-- This function is used to optionally prefix some long flags
prefixFlag :: Maybe String -> String -> String
prefixFlag prefix longFlag =
case prefix of
Nothing -> longFlag
Just prefix' -> prefix' <> "-" <> longFlag
bounded :: forall a. (Bounded a, Integral a, Show a) => String -> Opt.ReadM a
bounded t = Opt.eitherReader $ \s -> do
i <- Read.readEither @Integer s
when (i < fromIntegral (minBound @a)) $ Left $ t <> " must not be less than " <> show (minBound @a)
when (i > fromIntegral (maxBound @a)) $ Left $ t <> " must not greater than " <> show (maxBound @a)
pure (fromIntegral i)
parseFilePath :: String -> String -> Opt.Parser FilePath
parseFilePath optname desc =
Opt.strOption
( Opt.long optname
<> Opt.metavar "FILEPATH"
<> Opt.help desc
<> Opt.completer (Opt.bashCompleter "file")
)
pNetworkIdDeprecated :: Parser NetworkId
pNetworkIdDeprecated =
asum
[ Opt.flag' Mainnet $
mconcat
[ Opt.long "mainnet"
, Opt.help "DEPRECATED. This argument has no effect."
]
, fmap (Testnet . NetworkMagic) $
Opt.option (bounded "TESTNET_MAGIC") $
mconcat
[ Opt.long "testnet-magic"
, Opt.metavar "NATURAL"
, Opt.help "DEPRECATED. This argument has no effect."
]
]
pNetworkId :: EnvCli -> Parser NetworkId
pNetworkId envCli =
asum $
mconcat
[
[ Opt.flag' Mainnet $
mconcat
[ Opt.long "mainnet"
, Opt.help $
mconcat
[ "Use the mainnet magic id. This overrides the CARDANO_NODE_NETWORK_ID "
, "environment variable"
]
]
, fmap (Testnet . NetworkMagic) $
Opt.option (bounded "TESTNET_MAGIC") $
mconcat
[ Opt.long "testnet-magic"
, Opt.metavar "NATURAL"
, Opt.help $
mconcat
[ "Specify a testnet magic id. This overrides the CARDANO_NODE_NETWORK_ID "
, "environment variable"
]
]
]
, -- Default to the network id specified by the environment variable if it is available.
pure <$> maybeToList (envCliNetworkId envCli)
]
pTarget :: ShelleyBasedEra era -> Parser (Consensus.Target ChainPoint)
pTarget sbe =
maybe (pure Consensus.VolatileTip) pTargetFromConway (forShelleyBasedEraMaybeEon sbe)
where
pTargetFromConway :: ConwayEraOnwards era -> Parser (Consensus.Target ChainPoint)
pTargetFromConway _ =
asum $
mconcat
[
[ Opt.flag' Consensus.VolatileTip $
mconcat
[ Opt.long "volatile-tip"
, Opt.help $
mconcat
[ "Use the volatile tip as a target. (This is the default)"
]
]
, Opt.flag' Consensus.ImmutableTip $
mconcat
[ Opt.long "immutable-tip"
, Opt.help $
mconcat
[ "Use the immutable tip as a target."
]
]
]
, -- Default to volatile tip if not specified
[pure Consensus.VolatileTip]
]
toUnitIntervalOrErr :: Rational -> L.UnitInterval
toUnitIntervalOrErr r = case L.boundRational r of
Nothing ->
error $
mconcat
[ "toUnitIntervalOrErr: "
, "rational out of bounds " <> show r
]
Just n -> n
pConsensusModeParams :: Parser ConsensusModeParams
pConsensusModeParams =
asum
[ pCardanoMode *> pCardanoConsensusMode
, pDefaultConsensusMode
]
where
pCardanoMode :: Parser ()
pCardanoMode =
Opt.flag' () $
mconcat
[ Opt.long "cardano-mode"
, Opt.help "For talking to a node running in full Cardano mode (default)."
]
pCardanoConsensusMode :: Parser ConsensusModeParams
pCardanoConsensusMode = CardanoModeParams <$> pEpochSlots
pDefaultConsensusMode :: Parser ConsensusModeParams
pDefaultConsensusMode =
pure . CardanoModeParams $ EpochSlots defaultByronEpochSlots
defaultByronEpochSlots :: Word64
defaultByronEpochSlots = 21600
pEpochSlots :: Parser EpochSlots
pEpochSlots =
fmap EpochSlots $
Opt.option (bounded "SLOTS") $
mconcat
[ Opt.long "epoch-slots"
, Opt.metavar "SLOTS"
, Opt.help "The number of slots per epoch for the Byron era."
, Opt.value defaultByronEpochSlots -- Default to the mainnet value.
, Opt.showDefault
]
pSocketPath :: EnvCli -> Parser SocketPath
pSocketPath envCli =
asum $
mconcat
[
[ fmap File $
Opt.strOption $
mconcat
[ Opt.long "socket-path"
, Opt.metavar "SOCKET_PATH"
, Opt.help $
mconcat
[ "Path to the node socket. This overrides the CARDANO_NODE_SOCKET_PATH "
, "environment variable. The argument is optional if CARDANO_NODE_SOCKET_PATH "
, "is defined and mandatory otherwise."
]
, Opt.completer (Opt.bashCompleter "file")
]
]
, -- Default to the socket path specified by the environment variable if it is available.
pure . File <$> maybeToList (envCliSocketPath envCli)
]
readerFromParsecParser :: Parsec.Parser a -> Opt.ReadM a
readerFromParsecParser p =
Opt.eitherReader (first formatError . Parsec.parse (p <* Parsec.eof) "")
where
formatError err =
Parsec.showErrorMessages
"or"
"unknown parse error"
"expecting"
"unexpected"
"end of input"
(Parsec.errorMessages err)
parseTxIn :: Parsec.Parser TxIn
parseTxIn = TxIn <$> parseTxId <*> (Parsec.char '#' *> parseTxIx)
parseTxId :: Parsec.Parser TxId
parseTxId = do
str' <- some Parsec.hexDigit <?> "transaction id (hexadecimal)"
case deserialiseFromRawBytesHex AsTxId (BSC.pack str') of
Right addr -> return addr
Left e -> fail $ docToString $ "Incorrect transaction id format: " <> prettyError e
parseTxIx :: Parsec.Parser TxIx
parseTxIx = TxIx . fromIntegral <$> decimal
decimal :: Parsec.Parser Integer
Parsec.TokenParser{Parsec.decimal = decimal} = Parsec.haskell
pStakeIdentifier :: Maybe String -> Parser StakeIdentifier
pStakeIdentifier prefix =
asum
[ StakeIdentifierVerifier <$> pStakeVerifier prefix
, StakeIdentifierAddress <$> pStakeAddress prefix
]
pStakeVerifier :: Maybe String -> Parser StakeVerifier
pStakeVerifier prefix =
asum
[ StakeVerifierKey <$> pStakeVerificationKeyOrHashOrFile prefix
, StakeVerifierScriptFile
<$> pScriptFor (prefixFlag prefix "stake-script-file") Nothing "Filepath of the staking script."
]
pStakeAddress :: Maybe String -> Parser StakeAddress
pStakeAddress prefix =
Opt.option (readerFromParsecParser parseStakeAddress) $
mconcat
[ Opt.long $ prefixFlag prefix "stake-address"
, Opt.metavar "ADDRESS"
, Opt.help "Target stake address (bech32 format)."
]
parseStakeAddress :: Parsec.Parser StakeAddress
parseStakeAddress = do
str' <- lexPlausibleAddressString
case deserialiseAddress AsStakeAddress str' of
Nothing -> fail $ "invalid address: " <> Text.unpack str'
Just addr -> pure addr
-- | First argument is the optional prefix
pStakeVerificationKeyOrFile :: Maybe String -> Parser (VerificationKeyOrFile StakeKey)
pStakeVerificationKeyOrFile prefix =
VerificationKeyValue
<$> pStakeVerificationKey prefix
<|> VerificationKeyFilePath
<$> pStakeVerificationKeyFile prefix
pScriptFor :: String -> Maybe String -> String -> Parser ScriptFile
pScriptFor name Nothing help' =
fmap File $ parseFilePath name help'
pScriptFor name (Just deprecated) help' =
pScriptFor name Nothing help'
<|> File
<$> Opt.strOption
( Opt.long deprecated
<> Opt.internal
)
-- | The first argument is the optional prefix.
pStakeVerificationKey :: Maybe String -> Parser (VerificationKey StakeKey)
pStakeVerificationKey prefix =
Opt.option (readVerificationKey AsStakeKey) $
mconcat
[ Opt.long $ prefixFlag prefix "stake-verification-key"
, Opt.metavar "STRING"
, Opt.help "Stake verification key (Bech32 or hex-encoded)."
]
-- | Read a Bech32 or hex-encoded verification key.
readVerificationKey
:: forall keyrole
. SerialiseAsBech32 (VerificationKey keyrole)
=> AsType keyrole
-> Opt.ReadM (VerificationKey keyrole)
readVerificationKey asType =
Opt.eitherReader deserialiseFromBech32OrHex
where
keyFormats :: NonEmpty (InputFormat (VerificationKey keyrole))
keyFormats = fromList [InputFormatBech32, InputFormatHex]
deserialiseFromBech32OrHex
:: String
-> Either String (VerificationKey keyrole)
deserialiseFromBech32OrHex str' =
first (docToString . renderInputDecodeError) $
deserialiseInput (AsVerificationKey asType) keyFormats (BSC.pack str')
-- | The first argument is the optional prefix.
pStakeVerificationKeyFile :: Maybe String -> Parser (VerificationKeyFile In)
pStakeVerificationKeyFile prefix =
File
<$> asum
[ parseFilePath
(prefixFlag prefix "stake-verification-key-file")
"Filepath of the staking verification key."
, Opt.strOption $
mconcat
[ Opt.long $ prefixFlag prefix "staking-verification-key-file"
, Opt.internal
]
]
subInfoParser :: String -> InfoMod a -> [Maybe (Parser a)] -> Maybe (Parser a)
subInfoParser name i mps = case catMaybes mps of
[] -> Nothing
parsers -> Just $ subParser name $ Opt.info (asum parsers) i
pAnyShelleyBasedEra :: EnvCli -> Parser (EraInEon ShelleyBasedEra)
pAnyShelleyBasedEra envCli =
asum $
mconcat
[
[ Opt.flag' (EraInEon ShelleyBasedEraShelley) $
mconcat [Opt.long "shelley-era", Opt.help $ "Specify the Shelley era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyBasedEraAllegra) $
mconcat [Opt.long "allegra-era", Opt.help $ "Specify the Allegra era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyBasedEraMary) $
mconcat [Opt.long "mary-era", Opt.help $ "Specify the Mary era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyBasedEraAlonzo) $
mconcat [Opt.long "alonzo-era", Opt.help $ "Specify the Alonzo era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyBasedEraBabbage) $
mconcat [Opt.long "babbage-era", Opt.help $ "Specify the Babbage era (default)" <> deprecationText]
, Opt.flag' (EraInEon ShelleyBasedEraConway) $
mconcat [Opt.long "conway-era", Opt.help "Specify the Conway era"]
]
, maybeToList $ pure <$> envCliAnyEon envCli
, pure $ pure $ EraInEon ShelleyBasedEraBabbage
]
deprecationText :: String
deprecationText = " - DEPRECATED - will be removed in the future"
pAnyShelleyToBabbageEra :: EnvCli -> Parser (EraInEon ShelleyToBabbageEra)
pAnyShelleyToBabbageEra envCli =
asum $
mconcat
[
[ Opt.flag' (EraInEon ShelleyToBabbageEraShelley) $
mconcat [Opt.long "shelley-era", Opt.help $ "Specify the Shelley era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyToBabbageEraAllegra) $
mconcat [Opt.long "allegra-era", Opt.help $ "Specify the Allegra era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyToBabbageEraMary) $
mconcat [Opt.long "mary-era", Opt.help $ "Specify the Mary era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyToBabbageEraAlonzo) $
mconcat [Opt.long "alonzo-era", Opt.help $ "Specify the Alonzo era" <> deprecationText]
, Opt.flag' (EraInEon ShelleyToBabbageEraBabbage) $
mconcat [Opt.long "babbage-era", Opt.help $ "Specify the Babbage era (default)" <> deprecationText]
]
, maybeToList $ pure <$> envCliAnyEon envCli
, pure . pure $ EraInEon ShelleyToBabbageEraBabbage
]
pFileOutDirection :: String -> String -> Parser (File a Out)
pFileOutDirection l h = File <$> parseFilePath l h
pFileInDirection :: String -> String -> Parser (File a In)
pFileInDirection l h = File <$> parseFilePath l h
parseLovelace :: Parsec.Parser Lovelace
parseLovelace = do
i <- decimal
if i > toInteger (maxBound :: Word64)
then fail $ show i <> " lovelace exceeds the Word64 upper bound"
else return $ L.Coin i
-- | The first argument is the optional prefix.
pStakePoolVerificationKeyOrFile :: Maybe String -> Parser (VerificationKeyOrFile StakePoolKey)
pStakePoolVerificationKeyOrFile prefix =
asum
[ VerificationKeyValue <$> pStakePoolVerificationKey prefix
, VerificationKeyFilePath <$> pStakePoolVerificationKeyFile prefix
]
-- | The first argument is the optional prefix.
pStakePoolVerificationKey :: Maybe String -> Parser (VerificationKey StakePoolKey)
pStakePoolVerificationKey prefix =
Opt.option (readVerificationKey AsStakePoolKey) $
mconcat
[ Opt.long $ prefixFlag prefix "stake-pool-verification-key"
, Opt.metavar "STRING"
, Opt.help "Stake pool verification key (Bech32 or hex-encoded)."
]
-- | The first argument is the optional prefix.
pStakePoolVerificationKeyFile :: Maybe String -> Parser (VerificationKeyFile In)
pStakePoolVerificationKeyFile prefix =
File
<$> asum
[ parseFilePath "cold-verification-key-file" "Filepath of the stake pool verification key."
, Opt.strOption $
mconcat
[ Opt.long $ prefixFlag prefix "stake-pool-verification-key-file"
, Opt.internal
]
]
pOutputFile :: Parser (File content Out)
pOutputFile = File <$> parseFilePath "out-file" "The output file."
pMIRPot :: Parser L.MIRPot
pMIRPot =
asum
[ Opt.flag' L.ReservesMIR $
mconcat
[ Opt.long "reserves"
, Opt.help "Use the reserves pot."
]
, Opt.flag' L.TreasuryMIR $
mconcat
[ Opt.long "treasury"
, Opt.help "Use the treasury pot."
]
]
pRewardAmt :: Parser Lovelace
pRewardAmt =
Opt.option (readerFromParsecParser parseLovelace) $
mconcat
[ Opt.long "reward"
, Opt.metavar "LOVELACE"
, Opt.help "The reward for the relevant reward account."
]
pTransferAmt :: Parser Lovelace
pTransferAmt =
Opt.option (readerFromParsecParser parseLovelace) $
mconcat
[ Opt.long "transfer"
, Opt.metavar "LOVELACE"
, Opt.help "The amount to transfer."
]
pTreasuryWithdrawalAmt :: Parser Lovelace
pTreasuryWithdrawalAmt =
Opt.option (readerFromParsecParser parseLovelace) $
mconcat
[ Opt.long "transfer"
, Opt.metavar "LOVELACE"
, Opt.help $
mconcat
[ "The amount of lovelace the proposal intends to withdraw from the Treasury. "
, "Multiple withdrawals can be proposed in a single governance action "
, "by repeating the --funds-receiving-stake and --transfer options as many times as needed."
]
]
rHexHash
:: ()
=> SerialiseAsRawBytes (Hash a)
=> AsType a
-> Maybe String
-- ^ Optional prefix to the error message
-> ReadM (Hash a)
rHexHash a mErrPrefix =
Opt.eitherReader $
first (\e -> errPrefix <> (docToString $ prettyError e))
. deserialiseFromRawBytesHex (AsHash a)
. BSC.pack
where
errPrefix = maybe "" (": " <>) mErrPrefix
rBech32KeyHash :: SerialiseAsBech32 (Hash a) => AsType a -> ReadM (Hash a)
rBech32KeyHash a =
Opt.eitherReader $
first (docToString . prettyError)
. deserialiseFromBech32 (AsHash a)
. Text.pack
pGenesisDelegateVerificationKey :: Parser (VerificationKey GenesisDelegateKey)
pGenesisDelegateVerificationKey =
Opt.option deserialiseFromHex $
mconcat
[ Opt.long "genesis-delegate-verification-key"
, Opt.metavar "STRING"
, Opt.help "Genesis delegate verification key (hex-encoded)."
]
where
deserialiseFromHex =
rVerificationKey AsGenesisDelegateKey (Just "Invalid genesis delegate verification key")
-- | Reader for verification keys
rVerificationKey
:: ()
=> SerialiseAsRawBytes (VerificationKey a)
=> AsType a
-- ^ Singleton value identifying the kind of verification keys
-> Maybe String
-- ^ Optional prefix to the error message
-> ReadM (VerificationKey a)
rVerificationKey a mErrPrefix =
Opt.eitherReader $
first
(\e -> errPrefix <> (docToString $ prettyError e))
. deserialiseFromRawBytesHex (AsVerificationKey a)
. BSC.pack
where
errPrefix = maybe "" (": " <>) mErrPrefix
-- | The first argument is the optional prefix.
pColdVerificationKeyOrFile :: Maybe String -> Parser ColdVerificationKeyOrFile
pColdVerificationKeyOrFile prefix =
asum
[ ColdStakePoolVerificationKey <$> pStakePoolVerificationKey prefix
, ColdGenesisDelegateVerificationKey <$> pGenesisDelegateVerificationKey
, ColdVerificationKeyFile <$> pColdVerificationKeyFile
]
pColdVerificationKeyFile :: Parser (VerificationKeyFile direction)
pColdVerificationKeyFile =
fmap File $
asum
[ parseFilePath "cold-verification-key-file" "Filepath of the cold verification key."
, Opt.strOption $
mconcat
[ Opt.long "verification-key-file"
, Opt.internal
]
]
pColdSigningKeyFile :: Parser (File (SigningKey keyrole) direction)
pColdSigningKeyFile =
fmap File $
asum
[ parseFilePath "cold-signing-key-file" "Filepath of the cold signing key."
, Opt.strOption $
mconcat
[ Opt.long "signing-key-file"
, Opt.internal
]
]
pVerificationKeyFileOut :: Parser (File (VerificationKey keyrole) Out)
pVerificationKeyFileOut =
File <$> parseFilePath "verification-key-file" "Output filepath of the verification key."
pSigningKeyFileOut :: Parser (File (SigningKey keyrole) Out)
pSigningKeyFileOut =
File <$> parseFilePath "signing-key-file" "Output filepath of the signing key."
pOperatorCertIssueCounterFile :: Parser (File OpCertCounter direction)
pOperatorCertIssueCounterFile =
fmap File $
asum
[ parseFilePath
"operational-certificate-issue-counter-file"
"The file with the issue counter for the operational certificate."
, Opt.strOption $
mconcat
[ Opt.long "operational-certificate-issue-counter"
, Opt.internal
]
]
---
pAddCommitteeColdVerificationKeySource
:: Parser (VerificationKeyOrHashOrFileOrScriptHash CommitteeColdKey)
pAddCommitteeColdVerificationKeySource =
asum
[ VkhfshKeyHashFile . VerificationKeyOrFile <$> pAddCommitteeColdVerificationKeyOrFile
, VkhfshKeyHashFile . VerificationKeyHash <$> pAddCommitteeColdVerificationKeyHash
, VkhfshScriptHash
<$> pScriptHash
"add-cc-cold-script-hash"
"Cold Native or Plutus script file hash (hex-encoded). Obtain it with \"cardano-cli hash script ...\"."
]
pAddCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey)
pAddCommitteeColdVerificationKeyHash =
Opt.option deserialiseColdCCKeyHashFromHex $
mconcat
[ Opt.long "add-cc-cold-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
pAddCommitteeColdVerificationKeyOrFile :: Parser (VerificationKeyOrFile CommitteeColdKey)
pAddCommitteeColdVerificationKeyOrFile =
asum
[ VerificationKeyValue <$> pAddCommitteeColdVerificationKey
, VerificationKeyFilePath <$> pAddCommitteeColdVerificationKeyFile
]
pAddCommitteeColdVerificationKey :: Parser (VerificationKey CommitteeColdKey)
pAddCommitteeColdVerificationKey =
Opt.option deserialiseFromHex $
mconcat
[ Opt.long "add-cc-cold-verification-key"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee cold key (hex-encoded)."
]
where
deserialiseFromHex =
rVerificationKey AsCommitteeColdKey (Just "Invalid Constitutional Committee cold key")
pAddCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In)
pAddCommitteeColdVerificationKeyFile =
File
<$> parseFilePath
"add-cc-cold-verification-key-file"
"Filepath of the Constitutional Committee cold key."
---
pRemoveCommitteeColdVerificationKeySource
:: Parser (VerificationKeyOrHashOrFileOrScriptHash CommitteeColdKey)
pRemoveCommitteeColdVerificationKeySource =
asum
[ VkhfshKeyHashFile . VerificationKeyOrFile <$> pRemoveCommitteeColdVerificationKeyOrFile
, VkhfshKeyHashFile . VerificationKeyHash <$> pRemoveCommitteeColdVerificationKeyHash
, VkhfshScriptHash
<$> pScriptHash
"remove-cc-cold-script-hash"
"Cold Native or Plutus script file hash (hex-encoded). Obtain it with \"cardano-cli hash script ...\"."
]
pScriptHash
:: String
-- ^ long option name
-> String
-- ^ help text
-> Parser ScriptHash
pScriptHash longOptionName helpText =
Opt.option scriptHashReader $
mconcat
[ Opt.long longOptionName
, Opt.metavar "HASH"
, Opt.help helpText
]
pRemoveCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey)
pRemoveCommitteeColdVerificationKeyHash =
Opt.option deserialiseColdCCKeyHashFromHex $
mconcat
[ Opt.long "remove-cc-cold-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
pRemoveCommitteeColdVerificationKeyOrFile :: Parser (VerificationKeyOrFile CommitteeColdKey)
pRemoveCommitteeColdVerificationKeyOrFile =
asum
[ VerificationKeyValue <$> pRemoveCommitteeColdVerificationKey
, VerificationKeyFilePath <$> pRemoveCommitteeColdVerificationKeyFile
]
pRemoveCommitteeColdVerificationKey :: Parser (VerificationKey CommitteeColdKey)
pRemoveCommitteeColdVerificationKey =
Opt.option deserialiseColdCCKeyFromHex $
mconcat
[ Opt.long "remove-cc-cold-verification-key"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee cold key (hex-encoded)."
]
deserialiseColdCCKeyFromHex :: ReadM (VerificationKey CommitteeColdKey)
deserialiseColdCCKeyFromHex =
rVerificationKey AsCommitteeColdKey (Just "Invalid Constitutional Committee cold key")
deserialiseColdCCKeyHashFromHex :: ReadM (Hash CommitteeColdKey)
deserialiseColdCCKeyHashFromHex =
rHexHash AsCommitteeColdKey (Just "Invalid Constitutional Committee cold key hash")
pRemoveCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In)
pRemoveCommitteeColdVerificationKeyFile =
File
<$> parseFilePath
"remove-cc-cold-verification-key-file"
"Filepath of the Constitutional Committee cold key."
---
pCommitteeColdVerificationKeyOrHashOrFile :: Parser (VerificationKeyOrHashOrFile CommitteeColdKey)
pCommitteeColdVerificationKeyOrHashOrFile =
asum
[ VerificationKeyOrFile <$> pCommitteeColdVerificationKeyOrFile
, VerificationKeyHash <$> pCommitteeColdVerificationKeyHash
]
pCommitteeColdVerificationKeyOrFile :: Parser (VerificationKeyOrFile CommitteeColdKey)
pCommitteeColdVerificationKeyOrFile =
asum
[ VerificationKeyValue <$> pCommitteeColdVerificationKey
, VerificationKeyFilePath <$> pCommitteeColdVerificationKeyFile
]
pCommitteeColdVerificationKey :: Parser (VerificationKey CommitteeColdKey)
pCommitteeColdVerificationKey =
Opt.option deserialiseColdCCKeyFromHex $
mconcat
[ Opt.long "cold-verification-key"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee cold key (hex-encoded)."
]
pCommitteeColdVerificationKeyHash :: Parser (Hash CommitteeColdKey)
pCommitteeColdVerificationKeyHash =
Opt.option deserialiseColdCCKeyHashFromHex $
mconcat
[ Opt.long "cold-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
pCommitteeColdVerificationKeyFile :: Parser (File (VerificationKey keyrole) In)
pCommitteeColdVerificationKeyFile =
File
<$> parseFilePath "cold-verification-key-file" "Filepath of the Constitutional Committee cold key."
pVerificationKeyFileIn :: Parser (VerificationKeyFile In)
pVerificationKeyFileIn =
File <$> parseFilePath "verification-key-file" "Input filepath of the verification key."
pAnyVerificationKeyFileIn :: String -> Parser (VerificationKeyFile In)
pAnyVerificationKeyFileIn helpText =
File <$> parseFilePath "verification-key-file" ("Input filepath of the " <> helpText <> ".")
pAnyVerificationKeyText :: String -> Parser AnyVerificationKeyText
pAnyVerificationKeyText helpText =
fmap (AnyVerificationKeyText . Text.pack) $
Opt.strOption $
mconcat
[ Opt.long "verification-key"
, Opt.metavar "STRING"
, Opt.help $ helpText <> " (Bech32-encoded)"
]
pAnyVerificationKeySource :: String -> Parser AnyVerificationKeySource
pAnyVerificationKeySource helpText =
asum
[ AnyVerificationKeySourceOfText <$> pAnyVerificationKeyText helpText
, AnyVerificationKeySourceOfFile <$> pAnyVerificationKeyFileIn helpText
]
pCommitteeHotKey :: Parser (VerificationKey CommitteeHotKey)
pCommitteeHotKey = pCommitteeHotVerificationKey "hot-key"
pCommitteeHotVerificationKeyOrFile :: Parser (VerificationKeyOrFile CommitteeHotKey)
pCommitteeHotVerificationKeyOrFile =
asum
[ VerificationKeyValue <$> pCommitteeHotVerificationKey "hot-verification-key"
, VerificationKeyFilePath <$> pCommitteeHotVerificationKeyFile "hot-verification-key-file"
]
pCommitteeHotVerificationKeyHash :: Parser (Hash CommitteeHotKey)
pCommitteeHotVerificationKeyHash =
Opt.option deserialiseHotCCKeyHashFromHex $
mconcat
[ Opt.long "hot-verification-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
pCommitteeHotVerificationKey :: String -> Parser (VerificationKey CommitteeHotKey)
pCommitteeHotVerificationKey longFlag =
Opt.option deserialiseHotCCKeyFromHex $
mconcat
[ Opt.long longFlag
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee hot key (hex-encoded)."
]
deserialiseHotCCKeyFromHex :: ReadM (VerificationKey CommitteeHotKey)
deserialiseHotCCKeyFromHex =
rVerificationKey AsCommitteeHotKey (Just "Invalid Constitutional Committee hot key")
deserialiseHotCCKeyHashFromHex :: ReadM (Hash CommitteeHotKey)
deserialiseHotCCKeyHashFromHex =
rHexHash AsCommitteeHotKey (Just "Invalid Constitutional Committee hot key hash")
pCommitteeHotVerificationKeyFile :: String -> Parser (VerificationKeyFile In)
pCommitteeHotVerificationKeyFile longFlag =
File <$> parseFilePath longFlag "Filepath of the Constitutional Committee hot key."
-- | The first argument is the optional prefix.
pCommitteeHotKeyHash :: Maybe String -> Parser (Hash CommitteeHotKey)
pCommitteeHotKeyHash prefix =
Opt.option deserialiseHotCCKeyHashFromHex $
mconcat
[ Opt.long $ prefixFlag prefix "hot-key-hash"
, Opt.metavar "STRING"
, Opt.help "Constitutional Committee key hash (hex-encoded)."
]
pCommitteeHotKeyOrHashOrFile :: Parser (VerificationKeyOrHashOrFile CommitteeHotKey)
pCommitteeHotKeyOrHashOrFile =
asum
[ VerificationKeyOrFile . VerificationKeyValue <$> pCommitteeHotKey
, VerificationKeyOrFile . VerificationKeyFilePath <$> pCommitteeHotVerificationKeyFile "hot-key-file"
, VerificationKeyHash <$> pCommitteeHotKeyHash Nothing
]
pCommitteeHotVerificationKeyOrHashOrVerificationFile
:: Parser (VerificationKeyOrHashOrFile CommitteeHotKey)
pCommitteeHotVerificationKeyOrHashOrVerificationFile =
asum
[ VerificationKeyOrFile . VerificationKeyValue
<$> pCommitteeHotVerificationKey "cc-hot-verification-key"
, VerificationKeyOrFile . VerificationKeyFilePath
<$> pCommitteeHotVerificationKeyFile "cc-hot-verification-key-file"
, VerificationKeyHash <$> pCommitteeHotKeyHash (Just "cc")
]
pCommitteeHotVerificationKeyOrHashOrVerificationFileOrScriptHash
:: Parser (VerificationKeyOrHashOrFileOrScriptHash CommitteeHotKey)
pCommitteeHotVerificationKeyOrHashOrVerificationFileOrScriptHash =
asum
[ VkhfshKeyHashFile <$> pCommitteeHotVerificationKeyOrHashOrVerificationFile
, VkhfshScriptHash
<$> pScriptHash
"cc-hot-script-hash"
"Cold Native or Plutus script file hash (hex-encoded). Obtain it with \"cardano-cli hash script ...\"."
]
catCommands :: [Parser a] -> Maybe (Parser a)
catCommands = \case
[] -> Nothing
ps -> Just $ asum ps
pConstitutionUrl :: Parser ConstitutionUrl
pConstitutionUrl =
ConstitutionUrl
<$> pUrl "constitution-url" "Constitution URL."
pConstitutionHash :: Parser (L.SafeHash L.StandardCrypto L.AnchorData)
pConstitutionHash =
Opt.option readSafeHash $
mconcat
[ Opt.long "constitution-hash"
, Opt.metavar "HASH"
, Opt.help "Hash of the constitution data (obtain it with \"cardano-cli hash anchor-data ...\")."
]
pUrl :: String -> String -> Parser L.Url
pUrl l h =
let toUrl urlText =
fromMaybe (error "Url longer than 64 bytes") $
L.textToUrl (Text.length urlText) urlText
in fmap toUrl . Opt.strOption $
mconcat
[ Opt.long l
, Opt.metavar "TEXT"
, Opt.help h
]
pGovActionDeposit :: Parser Lovelace
pGovActionDeposit =
Opt.option (readerFromParsecParser parseLovelace) $
mconcat
[ Opt.long "governance-action-deposit"
, Opt.metavar "NATURAL"
, Opt.help "Deposit required to submit a governance action."
]
pNewGovActionDeposit :: Parser Lovelace
pNewGovActionDeposit =
Opt.option (readerFromParsecParser parseLovelace) $
mconcat
[ Opt.long "new-governance-action-deposit"
, Opt.metavar "NATURAL"
, Opt.help "Proposed new value of the deposit required to submit a governance action."
]
-- | First argument is the optional prefix
pStakeVerificationKeyOrHashOrFile :: Maybe String -> Parser (VerificationKeyOrHashOrFile StakeKey)
pStakeVerificationKeyOrHashOrFile prefix =
asum
[ VerificationKeyOrFile <$> pStakeVerificationKeyOrFile prefix
, VerificationKeyHash <$> pStakeVerificationKeyHash prefix
]
-- | First argument is the optional prefix
pStakeVerificationKeyHash :: Maybe String -> Parser (Hash StakeKey)
pStakeVerificationKeyHash prefix =
Opt.option (rHexHash AsStakeKey Nothing) $
mconcat
[ Opt.long $ prefixFlag prefix "stake-key-hash"
, Opt.metavar "HASH"
, Opt.help "Stake verification key hash (hex-encoded)."
]
-- | The first argument is the optional prefix.
pStakePoolVerificationKeyOrHashOrFile
:: Maybe String -> Parser (VerificationKeyOrHashOrFile StakePoolKey)
pStakePoolVerificationKeyOrHashOrFile prefix =
asum
[ VerificationKeyOrFile <$> pStakePoolVerificationKeyOrFile prefix
, VerificationKeyHash <$> pStakePoolVerificationKeyHash prefix
]
--------------------------------------------------------------------------------
pCBORInFile :: Parser FilePath
pCBORInFile =
asum
[ parseFilePath "in-file" "CBOR input file."
, Opt.strOption $
mconcat
[ Opt.long "file"
, Opt.internal
]
]
--------------------------------------------------------------------------------
pPollQuestion :: Parser Text
pPollQuestion =
Opt.strOption $
mconcat
[ Opt.long "question"
, Opt.metavar "STRING"
, Opt.help "The question for the poll."
]
pPollAnswer :: Parser Text
pPollAnswer =
Opt.strOption $
mconcat
[ Opt.long "answer"
, Opt.metavar "STRING"
, Opt.help "A possible choice for the poll. The option is repeatable."
]
pPollAnswerIndex :: Parser Word
pPollAnswerIndex =
Opt.option integralReader $
mconcat
[ Opt.long "answer"
, Opt.metavar "INT"
, Opt.help "The index of the chosen answer in the poll. Optional. Asked interactively if omitted."
]
pPollFile :: Parser (File GovernancePoll In)
pPollFile = File <$> parseFilePath "poll-file" "Filepath to the ongoing poll."
pPollTxFile :: Parser (TxFile In)
pPollTxFile =
File
<$> parseFilePath "tx-file" "Filepath to the JSON TxBody or JSON Tx carrying a valid poll answer."
pPollNonce :: Parser Word
pPollNonce =
Opt.option integralReader $