Skip to content

Commit

Permalink
Move data family up one level as in example
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Jan 13, 2025
1 parent 1c44113 commit a447f80
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 184 deletions.
1 change: 0 additions & 1 deletion cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ library internal
Cardano.Api.GeneralParsers
Cardano.Api.Genesis
Cardano.Api.GenesisParameters
Cardano.Api.Governance.Actions.CIP119
Cardano.Api.Governance.Actions.ProposalProcedure
Cardano.Api.Governance.Actions.VotingProcedure
Cardano.Api.Governance.Metadata.DrepRegistration
Expand Down
128 changes: 0 additions & 128 deletions cardano-api/internal/Cardano/Api/Governance/Actions/CIP119.hs

This file was deleted.

128 changes: 102 additions & 26 deletions cardano-api/internal/Cardano/Api/Governance/Metadata/DrepRegistration.hs
Original file line number Diff line number Diff line change
@@ -1,44 +1,120 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}

module Cardano.Api.Governance.Metadata.DrepRegistration where
module Cardano.Api.Governance.Metadata.DrepRegistration (CIP119 (..)) where

import Cardano.Api.GeneralParsers (textWithMaxLength)
import Cardano.Api.Governance.Metadata.Validation (GovActionMetadata (..), HashAlgorithm, Body, Authors)

import Data.Aeson (FromJSON, withObject, (.:), (.:?))
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (Parser)
import Data.Text (Text)
import GHC.Generics (Generic)

---------------------------------
-- EXISTS IN A SEPARATE MODULE --
---------------------------------
data CIP119 = DrepRegistrationMetadata
instance FromJSON (GovActionMetadata CIP119) where
parseJSON :: Aeson.Value -> Parser (GovActionMetadata CIP119)
parseJSON = withObject "CIP119Common" $ \v ->
GovActionMetadata
<$> v .: "hashAlgorithm"
<*> pure Absent
<*> v .: "body"

-- GovActionMetadata' Needs to be extended
-- to all fields of CIP-100
data GovActionMetadata' cip
= GovActionMetadata'
{ body :: Body' cip
}
-- Hash Algorithm (Enum)
data instance HashAlgorithm CIP119 = Blake2b256
deriving (Show, Generic)

data family Body' cip
instance FromJSON (HashAlgorithm CIP119) where
parseJSON :: Aeson.Value -> Parser (HashAlgorithm CIP119)
parseJSON = Aeson.withText "HashAlgorithm" $
\case
"blake2b-256" -> return Blake2b256
_ -> fail "Invalid hashAlgorithm, it must be: blake2b-256"

---------------------------------
---------------------------------
-- Body of the metadata document
data instance Body CIP119 = Body
{ paymentAddress :: Maybe Text
, givenName :: Text
, image :: Maybe ImageObject
, objectives :: Maybe Text
, motivations :: Maybe Text
, qualifications :: Maybe Text
, doNotList :: Maybe DoNotList
, references :: Maybe [Reference]
}
deriving (Show, Generic)

-- Everything below exists in this module
data CIP108 = DrepRegistrationMetadata
instance FromJSON (Body CIP119) where
parseJSON :: Aeson.Value -> Parser (Body CIP119)
parseJSON = withObject "Body" $ \v ->
Body
<$> v .:? "paymentAddress"
<*> (v .: "givenName" >>= textWithMaxLength "givenName" 80)
<*> v .:? "image"
<*> (v .:? "objectives" >>= traverse (textWithMaxLength "objectives" 1000))
<*> (v .:? "motivations" >>= traverse (textWithMaxLength "motivations" 1000))
<*> (v .:? "qualifications" >>= traverse (textWithMaxLength "qualifications" 1000))
<*> v .:? "doNotList"
<*> v .:? "references"

