Skip to content

Commit

Permalink
Add checks for max length of fields in CIP-0108
Browse files Browse the repository at this point in the history
  • Loading branch information
palas committed Dec 17, 2024
1 parent f5b01c4 commit 101ac6d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Data.ByteString (ByteString)
import Data.Either.Combinators (mapRight)
import Data.Text (Text)
import GHC.Generics (Generic)
import Cardano.Api.GeneralParsers (textWithMaxLength)

validateGovActionAnchorData :: ByteString -> Either String ()
validateGovActionAnchorData bytes = mapRight (const ()) (eitherDecodeStrict bytes :: Either String CIP108Common)
Expand Down Expand Up @@ -98,8 +99,8 @@ instance FromJSON Body where
parseJSON :: Value -> Parser Body
parseJSON = withObject "Body" $ \v ->
Body
<$> v .: "title"
<*> v .: "abstract"
<$> (v .: "title" >>= textWithMaxLength "title" 80)
<*> (v .: "abstract" >>= textWithMaxLength "abstract" 2500)
<*> v .: "motivation"
<*> v .: "rationale"
<*> v .:? "references"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ tests =
, testProperty
"Positive smoke test for treasury withdrawal anchor data JSON schema"
prop_positive_smoke_test_threasury_withdrawal_anchor_data_json_schema
, testProperty
"Title name too long smoke test for treasury withdrawal anchor data JSON schema"
prop_title_name_too_long_smoke_test_threasury_withdrawal_anchor_data_json_schema
]

prop_positive_smoke_test_drep_registration_anchor_data_json_schema :: Property
Expand Down Expand Up @@ -95,3 +98,14 @@ prop_positive_smoke_test_threasury_withdrawal_anchor_data_json_schema = property
)
value <- H.evalEither eitherValue
validateGovActionAnchorData value === Right ()

prop_title_name_too_long_smoke_test_threasury_withdrawal_anchor_data_json_schema :: Property
prop_title_name_too_long_smoke_test_threasury_withdrawal_anchor_data_json_schema = propertyOnce $ do
(eitherValue :: Either (FileError Any) ByteString) <-
readByteStringFile
( File "test/cardano-api-test/files/input/gov-anchor-data/too-long-title-treasury-withdraw.jsonld"
:: File DRepMetadata In
)
value <- H.evalEither eitherValue
validateGovActionAnchorData value
=== Left "Error in $.body: key \"title\" exceeds maximum length of 80 characters. Got length: 112"
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"@context": {
"@language": "en-us",
"CIP100": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#",
"CIP108": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0108/README.md#",
"hashAlgorithm": "CIP100:hashAlgorithm",
"body": {
"@id": "CIP108:body",
"@context": {
"references": {
"@id": "CIP108:references",
"@container": "@set",
"@context": {
"GovernanceMetadata": "CIP100:GovernanceMetadataReference",
"Other": "CIP100:OtherReference",
"label": "CIP100:reference-label",
"uri": "CIP100:reference-uri",
"referenceHash": {
"@id": "CIP108:referenceHash",
"@context": {
"hashDigest": "CIP108:hashDigest",
"hashAlgorithm": "CIP100:hashAlgorithm"
}
}
}
},
"title": "CIP108:title",
"abstract": "CIP108:abstract",
"motivation": "CIP108:motivation",
"rationale": "CIP108:rationale"
}
},
"authors": {
"@id": "CIP100:authors",
"@container": "@set",
"@context": {
"name": "http://xmlns.com/foaf/0.1/name",
"witness": {
"@id": "CIP100:witness",
"@context": {
"witnessAlgorithm": "CIP100:witnessAlgorithm",
"publicKey": "CIP100:publicKey",
"signature": "CIP100:signature"
}
}
}
}
},
"hashAlgorithm": "blake2b-256",
"body": {
"title": "Buy Ryan an island that is sunny and full of palm trees and has a lot of sand and is quiet yet not too far away.",
"abstract": "Withdraw 200000000000 ADA from the treasury so Ryan can buy an island.",
"motivation": "The current problem is that Ryan does not have an island, but he would really like an island.",
"rationale": "With these funds from the treasury will be sold for **cold hard cash**, this cash can then be used to purchase an island for Ryan. An example of this island is provided in the references."
},
"authors": [
{
"name": "Ryan Williams",
"witness": {
"witnessAlgorithm": "ed25519",
"publicKey": "7ea09a34aebb13c9841c71397b1cabfec5ddf950405293dee496cac2f437480a",
"signature": "a476985b4cc0d457f247797611799a6f6a80fc8cb7ec9dcb5a8223888d0618e30de165f3d869c4a0d9107d8a5b612ad7c5e42441907f5b91796f0d7187d64a01"
}
}
]
}

0 comments on commit 101ac6d

Please sign in to comment.