{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Crypto.Orphans () where

import Cardano.Ledger.Binary (
  DecCBOR (..),
  EncCBOR (..),
  FromCBOR (..),
  Size,
  ToCBOR (..),
  encodeBytes,
  fromByronCBOR,
  toByronCBOR,
  toCborError,
  withWordSize,
 )
import Cardano.Prelude hiding (toCborError)
import Crypto.Error (CryptoFailable (..))
import qualified Crypto.PubKey.Ed25519 as Ed25519
import Data.Aeson (FromJSON (..), ToJSON (..))
import qualified Data.ByteArray as BA
import qualified Data.ByteString as BS
import Data.ByteString.Base64.Type (getByteString64, makeByteString64)
import qualified Data.Text as T

fromByteStringToBytes :: ByteString -> BA.Bytes
fromByteStringToBytes :: ByteString -> Bytes
fromByteStringToBytes = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert

fromByteStringToScrubbedBytes :: ByteString -> BA.ScrubbedBytes
fromByteStringToScrubbedBytes :: ByteString -> ScrubbedBytes
fromByteStringToScrubbedBytes = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert

toByteString :: BA.ByteArrayAccess bin => bin -> ByteString
toByteString :: forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString = forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert

fromCryptoFailable :: T.Text -> CryptoFailable a -> Either T.Text a
fromCryptoFailable :: forall a. Text -> CryptoFailable a -> Either Text a
fromCryptoFailable Text
item (CryptoFailed CryptoError
e) =
  forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ Text
"Cardano.Crypto.Orphan." forall a. Semigroup a => a -> a -> a
<> Text
item forall a. Semigroup a => a -> a -> a
<> Text
" failed because " forall a. Semigroup a => a -> a -> a
<> forall a b. (Show a, ConvertText String b) => a -> b
show CryptoError
e
fromCryptoFailable Text
_ (CryptoPassed a
r) = forall (m :: * -> *) a. Monad m => a -> m a
return a
r

instance FromJSON Ed25519.PublicKey where
  parseJSON :: Value -> Parser PublicKey
parseJSON Value
v = do
    CryptoFailable PublicKey
res <-
      forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed25519.publicKey
        forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Bytes
fromByteStringToBytes
        forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString64 -> ByteString
getByteString64
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
    forall e a. Buildable e => Either e a -> Parser a
toAesonError forall a b. (a -> b) -> a -> b
$ forall a. Text -> CryptoFailable a -> Either Text a
fromCryptoFailable Text
"parseJSON Ed25519.PublicKey" CryptoFailable PublicKey
res

instance ToJSON Ed25519.PublicKey where
  toJSON :: PublicKey -> Value
toJSON = forall a. ToJSON a => a -> Value
toJSON forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> ByteString64
makeByteString64 forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString

instance FromJSON Ed25519.Signature where
  parseJSON :: Value -> Parser Signature
parseJSON Value
v = do
    CryptoFailable Signature
res <-
      forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature
        forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Bytes
fromByteStringToBytes
        forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString64 -> ByteString
getByteString64
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. FromJSON a => Value -> Parser a
parseJSON Value
v
    forall e a. Buildable e => Either e a -> Parser a
toAesonError forall a b. (a -> b) -> a -> b
$ forall a. Text -> CryptoFailable a -> Either Text a
fromCryptoFailable Text
"parseJSON Ed25519.Signature" CryptoFailable Signature
res

instance ToJSON Ed25519.Signature where
  toJSON :: Signature -> Value
toJSON = forall a. ToJSON a => a -> Value
toJSON forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> ByteString64
makeByteString64 forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString

instance ToCBOR Ed25519.PublicKey where
  toCBOR :: PublicKey -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR
instance FromCBOR Ed25519.PublicKey where
  fromCBOR :: forall s. Decoder s PublicKey
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR
instance ToCBOR Ed25519.SecretKey where
  toCBOR :: SecretKey -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR
instance FromCBOR Ed25519.SecretKey where
  fromCBOR :: forall s. Decoder s SecretKey
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR
instance ToCBOR Ed25519.Signature where
  toCBOR :: Signature -> Encoding
toCBOR = forall a. EncCBOR a => a -> Encoding
toByronCBOR
instance FromCBOR Ed25519.Signature where
  fromCBOR :: forall s. Decoder s Signature
fromCBOR = forall a s. DecCBOR a => Decoder s a
fromByronCBOR

instance EncCBOR Ed25519.PublicKey where
  encCBOR :: PublicKey -> Encoding
encCBOR = ByteString -> Encoding
encodeBytes forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString
  encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size) -> Proxy PublicKey -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
