Skip to content

Commit

Permalink
Reformat FromJSON Script to make shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Oct 22, 2020
1 parent 6a1ebd1 commit ea92cac
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions core/lib/Cardano/Address/Script.hs
Expand Up @@ -199,7 +199,7 @@ toScriptHash = ScriptHash . blake2b224 . serialize
-- 28-byte.
--
-- @since 3.0.0
newtype ScriptHash = ScriptHash ByteString
newtype ScriptHash = ScriptHash { unScriptHash :: ByteString }
deriving (Generic, Show, Eq)
instance NFData ScriptHash

Expand All @@ -215,7 +215,7 @@ scriptHashFromBytes bytes
-- multi-signature script. The hash is expected to have size of 28-byte.
--
-- @since 3.0.0
newtype KeyHash = KeyHash ByteString
newtype KeyHash = KeyHash { unKeyHash :: ByteString }
deriving (Generic, Show, Eq)
instance NFData KeyHash

Expand Down Expand Up @@ -339,14 +339,12 @@ instance ToJSON Script where
instance FromJSON Script where
parseJSON v = parseKey v <|> parseAnyOf v <|> parseAllOf v <|> parseAtLeast v
where
parseKey = withText "Script KeyHash" $
either (fail . showErr) (pure . RequireSignatureOf) . keyHashFromText
parseAnyOf =
withObject "Script AnyOf" $ \o -> RequireAnyOf <$> (o .: "any" )
parseAllOf =
withObject "Script AllOf" $ \o -> RequireAllOf <$> (o .: "all" )
parseAtLeast = withObject "Script SomeOf" $ \o -> do
obj <- o .: "some"
content <- obj .: "from"
m <- obj .: "at_least"
RequireSomeOf m <$> parseJSON content
parseKey = withText "Script KeyHash" $
either (fail . showErr) (pure . RequireSignatureOf) . keyHashFromText
parseAnyOf = withObject "Script AnyOf" $ \o ->
RequireAnyOf <$> (o .: "any" )
parseAllOf = withObject "Script AllOf" $ \o ->
RequireAllOf <$> (o .: "all" )
parseAtLeast = withObject "Script SomeOf" $ \o -> do
some <- o .: "some"
RequireSomeOf <$> some .: "at_least" <*> some .: "from"

0 comments on commit ea92cac

Please sign in to comment.