data instance Body' CIP108 = Body
{ title :: Text
, abstract :: Text
, motivation :: Text
, rationale :: Text
, references :: Maybe [Reference]
-- Profile picture
data ImageObject = ImageObject
{ contentUrl :: Text -- Base64 encoded image or URL
, sha256 :: Maybe Text -- Only present for URL images
}
deriving (Show, Generic)
instance FromJSON ImageObject where
parseJSON :: Aeson.Value -> Parser ImageObject
parseJSON = withObject "ImageObject" $ \v ->
ImageObject
<$> v .: "contentUrl"
<*> v .:? "sha256"
-- DoNotList Enum
data DoNotList = DoNotListTrue | DoNotListFalse
deriving (Show, Generic)

instance FromJSON DoNotList where
parseJSON :: Aeson.Value -> Parser DoNotList
parseJSON = Aeson.withText "DoNotList" $
\case
"true" -> return DoNotListTrue
"false" -> return DoNotListFalse
_ -> fail "Invalid doNotList value, must be one of: true, false"

-- Reference type
data Reference = Reference
{ refType :: ReferenceType
, label :: Text
, uri :: Text
}
deriving Show
deriving (Show, Generic)

instance FromJSON Reference where
parseJSON :: Aeson.Value -> Parser Reference
parseJSON = withObject "Reference" $ \v ->
Reference
<$> v .: "@type"
<*> v .: "label"
<*> v .: "uri"

-- ReferenceType Enum
data ReferenceType = GovernanceMetadata | Other | Link | Identity
deriving (Show, Generic)

instance FromJSON ReferenceType where
parseJSON :: Aeson.Value -> Parser ReferenceType
parseJSON = Aeson.withText "ReferenceType" $
\case
"GovernanceMetadata" -> return GovernanceMetadata
"Other" -> return Other
"Link" -> return Link
"Identity" -> return Identity
_ ->
fail "Invalid reference type, must be one of: GovernanceMetadata, Other, Link, Identity"

data ReferenceType = GovernanceMetadata | Other
deriving Show
-- We don't need to validate Authors because it is optional in CIP-119
data instance Authors CIP119 = Absent
43 changes: 23 additions & 20 deletions cardano-api/internal/Cardano/Api/Governance/Metadata/GovAction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,46 @@
module Cardano.Api.Governance.Metadata.GovAction (CIP108 (..)) where

import Cardano.Api.GeneralParsers (textWithMaxLength)
import Cardano.Api.Governance.Metadata.Validation (GovActionMetadata)
import Cardano.Api.Governance.Metadata.Validation (GovActionMetadata (..), HashAlgorithm, Authors, Body)

import Data.Aeson (FromJSON, withObject, withText, (.:), (.:?))
import Data.Aeson (FromJSON, withObject, withText, (.:), (.:?), withArray)
import qualified Data.Aeson as Aeson
import Data.Aeson.Types (Parser, Value)
import Data.Aeson.Types (Parser, Value (..))
import Data.Text (Text)
import GHC.Generics (Generic)

data CIP108 = CIP108

-- Root object: GovActionMetadata CIP108
data instance GovActionMetadata CIP108 = CIP108Common
{ hashAlgorithm :: HashAlgorithm
, authors :: [Author]
, body :: Body
}
deriving (Show, Generic)
data CIP108 = BaseGovActionMetadata

instance FromJSON (GovActionMetadata CIP108) where
parseJSON :: Value -> Parser (GovActionMetadata CIP108)
parseJSON = withObject "CIP108Common" $ \v ->
CIP108Common
GovActionMetadata
<$> v .: "hashAlgorithm"
<*> v .: "authors"
<*> v .: "body"

-- Enum for HashAlgorithm
data HashAlgorithm = Blake2b256

data instance HashAlgorithm CIP108 = Blake2b256
deriving (Show, Generic)

instance FromJSON HashAlgorithm where
parseJSON :: Value -> Parser HashAlgorithm
instance FromJSON (HashAlgorithm CIP108) where
parseJSON :: Value -> Parser (HashAlgorithm CIP108)
parseJSON = withText "HashAlgorithm" $
\case
"blake2b-256" -> return Blake2b256
_ -> fail "Invalid hashAlgorithm value, must be: blake2b-256"

-- Author object

newtype instance Authors CIP108 = Authors [Author]
deriving (Show, Generic)

instance FromJSON (Authors CIP108) where
parseJSON :: Value -> Parser (Authors CIP108)
parseJSON = withArray "Authors" $ \arr ->
Authors <$> Aeson.parseJSON (Array arr)

data Author = Author
{ name :: Maybe Text
, witness :: Witness
Expand Down Expand Up @@ -87,7 +89,8 @@ instance FromJSON WitnessAlgorithm where
_ -> fail "Invalid witnessAlgorithm value, must be: ed25519 or CIP-0008"

-- Body of the metadata document
data Body = Body

data instance Body CIP108 = Body
{ title :: Text
, abstract :: Text
, motivation :: Text
Expand All @@ -96,8 +99,8 @@ data Body = Body
}
deriving (Show, Generic)

instance FromJSON Body where
parseJSON :: Value -> Parser Body
instance FromJSON (Body CIP108) where
parseJSON :: Value -> Parser (Body CIP108)
parseJSON = withObject "Body" $ \v ->
Body
<$> (v .: "title" >>= textWithMaxLength "title" 80)
Expand Down Expand Up @@ -139,7 +142,7 @@ instance FromJSON ReferenceType where
-- ReferenceHash object
data ReferenceHash = ReferenceHash
{ referenceHashDigest :: Text
, referenceHashAlgorithm :: HashAlgorithm
, referenceHashAlgorithm :: HashAlgorithm CIP108
}
deriving (Show, Generic)

Expand Down
Loading

0 comments on commit a447f80

Please sign in to comment.