Skip to content

Commit d948f9f

Browse files
Merge #1298
1298: More typed API migrations r=intricate a=intricate Migrate some more command implementations to the typed API and also fix compatibility issues introduced by #1284. Co-authored-by: Luke Nadur <19835357+intricate@users.noreply.github.com>
2 parents aabebb9 + 75d996e commit d948f9f

File tree

10 files changed

+188
-81
lines changed

10 files changed

+188
-81
lines changed

cardano-api/src/Cardano/Api/Typed.hs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ import Data.Bifunctor (first)
227227
import Data.List as List
228228
--import Data.Either
229229
import Data.String (IsString(fromString))
230+
import qualified Data.Text as Text (unpack)
230231
import Numeric.Natural
231232

232233
import Data.ByteString (ByteString)
@@ -866,7 +867,9 @@ data OperationalCertificate =
866867
data OperationalCertificateIssueCounter =
867868
OperationalCertificateIssueCounter
868869
!Natural
869-
!(VerificationKey StakePoolKey) -- For consistency checking
870+
-- TODO: Commenting this out as we're temporarily supporting the old op
871+
-- cert issue counter format.
872+
-- !(VerificationKey StakePoolKey) -- For consistency checking
870873
deriving (Eq, Show)
871874
deriving anyclass SerialiseAsCBOR
872875

@@ -880,13 +883,20 @@ instance FromCBOR OperationalCertificate where
880883
return (OperationalCertificate ocert vkey)
881884

882885
instance ToCBOR OperationalCertificateIssueCounter where
883-
toCBOR (OperationalCertificateIssueCounter counter vkey) =
884-
toCBOR (counter, vkey)
886+
-- TODO: Commenting this out as we're temporarily supporting the old op
887+
-- cert issue counter format.
888+
-- toCBOR (OperationalCertificateIssueCounter counter vkey) =
889+
-- toCBOR (counter, vkey)
890+
toCBOR (OperationalCertificateIssueCounter counter) =
891+
toCBOR counter
885892

886893
instance FromCBOR OperationalCertificateIssueCounter where
887-
fromCBOR = do
888-
(counter, vkey) <- fromCBOR
889-
return (OperationalCertificateIssueCounter counter vkey)
894+
-- TODO: Commenting this out as we're temporarily supporting the old op
895+
-- cert issue counter format.
896+
-- fromCBOR = do
897+
-- (counter, vkey) <- fromCBOR
898+
-- return (OperationalCertificateIssueCounter counter vkey)
899+
fromCBOR = OperationalCertificateIssueCounter <$> fromCBOR
890900

