@@ -25,6 +25,7 @@ import Data.ByteString.Lazy qualified as BSL
2525import Data.Map (Map )
2626import Data.Map qualified as Map
2727import Data.Proxy (Proxy (.. ))
28+ import Data.String (IsString (.. ))
2829import Data.Text (Text )
2930import Data.Text qualified as T
3031import Data.Text.Encoding qualified as TE
@@ -41,32 +42,39 @@ instance ToJSON BlueprintScriptVersion where
4142 C. PlutusScriptV3 -> toJSON @ String " v3"
4243
4344instance FromJSON BlueprintScriptVersion where
44- parseJSON = fmap (fmap BlueprintScriptVersion ) $ Aeson. withText " BlueprintScriptVersion" $ \ x -> case T. unpack x of
45- " v1" -> pure (C. AnyPlutusScriptVersion C. PlutusScriptV1 )
46- " v2" -> pure (C. AnyPlutusScriptVersion C. PlutusScriptV2 )
47- " v3" -> pure (C. AnyPlutusScriptVersion C. PlutusScriptV3 )
48- v -> fail $ " Unexpected plutus script version: " <> v
45+ parseJSON = fmap (fmap BlueprintScriptVersion ) $ Aeson. withText " BlueprintScriptVersion" $ \ x -> case T. unpack x of
46+ " v1" -> pure (C. AnyPlutusScriptVersion C. PlutusScriptV1 )
47+ " v2" -> pure (C. AnyPlutusScriptVersion C. PlutusScriptV2 )
48+ " v3" -> pure (C. AnyPlutusScriptVersion C. PlutusScriptV3 )
49+ v -> fail $ " Unexpected plutus script version: " <> v
4950
5051data Blueprint = Blueprint
5152 { preamble :: Preamble
52- , validators :: [ BlueprintValidator ]
53+ , validators :: Map BlueprintKey ScriptInAnyLang
5354 }
5455 deriving stock (Eq , Show , Generic )
55- deriving anyclass (FromJSON )
56+
57+ instance FromJSON Blueprint where
58+ parseJSON = withObject " Blueprint" $ \ obj ->
59+ let mkb p v = Blueprint p <$> deserialise p v in
60+ (mkb
61+ <$> obj .: " preamble"
62+ <*> obj .: " validators" )
63+ >>= either fail pure
5664
5765data BlueprintValidator = BlueprintValidator
58- { title :: BlueprintKey
59- , compiledCode :: Text
60- , hash :: Text
61- }
62- deriving stock (Eq , Show , Generic )
63- deriving anyclass (ToJSON , FromJSON )
66+ { title :: BlueprintKey
67+ , compiledCode :: Text
68+ , hash :: Text
69+ }
70+ deriving stock (Eq , Show , Generic )
71+ deriving anyclass (ToJSON , FromJSON )
6472
6573data Preamble = Preamble
66- { description :: Text
67- , plutusVersion :: BlueprintScriptVersion
68- }
69- deriving stock (Eq , Show , Generic )
74+ { description :: Text
75+ , plutusVersion :: BlueprintScriptVersion
76+ }
77+ deriving stock (Eq , Show , Generic )
7078
7179instance FromJSON Preamble where
7280 parseJSON = withObject " Preamble" $ \ obj ->
@@ -75,13 +83,13 @@ instance FromJSON Preamble where
7583 <*> obj .: " plutusVersion"
7684
7785newtype BlueprintKey = BlueprintKey { unBlueprintKey :: Text }
78- deriving newtype (Eq , Ord , Show , ToJSON , FromJSON )
86+ deriving newtype (Eq , Ord , Show , ToJSON , FromJSON , IsString )
7987
8088loadFromFile :: FilePath -> IO (Either String Blueprint )
8189loadFromFile fp = Aeson. eitherDecode . BSL. fromStrict <$> BS. readFile fp
8290
83- deserialise :: Blueprint -> Either String (Map BlueprintKey ScriptInAnyLang )
84- deserialise Blueprint {preamble = Preamble {plutusVersion = BlueprintScriptVersion v}, validators} =
91+ deserialise :: Preamble -> [ BlueprintValidator ] -> Either String (Map BlueprintKey ScriptInAnyLang )
92+ deserialise Preamble {plutusVersion = BlueprintScriptVersion v} validators =
8593 Map. fromList <$> traverse (deserialiseScript v) validators
8694
8795deserialiseScript :: AnyPlutusScriptVersion -> BlueprintValidator -> Either String (BlueprintKey , ScriptInAnyLang )
0 commit comments