Skip to content

Commit

Permalink
CIP-25 v2 parses PolicyId correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornkihlberg committed Mar 27, 2023
1 parent 5bc215b commit d7b1654
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
1 change: 0 additions & 1 deletion marlowe-runtime/marlowe-runtime.cabal
Expand Up @@ -550,7 +550,6 @@ test-suite marlowe-runtime-test
build-depends:
base >= 4.9 && < 5
, aeson
, base16
, bytestring
, cardano-api
, cardano-api:gen
Expand Down
@@ -1,25 +1,22 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
{-# OPTIONS_GHC -Wno-unused-local-binds #-}
{-# OPTIONS_GHC -Wno-unused-imports #-}
{-# LANGUAGE BangPatterns #-}

module Language.Marlowe.Runtime.Transaction.ApiSpec
( spec
) where

import Language.Marlowe.Runtime.Transaction.Api (CIP25Metadata(..), CIP25MetadataLabel(..))

import qualified Control.Monad
import Control.Arrow ((&&&))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Key as Aeson.Key
import qualified Data.ByteString.Base16 as BS.Base16
import qualified Data.ByteString.Char8 as BS.Char8
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Coerce (coerce)
import qualified Data.Maybe as Maybe
import qualified Data.Text as Text
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text.Encoding as Text.Encoding
import Language.Marlowe.Runtime.ChainSync.Api (PolicyId(PolicyId))
import Test.Hspec (Spec, it, shouldBe, shouldThrow)
import qualified Test.Hspec as Hspec
Expand All @@ -39,14 +36,14 @@ TODO CIP25Metadata -> Metadata procedure
TODO Reorganizes strings into lists of strings such that they are each no longer than 64 characters long
-}

encodeBase16String :: String -> String
encodeBase16String = Text.unpack . BS.Base16.encodeBase16 . BS.Char8.pack
mkPolicyId :: Text -> PolicyId
mkPolicyId = coerce Text.Encoding.encodeUtf8

unsafeMkPolicyId :: String -> PolicyId
unsafeMkPolicyId = coerce BS.Char8.pack
mkKey :: Text -> Aeson.Key
mkKey = Aeson.Key.fromText

mkKey :: String -> Aeson.Key
mkKey = Aeson.Key.fromString
base16EncodedTextGen :: Int -> Gen Text
base16EncodedTextGen n = fromString <$> Gen.vectorOf n (Gen.elements $ ['0' .. '9'] <> ['a' .. 'f'])

spec :: Spec
spec = do
Expand All @@ -55,7 +52,7 @@ spec = do
prop "deserialization is not supported" do
version1 <- Gen.elements [Nothing, Just (1 :: Int)]
n <- Gen.chooseInt (0, 64)
(mkKey . encodeBase16String -> key) <- Gen.vectorOf n Gen.arbitrary
(mkKey -> key) <- base16EncodedTextGen n
let input :: BL.ByteString
input = Aeson.encode $
Aeson.Object [("721", Aeson.Object
Expand All @@ -70,25 +67,27 @@ spec = do
prop "serialization is not supported" do
version1 <- Gen.elements [Nothing, Just (1 :: Int)]
n <- Gen.chooseInt (0, 64)
(PolicyId . BS.Char8.pack -> policyId) <- Gen.vectorOf n Gen.arbitrary
(mkPolicyId -> policyId) <- base16EncodedTextGen n
let input :: CIP25Metadata
input = CIP25Metadata $ CIP25MetadataLabel [(policyId, [])] version1
actual :: IO ()
actual = let !_ = Aeson.encode input in pure ()
pure $ actual `shouldThrow` Hspec.anyErrorCall
Hspec.describe "version 2" do
it "Parses PolicyId as raw byte string" do
prop "Parses PolicyId as raw byte string" do
((* 2) -> n) <- Gen.chooseInt (0, 32)
(key, policyId) <- (mkKey &&& mkPolicyId) <$> base16EncodedTextGen n
let input :: BL.ByteString
input = Aeson.encode $
Aeson.Object [("721", Aeson.Object
[ ("Sun", Aeson.Object [])
[ (key, Aeson.Object [])
, ("version", Aeson.Number 2)
])]
actual :: Either String CIP25Metadata
actual = Aeson.eitherDecode' input
expected :: Either String CIP25Metadata
expected = Right $ CIP25Metadata $ CIP25MetadataLabel [("53756e", [])] (Just 2)
actual `shouldBe` expected
expected = Right $ CIP25Metadata $ CIP25MetadataLabel [(policyId, [])] (Just 2)
pure $ actual `shouldBe` expected
it "Serializes PolicyId as raw byte string" do
let input :: CIP25Metadata
input = CIP25Metadata $ CIP25MetadataLabel [("53756e", [])] (Just 2)
Expand Down

0 comments on commit d7b1654

Please sign in to comment.