{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
module Test.Crypto.Vector.SerializationUtils (
unHex,
unsafeUnHex,
SignatureResult,
HexStringInCBOR (..),
sKeyParser,
vKeyParser,
sigParser,
dropBytes,
hexByteStringLength,
)
where
import Cardano.Binary (FromCBOR, serialize', unsafeDeserialize')
import Cardano.Crypto.DSIGN (
DSIGNAlgorithm (SigDSIGN, SignKeyDSIGN, VerKeyDSIGN),
)
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as BS16
import qualified Data.ByteString.Char8 as BS8
import Data.String (IsString (fromString))
import Prelude hiding (drop)
newtype HexStringInCBOR = HexCBOR ByteString
instance IsString HexStringInCBOR where
fromString :: String -> HexStringInCBOR
fromString String
s =
let bs :: ByteString
bs = ByteString -> ByteString
unsafeUnHex forall a b. (a -> b) -> a -> b
$ String -> ByteString
BS8.pack String
s
cborBs :: ByteString
cborBs = forall a. ToCBOR a => a -> ByteString
serialize' ByteString
bs
in ByteString -> HexStringInCBOR
HexCBOR ByteString
cborBs
instance Show HexStringInCBOR where
show :: HexStringInCBOR -> String
show (HexCBOR ByteString
bs) = ByteString -> String
BS8.unpack forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
BS16.encode ByteString
bs
dropBytes :: Int -> HexStringInCBOR -> HexStringInCBOR
dropBytes :: Int -> HexStringInCBOR -> HexStringInCBOR
dropBytes Int
n (HexCBOR ByteString
bs) = ByteString -> HexStringInCBOR
HexCBOR forall a b. (a -> b) -> a -> b
$ forall a. ToCBOR a => a -> ByteString
serialize' forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.drop Int
n (forall a. FromCBOR a => ByteString -> a
unsafeDeserialize' ByteString
bs)
hexByteStringLength :: HexStringInCBOR -> Integer
hexByteStringLength :: HexStringInCBOR -> Integer
hexByteStringLength (HexCBOR ByteString
bs) = forall a. Integral a => a -> Integer
toInteger forall a b. (a -> b) -> a -> b
$ ByteString -> Int
BS.length forall a b. (a -> b) -> a -> b
$ forall a. FromCBOR a => ByteString -> a
unsafeDeserialize' ByteString
bs
unHex :: ByteString -> Either String ByteString
unHex :: ByteString -> Either String ByteString
unHex = ByteString -> Either String ByteString
BS16.decode
unsafeUnHex :: ByteString -> ByteString
unsafeUnHex :: ByteString -> ByteString
unsafeUnHex ByteString
hexBs = case ByteString -> Either String ByteString
unHex ByteString
hexBs of
Left String
err -> forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"Couldn't unHex the Hex string. Incorrect format: " forall a. [a] -> [a] -> [a]
++ String
err
Right ByteString
bytes' -> ByteString
bytes'
type SignatureResult = (Either String ())
sKeyParser :: forall d. FromCBOR (SignKeyDSIGN d) => HexStringInCBOR -> SignKeyDSIGN d
sKeyParser :: forall d.
FromCBOR (SignKeyDSIGN d) =>
HexStringInCBOR -> SignKeyDSIGN d
sKeyParser (HexCBOR ByteString
bs) = forall a. FromCBOR a => ByteString -> a
unsafeDeserialize' ByteString
bs
vKeyParser :: forall d. FromCBOR (VerKeyDSIGN d) => HexStringInCBOR -> VerKeyDSIGN d
vKeyParser :: forall d.
FromCBOR (VerKeyDSIGN d) =>
HexStringInCBOR -> VerKeyDSIGN d
vKeyParser (HexCBOR ByteString
bs) = forall a. FromCBOR a => ByteString -> a
unsafeDeserialize' ByteString
bs
sigParser :: forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser :: forall d. FromCBOR (SigDSIGN d) => HexStringInCBOR -> SigDSIGN d
sigParser (HexCBOR ByteString
bs) = forall a. FromCBOR a => ByteString -> a
unsafeDeserialize' ByteString
bs