files: file description types (#631)

This commit is contained in:
JRoberts
2023-02-09 21:22:58 +04:00
committed by GitHub
parent 3637d0c610
commit 1523c00bcc
7 changed files with 131 additions and 2 deletions
+91 -1
View File
@@ -1 +1,91 @@
module Simplex.FileTransfer.Description where
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
module Simplex.FileTransfer.Description
( FileDescription (..),
FileDigest (..),
FileChunk (..),
FileChunkReplica (..),
YAMLFileDescription (..),
YAMLFilePart (..),
)
where
import Data.Aeson (FromJSON, ToJSON)
import Data.ByteString.Char8 (ByteString)
import Data.Int (Int64)
import Data.Word (Word32)
import qualified Data.Yaml as Y
import GHC.Generics (Generic)
import qualified Simplex.Messaging.Crypto as C
import Simplex.Messaging.Encoding.String
data FileDescription = FileDescription
{ name :: String,
size :: Int64,
digest :: FileDigest,
encKey :: C.Key,
iv :: C.IV,
chunks :: [FileChunk]
}
deriving (Show)
newtype FileDigest = FileDigest {unFileDigest :: ByteString}
deriving (Eq, Show)
instance StrEncoding FileDigest where
strEncode (FileDigest fd) = strEncode fd
strDecode s = FileDigest <$> strDecode s
strP = FileDigest <$> strP
instance FromJSON FileDigest where
parseJSON = strParseJSON "FileDigest"
instance ToJSON FileDigest where
toJSON = strToJSON
toEncoding = strToJEncoding
data FileChunk = FileChunk
{ chunkNo :: Int,
digest :: ByteString,
chunkSize :: Word32,
replicas :: [FileChunkReplica]
}
deriving (Show)
data FileChunkReplica = FileChunkReplica
{ server :: String,
rcvId :: ByteString,
rcvKey :: C.APrivateSignKey
}
deriving (Show)
data YAMLFileDescription = YAMLFileDescription
{ name :: String,
size :: Int64,
chunkSize :: Word32,
digest :: FileDigest,
encKey :: C.Key,
iv :: C.IV,
parts :: [YAMLFilePart]
}
deriving (Eq, Show, Generic)
instance FromJSON YAMLFileDescription
data YAMLFilePart = YAMLFilePart
{ server :: String,
chunks :: [String]
}
deriving (Eq, Show, Generic)
instance FromJSON YAMLFilePart
data FilePartChunk = FilePartChunk
{ chunkNo :: Int,
rcvId :: ByteString,
rcvKey :: C.APrivateSignKey,
digest :: Maybe ByteString,
chunkSize :: Maybe Word32
}
deriving (Show)
+8
View File
@@ -742,11 +742,19 @@ instance FromJSON Key where
-- | IV bytes newtype.
newtype IV = IV {unIV :: ByteString}
deriving (Eq, Show)
instance Encoding IV where
smpEncode = unIV
smpP = IV <$> A.take (ivSize @AES256)
instance ToJSON IV where
toJSON = strToJSON . unIV
toEncoding = strToJEncoding . unIV
instance FromJSON IV where
parseJSON = fmap IV . strParseJSON "IV"
newtype AuthTag = AuthTag {unAuthTag :: AES.AuthTag}
instance Encoding AuthTag where