module Cometa.Crypto.Ed25519.SigningKey
( SigningKey,
withNormalKey,
withExtendedKey,
withNormalKeyHex,
withExtendedKeyHex,
withClone,
close,
byteLength,
hexLength,
publicKey,
sign,
exportBytes,
exportHex,
)
where
import Cometa.Crypto.Ed25519.PublicKey (PublicKey)
import qualified Cometa.Crypto.Ed25519.PublicKey as PublicKey
import Cometa.Crypto.Ed25519.Signature (Signature)
import qualified Cometa.Crypto.Ed25519.Signature as Signature
import Cometa.Error (Error (..))
import Cometa.Internal.Ed25519SigningKey (SigningKey (..))
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.Hex as Hex
import qualified Cometa.Internal.SigningKey as Internal
import Control.Monad (join)
import Data.ByteString (ByteString)
import Data.Text (Text)
withNormalKey :: ByteString -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value)
withNormalKey :: forall value.
ByteString
-> (SigningKey -> IO (Either Error value))
-> IO (Either Error value)
withNormalKey ByteString
bytes SigningKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
ByteString -> (Key -> IO value) -> IO (Either Failure value)
Internal.withNormalKey ByteString
bytes (SigningKey -> IO (Either Error value)
action (SigningKey -> IO (Either Error value))
-> (Key -> SigningKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> SigningKey
SigningKey)
withExtendedKey :: ByteString -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value)
withExtendedKey :: forall value.
ByteString
-> (SigningKey -> IO (Either Error value))
-> IO (Either Error value)
withExtendedKey ByteString
bytes SigningKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
ByteString -> (Key -> IO value) -> IO (Either Failure value)
Internal.withExtendedKey ByteString
bytes (SigningKey -> IO (Either Error value)
action (SigningKey -> IO (Either Error value))
-> (Key -> SigningKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> SigningKey
SigningKey)
withNormalKeyHex :: Text -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value)
withNormalKeyHex :: forall value.
Text
-> (SigningKey -> IO (Either Error value))
-> IO (Either Error value)
withNormalKeyHex Text
text SigningKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
Text -> (Key -> IO value) -> IO (Either Failure value)
Internal.withNormalKeyHex Text
text (SigningKey -> IO (Either Error value)
action (SigningKey -> IO (Either Error value))
-> (Key -> SigningKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> SigningKey
SigningKey)
withExtendedKeyHex :: Text -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value)
withExtendedKeyHex :: forall value.
Text
-> (SigningKey -> IO (Either Error value))
-> IO (Either Error value)
withExtendedKeyHex Text
text SigningKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value.
Text -> (Key -> IO value) -> IO (Either Failure value)
Internal.withExtendedKeyHex Text
text (SigningKey -> IO (Either Error value)
action (SigningKey -> IO (Either Error value))
-> (Key -> SigningKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> SigningKey
SigningKey)
withClone :: SigningKey -> (SigningKey -> IO (Either Error value)) -> IO (Either Error value)
withClone :: forall value.
SigningKey
-> (SigningKey -> IO (Either Error value))
-> IO (Either Error value)
withClone (SigningKey Key
key) SigningKey -> IO (Either Error value)
action = Either Failure (Either Error value) -> Either Error value
forall value.
Either Failure (Either Error value) -> Either Error value
flatten (Either Failure (Either Error value) -> Either Error value)
-> IO (Either Failure (Either Error value))
-> IO (Either Error value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key
-> (Key -> IO (Either Error value))
-> IO (Either Failure (Either Error value))
forall value. Key -> (Key -> IO value) -> IO (Either Failure value)
Internal.withClone Key
key (SigningKey -> IO (Either Error value)
action (SigningKey -> IO (Either Error value))
-> (Key -> SigningKey) -> Key -> IO (Either Error value)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> SigningKey
SigningKey)
close :: SigningKey -> IO ()
close :: SigningKey -> IO ()
close (SigningKey Key
key) = Key -> IO ()
Internal.close Key
key
byteLength :: SigningKey -> IO (Either Error Int)
byteLength :: SigningKey -> IO (Either Error Int)
byteLength (SigningKey Key
key) = Either Failure Int -> Either Error Int
forall value. Either Failure value -> Either Error value
mapFailure (Either Failure Int -> Either Error Int)
-> IO (Either Failure Int) -> IO (Either Error Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> IO (Either Failure Int)
Internal.byteLength Key
key
hexLength :: SigningKey -> IO (Either Error Int)
hexLength :: SigningKey -> IO (Either Error Int)
hexLength SigningKey
key = (Int -> Int) -> Either Error Int -> Either Error Int
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (Either Error Int -> Either Error Int)
-> IO (Either Error Int) -> IO (Either Error Int)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SigningKey -> IO (Either Error Int)
byteLength SigningKey
key
publicKey :: SigningKey -> IO (Either Error PublicKey)
publicKey :: SigningKey -> IO (Either Error PublicKey)
publicKey (SigningKey Key
key) = do
Either Failure ByteString
result <- Key -> IO (Either Failure ByteString)
Internal.publicKeyBytes Key
key
Either Error PublicKey -> IO (Either Error PublicKey)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Failure ByteString -> Either Error ByteString
forall value. Either Failure value -> Either Error value
mapFailure Either Failure ByteString
result Either Error ByteString
-> (ByteString -> Either Error PublicKey) -> Either Error PublicKey
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error PublicKey
PublicKey.fromBytes)
sign :: SigningKey -> ByteString -> IO (Either Error Signature)
sign :: SigningKey -> ByteString -> IO (Either Error Signature)
sign (SigningKey Key
key) ByteString
message = do
Either Failure ByteString
result <- Key -> ByteString -> IO (Either Failure ByteString)
Internal.signBytes Key
key ByteString
message
Either Error Signature -> IO (Either Error Signature)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Failure ByteString -> Either Error ByteString
forall value. Either Failure value -> Either Error value
mapFailure Either Failure ByteString
result Either Error ByteString
-> (ByteString -> Either Error Signature) -> Either Error Signature
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error Signature
Signature.fromBytes)
exportBytes :: SigningKey -> IO (Either Error ByteString)
exportBytes :: SigningKey -> IO (Either Error ByteString)
exportBytes (SigningKey Key
key) = Either Failure ByteString -> Either Error ByteString
forall value. Either Failure value -> Either Error value
mapFailure (Either Failure ByteString -> Either Error ByteString)
-> IO (Either Failure ByteString) -> IO (Either Error ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Key -> IO (Either Failure ByteString)
Internal.exportBytes Key
key
exportHex :: SigningKey -> IO (Either Error Text)
exportHex :: SigningKey -> IO (Either Error Text)
exportHex SigningKey
key = (ByteString -> Text)
-> Either Error ByteString -> Either Error Text
forall a b. (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ByteString -> Text
Hex.encodeHex (Either Error ByteString -> Either Error Text)
-> IO (Either Error ByteString) -> IO (Either Error Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SigningKey -> IO (Either Error ByteString)
exportBytes SigningKey
key
flatten :: Either Internal.Failure (Either Error value) -> Either Error value
flatten :: forall value.
Either Failure (Either Error value) -> Either Error value
flatten Either Failure (Either Error value)
result = Either Error (Either Error value) -> Either Error value
forall (m :: * -> *) a. Monad m => m (m a) -> m a
join (Either Failure (Either Error value)
-> Either Error (Either Error value)
forall value. Either Failure value -> Either Error value
mapFailure Either Failure (Either Error value)
result)
mapFailure :: Either Internal.Failure value -> Either Error value
mapFailure :: forall value. Either Failure value -> Either Error value
mapFailure = (Failure -> Either Error value)
-> (value -> Either Error value)
-> Either Failure value
-> Either Error value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error value
forall a b. a -> Either a b
Left (Error -> Either Error value)
-> (Failure -> Error) -> Failure -> Either Error value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Failure -> Error
publicFailure) value -> Either Error value
forall a b. b -> Either a b
Right
publicFailure :: Internal.Failure -> Error
publicFailure :: Failure -> Error
publicFailure Failure
failure = case Failure
failure of
Failure
Internal.Closed -> Error
SigningKeyClosed
Internal.InvalidLength Int
expected Int
actual -> Int -> Int -> Error
InvalidEd25519SigningKeyLength Int
expected Int
actual
Failure
Internal.InvalidHex -> Error
InvalidHex
Internal.NativeFailure NativeFailure
failureValue -> NativeFailure -> Error
fromNativeFailure NativeFailure
failureValue