module Singular.Registry.Blueprint.Load
(
loadBlueprint
, ApplicationCodes (..)
, extractScriptHash
, 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 (..)
)
loadBlueprint
:: FilePath
-> 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
extractScriptHash
:: Text
-> Blueprint
-> Maybe Text
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
extractCompiledCode
:: Text
-> Blueprint
-> Maybe SBS.ShortByteString
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
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
data ApplicationCodes = ApplicationCodes
{ ApplicationCodes -> ShortByteString
ncApplication :: SBS.ShortByteString
, ApplicationCodes -> ShortByteString
ncWitness :: SBS.ShortByteString
}