_ Proxy PublicKey
_ = Int -> Size
bsSize Int
32

instance DecCBOR Ed25519.PublicKey where
  decCBOR :: forall s. Decoder s PublicKey
decCBOR = do
    CryptoFailable PublicKey
res <- forall ba. ByteArrayAccess ba => ba -> CryptoFailable PublicKey
Ed25519.publicKey forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Bytes
fromByteStringToBytes forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR
    forall (m :: * -> *) e a.
(MonadFail m, Buildable e) =>
Either e a -> m a
toCborError forall a b. (a -> b) -> a -> b
$ forall a. Text -> CryptoFailable a -> Either Text a
fromCryptoFailable Text
"decCBOR Ed25519.PublicKey" CryptoFailable PublicKey
res

instance EncCBOR Ed25519.SecretKey where
  encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size) -> Proxy SecretKey -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
_ Proxy SecretKey
_ = Int -> Size
bsSize Int
64
  encCBOR :: SecretKey -> Encoding
encCBOR SecretKey
sk =
    ByteString -> Encoding
encodeBytes
      forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString -> ByteString
BS.append (forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString SecretKey
sk) (forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString forall a b. (a -> b) -> a -> b
$ SecretKey -> PublicKey
Ed25519.toPublic SecretKey
sk)

instance DecCBOR Ed25519.SecretKey where
  decCBOR :: forall s. Decoder s SecretKey
decCBOR = do
    CryptoFailable SecretKey
res <-
      forall ba. ByteArrayAccess ba => ba -> CryptoFailable SecretKey
Ed25519.secretKey
        forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> ScrubbedBytes
fromByteStringToScrubbedBytes
        forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Int -> ByteString -> ByteString
BS.take Int
Ed25519.secretKeySize
        forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR
    forall (m :: * -> *) e a.
(MonadFail m, Buildable e) =>
Either e a -> m a
toCborError forall a b. (a -> b) -> a -> b
$ forall a. Text -> CryptoFailable a -> Either Text a
fromCryptoFailable Text
"decCBOR Ed25519.SecretKey" CryptoFailable SecretKey
res

instance EncCBOR Ed25519.Signature where
  encodedSizeExpr :: (forall t. EncCBOR t => Proxy t -> Size) -> Proxy Signature -> Size
encodedSizeExpr forall t. EncCBOR t => Proxy t -> Size
_ Proxy Signature
_ = Int -> Size
bsSize Int
64
  encCBOR :: Signature -> Encoding
encCBOR = ByteString -> Encoding
encodeBytes forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. forall bin. ByteArrayAccess bin => bin -> ByteString
toByteString

instance DecCBOR Ed25519.Signature where
  decCBOR :: forall s. Decoder s Signature
decCBOR = do
    CryptoFailable Signature
res <- forall ba. ByteArrayAccess ba => ba -> CryptoFailable Signature
Ed25519.signature forall {k} (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. ByteString -> Bytes
fromByteStringToBytes forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a s. DecCBOR a => Decoder s a
decCBOR
    forall (m :: * -> *) e a.
(MonadFail m, Buildable e) =>
Either e a -> m a
toCborError forall a b. (a -> b) -> a -> b
$ forall a. Text -> CryptoFailable a -> Either Text a
fromCryptoFailable Text
"decCBOR Ed25519.Signature" CryptoFailable Signature
res

-- Helper for encodedSizeExpr in EncCBOR instances
bsSize :: Int -> Size
bsSize :: Int -> Size
bsSize Int
x = forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int
x forall a. Num a => a -> a -> a
+ forall s a. (Integral s, Integral a) => s -> a
withWordSize Int
x)