-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move data family up one level as in example
- Loading branch information
Showing
7 changed files
with
143 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 0 additions & 128 deletions
128
cardano-api/internal/Cardano/Api/Governance/Actions/CIP119.hs
This file was deleted.
Oops, something went wrong.
128 changes: 102 additions & 26 deletions
128
cardano-api/internal/Cardano/Api/Governance/Metadata/DrepRegistration.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.