module OpenDHT.Internal.Certificate where
import Data.Functor
import qualified Data.ByteString as BS
import Control.Monad.IO.Class
import Foreign.Ptr
import Foreign.C.Types
import Foreign.Marshal.Array
import OpenDHT.Types
import OpenDHT.Internal.InfoHash
import OpenDHT.Internal.PublicKey
newtype CCertificate = CCertificate { CCertificate -> CCertificatePtr
_certificatePtr :: CCertificatePtr }
type CCertificatePtr = Ptr ()
foreign import ccall "dht_certificate_import" dhtCertificateImportC :: Ptr CUChar -> CUInt -> IO CCertificatePtr
fromBytes :: BS.ByteString -> Dht CCertificate
fromBytes :: ByteString -> Dht CCertificate
fromBytes ByteString
bs = IO CCertificate -> Dht CCertificate
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO CCertificate -> Dht CCertificate)
-> IO CCertificate -> Dht CCertificate
forall a b. (a -> b) -> a -> b
$ [CUChar] -> (Ptr CUChar -> IO CCertificate) -> IO CCertificate
forall a b. Storable a => [a] -> (Ptr a -> IO b) -> IO b
withArray ((Word8 -> CUChar) -> [Word8] -> [CUChar]
forall a b. (a -> b) -> [a] -> [b]
map Word8 -> CUChar
CUChar ([Word8] -> [CUChar]) -> [Word8] -> [CUChar]
forall a b. (a -> b) -> a -> b
$ ByteString -> [Word8]
BS.unpack ByteString
bs)
((Ptr CUChar -> IO CCertificate) -> IO CCertificate)
-> (Ptr CUChar -> IO CCertificate) -> IO CCertificate
forall a b. (a -> b) -> a -> b
$ \ Ptr CUChar
ptrBytes -> Ptr CUChar -> CUInt -> IO CCertificatePtr
dhtCertificateImportC Ptr CUChar
ptrBytes (Int -> CUInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> CUInt) -> Int -> CUInt
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
BS.length ByteString
bs) IO CCertificatePtr
-> (CCertificatePtr -> CCertificate) -> IO CCertificate
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> CCertificatePtr -> CCertificate
CCertificate
foreign import ccall "wr_dht_certificate_get_id" dhtCertificateGetIdC :: CCertificatePtr -> CInfoHashPtr -> IO ()
idFromCertificate :: CCertificate -> Dht InfoHash
idFromCertificate :: CCertificate -> Dht InfoHash
idFromCertificate (CCertificate CCertificatePtr
cPtr) = IO String -> Dht String
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Ptr CInfoHash -> IO String) -> IO String
forall b. (Ptr CInfoHash -> IO b) -> IO b
withCInfohash Ptr CInfoHash -> IO String
strFromInfoHash) Dht String -> (String -> InfoHash) -> Dht InfoHash
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> String -> InfoHash
InfoHash
where strFromInfoHash :: Ptr CInfoHash -> IO String
strFromInfoHash Ptr CInfoHash
hPtr = do
CCertificatePtr -> Ptr CInfoHash -> IO ()
dhtCertificateGetIdC CCertificatePtr
cPtr Ptr CInfoHash
hPtr
Ptr CInfoHash -> IO String
infoHashToString Ptr CInfoHash
hPtr
foreign import ccall "wr_dht_certificate_get_long_id" dhtCertificateGetLongIdC :: CCertificatePtr -> CPKIdPtr -> IO ()
pkIdFromCertificate :: CCertificate -> Dht PKId
pkIdFromCertificate :: CCertificate -> Dht PKId
pkIdFromCertificate (CCertificate CCertificatePtr
cPtr) = IO String -> Dht String
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO ((Ptr CPKId -> IO String) -> IO String
forall b. (Ptr CPKId -> IO b) -> IO b
withCPKId Ptr CPKId -> IO String
strFromPkId) Dht String -> (String -> PKId) -> Dht PKId
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> String -> PKId
PKId
where strFromPkId :: Ptr CPKId -> IO String
strFromPkId Ptr CPKId
pkidPtr = do
CCertificatePtr -> Ptr CPKId -> IO ()
dhtCertificateGetLongIdC CCertificatePtr
cPtr Ptr CPKId
pkidPtr
Ptr CPKId -> IO String
pkIdPtrToString Ptr CPKId
pkidPtr
foreign import ccall "dht_certificate_get_publickey" dhtCertificateGetPublickeyC :: CCertificatePtr -> IO CPublicKeyPtr
publicKeyFromCertificate :: CCertificate -> Dht CPublicKey
publicKeyFromCertificate :: CCertificate -> Dht CPublicKey
publicKeyFromCertificate (CCertificate CCertificatePtr
cPtr) = IO CCertificatePtr -> Dht CCertificatePtr
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (CCertificatePtr -> IO CCertificatePtr
dhtCertificateGetPublickeyC CCertificatePtr
cPtr) Dht CCertificatePtr
-> (CCertificatePtr -> CPublicKey) -> Dht CPublicKey
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> CCertificatePtr -> CPublicKey
CPublicKey
foreign import ccall "dht_certificate_delete" dhtCertificateDeleteC :: CCertificatePtr -> IO ()
delete :: CCertificate -> Dht ()
delete :: CCertificate -> Dht ()
delete (CCertificate CCertificatePtr
cPtr) = IO () -> Dht ()
forall a. IO a -> Dht a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> Dht ()) -> IO () -> Dht ()
forall a b. (a -> b) -> a -> b
$ CCertificatePtr -> IO ()
dhtCertificateDeleteC CCertificatePtr
cPtr