{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.Message.Cip8
( Binding,
addressBinding,
keyHashBinding,
SignedMessage (..),
sign,
)
where
import qualified Cometa.Address as Address
import qualified Cometa.Credential as Credential
import Cometa.Crypto.Ed25519.SigningKey (SigningKey)
import Cometa.Error (Error (..))
import Cometa.Internal.Ed25519SigningKey (SigningKey (..))
import Cometa.Internal.Error (fromNativeFailure)
import qualified Cometa.Internal.FFI.Cip8 as Native
import qualified Cometa.Internal.FFI.Native as Status
import qualified Cometa.Internal.FFI.PasswordCrypto as Buffer
import Cometa.Internal.Failure (NativeFailure (..))
import Cometa.Internal.Ownership (copyStaticText)
import qualified Cometa.Internal.SigningKey as Key
import Control.Exception (bracket)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Word (Word8)
import Foreign.C.Types (CSize, CUInt)
import Foreign.Marshal.Alloc (alloca)
import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.Storable (peek, poke)
data Binding = Binding !CUInt !ByteString deriving (Binding -> Binding -> Bool
(Binding -> Binding -> Bool)
-> (Binding -> Binding -> Bool) -> Eq Binding
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Binding -> Binding -> Bool
== :: Binding -> Binding -> Bool
$c/= :: Binding -> Binding -> Bool
/= :: Binding -> Binding -> Bool
Eq, Int -> Binding -> ShowS
[Binding] -> ShowS
Binding -> String
(Int -> Binding -> ShowS)
-> (Binding -> String) -> ([Binding] -> ShowS) -> Show Binding
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Binding -> ShowS
showsPrec :: Int -> Binding -> ShowS
$cshow :: Binding -> String
show :: Binding -> String
$cshowList :: [Binding] -> ShowS
showList :: [Binding] -> ShowS
Show)
data SignedMessage = SignedMessage
{
SignedMessage -> ByteString
coseSign1 :: !ByteString,
SignedMessage -> ByteString
coseKey :: !ByteString
}
deriving (SignedMessage -> SignedMessage -> Bool
(SignedMessage -> SignedMessage -> Bool)
-> (SignedMessage -> SignedMessage -> Bool) -> Eq SignedMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SignedMessage -> SignedMessage -> Bool
== :: SignedMessage -> SignedMessage -> Bool
$c/= :: SignedMessage -> SignedMessage -> Bool
/= :: SignedMessage -> SignedMessage -> Bool
Eq, Int -> SignedMessage -> ShowS
[SignedMessage] -> ShowS
SignedMessage -> String
(Int -> SignedMessage -> ShowS)
-> (SignedMessage -> String)
-> ([SignedMessage] -> ShowS)
-> Show SignedMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SignedMessage -> ShowS
showsPrec :: Int -> SignedMessage -> ShowS
$cshow :: SignedMessage -> String
show :: SignedMessage -> String
$cshowList :: [SignedMessage] -> ShowS
showList :: [SignedMessage] -> ShowS
Show)
addressBinding :: Address.Address -> Binding
addressBinding :: Address -> Binding
addressBinding = NativeCode -> ByteString -> Binding
Binding NativeCode
0 (ByteString -> Binding)
-> (Address -> ByteString) -> Address -> Binding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> ByteString
Address.toBytes
keyHashBinding :: Credential.KeyHash purpose -> Binding
keyHashBinding :: forall purpose. KeyHash purpose -> Binding
keyHashBinding = NativeCode -> ByteString -> Binding
Binding NativeCode
1 (ByteString -> Binding)
-> (KeyHash purpose -> ByteString) -> KeyHash purpose -> Binding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash purpose -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes
sign :: SigningKey -> Binding -> ByteString -> IO (Either Error SignedMessage)
sign :: SigningKey
-> Binding -> ByteString -> IO (Either Error SignedMessage)
sign (SigningKey Key
key) (Binding NativeCode
kind ByteString
binding) ByteString
message =
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error SignedMessage))
-> IO (Either Error SignedMessage)
forall value.
ByteString -> (Ptr Word8 -> CSize -> IO value) -> IO value
withBytes ByteString
binding ((Ptr Word8 -> CSize -> IO (Either Error SignedMessage))
-> IO (Either Error SignedMessage))
-> (Ptr Word8 -> CSize -> IO (Either Error SignedMessage))
-> IO (Either Error SignedMessage)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
bindingPointer CSize
bindingSize ->
ByteString
-> (Ptr Word8 -> CSize -> IO (Either Error SignedMessage))
-> IO (Either Error SignedMessage)
forall value.
ByteString -> (Ptr Word8 -> CSize -> IO value) -> IO value
withBytes ByteString
message ((Ptr Word8 -> CSize -> IO (Either Error SignedMessage))
-> IO (Either Error SignedMessage))
-> (Ptr Word8 -> CSize -> IO (Either Error SignedMessage))
-> IO (Either Error SignedMessage)
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
messagePointer CSize
messageSize -> do
Either Failure SignedMessage
result <- Key
-> (Ptr NativeKey -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage)
forall value.
Key
-> (Ptr NativeKey -> IO (Either Failure value))
-> IO (Either Failure value)
Key.withKey Key
key ((Ptr NativeKey -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage))
-> (Ptr NativeKey -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage)
forall a b. (a -> b) -> a -> b
$ \Ptr NativeKey
pointer ->
(Ptr (Ptr Buffer) -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage)
forall value. (Ptr (Ptr Buffer) -> IO value) -> IO value
withOutput ((Ptr (Ptr Buffer) -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage))
-> (Ptr (Ptr Buffer) -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Buffer)
signSlot -> (Ptr (Ptr Buffer) -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage)
forall value. (Ptr (Ptr Buffer) -> IO value) -> IO value
withOutput ((Ptr (Ptr Buffer) -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage))
-> (Ptr (Ptr Buffer) -> IO (Either Failure SignedMessage))
-> IO (Either Failure SignedMessage)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Buffer)
keySlot -> do
NativeCode
code <- Ptr NativeKey
-> NativeCode
-> Ptr Word8
-> CSize
-> Ptr Word8
-> CSize
-> Ptr (Ptr Buffer)
-> Ptr (Ptr Buffer)
-> IO NativeCode
Native.cSign Ptr NativeKey
pointer NativeCode
kind Ptr Word8
bindingPointer CSize
bindingSize Ptr Word8
messagePointer CSize
messageSize Ptr (Ptr Buffer)
signSlot Ptr (Ptr Buffer)
keySlot
if NativeCode
code NativeCode -> NativeCode -> Bool
forall a. Eq a => a -> a -> Bool
/= NativeCode
Status.success
then do
Text
diagnostic <- NativeCode -> IO CString
Status.cErrorMessage NativeCode
code IO CString -> (CString -> IO Text) -> IO Text
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO Text
copyStaticText
Either Failure SignedMessage -> IO (Either Failure SignedMessage)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Failure -> Either Failure SignedMessage
forall a b. a -> Either a b
Left (NativeFailure -> Failure
Key.NativeFailure (Text -> Integer -> Text -> NativeFailure
NativeFailure Text
"cip8.sign" (NativeCode -> Integer
forall a. Integral a => a -> Integer
toInteger NativeCode
code) Text
diagnostic)))
else do
Maybe ByteString
encodedSign <- Ptr (Ptr Buffer) -> IO (Maybe ByteString)
copyBuffer Ptr (Ptr Buffer)
signSlot
Maybe ByteString
encodedKey <- Ptr (Ptr Buffer) -> IO (Maybe ByteString)
copyBuffer Ptr (Ptr Buffer)
keySlot
Either Failure SignedMessage -> IO (Either Failure SignedMessage)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Failure SignedMessage -> IO (Either Failure SignedMessage))
-> Either Failure SignedMessage
-> IO (Either Failure SignedMessage)
forall a b. (a -> b) -> a -> b
$ case (Maybe ByteString
encodedSign, Maybe ByteString
encodedKey) of
(Just ByteString
signBytes, Just ByteString
keyBytes) -> SignedMessage -> Either Failure SignedMessage
forall a b. b -> Either a b
Right (ByteString -> ByteString -> SignedMessage
SignedMessage ByteString
signBytes ByteString
keyBytes)
(Maybe ByteString, Maybe ByteString)
_ -> Failure -> Either Failure SignedMessage
forall a b. a -> Either a b
Left (NativeFailure -> Failure
Key.NativeFailure (Text -> NativeFailure
NativeContractFailure Text
"cip8.sign"))
Either Error SignedMessage -> IO (Either Error SignedMessage)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error SignedMessage -> IO (Either Error SignedMessage))
-> Either Error SignedMessage -> IO (Either Error SignedMessage)
forall a b. (a -> b) -> a -> b
$ case Either Failure SignedMessage
result of
Right SignedMessage
value -> SignedMessage -> Either Error SignedMessage
forall a b. b -> Either a b
Right SignedMessage
value
Left Failure
Key.Closed -> Error -> Either Error SignedMessage
forall a b. a -> Either a b
Left Error
SigningKeyClosed
Left (Key.NativeFailure NativeFailure
failure) -> Error -> Either Error SignedMessage
forall a b. a -> Either a b
Left (NativeFailure -> Error
fromNativeFailure NativeFailure
failure)
Left Failure
_ -> Error -> Either Error SignedMessage
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"cip8.sign")
withBytes :: ByteString -> (Ptr Word8 -> CSize -> IO value) -> IO value
withBytes :: forall value.
ByteString -> (Ptr Word8 -> CSize -> IO value) -> IO value
withBytes ByteString
bytes Ptr Word8 -> CSize -> IO value
action = ByteString -> (CStringLen -> IO value) -> IO value
forall a. ByteString -> (CStringLen -> IO a) -> IO a
ByteString.useAsCStringLen ByteString
bytes ((CStringLen -> IO value) -> IO value)
-> (CStringLen -> IO value) -> IO value
forall a b. (a -> b) -> a -> b
$ \(CString
pointer, Int
size) -> Ptr Word8 -> CSize -> IO value
action (CString -> Ptr Word8
forall a b. Ptr a -> Ptr b
castPtr CString
pointer) (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
size)
withOutput :: (Ptr (Ptr Buffer.Buffer) -> IO value) -> IO value
withOutput :: forall value. (Ptr (Ptr Buffer) -> IO value) -> IO value
withOutput Ptr (Ptr Buffer) -> IO value
action = (Ptr (Ptr Buffer) -> IO value) -> IO value
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr Buffer) -> IO value) -> IO value)
-> (Ptr (Ptr Buffer) -> IO value) -> IO value
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr Buffer)
slot -> IO (Ptr (Ptr Buffer))
-> (Ptr (Ptr Buffer) -> IO ())
-> (Ptr (Ptr Buffer) -> IO value)
-> IO value
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket (Ptr (Ptr Buffer) -> Ptr Buffer -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr (Ptr Buffer)
slot Ptr Buffer
forall a. Ptr a
nullPtr IO () -> IO (Ptr (Ptr Buffer)) -> IO (Ptr (Ptr Buffer))
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr (Ptr Buffer) -> IO (Ptr (Ptr Buffer))
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Ptr (Ptr Buffer)
slot) Ptr (Ptr Buffer) -> IO ()
Buffer.cRelease Ptr (Ptr Buffer) -> IO value
action
copyBuffer :: Ptr (Ptr Buffer.Buffer) -> IO (Maybe ByteString)
copyBuffer :: Ptr (Ptr Buffer) -> IO (Maybe ByteString)
copyBuffer Ptr (Ptr Buffer)
slot = do
Ptr Buffer
pointer <- Ptr (Ptr Buffer) -> IO (Ptr Buffer)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr Buffer)
slot
if Ptr Buffer
pointer Ptr Buffer -> Ptr Buffer -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Buffer
forall a. Ptr a
nullPtr
then Maybe ByteString -> IO (Maybe ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ByteString
forall a. Maybe a
Nothing
else do
CSize
size <- Ptr Buffer -> IO CSize
Buffer.cSize Ptr Buffer
pointer
Ptr Word8
bytes <- Ptr Buffer -> IO (Ptr Word8)
Buffer.cData Ptr Buffer
pointer
if CSize
size CSize -> CSize -> Bool
forall a. Eq a => a -> a -> Bool
== CSize
0 Bool -> Bool -> Bool
|| CSize -> Integer
forall a. Integral a => a -> Integer
toInteger CSize
size Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int
forall a. Bounded a => a
maxBound :: Int) Bool -> Bool -> Bool
|| Ptr Word8
bytes Ptr Word8 -> Ptr Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr Word8
forall a. Ptr a
nullPtr
then Maybe ByteString -> IO (Maybe ByteString)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ByteString
forall a. Maybe a
Nothing
else ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString)
-> IO ByteString -> IO (Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CStringLen -> IO ByteString
ByteString.packCStringLen (Ptr Word8 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word8
bytes, CSize -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral CSize
size)