891901
instance HasTypeProxy OperationalCertificate where
892902
data AsType OperationalCertificate = AsOperationalCertificate
@@ -928,13 +938,20 @@ issueOperationalCertificate :: VerificationKey KesKey
928938
issueOperationalCertificate (KesVerificationKey kesVKey)
929939
(StakePoolSigningKey poolSKey)
930940
kesPeriod
931-
(OperationalCertificateIssueCounter counter poolVKey)
932-
| poolVKey /= poolVKey'
933-
= Left (OperationalCertKeyMismatch poolVKey poolVKey')
934-
935-
| otherwise
936-
= Right (OperationalCertificate ocert poolVKey,
937-
OperationalCertificateIssueCounter (succ counter) poolVKey)
941+
-- TODO: Commenting this out as we're temporarily supporting the old op
942+
-- cert issue counter format.
943+
-- (OperationalCertificateIssueCounter counter poolVKey)
944+
(OperationalCertificateIssueCounter counter)
945+
-- TODO: Commenting this out as we're temporarily supporting the old op
946+
-- cert issue counter format.
947+
-- \| poolVKey /= poolVKey'
948+
-- = Left (OperationalCertKeyMismatch poolVKey poolVKey')
949+
--
950+
-- \| otherwise
951+
-- = Right (OperationalCertificate ocert poolVKey,
952+
-- OperationalCertificateIssueCounter (succ counter) poolVKey)
953+
= Right (OperationalCertificate ocert poolVKey',
954+
OperationalCertificateIssueCounter (succ counter))
938955
where
939956
poolVKey' = getVerificationKey (StakePoolSigningKey poolSKey)
940957

@@ -1025,7 +1042,7 @@ instance Error e => Error (FileError e) where
10251042
path ++ ": " ++ displayError e
10261043

10271044
instance Error TextView.TextViewError where
1028-
displayError _ = "TODO"
1045+
displayError = Text.unpack . TextView.renderTextViewError
10291046

10301047
serialiseToTextEnvelope :: forall a. HasTextEnvelope a
10311048
=> Maybe TextEnvelopeDescr -> a -> TextEnvelope
@@ -1499,6 +1516,13 @@ instance HasTextEnvelope (SigningKey GenesisDelegateKey) where
14991516
-- TODO: use a different type from the stake pool key, since some operations
15001517
-- need a genesis key specifically
15011518

1519+
instance CastKeyRole GenesisDelegateKey StakePoolKey where
1520+
castVerificationKey (GenesisDelegateVerificationKey (Shelley.VKey vkey)) =
1521+
StakePoolVerificationKey (Shelley.VKey vkey)
1522+
1523+
castSigningKey (GenesisDelegateSigningKey skey) =
1524+
StakePoolSigningKey skey
1525+
15021526

15031527
--
15041528
-- Genesis UTxO keys
@@ -1745,13 +1769,20 @@ instance SerialiseAsRawBytes (Hash VrfKey) where
17451769
VrfKeyHash <$> Crypto.hashFromBytes bs
17461770

17471771
instance HasTextEnvelope (VerificationKey VrfKey) where
1748-
textEnvelopeType _ = "VerKeyVRF " <> fromString (Crypto.algorithmNameVRF proxy)
1772+
textEnvelopeType _ = "VerKeyVRF " <> fromString (backCompatAlgorithmNameVrf proxy)
17491773
where
17501774
proxy :: Proxy (Shelley.VRF ShelleyCrypto)
17511775
proxy = Proxy
17521776

17531777
instance HasTextEnvelope (SigningKey VrfKey) where
1754-
textEnvelopeType _ = "SignKeyVRF " <> fromString (Crypto.algorithmNameVRF proxy)
1778+
textEnvelopeType _ = "SignKeyVRF " <> fromString (backCompatAlgorithmNameVrf proxy)
17551779
where
17561780
proxy :: Proxy (Shelley.VRF ShelleyCrypto)
17571781
proxy = Proxy
1782+
1783+
-- | Temporary solution for maintaining backward compatibility with the output
1784+
-- of 'Cardano.Config.Shelley.VRF.encodeVRFVerificationKey'.
1785+
backCompatAlgorithmNameVrf :: Proxy (Shelley.VRF ShelleyCrypto) -> String
1786+
backCompatAlgorithmNameVrf p =
1787+
let algoName = Crypto.algorithmNameVRF p
1788+
in if algoName == "simple" then "SimpleVRF" else algoName

cardano-api/test/Test/Cardano/Api/Typed/Gen.hs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ genOperationalCertificateWithCounter = do
5959
kesP <- genKESPeriod
6060
c <- Gen.integral $ Range.linear 0 1000
6161
let stakePoolVer = getVerificationKey stakePoolSign
62-
iCounter = OperationalCertificateIssueCounter c stakePoolVer
62+
-- TODO: Commenting this out as we're temporarily supporting the old op
63+
-- cert issue counter format.
64+
-- iCounter = OperationalCertificateIssueCounter c stakePoolVer
65+
iCounter = OperationalCertificateIssueCounter c
6366

6467
case issueOperationalCertificate kesVKey stakePoolSign kesP iCounter of
6568
-- This case should be impossible as we clearly derive the verification

cardano-cli/src/Cardano/CLI/Shelley/Commands.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ data GenesisCmd
205205
| GenesisKeyGenGenesis VerificationKeyFile SigningKeyFile
206206
| GenesisKeyGenDelegate VerificationKeyFile SigningKeyFile OpCertCounterFile
207207
| GenesisKeyGenUTxO VerificationKeyFile SigningKeyFile
208-
| GenesisKeyHash VerificationKeyFile
208+
| GenesisCmdKeyHash VerificationKeyFile
209209
| GenesisVerKey VerificationKeyFile SigningKeyFile
210210
| GenesisTxIn VerificationKeyFile Network (Maybe OutputFile)
211211
| GenesisAddr VerificationKeyFile Network (Maybe OutputFile)

cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ pGenesisCmd =
597597

598598
pGenesisKeyHash :: Parser GenesisCmd
599599
pGenesisKeyHash =
600-
GenesisKeyHash <$> pVerificationKeyFile Input
600+
GenesisCmdKeyHash <$> pVerificationKeyFile Input
601601

602602
pGenesisVerKey :: Parser GenesisCmd
603603
pGenesisVerKey =

0 commit comments

Comments
 (0)