|
4 | 4 | {-# LANGUAGE ScopedTypeVariables #-} |
5 | 5 |
|
6 | 6 | module Cardano.CLI.Byron.Delegation |
7 | | - ( ByronDelegationError (..) |
8 | | - , checkByronGenesisDelegation |
9 | | - , issueByronGenesisDelegation |
10 | | - , renderByronDelegationError |
11 | | - , serialiseDelegationCert |
| 7 | + ( serialiseDelegationCert |
12 | 8 | , serialiseByronWitness |
13 | 9 | ) |
14 | 10 | where |
15 | 11 |
|
16 | | -import Cardano.Api.Byron (ACertificate (delegateVK)) |
17 | 12 | import Cardano.Api.Byron hiding (delegateVK) |
18 | | -import Cardano.Api.Ledger qualified as L |
19 | | -import Cardano.Api.Monad.Error |
20 | | -import Cardano.Api.Pretty |
21 | 13 | import Cardano.Api.Serialise.Raw |
22 | 14 |
|
23 | | -import Cardano.CLI.Type.Common (CertificateFile (..)) |
24 | | -import Cardano.Crypto (ProtocolMagicId) |
25 | | -import Cardano.Crypto qualified as Crypto |
26 | | -import Cardano.Prelude (canonicalDecodePretty, canonicalEncodePretty) |
| 15 | +import Cardano.Prelude (canonicalEncodePretty) |
27 | 16 |
|
28 | 17 | import Prelude hiding ((.)) |
29 | 18 |
|
30 | 19 | import Control.Category |
31 | | -import Control.Monad (unless) |
32 | 20 | import Data.ByteString (ByteString) |
33 | 21 | import Data.ByteString.Lazy qualified as LB |
34 | | -import Data.Text (Text) |
35 | | -import Formatting (Format, sformat) |
36 | | - |
37 | | -data ByronDelegationError |
38 | | - = CertificateValidationErrors !FilePath ![Text] |
39 | | - | DlgCertificateDeserialisationFailed !FilePath !Text |
40 | | - deriving Show |
41 | | - |
42 | | -renderByronDelegationError :: ByronDelegationError -> Doc ann |
43 | | -renderByronDelegationError = \case |
44 | | - CertificateValidationErrors certFp errs -> |
45 | | - "Certificate validation error(s) at: " <> pshow certFp <> " Errors: " <> pshow errs |
46 | | - DlgCertificateDeserialisationFailed certFp deSererr -> |
47 | | - "Certificate deserialisation error at: " <> pshow certFp <> " Error: " <> pshow deSererr |
48 | | - |
49 | | --- TODO: we need to support password-protected secrets. |
50 | | - |
51 | | --- | Issue a certificate for genesis delegation to a delegate key, signed by the |
52 | | --- issuer key, for a given protocol magic and coming into effect at given epoch. |
53 | | -issueByronGenesisDelegation |
54 | | - :: ProtocolMagicId |
55 | | - -> EpochNumber |
56 | | - -> Crypto.SigningKey |
57 | | - -> Crypto.VerificationKey |
58 | | - -> Certificate |
59 | | -issueByronGenesisDelegation magic epoch issuerSK delegateVK' = |
60 | | - signCertificate magic delegateVK' epoch $ |
61 | | - Crypto.noPassSafeSigner issuerSK |
62 | | - |
63 | | --- | Verify that a certificate signifies genesis delegation by assumed genesis key |
64 | | --- to a delegate key, for a given protocol magic. |
65 | | --- If certificate fails validation, throw an error. |
66 | | -checkByronGenesisDelegation |
67 | | - :: CertificateFile |
68 | | - -> ProtocolMagicId |
69 | | - -> Crypto.VerificationKey |
70 | | - -> Crypto.VerificationKey |
71 | | - -> ExceptT ByronDelegationError IO () |
72 | | -checkByronGenesisDelegation (CertificateFile certF) magic issuer delegate = do |
73 | | - ecert <- liftIO $ canonicalDecodePretty <$> LB.readFile certF |
74 | | - case ecert of |
75 | | - Left e -> left $ DlgCertificateDeserialisationFailed certF e |
76 | | - Right (cert :: Certificate) -> do |
77 | | - let issues = checkDlgCert cert magic issuer delegate |
78 | | - unless (null issues) $ |
79 | | - left $ |
80 | | - CertificateValidationErrors certF issues |
81 | | - |
82 | | -checkDlgCert |
83 | | - :: ACertificate a |
84 | | - -> ProtocolMagicId |
85 | | - -> Crypto.VerificationKey |
86 | | - -> Crypto.VerificationKey |
87 | | - -> [Text] |
88 | | -checkDlgCert cert magic issuerVK' delegateVK' = |
89 | | - mconcat |
90 | | - [ [ sformat "Certificate does not have a valid signature." |
91 | | - | not (isValid magic' cert') |
92 | | - ] |
93 | | - , [ sformat |
94 | | - ("Certificate issuer " . vkF . " doesn't match expected: " . vkF) |
95 | | - (issuerVK cert) |
96 | | - issuerVK' |
97 | | - | issuerVK cert /= issuerVK' |
98 | | - ] |
99 | | - , [ sformat |
100 | | - ("Certificate delegate " . vkF . " doesn't match expected: " . vkF) |
101 | | - (delegateVK cert) |
102 | | - delegateVK' |
103 | | - | delegateVK cert /= delegateVK' |
104 | | - ] |
105 | | - ] |
106 | | - where |
107 | | - magic' :: L.Annotated ProtocolMagicId ByteString |
108 | | - magic' = L.Annotated magic (L.serialize' L.byronProtVer magic) |
109 | | - |
110 | | - epoch :: EpochNumber |
111 | | - epoch = L.unAnnotated $ aEpoch cert |
112 | | - |
113 | | - cert' :: ACertificate ByteString |
114 | | - cert' = |
115 | | - let unannotated = |
116 | | - cert |
117 | | - { aEpoch = L.Annotated epoch () |
118 | | - , annotation = () |
119 | | - } |
120 | | - in unannotated |
121 | | - { annotation = L.serialize' L.byronProtVer unannotated |
122 | | - , aEpoch = L.Annotated epoch (L.serialize' L.byronProtVer epoch) |
123 | | - } |
124 | | - |
125 | | - vkF :: forall r. Format r (Crypto.VerificationKey -> r) |
126 | | - vkF = Crypto.fullVerificationKeyF |
127 | 22 |
|
128 | 23 | serialiseDelegationCert :: Certificate -> ByteString |
129 | 24 | serialiseDelegationCert = LB.toStrict . canonicalEncodePretty |
|
0 commit comments