{- |
Module      : Singular.Registry.Blueprint.Load
Description : blueprint loading, code selection and application code
License     : Apache-2.0

Loads a @plutus.json@ blueprint produced by the Aiken compiler,
selects validators by title prefix ('extractScriptHash',
'extractCompiledCode'). The shared compiled-code carrier is 'ApplicationCodes'.

This is the loading-and-code-selection owner extracted from
@Singular.Registry.Blueprint@; the public module re-exports it and is
its only intended consumer surface.
-}
module Singular.Registry.Blueprint.Load
    ( -- * Loading
      loadBlueprint

      -- * Supplied application and token-policy code
    , ApplicationCodes (..)

      -- * Script hash extraction
    , extractScriptHash

      -- * Compiled code extraction
    , extractCompiledCode
    ) where

import Data.Aeson qualified as Aeson
import Data.ByteString qualified as BS
import Data.ByteString.Short qualified as SBS
import Data.Char (isDigit)
import Data.Text (Text)
import Data.Text qualified as T
import Data.Word (Word8)
import Singular.Registry.Blueprint.Schema
    ( Blueprint (..)
    , Validator (..)
    )

-- ---------------------------------------------------------
-- Loading
-- ---------------------------------------------------------

{- | Load and parse a CIP-57 blueprint from a file
path.
-}
loadBlueprint
    :: FilePath
    -- ^ Path to the @plutus.json@ file
    -> IO (Either String Blueprint)
loadBlueprint :: FilePath -> IO (Either FilePath Blueprint)
loadBlueprint FilePath
path = do
    bs <- FilePath -> IO ByteString
BS.readFile FilePath
path
    pure $ Aeson.eitherDecodeStrict' bs

-- ---------------------------------------------------------
-- Script hash extraction
-- ---------------------------------------------------------

{- | Find the first validator whose title starts with
the given prefix and return its hash.
-}
extractScriptHash
    :: Text
    -- ^ Title prefix to match
    -> Blueprint
    -- ^ Blueprint to search
    -> Maybe Text
extractScriptHash :: Text -> Blueprint -> Maybe Text
extractScriptHash Text
prefix Blueprint
bp =
    case (Validator -> Bool) -> [Validator] -> [Validator]
forall a. (a -> Bool) -> [a] -> [a]
filter
        (Text -> Text -> Bool
T.isPrefixOf Text
prefix (Text -> Bool) -> (Validator -> Text) -> Validator -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Validator -> Text
vTitle)
        (Blueprint -> [Validator]
validators Blueprint
bp) of
        (Validator
v : [Validator]
_) -> Text -> Maybe Text
forall a. a -> Maybe a
Just (Validator -> Text
vHash Validator
v)
        [] -> Maybe Text
forall a. Maybe a
Nothing

-- ---------------------------------------------------------
-- Compiled code extraction
-- ---------------------------------------------------------

{- | Find the first validator whose title starts with
the given prefix and return its compiled script
bytes as a 'ShortByteString'. The hex-encoded
@compiledCode@ is decoded to raw bytes suitable
for 'PlutusBinary'.
-}
extractCompiledCode
    :: Text
    -- ^ Title prefix to match
    -> Blueprint
    -- ^ Blueprint to search
    -> Maybe SBS.ShortByteString
extractCompiledCode :: Text -> Blueprint -> Maybe ShortByteString
extractCompiledCode Text
prefix Blueprint
bp = do
    v <-
        case (Validator -> Bool) -> [Validator] -> [Validator]
forall a. (a -> Bool) -> [a] -> [a]
filter
            (Text -> Text -> Bool
T.isPrefixOf Text
prefix (Text -> Bool) -> (Validator -> Text) -> Validator -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Validator -> Text
vTitle)
            (Blueprint -> [Validator]
validators Blueprint
bp) of
            (Validator
x : [Validator]
_) -> Validator -> Maybe Validator
forall a. a -> Maybe a
Just Validator
x
            [] -> Maybe Validator
forall a. Maybe a
Nothing
    hex <- vCompiledCode v
    SBS.toShort <$> decodeHex hex

{- | Decode a hex 'Text' to 'ByteString'.
Returns 'Nothing' on invalid input.
-}
decodeHex :: Text -> Maybe BS.ByteString
decodeHex :: Text -> Maybe ByteString
decodeHex Text
t
    | Int -> Bool
forall a. Integral a => a -> Bool
odd (Text -> Int
T.length Text
t) = Maybe ByteString
forall a. Maybe a
Nothing
    | Bool
otherwise =
        [Word8] -> ByteString
BS.pack ([Word8] -> ByteString) -> Maybe [Word8] -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> Maybe [Word8]
go (Text -> FilePath
T.unpack Text
t)
  where
    go :: FilePath -> Maybe [Word8]
go [] = [Word8] -> Maybe [Word8]
forall a. a -> Maybe a
Just []
    go (Char
a : Char
b : FilePath
rest) = do
        hi <- Char -> Maybe Word8
hexDigit Char
a
        lo <- hexDigit b
        (hi * 16 + lo :) <$> go rest
    go FilePath
_ = Maybe [Word8]
forall a. Maybe a
Nothing

    hexDigit :: Char -> Maybe Word8
    hexDigit :: Char -> Maybe Word8
hexDigit Char
c
        | Char -> Bool
isDigit Char
c =
            Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (Word8 -> Maybe Word8) -> Word8 -> Maybe Word8
forall a b. (a -> b) -> a -> b
$
                Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral
                    (Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'0')
        | Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'f' =
            Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (Word8 -> Maybe Word8) -> Word8 -> Maybe Word8
forall a b. (a -> b) -> a -> b
$
                Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral
                    ( Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c
                        Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'a'
                        Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10
                    )
        | Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'F' =
            Word8 -> Maybe Word8
forall a. a -> Maybe a
Just (Word8 -> Maybe Word8) -> Word8 -> Maybe Word8
forall a b. (a -> b) -> a -> b
$
                Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral
                    ( Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
c
                        Int -> Int -> Int
forall a. Num a => a -> a -> a
- Char -> Int
forall a. Enum a => a -> Int
fromEnum Char
'A'
                        Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
10
                    )
        | Bool
otherwise = Maybe Word8
forall a. Maybe a
Nothing

{- | Supplied application and active-token-policy code for the shared
identity adapters. The caller chooses the application; no application
is built into the registry library.
-}
data ApplicationCodes = ApplicationCodes
    { ApplicationCodes -> ShortByteString
ncApplication :: SBS.ShortByteString
    , ApplicationCodes -> ShortByteString
ncWitness :: SBS.ShortByteString
    }