{-# LANGUAGE DeriveLift #-}
module Morley.Tezos.Crypto
(
PublicKey (..)
, SecretKey (..)
, Signature (..)
, HashTag (..)
, KeyHashTag
, Hash (..)
, HashKind (..)
, KeyHash
, KeyHashL2
, ContractHash
, TxRollupHash
, BLS12381.Bls12381Fr
, BLS12381.Bls12381G1
, BLS12381.Bls12381G2
, detSecretKey
, toPublic
, signatureToBytes
, mkSignature
, parseSignatureRaw
, signatureLengthBytes
, checkSignature
, sign
, CryptoParseError (..)
, formatPublicKey
, mformatPublicKey
, parsePublicKey
, parsePublicKeyRaw
, formatSignature
, mformatSignature
, parseSignature
, formatHash
, mformatHash
, parseHash
, parseKeyHashRaw
, parseKeyHashL2Raw
, hashLengthBytes
, formatSecretKey
, parseSecretKey
, hashKey
, blake2b
, blake2b160
, keccak
, sha256
, sha3
, sha512
, Chest
, ChestKey
, OpeningResult(..)
, TLTime(..)
, openChest
, mkTLTime
, toTLTime
, encodeBase58Check
, decodeBase58Check
, B58CheckWithPrefixError (..)
, decodeBase58CheckWithPrefix
, keyDecoders
, keyHashDecoders
, allHashTags
) where
import Crypto.Random (MonadRandom)
import Data.Aeson (FromJSON(..), FromJSONKey, ToJSON(..), ToJSONKey)
import Data.Aeson qualified as Aeson
import Data.Aeson.Encoding qualified as Aeson
import Data.Aeson.Types qualified as AesonTypes
import Data.Binary.Get qualified as Get
import Data.ByteArray qualified as BA
import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as LBS
import Data.Text qualified as T
import Fmt (Buildable, build, hexF, pretty)
import Instances.TH.Lift ()
import Language.Haskell.TH.Syntax (Lift)
import Morley.Michelson.Text
import Morley.Tezos.Crypto.BLS12381 qualified as BLS12381
import Morley.Tezos.Crypto.Ed25519 qualified as Ed25519
import Morley.Tezos.Crypto.Hash
import Morley.Tezos.Crypto.P256 qualified as P256
import Morley.Tezos.Crypto.Secp256k1 qualified as Secp256k1
import Morley.Tezos.Crypto.Timelock
(Chest, ChestKey, OpeningResult(..), TLTime(..), mkTLTime, openChest, toTLTime)
import Morley.Tezos.Crypto.Util
import Morley.Util.Binary
import Morley.Util.CLI
import Morley.Util.TH (deriveGADTNFData)
data HashKind
= HashKindPublicKey
| HashKindL2PublicKey
| HashKindContract
| HashKindTxRollup
data HashTag (kind :: HashKind) where
HashEd25519 :: HashTag 'HashKindPublicKey
HashSecp256k1 :: HashTag 'HashKindPublicKey
HashP256 :: HashTag 'HashKindPublicKey
HashContract :: HashTag 'HashKindContract
HashBLS :: HashTag 'HashKindL2PublicKey
HashTXR :: HashTag 'HashKindTxRollup
deriving stock instance Show (HashTag kind)
deriving stock instance Eq (HashTag kind)
deriving stock instance Ord (HashTag kind)
deriving stock instance Lift (HashTag kind)
deriveGADTNFData ''HashTag
data PublicKey
= PublicKeyEd25519 Ed25519.PublicKey
| PublicKeySecp256k1 Secp256k1.PublicKey
| PublicKeyP256 P256.PublicKey
deriving stock (Int -> PublicKey -> ShowS
[PublicKey] -> ShowS
PublicKey -> String
(Int -> PublicKey -> ShowS)
-> (PublicKey -> String)
-> ([PublicKey] -> ShowS)
-> Show PublicKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PublicKey] -> ShowS
$cshowList :: [PublicKey] -> ShowS
show :: PublicKey -> String
$cshow :: PublicKey -> String
showsPrec :: Int -> PublicKey -> ShowS
$cshowsPrec :: Int -> PublicKey -> ShowS
Show, PublicKey -> PublicKey -> Bool
(PublicKey -> PublicKey -> Bool)
-> (PublicKey -> PublicKey -> Bool) -> Eq PublicKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PublicKey -> PublicKey -> Bool
$c/= :: PublicKey -> PublicKey -> Bool
== :: PublicKey -> PublicKey -> Bool
$c== :: PublicKey -> PublicKey -> Bool
Eq, Eq PublicKey
Eq PublicKey
-> (PublicKey -> PublicKey -> Ordering)
-> (PublicKey -> PublicKey -> Bool)
-> (PublicKey -> PublicKey -> Bool)
-> (PublicKey -> PublicKey -> Bool)
-> (PublicKey -> PublicKey -> Bool)
-> (PublicKey -> PublicKey -> PublicKey)
-> (PublicKey -> PublicKey -> PublicKey)
-> Ord PublicKey
PublicKey -> PublicKey -> Bool
PublicKey -> PublicKey -> Ordering
PublicKey -> PublicKey -> PublicKey
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: PublicKey -> PublicKey -> PublicKey
$cmin :: PublicKey -> PublicKey -> PublicKey
max :: PublicKey -> PublicKey -> PublicKey
$cmax :: PublicKey -> PublicKey -> PublicKey
>= :: PublicKey -> PublicKey -> Bool
$c>= :: PublicKey -> PublicKey -> Bool
> :: PublicKey -> PublicKey -> Bool
$c> :: PublicKey -> PublicKey -> Bool
<= :: PublicKey -> PublicKey -> Bool
$c<= :: PublicKey -> PublicKey -> Bool
< :: PublicKey -> PublicKey -> Bool
$c< :: PublicKey -> PublicKey -> Bool
compare :: PublicKey -> PublicKey -> Ordering
$ccompare :: PublicKey -> PublicKey -> Ordering
Ord, (forall x. PublicKey -> Rep PublicKey x)
-> (forall x. Rep PublicKey x -> PublicKey) -> Generic PublicKey
forall x. Rep PublicKey x -> PublicKey
forall x. PublicKey -> Rep PublicKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PublicKey x -> PublicKey
$cfrom :: forall x. PublicKey -> Rep PublicKey x
Generic)
instance NFData PublicKey
data SecretKey
= SecretKeyEd25519 Ed25519.SecretKey
| SecretKeySecp256k1 Secp256k1.SecretKey
| SecretKeyP256 P256.SecretKey
deriving stock (Int -> SecretKey -> ShowS
[SecretKey] -> ShowS
SecretKey -> String
(Int -> SecretKey -> ShowS)
-> (SecretKey -> String)
-> ([SecretKey] -> ShowS)
-> Show SecretKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SecretKey] -> ShowS
$cshowList :: [SecretKey] -> ShowS
show :: SecretKey -> String
$cshow :: SecretKey -> String
showsPrec :: Int -> SecretKey -> ShowS
$cshowsPrec :: Int -> SecretKey -> ShowS
Show, SecretKey -> SecretKey -> Bool
(SecretKey -> SecretKey -> Bool)
-> (SecretKey -> SecretKey -> Bool) -> Eq SecretKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SecretKey -> SecretKey -> Bool
$c/= :: SecretKey -> SecretKey -> Bool
== :: SecretKey -> SecretKey -> Bool
$c== :: SecretKey -> SecretKey -> Bool
Eq, (forall x. SecretKey -> Rep SecretKey x)
-> (forall x. Rep SecretKey x -> SecretKey) -> Generic SecretKey
forall x. Rep SecretKey x -> SecretKey
forall x. SecretKey -> Rep SecretKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep SecretKey x -> SecretKey
$cfrom :: forall x. SecretKey -> Rep SecretKey x
Generic)
instance NFData SecretKey
instance HasCLReader SecretKey where
getReader :: ReadM SecretKey
getReader = (String -> Either String SecretKey) -> ReadM SecretKey
forall a. (String -> Either String a) -> ReadM a
eitherReader ((CryptoParseError -> String)
-> Either CryptoParseError SecretKey -> Either String SecretKey
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first CryptoParseError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty (Either CryptoParseError SecretKey -> Either String SecretKey)
-> (String -> Either CryptoParseError SecretKey)
-> String
-> Either String SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError SecretKey
parseSecretKey (Text -> Either CryptoParseError SecretKey)
-> (String -> Text) -> String -> Either CryptoParseError SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. ToText a => a -> Text
toText)
getMetavar :: String
getMetavar = String
"SECRET_KEY"
detSecretKey :: HasCallStack => ByteString -> SecretKey
detSecretKey :: HasCallStack => ByteString -> SecretKey
detSecretKey ByteString
seed = ByteString
seed ByteString -> (ByteString -> SecretKey) -> SecretKey
forall a b. a -> (a -> b) -> b
& case (ByteString -> Int
forall t. Container t => t -> Int
length ByteString
seed Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
3 of
Int
0 -> SecretKey -> SecretKey
SecretKeyEd25519 (SecretKey -> SecretKey)
-> (ByteString -> SecretKey) -> ByteString -> SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> SecretKey
Ed25519.detSecretKey
Int
1 -> SecretKey -> SecretKey
SecretKeySecp256k1 (SecretKey -> SecretKey)
-> (ByteString -> SecretKey) -> ByteString -> SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> SecretKey
Secp256k1.detSecretKey
Int
2 -> SecretKey -> SecretKey
SecretKeyP256 (SecretKey -> SecretKey)
-> (ByteString -> SecretKey) -> ByteString -> SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> SecretKey
P256.detSecretKey
Int
_ -> Text -> ByteString -> SecretKey
forall a. HasCallStack => Text -> a
error Text
"detSecretKey: unexpected happened"
toPublic :: SecretKey -> PublicKey
toPublic :: SecretKey -> PublicKey
toPublic = \case
SecretKeyEd25519 SecretKey
sk -> PublicKey -> PublicKey
PublicKeyEd25519 (PublicKey -> PublicKey)
-> (SecretKey -> PublicKey) -> SecretKey -> PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretKey -> PublicKey
Ed25519.toPublic (SecretKey -> PublicKey) -> SecretKey -> PublicKey
forall a b. (a -> b) -> a -> b
$ SecretKey
sk
SecretKeySecp256k1 SecretKey
sk -> PublicKey -> PublicKey
PublicKeySecp256k1 (PublicKey -> PublicKey)
-> (SecretKey -> PublicKey) -> SecretKey -> PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretKey -> PublicKey
Secp256k1.toPublic (SecretKey -> PublicKey) -> SecretKey -> PublicKey
forall a b. (a -> b) -> a -> b
$ SecretKey
sk
SecretKeyP256 SecretKey
sk -> PublicKey -> PublicKey
PublicKeyP256 (PublicKey -> PublicKey)
-> (SecretKey -> PublicKey) -> SecretKey -> PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretKey -> PublicKey
P256.toPublic (SecretKey -> PublicKey) -> SecretKey -> PublicKey
forall a b. (a -> b) -> a -> b
$ SecretKey
sk
data Signature
= SignatureEd25519 Ed25519.Signature
| SignatureSecp256k1 Secp256k1.Signature
| SignatureP256 P256.Signature
| SignatureGeneric ByteString
deriving stock (Int -> Signature -> ShowS
[Signature] -> ShowS
Signature -> String
(Int -> Signature -> ShowS)
-> (Signature -> String)
-> ([Signature] -> ShowS)
-> Show Signature
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Signature] -> ShowS
$cshowList :: [Signature] -> ShowS
show :: Signature -> String
$cshow :: Signature -> String
showsPrec :: Int -> Signature -> ShowS
$cshowsPrec :: Int -> Signature -> ShowS
Show, (forall x. Signature -> Rep Signature x)
-> (forall x. Rep Signature x -> Signature) -> Generic Signature
forall x. Rep Signature x -> Signature
forall x. Signature -> Rep Signature x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Signature x -> Signature
$cfrom :: forall x. Signature -> Rep Signature x
Generic)
instance NFData Signature
instance Eq Signature where
Signature
sig1 == :: Signature -> Signature -> Bool
== Signature
sig2 = case (Signature
sig1, Signature
sig2) of
(SignatureGeneric ByteString
bytes1, SignatureGeneric ByteString
bytes2) -> ByteString
bytes1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes2
(SignatureGeneric ByteString
bytes1, SignatureEd25519 (Signature -> ByteString
forall ba. ByteArray ba => Signature -> ba
Ed25519.signatureToBytes -> ByteString
bytes2)) ->
ByteString
bytes1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes2
(SignatureGeneric ByteString
bytes1, SignatureSecp256k1 (Signature -> ByteString
forall ba. ByteArray ba => Signature -> ba
Secp256k1.signatureToBytes -> ByteString
bytes2)) ->
ByteString
bytes1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes2
(SignatureGeneric ByteString
bytes1, SignatureP256 (Signature -> ByteString
forall ba. ByteArray ba => Signature -> ba
P256.signatureToBytes -> ByteString
bytes2)) ->
ByteString
bytes1 ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== ByteString
bytes2
(Signature
_, SignatureGeneric {}) -> Signature
sig2 Signature -> Signature -> Bool
forall a. Eq a => a -> a -> Bool
== Signature
sig1
(SignatureEd25519 Signature
s1, SignatureEd25519 Signature
s2) -> Signature
s1 Signature -> Signature -> Bool
forall a. Eq a => a -> a -> Bool
== Signature
s2
(SignatureEd25519 {}, Signature
_) -> Bool
False
(SignatureSecp256k1 Signature
s1, SignatureSecp256k1 Signature
s2) -> Signature
s1 Signature -> Signature -> Bool
forall a. Eq a => a -> a -> Bool
== Signature
s2
(SignatureSecp256k1 {}, Signature
_) -> Bool
False
(SignatureP256 Signature
s1, SignatureP256 Signature
s2) -> Signature
s1 Signature -> Signature -> Bool
forall a. Eq a => a -> a -> Bool
== Signature
s2
(SignatureP256 {}, Signature
_) -> Bool
False
instance Ord Signature where
compare :: Signature -> Signature -> Ordering
compare = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (ByteString -> ByteString -> Ordering)
-> (Signature -> ByteString) -> Signature -> Signature -> Ordering
forall b c a. (b -> b -> c) -> (a -> b) -> a -> a -> c
`on` (Signature -> ByteString
forall ba. ByteArray ba => Signature -> ba
signatureToBytes :: Signature -> ByteString)
signatureToBytes :: BA.ByteArray ba => Signature -> ba
signatureToBytes :: forall ba. ByteArray ba => Signature -> ba
signatureToBytes = \case
SignatureEd25519 Signature
sig -> Signature -> ba
forall ba. ByteArray ba => Signature -> ba
Ed25519.signatureToBytes Signature
sig
SignatureSecp256k1 Signature
sig -> Signature -> ba
forall ba. ByteArray ba => Signature -> ba
Secp256k1.signatureToBytes Signature
sig
SignatureP256 Signature
sig -> Signature -> ba
forall ba. ByteArray ba => Signature -> ba
P256.signatureToBytes Signature
sig
SignatureGeneric ByteString
bytes -> ByteString -> ba
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ByteString
bytes
mkSignature :: BA.ByteArray ba => ba -> Maybe Signature
mkSignature :: forall ba. ByteArray ba => ba -> Maybe Signature
mkSignature ba
ba =
ByteString -> Signature
SignatureGeneric (ba -> ByteString
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
BA.convert ba
ba) Signature -> Maybe () -> Maybe Signature
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
l Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall n. (HasCallStack, Integral n) => n
signatureLengthBytes)
where
l :: Int
l = ba -> Int
forall ba. ByteArrayAccess ba => ba -> Int
BA.length ba
ba
parseSignatureRaw :: ByteString -> Either ParseSignatureRawError Signature
parseSignatureRaw :: ByteString -> Either ParseSignatureRawError Signature
parseSignatureRaw ByteString
ba = ParseSignatureRawError
-> Maybe Signature -> Either ParseSignatureRawError Signature
forall l r. l -> Maybe r -> Either l r
maybeToRight (ByteString -> ParseSignatureRawError
ParseSignatureRawWrongSize ByteString
ba) (Maybe Signature -> Either ParseSignatureRawError Signature)
-> Maybe Signature -> Either ParseSignatureRawError Signature
forall a b. (a -> b) -> a -> b
$ ByteString -> Maybe Signature
forall ba. ByteArray ba => ba -> Maybe Signature
mkSignature ByteString
ba
data ParseSignatureRawError
= ParseSignatureRawWrongSize ByteString
deriving stock (ParseSignatureRawError -> ParseSignatureRawError -> Bool
(ParseSignatureRawError -> ParseSignatureRawError -> Bool)
-> (ParseSignatureRawError -> ParseSignatureRawError -> Bool)
-> Eq ParseSignatureRawError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParseSignatureRawError -> ParseSignatureRawError -> Bool
$c/= :: ParseSignatureRawError -> ParseSignatureRawError -> Bool
== :: ParseSignatureRawError -> ParseSignatureRawError -> Bool
$c== :: ParseSignatureRawError -> ParseSignatureRawError -> Bool
Eq, Int -> ParseSignatureRawError -> ShowS
[ParseSignatureRawError] -> ShowS
ParseSignatureRawError -> String
(Int -> ParseSignatureRawError -> ShowS)
-> (ParseSignatureRawError -> String)
-> ([ParseSignatureRawError] -> ShowS)
-> Show ParseSignatureRawError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseSignatureRawError] -> ShowS
$cshowList :: [ParseSignatureRawError] -> ShowS
show :: ParseSignatureRawError -> String
$cshow :: ParseSignatureRawError -> String
showsPrec :: Int -> ParseSignatureRawError -> ShowS
$cshowsPrec :: Int -> ParseSignatureRawError -> ShowS
Show, (forall x. ParseSignatureRawError -> Rep ParseSignatureRawError x)
-> (forall x.
Rep ParseSignatureRawError x -> ParseSignatureRawError)
-> Generic ParseSignatureRawError
forall x. Rep ParseSignatureRawError x -> ParseSignatureRawError
forall x. ParseSignatureRawError -> Rep ParseSignatureRawError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ParseSignatureRawError x -> ParseSignatureRawError
$cfrom :: forall x. ParseSignatureRawError -> Rep ParseSignatureRawError x
Generic)
instance Buildable ParseSignatureRawError where
build :: ParseSignatureRawError -> Builder
build =
\case
ParseSignatureRawWrongSize ByteString
ba -> Builder
"Given raw signature " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>
ByteString -> Builder
forall a. FormatAsHex a => a -> Builder
hexF ByteString
ba Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
" has invalid length " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Int -> Builder
forall b a. (PrettyShow a, Show a, IsString b) => a -> b
show (ByteString -> Int
forall t. Container t => t -> Int
length ByteString
ba)
signatureLengthBytes :: HasCallStack => Integral n => n
signatureLengthBytes :: forall n. (HasCallStack, Integral n) => n
signatureLengthBytes
| (Element [Int] -> Bool) -> [Int] -> Bool
forall t. Container t => (Element t -> Bool) -> t -> Bool
all Int -> Bool
Element [Int] -> Bool
is64
[ Int
forall n. (Integral n, CheckIntSubType Int n) => n
Ed25519.signatureLengthBytes
, Int
forall n. (Integral n, CheckIntSubType Int n) => n
P256.signatureLengthBytes
, Int
forall n. (Integral n, CheckIntSubType Int n) => n
Secp256k1.signatureLengthBytes
] = n
64
| Bool
otherwise =
Text -> n
forall a. HasCallStack => Text -> a
error Text
"Apparently our understanding of signatures in Tezos is broken"
where
is64 :: Int -> Bool
is64 :: Int -> Bool
is64 = (Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
64)
genericSignatureTag :: ByteString
genericSignatureTag :: ByteString
genericSignatureTag = ByteString
"\004\130\043"
checkSignature :: PublicKey -> Signature -> ByteString -> Bool
checkSignature :: PublicKey -> Signature -> ByteString -> Bool
checkSignature PublicKey
pk0 Signature
sig0 ByteString
bytes =
case (PublicKey
pk0, Signature
sig0) of
(PublicKeyEd25519 PublicKey
pk, SignatureEd25519 Signature
sig) ->
PublicKey -> Signature -> ByteString -> Bool
Ed25519.checkSignature PublicKey
pk Signature
sig ByteString
bytes
(PublicKeySecp256k1 PublicKey
pk, SignatureSecp256k1 Signature
sig) ->
PublicKey -> Signature -> ByteString -> Bool
Secp256k1.checkSignature PublicKey
pk Signature
sig ByteString
bytes
(PublicKeyP256 PublicKey
pk, SignatureP256 Signature
sig) ->
PublicKey -> Signature -> ByteString -> Bool
P256.checkSignature PublicKey
pk Signature
sig ByteString
bytes
(PublicKeyEd25519 PublicKey
pk, SignatureGeneric ByteString
sBytes) ->
case ByteString -> Either CryptoParseError Signature
forall ba.
ByteArrayAccess ba =>
ba -> Either CryptoParseError Signature
Ed25519.mkSignature ByteString
sBytes of
Right Signature
sig -> PublicKey -> Signature -> ByteString -> Bool
Ed25519.checkSignature PublicKey
pk Signature
sig ByteString
bytes
Left CryptoParseError
_ -> Bool
False
(PublicKeySecp256k1 PublicKey
pk, SignatureGeneric ByteString
sBytes) ->
case ByteString -> Either CryptoParseError Signature
forall ba. ByteArray ba => ba -> Either CryptoParseError Signature
Secp256k1.mkSignature ByteString
sBytes of
Right Signature
sig -> PublicKey -> Signature -> ByteString -> Bool
Secp256k1.checkSignature PublicKey
pk Signature
sig ByteString
bytes
Left CryptoParseError
_ -> Bool
False
(PublicKeyP256 PublicKey
pk, SignatureGeneric ByteString
sBytes) ->
case ByteString -> Either CryptoParseError Signature
forall ba. ByteArray ba => ba -> Either CryptoParseError Signature
P256.mkSignature ByteString
sBytes of
Right Signature
sig -> PublicKey -> Signature -> ByteString -> Bool
P256.checkSignature PublicKey
pk Signature
sig ByteString
bytes
Left CryptoParseError
_ -> Bool
False
(PublicKey, Signature)
_ -> Bool
False
sign :: MonadRandom m => SecretKey -> ByteString -> m Signature
sign :: forall (m :: * -> *).
MonadRandom m =>
SecretKey -> ByteString -> m Signature
sign SecretKey
sk ByteString
bs =
case SecretKey
sk of
SecretKeyEd25519 SecretKey
sk' -> Signature -> m Signature
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Signature -> m Signature) -> Signature -> m Signature
forall a b. (a -> b) -> a -> b
$ Signature -> Signature
SignatureEd25519 (Signature -> Signature) -> Signature -> Signature
forall a b. (a -> b) -> a -> b
$ SecretKey -> ByteString -> Signature
Ed25519.sign SecretKey
sk' ByteString
bs
SecretKeySecp256k1 SecretKey
sk' -> Signature -> Signature
SignatureSecp256k1 (Signature -> Signature) -> m Signature -> m Signature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SecretKey -> ByteString -> m Signature
forall (m :: * -> *).
MonadRandom m =>
SecretKey -> ByteString -> m Signature
Secp256k1.sign SecretKey
sk' ByteString
bs
SecretKeyP256 SecretKey
sk' -> Signature -> Signature
SignatureP256 (Signature -> Signature) -> m Signature -> m Signature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SecretKey -> ByteString -> m Signature
forall (m :: * -> *).
MonadRandom m =>
SecretKey -> ByteString -> m Signature
P256.sign SecretKey
sk' ByteString
bs
formatPublicKey :: PublicKey -> Text
formatPublicKey :: PublicKey -> Text
formatPublicKey = \case
PublicKeyEd25519 PublicKey
pk -> PublicKey -> Text
Ed25519.formatPublicKey PublicKey
pk
PublicKeySecp256k1 PublicKey
pk -> PublicKey -> Text
Secp256k1.formatPublicKey PublicKey
pk
PublicKeyP256 PublicKey
pk -> PublicKey -> Text
P256.formatPublicKey PublicKey
pk
mformatPublicKey :: PublicKey -> MText
mformatPublicKey :: PublicKey -> MText
mformatPublicKey = Either Text MText -> MText
forall a b. (HasCallStack, Buildable a) => Either a b -> b
unsafe (Either Text MText -> MText)
-> (PublicKey -> Either Text MText) -> PublicKey -> MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text MText
mkMText (Text -> Either Text MText)
-> (PublicKey -> Text) -> PublicKey -> Either Text MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> Text
formatPublicKey
instance Buildable PublicKey where
build :: PublicKey -> Builder
build = Text -> Builder
forall p. Buildable p => p -> Builder
build (Text -> Builder) -> (PublicKey -> Text) -> PublicKey -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> Text
formatPublicKey
parsePublicKey :: Text -> Either CryptoParseError PublicKey
parsePublicKey :: Text -> Either CryptoParseError PublicKey
parsePublicKey Text
txt =
NonEmpty (Either CryptoParseError PublicKey)
-> Either CryptoParseError PublicKey
forall e a. NonEmpty (Either e a) -> Either e a
firstRight (NonEmpty (Either CryptoParseError PublicKey)
-> Either CryptoParseError PublicKey)
-> NonEmpty (Either CryptoParseError PublicKey)
-> Either CryptoParseError PublicKey
forall a b. (a -> b) -> a -> b
$ ((Text -> Either CryptoParseError PublicKey)
-> Either CryptoParseError PublicKey)
-> NonEmpty (Text -> Either CryptoParseError PublicKey)
-> NonEmpty (Either CryptoParseError PublicKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map ((Text -> Either CryptoParseError PublicKey)
-> Text -> Either CryptoParseError PublicKey
forall a b. (a -> b) -> a -> b
$ Text
txt)
( (PublicKey -> PublicKey)
-> Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey
PublicKeyEd25519 (Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey)
-> (Text -> Either CryptoParseError PublicKey)
-> Text
-> Either CryptoParseError PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError PublicKey
Ed25519.parsePublicKey (Text -> Either CryptoParseError PublicKey)
-> [Text -> Either CryptoParseError PublicKey]
-> NonEmpty (Text -> Either CryptoParseError PublicKey)
forall a. a -> [a] -> NonEmpty a
:|
[ (PublicKey -> PublicKey)
-> Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey
PublicKeySecp256k1 (Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey)
-> (Text -> Either CryptoParseError PublicKey)
-> Text
-> Either CryptoParseError PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError PublicKey
Secp256k1.parsePublicKey
, (PublicKey -> PublicKey)
-> Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey
PublicKeyP256 (Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey)
-> (Text -> Either CryptoParseError PublicKey)
-> Text
-> Either CryptoParseError PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError PublicKey
P256.parsePublicKey
])
parsePublicKeyRaw :: ByteString -> Either Text PublicKey
parsePublicKeyRaw :: ByteString -> Either Text PublicKey
parsePublicKeyRaw ByteString
ba =
case Get PublicKey
-> ByteString
-> Either
(ByteString, ByteOffset, String)
(ByteString, ByteOffset, PublicKey)
forall a.
Get a
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
Get.runGetOrFail (String -> [TaggedDecoder PublicKey] -> Get PublicKey
forall a. String -> [TaggedDecoder a] -> Get a
decodeWithTag String
"key" [TaggedDecoder PublicKey]
keyDecoders) (ByteString -> ByteString
LBS.fromStrict ByteString
ba) of
Right (ByteString
_, ByteOffset
_, PublicKey
result) -> PublicKey -> Either Text PublicKey
forall a b. b -> Either a b
Right PublicKey
result
Left (ByteString
_, ByteOffset
_, String
err) -> Text -> Either Text PublicKey
forall a b. a -> Either a b
Left (String -> Text
forall a. ToText a => a -> Text
toText String
err)
formatSignature :: Signature -> Text
formatSignature :: Signature -> Text
formatSignature = \case
SignatureEd25519 Signature
sig -> Signature -> Text
Ed25519.formatSignature Signature
sig
SignatureSecp256k1 Signature
sig -> Signature -> Text
Secp256k1.formatSignature Signature
sig
SignatureP256 Signature
sig -> Signature -> Text
P256.formatSignature Signature
sig
SignatureGeneric ByteString
sig -> ByteString -> ByteString -> Text
forall x. ByteArrayAccess x => ByteString -> x -> Text
formatImpl ByteString
genericSignatureTag ByteString
sig
mformatSignature :: Signature -> MText
mformatSignature :: Signature -> MText
mformatSignature = Either Text MText -> MText
forall a b. (HasCallStack, Buildable a) => Either a b -> b
unsafe (Either Text MText -> MText)
-> (Signature -> Either Text MText) -> Signature -> MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text MText
mkMText (Text -> Either Text MText)
-> (Signature -> Text) -> Signature -> Either Text MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> Text
formatSignature
instance Buildable Signature where
build :: Signature -> Builder
build = Text -> Builder
forall p. Buildable p => p -> Builder
build (Text -> Builder) -> (Signature -> Text) -> Signature -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> Text
formatSignature
parseSignature :: Text -> Either CryptoParseError Signature
parseSignature :: Text -> Either CryptoParseError Signature
parseSignature Text
txt =
NonEmpty (Either CryptoParseError Signature)
-> Either CryptoParseError Signature
forall e a. NonEmpty (Either e a) -> Either e a
firstRight (NonEmpty (Either CryptoParseError Signature)
-> Either CryptoParseError Signature)
-> NonEmpty (Either CryptoParseError Signature)
-> Either CryptoParseError Signature
forall a b. (a -> b) -> a -> b
$ ((Text -> Either CryptoParseError Signature)
-> Either CryptoParseError Signature)
-> NonEmpty (Text -> Either CryptoParseError Signature)
-> NonEmpty (Either CryptoParseError Signature)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map ((Text -> Either CryptoParseError Signature)
-> Text -> Either CryptoParseError Signature
forall a b. (a -> b) -> a -> b
$ Text
txt)
( (Signature -> Signature)
-> Either CryptoParseError Signature
-> Either CryptoParseError Signature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Signature -> Signature
SignatureEd25519 (Either CryptoParseError Signature
-> Either CryptoParseError Signature)
-> (Text -> Either CryptoParseError Signature)
-> Text
-> Either CryptoParseError Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError Signature
Ed25519.parseSignature (Text -> Either CryptoParseError Signature)
-> [Text -> Either CryptoParseError Signature]
-> NonEmpty (Text -> Either CryptoParseError Signature)
forall a. a -> [a] -> NonEmpty a
:|
[ (Signature -> Signature)
-> Either CryptoParseError Signature
-> Either CryptoParseError Signature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Signature -> Signature
SignatureSecp256k1 (Either CryptoParseError Signature
-> Either CryptoParseError Signature)
-> (Text -> Either CryptoParseError Signature)
-> Text
-> Either CryptoParseError Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError Signature
Secp256k1.parseSignature
, (Signature -> Signature)
-> Either CryptoParseError Signature
-> Either CryptoParseError Signature
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Signature -> Signature
SignatureP256 (Either CryptoParseError Signature
-> Either CryptoParseError Signature)
-> (Text -> Either CryptoParseError Signature)
-> Text
-> Either CryptoParseError Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError Signature
P256.parseSignature
, ByteString
-> (ByteString -> Either CryptoParseError Signature)
-> Text
-> Either CryptoParseError Signature
forall res.
ByteString
-> (ByteString -> Either CryptoParseError res)
-> Text
-> Either CryptoParseError res
parseImpl ByteString
genericSignatureTag (Signature -> Either CryptoParseError Signature
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Signature -> Either CryptoParseError Signature)
-> (ByteString -> Signature)
-> ByteString
-> Either CryptoParseError Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Signature
SignatureGeneric)
])
formatSecretKey :: SecretKey -> Text
formatSecretKey :: SecretKey -> Text
formatSecretKey SecretKey
key = Text
"unencrypted:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> case SecretKey
key of
SecretKeyEd25519 SecretKey
sig -> SecretKey -> Text
Ed25519.formatSecretKey SecretKey
sig
SecretKeySecp256k1 SecretKey
sig -> SecretKey -> Text
Secp256k1.formatSecretKey SecretKey
sig
SecretKeyP256 SecretKey
sig -> SecretKey -> Text
P256.formatSecretKey SecretKey
sig
instance Buildable SecretKey where
build :: SecretKey -> Builder
build = Text -> Builder
forall p. Buildable p => p -> Builder
build (Text -> Builder) -> (SecretKey -> Text) -> SecretKey -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretKey -> Text
formatSecretKey
parseSecretKey :: Text -> Either CryptoParseError SecretKey
parseSecretKey :: Text -> Either CryptoParseError SecretKey
parseSecretKey Text
txt =
NonEmpty (Either CryptoParseError SecretKey)
-> Either CryptoParseError SecretKey
forall e a. NonEmpty (Either e a) -> Either e a
firstRight (NonEmpty (Either CryptoParseError SecretKey)
-> Either CryptoParseError SecretKey)
-> NonEmpty (Either CryptoParseError SecretKey)
-> Either CryptoParseError SecretKey
forall a b. (a -> b) -> a -> b
$ ((Text -> Either CryptoParseError SecretKey)
-> Either CryptoParseError SecretKey)
-> NonEmpty (Text -> Either CryptoParseError SecretKey)
-> NonEmpty (Either CryptoParseError SecretKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map (\Text -> Either CryptoParseError SecretKey
f -> Text -> Either CryptoParseError SecretKey
f (Text -> Either CryptoParseError SecretKey)
-> Text -> Either CryptoParseError SecretKey
forall a b. (a -> b) -> a -> b
$ Text -> Text
removePrefix Text
txt)
( (SecretKey -> SecretKey)
-> Either CryptoParseError SecretKey
-> Either CryptoParseError SecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SecretKey -> SecretKey
SecretKeyEd25519 (Either CryptoParseError SecretKey
-> Either CryptoParseError SecretKey)
-> (Text -> Either CryptoParseError SecretKey)
-> Text
-> Either CryptoParseError SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError SecretKey
Ed25519.parseSecretKey (Text -> Either CryptoParseError SecretKey)
-> [Text -> Either CryptoParseError SecretKey]
-> NonEmpty (Text -> Either CryptoParseError SecretKey)
forall a. a -> [a] -> NonEmpty a
:|
[ (SecretKey -> SecretKey)
-> Either CryptoParseError SecretKey
-> Either CryptoParseError SecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SecretKey -> SecretKey
SecretKeySecp256k1 (Either CryptoParseError SecretKey
-> Either CryptoParseError SecretKey)
-> (Text -> Either CryptoParseError SecretKey)
-> Text
-> Either CryptoParseError SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError SecretKey
Secp256k1.parseSecretKey
, (SecretKey -> SecretKey)
-> Either CryptoParseError SecretKey
-> Either CryptoParseError SecretKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SecretKey -> SecretKey
SecretKeyP256 (Either CryptoParseError SecretKey
-> Either CryptoParseError SecretKey)
-> (Text -> Either CryptoParseError SecretKey)
-> Text
-> Either CryptoParseError SecretKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError SecretKey
P256.parseSecretKey
])
where
removePrefix :: Text -> Text
removePrefix :: Text -> Text
removePrefix Text
input =
let unencrypted :: Text
unencrypted = Text
"unencrypted:"
(Text
prefix, Text
payload) = Int -> Text -> (Text, Text)
T.splitAt (Text -> Int
forall t. Container t => t -> Int
length Text
unencrypted) Text
input
in case Text
prefix Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
unencrypted of
Bool
True -> Text
payload
Bool
False -> Text
input
instance ToJSON PublicKey where
toJSON :: PublicKey -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value) -> (PublicKey -> Text) -> PublicKey -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> Text
formatPublicKey
toEncoding :: PublicKey -> Encoding
toEncoding = Text -> Encoding
forall a. Text -> Encoding' a
Aeson.text (Text -> Encoding) -> (PublicKey -> Text) -> PublicKey -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> Text
formatPublicKey
instance FromJSON PublicKey where
parseJSON :: Value -> Parser PublicKey
parseJSON =
String -> (Text -> Parser PublicKey) -> Value -> Parser PublicKey
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"PublicKey" ((Text -> Parser PublicKey) -> Value -> Parser PublicKey)
-> (Text -> Parser PublicKey) -> Value -> Parser PublicKey
forall a b. (a -> b) -> a -> b
$
(CryptoParseError -> Parser PublicKey)
-> (PublicKey -> Parser PublicKey)
-> Either CryptoParseError PublicKey
-> Parser PublicKey
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser PublicKey
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser PublicKey)
-> (CryptoParseError -> String)
-> CryptoParseError
-> Parser PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoParseError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) PublicKey -> Parser PublicKey
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either CryptoParseError PublicKey -> Parser PublicKey)
-> (Text -> Either CryptoParseError PublicKey)
-> Text
-> Parser PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError PublicKey
parsePublicKey
instance ToJSON Signature where
toJSON :: Signature -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value) -> (Signature -> Text) -> Signature -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> Text
formatSignature
toEncoding :: Signature -> Encoding
toEncoding = Text -> Encoding
forall a. Text -> Encoding' a
Aeson.text (Text -> Encoding) -> (Signature -> Text) -> Signature -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Signature -> Text
formatSignature
instance FromJSON Signature where
parseJSON :: Value -> Parser Signature
parseJSON =
String -> (Text -> Parser Signature) -> Value -> Parser Signature
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Signature" ((Text -> Parser Signature) -> Value -> Parser Signature)
-> (Text -> Parser Signature) -> Value -> Parser Signature
forall a b. (a -> b) -> a -> b
$
(CryptoParseError -> Parser Signature)
-> (Signature -> Parser Signature)
-> Either CryptoParseError Signature
-> Parser Signature
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser Signature
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser Signature)
-> (CryptoParseError -> String)
-> CryptoParseError
-> Parser Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoParseError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) Signature -> Parser Signature
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either CryptoParseError Signature -> Parser Signature)
-> (Text -> Either CryptoParseError Signature)
-> Text
-> Parser Signature
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError Signature
parseSignature
instance ToJSON (Hash kind) where
toJSON :: Hash kind -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value) -> (Hash kind -> Text) -> Hash kind -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash kind -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash
toEncoding :: Hash kind -> Encoding
toEncoding = Text -> Encoding
forall a. Text -> Encoding' a
Aeson.text (Text -> Encoding) -> (Hash kind -> Text) -> Hash kind -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash kind -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash
instance ToJSONKey (Hash kind) where
toJSONKey :: ToJSONKeyFunction (Hash kind)
toJSONKey = (Hash kind -> Text) -> ToJSONKeyFunction (Hash kind)
forall a. (a -> Text) -> ToJSONKeyFunction a
AesonTypes.toJSONKeyText Hash kind -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash
instance AllTags kind => FromJSON (Hash kind) where
parseJSON :: Value -> Parser (Hash kind)
parseJSON =
String
-> (Text -> Parser (Hash kind)) -> Value -> Parser (Hash kind)
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Hash" ((Text -> Parser (Hash kind)) -> Value -> Parser (Hash kind))
-> (Text -> Parser (Hash kind)) -> Value -> Parser (Hash kind)
forall a b. (a -> b) -> a -> b
$
(CryptoParseError -> Parser (Hash kind))
-> (Hash kind -> Parser (Hash kind))
-> Either CryptoParseError (Hash kind)
-> Parser (Hash kind)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser (Hash kind)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (Hash kind))
-> (CryptoParseError -> String)
-> CryptoParseError
-> Parser (Hash kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoParseError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) Hash kind -> Parser (Hash kind)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either CryptoParseError (Hash kind) -> Parser (Hash kind))
-> (Text -> Either CryptoParseError (Hash kind))
-> Text
-> Parser (Hash kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError (Hash kind)
forall (kind :: HashKind).
AllTags kind =>
Text -> Either CryptoParseError (Hash kind)
parseHash
instance AllTags kind => FromJSONKey (Hash kind) where
fromJSONKey :: FromJSONKeyFunction (Hash kind)
fromJSONKey =
(Text -> Parser (Hash kind)) -> FromJSONKeyFunction (Hash kind)
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
AesonTypes.FromJSONKeyTextParser ((Text -> Parser (Hash kind)) -> FromJSONKeyFunction (Hash kind))
-> (Text -> Parser (Hash kind)) -> FromJSONKeyFunction (Hash kind)
forall a b. (a -> b) -> a -> b
$
(CryptoParseError -> Parser (Hash kind))
-> (Hash kind -> Parser (Hash kind))
-> Either CryptoParseError (Hash kind)
-> Parser (Hash kind)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser (Hash kind)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (Hash kind))
-> (CryptoParseError -> String)
-> CryptoParseError
-> Parser (Hash kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CryptoParseError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) Hash kind -> Parser (Hash kind)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either CryptoParseError (Hash kind) -> Parser (Hash kind))
-> (Text -> Either CryptoParseError (Hash kind))
-> Text
-> Parser (Hash kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError (Hash kind)
forall (kind :: HashKind).
AllTags kind =>
Text -> Either CryptoParseError (Hash kind)
parseHash
type KeyHashTag = HashTag 'HashKindPublicKey
class AllTags kind where
allHashTags :: NonEmpty (HashTag kind)
instance AllTags 'HashKindPublicKey where
allHashTags :: NonEmpty (HashTag 'HashKindPublicKey)
allHashTags = HashTag 'HashKindPublicKey
HashEd25519 HashTag 'HashKindPublicKey
-> [HashTag 'HashKindPublicKey]
-> NonEmpty (HashTag 'HashKindPublicKey)
forall a. a -> [a] -> NonEmpty a
:| [HashTag 'HashKindPublicKey
HashSecp256k1, HashTag 'HashKindPublicKey
HashP256]
instance AllTags 'HashKindContract where
allHashTags :: NonEmpty (HashTag 'HashKindContract)
allHashTags = HashTag 'HashKindContract -> NonEmpty (HashTag 'HashKindContract)
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashTag 'HashKindContract
HashContract
instance AllTags 'HashKindL2PublicKey where
allHashTags :: NonEmpty (HashTag 'HashKindL2PublicKey)
allHashTags = HashTag 'HashKindL2PublicKey
-> NonEmpty (HashTag 'HashKindL2PublicKey)
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashTag 'HashKindL2PublicKey
HashBLS
instance AllTags 'HashKindTxRollup where
allHashTags :: NonEmpty (HashTag 'HashKindTxRollup)
allHashTags = HashTag 'HashKindTxRollup -> NonEmpty (HashTag 'HashKindTxRollup)
forall (f :: * -> *) a. Applicative f => a -> f a
pure HashTag 'HashKindTxRollup
HashTXR
data Hash (kind :: HashKind) = Hash
{ forall (kind :: HashKind). Hash kind -> HashTag kind
hTag :: HashTag kind
, forall (kind :: HashKind). Hash kind -> ByteString
hBytes :: ByteString
} deriving stock (Int -> Hash kind -> ShowS
[Hash kind] -> ShowS
Hash kind -> String
(Int -> Hash kind -> ShowS)
-> (Hash kind -> String)
-> ([Hash kind] -> ShowS)
-> Show (Hash kind)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (kind :: HashKind). Int -> Hash kind -> ShowS
forall (kind :: HashKind). [Hash kind] -> ShowS
forall (kind :: HashKind). Hash kind -> String
showList :: [Hash kind] -> ShowS
$cshowList :: forall (kind :: HashKind). [Hash kind] -> ShowS
show :: Hash kind -> String
$cshow :: forall (kind :: HashKind). Hash kind -> String
showsPrec :: Int -> Hash kind -> ShowS
$cshowsPrec :: forall (kind :: HashKind). Int -> Hash kind -> ShowS
Show, Hash kind -> Hash kind -> Bool
(Hash kind -> Hash kind -> Bool)
-> (Hash kind -> Hash kind -> Bool) -> Eq (Hash kind)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
/= :: Hash kind -> Hash kind -> Bool
$c/= :: forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
== :: Hash kind -> Hash kind -> Bool
$c== :: forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
Eq, Eq (Hash kind)
Eq (Hash kind)
-> (Hash kind -> Hash kind -> Ordering)
-> (Hash kind -> Hash kind -> Bool)
-> (Hash kind -> Hash kind -> Bool)
-> (Hash kind -> Hash kind -> Bool)
-> (Hash kind -> Hash kind -> Bool)
-> (Hash kind -> Hash kind -> Hash kind)
-> (Hash kind -> Hash kind -> Hash kind)
-> Ord (Hash kind)
Hash kind -> Hash kind -> Bool
Hash kind -> Hash kind -> Ordering
Hash kind -> Hash kind -> Hash kind
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (kind :: HashKind). Eq (Hash kind)
forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
forall (kind :: HashKind). Hash kind -> Hash kind -> Ordering
forall (kind :: HashKind). Hash kind -> Hash kind -> Hash kind
min :: Hash kind -> Hash kind -> Hash kind
$cmin :: forall (kind :: HashKind). Hash kind -> Hash kind -> Hash kind
max :: Hash kind -> Hash kind -> Hash kind
$cmax :: forall (kind :: HashKind). Hash kind -> Hash kind -> Hash kind
>= :: Hash kind -> Hash kind -> Bool
$c>= :: forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
> :: Hash kind -> Hash kind -> Bool
$c> :: forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
<= :: Hash kind -> Hash kind -> Bool
$c<= :: forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
< :: Hash kind -> Hash kind -> Bool
$c< :: forall (kind :: HashKind). Hash kind -> Hash kind -> Bool
compare :: Hash kind -> Hash kind -> Ordering
$ccompare :: forall (kind :: HashKind). Hash kind -> Hash kind -> Ordering
Ord, (forall x. Hash kind -> Rep (Hash kind) x)
-> (forall x. Rep (Hash kind) x -> Hash kind)
-> Generic (Hash kind)
forall x. Rep (Hash kind) x -> Hash kind
forall x. Hash kind -> Rep (Hash kind) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (kind :: HashKind) x. Rep (Hash kind) x -> Hash kind
forall (kind :: HashKind) x. Hash kind -> Rep (Hash kind) x
$cto :: forall (kind :: HashKind) x. Rep (Hash kind) x -> Hash kind
$cfrom :: forall (kind :: HashKind) x. Hash kind -> Rep (Hash kind) x
Generic, (forall (m :: * -> *). Quote m => Hash kind -> m Exp)
-> (forall (m :: * -> *).
Quote m =>
Hash kind -> Code m (Hash kind))
-> Lift (Hash kind)
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (kind :: HashKind) (m :: * -> *).
Quote m =>
Hash kind -> m Exp
forall (kind :: HashKind) (m :: * -> *).
Quote m =>
Hash kind -> Code m (Hash kind)
forall (m :: * -> *). Quote m => Hash kind -> m Exp
forall (m :: * -> *). Quote m => Hash kind -> Code m (Hash kind)
liftTyped :: forall (m :: * -> *). Quote m => Hash kind -> Code m (Hash kind)
$cliftTyped :: forall (kind :: HashKind) (m :: * -> *).
Quote m =>
Hash kind -> Code m (Hash kind)
lift :: forall (m :: * -> *). Quote m => Hash kind -> m Exp
$clift :: forall (kind :: HashKind) (m :: * -> *).
Quote m =>
Hash kind -> m Exp
Lift)
instance NFData (Hash kind)
type KeyHash = Hash 'HashKindPublicKey
type KeyHashL2 = Hash 'HashKindL2PublicKey
type ContractHash = Hash 'HashKindContract
type TxRollupHash = Hash 'HashKindTxRollup
hashLengthBytes :: Integral n => n
hashLengthBytes :: forall n. Integral n => n
hashLengthBytes = n
20
hashKey :: PublicKey -> KeyHash
hashKey :: PublicKey -> KeyHash
hashKey =
\case
PublicKeyEd25519 PublicKey
pk ->
HashTag 'HashKindPublicKey -> ByteString -> KeyHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindPublicKey
HashEd25519 (ByteString -> ByteString
blake2b160 (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ PublicKey -> ByteString
forall ba. ByteArray ba => PublicKey -> ba
Ed25519.publicKeyToBytes PublicKey
pk)
PublicKeySecp256k1 PublicKey
pk ->
HashTag 'HashKindPublicKey -> ByteString -> KeyHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindPublicKey
HashSecp256k1 (ByteString -> ByteString
blake2b160 (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ PublicKey -> ByteString
forall ba. ByteArray ba => PublicKey -> ba
Secp256k1.publicKeyToBytes PublicKey
pk)
PublicKeyP256 PublicKey
pk ->
HashTag 'HashKindPublicKey -> ByteString -> KeyHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindPublicKey
HashP256 (ByteString -> ByteString
blake2b160 (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ PublicKey -> ByteString
forall ba. ByteArray ba => PublicKey -> ba
P256.publicKeyToBytes PublicKey
pk)
formatHash :: (Hash kind) -> Text
formatHash :: forall (kind :: HashKind). Hash kind -> Text
formatHash (Hash HashTag kind
tag ByteString
bytes) = ByteString -> ByteString -> Text
forall x. ByteArrayAccess x => ByteString -> x -> Text
formatImpl (HashTag kind -> ByteString
forall (kind :: HashKind). HashTag kind -> ByteString
hashTagBytes HashTag kind
tag) ByteString
bytes
mformatHash :: (Hash kind) -> MText
mformatHash :: forall (kind :: HashKind). Hash kind -> MText
mformatHash = Either Text MText -> MText
forall a b. (HasCallStack, Buildable a) => Either a b -> b
unsafe (Either Text MText -> MText)
-> (Hash kind -> Either Text MText) -> Hash kind -> MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text MText
mkMText (Text -> Either Text MText)
-> (Hash kind -> Text) -> Hash kind -> Either Text MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash kind -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash
instance Buildable (Hash kind) where
build :: Hash kind -> Builder
build = Text -> Builder
forall p. Buildable p => p -> Builder
build (Text -> Builder) -> (Hash kind -> Text) -> Hash kind -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Hash kind -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash
parseHash
:: AllTags kind
=> Text
-> Either CryptoParseError (Hash kind)
parseHash :: forall (kind :: HashKind).
AllTags kind =>
Text -> Either CryptoParseError (Hash kind)
parseHash Text
txt =
let
mkKeyHash :: HashTag kind -> ByteString -> Either CryptoParseError (Hash kind)
mkKeyHash HashTag kind
tag ByteString
bs =
HashTag kind -> ByteString -> Hash kind
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag kind
tag ByteString
bs Hash kind
-> Either CryptoParseError ()
-> Either CryptoParseError (Hash kind)
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$
Bool -> Either CryptoParseError () -> Either CryptoParseError ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int
forall t. Container t => t -> Int
length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall n. Integral n => n
hashLengthBytes)
(CryptoParseError -> Either CryptoParseError ()
forall a b. a -> Either a b
Left (CryptoParseError -> Either CryptoParseError ())
-> CryptoParseError -> Either CryptoParseError ()
forall a b. (a -> b) -> a -> b
$ Builder -> Int -> CryptoParseError
CryptoParseUnexpectedLength Builder
"KeyHash" (ByteString -> Int
forall t. Container t => t -> Int
length ByteString
bs))
parse :: HashTag kind -> Either CryptoParseError (Hash kind)
parse :: forall (kind :: HashKind).
HashTag kind -> Either CryptoParseError (Hash kind)
parse HashTag kind
tag = HashTag kind -> ByteString -> Either CryptoParseError (Hash kind)
forall {kind :: HashKind}.
HashTag kind -> ByteString -> Either CryptoParseError (Hash kind)
mkKeyHash HashTag kind
tag (ByteString -> Either CryptoParseError (Hash kind))
-> Either CryptoParseError ByteString
-> Either CryptoParseError (Hash kind)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ByteString
-> (ByteString -> Either CryptoParseError ByteString)
-> Text
-> Either CryptoParseError ByteString
forall res.
ByteString
-> (ByteString -> Either CryptoParseError res)
-> Text
-> Either CryptoParseError res
parseImpl (HashTag kind -> ByteString
forall (kind :: HashKind). HashTag kind -> ByteString
hashTagBytes HashTag kind
tag) ByteString -> Either CryptoParseError ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
txt
in NonEmpty (Either CryptoParseError (Hash kind))
-> Either CryptoParseError (Hash kind)
forall e a. NonEmpty (Either e a) -> Either e a
firstRight (NonEmpty (Either CryptoParseError (Hash kind))
-> Either CryptoParseError (Hash kind))
-> NonEmpty (Either CryptoParseError (Hash kind))
-> Either CryptoParseError (Hash kind)
forall a b. (a -> b) -> a -> b
$ (HashTag kind -> Either CryptoParseError (Hash kind))
-> NonEmpty (HashTag kind)
-> NonEmpty (Either CryptoParseError (Hash kind))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map HashTag kind -> Either CryptoParseError (Hash kind)
forall (kind :: HashKind).
HashTag kind -> Either CryptoParseError (Hash kind)
parse NonEmpty (HashTag kind)
forall (kind :: HashKind). AllTags kind => NonEmpty (HashTag kind)
allHashTags
parseKeyHashRaw :: ByteString -> Either CryptoParseError KeyHash
parseKeyHashRaw :: ByteString -> Either CryptoParseError KeyHash
parseKeyHashRaw ByteString
ba =
if (ByteString -> Int
BS.length ByteString
ba Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall n. Integral n => n
hashLengthBytes) then
case Get KeyHash
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, KeyHash)
forall a.
Get a
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
Get.runGetOrFail (String -> [TaggedDecoder KeyHash] -> Get KeyHash
forall a. String -> [TaggedDecoder a] -> Get a
decodeWithTag String
"key_hash" [TaggedDecoder KeyHash]
keyHashDecoders) (ByteString -> ByteString
LBS.fromStrict ByteString
ba) of
Right (ByteString
_, ByteOffset
_, KeyHash
result) -> KeyHash -> Either CryptoParseError KeyHash
forall a b. b -> Either a b
Right KeyHash
result
Left (ByteString
_, ByteOffset
_, String
err) -> CryptoParseError -> Either CryptoParseError KeyHash
forall a b. a -> Either a b
Left (CryptoParseError -> Either CryptoParseError KeyHash)
-> CryptoParseError -> Either CryptoParseError KeyHash
forall a b. (a -> b) -> a -> b
$ Text -> CryptoParseError
CryptoParseBinaryError (String -> Text
forall a. ToText a => a -> Text
toText String
err)
else
CryptoParseError -> Either CryptoParseError KeyHash
forall a b. a -> Either a b
Left (CryptoParseError -> Either CryptoParseError KeyHash)
-> CryptoParseError -> Either CryptoParseError KeyHash
forall a b. (a -> b) -> a -> b
$ Builder -> Int -> CryptoParseError
CryptoParseUnexpectedLength Builder
"key_hash" (ByteString -> Int
BS.length ByteString
ba)
parseKeyHashL2Raw :: ByteString -> Either CryptoParseError KeyHashL2
parseKeyHashL2Raw :: ByteString -> Either CryptoParseError KeyHashL2
parseKeyHashL2Raw ByteString
ba =
if (ByteString -> Int
BS.length ByteString
ba Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall n. Integral n => n
hashLengthBytes) then
case Get KeyHashL2
-> ByteString
-> Either
(ByteString, ByteOffset, String)
(ByteString, ByteOffset, KeyHashL2)
forall a.
Get a
-> ByteString
-> Either
(ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
Get.runGetOrFail (HashTag 'HashKindL2PublicKey -> ByteString -> KeyHashL2
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindL2PublicKey
HashBLS (ByteString -> KeyHashL2) -> Get ByteString -> Get KeyHashL2
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get ByteString
getByteStringCopy Int
forall n. Integral n => n
hashLengthBytes)
(ByteString -> ByteString
LBS.fromStrict ByteString
ba) of
Right (ByteString
_, ByteOffset
_, KeyHashL2
result) -> KeyHashL2 -> Either CryptoParseError KeyHashL2
forall a b. b -> Either a b
Right KeyHashL2
result
Left (ByteString
_, ByteOffset
_, String
err) -> CryptoParseError -> Either CryptoParseError KeyHashL2
forall a b. a -> Either a b
Left (CryptoParseError -> Either CryptoParseError KeyHashL2)
-> CryptoParseError -> Either CryptoParseError KeyHashL2
forall a b. (a -> b) -> a -> b
$ Text -> CryptoParseError
CryptoParseBinaryError (String -> Text
forall a. ToText a => a -> Text
toText String
err)
else
CryptoParseError -> Either CryptoParseError KeyHashL2
forall a b. a -> Either a b
Left (CryptoParseError -> Either CryptoParseError KeyHashL2)
-> CryptoParseError -> Either CryptoParseError KeyHashL2
forall a b. (a -> b) -> a -> b
$ Builder -> Int -> CryptoParseError
CryptoParseUnexpectedLength Builder
"tx_rollup_l2_address" (ByteString -> Int
BS.length ByteString
ba)
hashTagBytes :: HashTag kind -> ByteString
hashTagBytes :: forall (kind :: HashKind). HashTag kind -> ByteString
hashTagBytes =
\case
HashTag kind
HashEd25519 -> ByteString
"\006\161\159"
HashTag kind
HashSecp256k1 -> ByteString
"\006\161\161"
HashTag kind
HashP256 -> ByteString
"\006\161\164"
HashTag kind
HashContract -> ByteString
"\2\90\121"
HashTag kind
HashBLS -> ByteString
"\006\161\166"
HashTag kind
HashTXR -> ByteString
"\001\128\120\031"
instance AllTags kind => HasCLReader (Hash kind) where
getReader :: ReadM (Hash kind)
getReader = (String -> Either String (Hash kind)) -> ReadM (Hash kind)
forall a. (String -> Either String a) -> ReadM a
eitherReader ((CryptoParseError -> String)
-> Either CryptoParseError (Hash kind) -> Either String (Hash kind)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first CryptoParseError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty (Either CryptoParseError (Hash kind) -> Either String (Hash kind))
-> (String -> Either CryptoParseError (Hash kind))
-> String
-> Either String (Hash kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either CryptoParseError (Hash kind)
forall (kind :: HashKind).
AllTags kind =>
Text -> Either CryptoParseError (Hash kind)
parseHash (Text -> Either CryptoParseError (Hash kind))
-> (String -> Text)
-> String
-> Either CryptoParseError (Hash kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. ToText a => a -> Text
toText)
getMetavar :: String
getMetavar = String
"KEY_HASH"
keyDecoders :: [TaggedDecoder PublicKey]
keyDecoders :: [TaggedDecoder PublicKey]
keyDecoders =
[ Word8
0x00 Word8 -> Get PublicKey -> TaggedDecoder PublicKey
forall a. Word8 -> Get a -> TaggedDecoder a
#: String
-> (ByteString -> Either CryptoParseError PublicKey)
-> Get PublicKey
forall err a.
Buildable err =>
String -> (ByteString -> Either err a) -> Get a
decodeBytesLike String
"key Ed25519"
((PublicKey -> PublicKey)
-> Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey
PublicKeyEd25519 (Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey)
-> (ByteString -> Either CryptoParseError PublicKey)
-> ByteString
-> Either CryptoParseError PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either CryptoParseError PublicKey
forall ba.
ByteArrayAccess ba =>
ba -> Either CryptoParseError PublicKey
Ed25519.mkPublicKey)
, Word8
0x01 Word8 -> Get PublicKey -> TaggedDecoder PublicKey
forall a. Word8 -> Get a -> TaggedDecoder a
#: String
-> (ByteString -> Either CryptoParseError PublicKey)
-> Get PublicKey
forall err a.
Buildable err =>
String -> (ByteString -> Either err a) -> Get a
decodeBytesLike String
"key Secp256k1"
((PublicKey -> PublicKey)
-> Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey
PublicKeySecp256k1 (Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey)
-> (ByteString -> Either CryptoParseError PublicKey)
-> ByteString
-> Either CryptoParseError PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either CryptoParseError PublicKey
forall ba.
ByteArrayAccess ba =>
ba -> Either CryptoParseError PublicKey
Secp256k1.mkPublicKey)
, Word8
0x02 Word8 -> Get PublicKey -> TaggedDecoder PublicKey
forall a. Word8 -> Get a -> TaggedDecoder a
#: String
-> (ByteString -> Either CryptoParseError PublicKey)
-> Get PublicKey
forall err a.
Buildable err =>
String -> (ByteString -> Either err a) -> Get a
decodeBytesLike String
"key P256"
((PublicKey -> PublicKey)
-> Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap PublicKey -> PublicKey
PublicKeyP256 (Either CryptoParseError PublicKey
-> Either CryptoParseError PublicKey)
-> (ByteString -> Either CryptoParseError PublicKey)
-> ByteString
-> Either CryptoParseError PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either CryptoParseError PublicKey
forall ba.
ByteArrayAccess ba =>
ba -> Either CryptoParseError PublicKey
P256.mkPublicKey)
]
keyHashDecoders :: [TaggedDecoder KeyHash]
keyHashDecoders :: [TaggedDecoder KeyHash]
keyHashDecoders =
[ Word8
0x00 Word8 -> Get KeyHash -> TaggedDecoder KeyHash
forall a. Word8 -> Get a -> TaggedDecoder a
#: HashTag 'HashKindPublicKey -> ByteString -> KeyHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindPublicKey
HashEd25519 (ByteString -> KeyHash) -> Get ByteString -> Get KeyHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
getPayload
, Word8
0x01 Word8 -> Get KeyHash -> TaggedDecoder KeyHash
forall a. Word8 -> Get a -> TaggedDecoder a
#: HashTag 'HashKindPublicKey -> ByteString -> KeyHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindPublicKey
HashSecp256k1 (ByteString -> KeyHash) -> Get ByteString -> Get KeyHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
getPayload
, Word8
0x02 Word8 -> Get KeyHash -> TaggedDecoder KeyHash
forall a. Word8 -> Get a -> TaggedDecoder a
#: HashTag 'HashKindPublicKey -> ByteString -> KeyHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindPublicKey
HashP256 (ByteString -> KeyHash) -> Get ByteString -> Get KeyHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
getPayload
]
where
getPayload :: Get ByteString
getPayload = Int -> Get ByteString
getByteStringCopy Int
forall n. Integral n => n
hashLengthBytes