From 1177dc760f2d12c551859054323580b93f2d38d9 Mon Sep 17 00:00:00 2001 From: Joe Crayne Date: Sun, 25 Nov 2018 22:26:24 -0500 Subject: ChatID is a type alias for an Ed25519 signature key. --- src/Data/Tox/Msg.hs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/Data/Tox/Msg.hs') diff --git a/src/Data/Tox/Msg.hs b/src/Data/Tox/Msg.hs index d42b092b..e8c26a56 100644 --- a/src/Data/Tox/Msg.hs +++ b/src/Data/Tox/Msg.hs @@ -10,6 +10,9 @@ {-# LANGUAGE TypeFamilies #-} module Data.Tox.Msg where +import Crypto.Error +import qualified Crypto.PubKey.Ed25519 as Ed25519 +import Data.ByteArray as BA import Data.ByteString as B import Data.Dependent.Sum import Data.Functor.Contravariant @@ -234,8 +237,28 @@ lossyness m = case msgbyte m of | otherwise -> Lossless -newtype ChatID = ChatID Nonce32 - deriving (Eq,Show,Serialize,Sized) +newtype ChatID = ChatID Ed25519.PublicKey + deriving Eq + +instance Sized ChatID where size = ConstSize 32 + +instance Serialize ChatID where + get = do + bs <- getBytes 32 + case Ed25519.publicKey bs of + CryptoPassed ed -> return $ ChatID ed + CryptoFailed e -> fail (show e) + put (ChatID ed) = putByteString $ BA.convert ed + +instance Read ChatID where + readsPrec _ s + | Right bs <- parseToken32 s + , CryptoPassed ed <- Ed25519.publicKey bs + = [ (ChatID ed, Prelude.drop 43 s) ] + | otherwise = [] + +instance Show ChatID where + show (ChatID ed) = showToken32 ed data InviteType = GroupInvite { groupName :: Text } | AccptedInvite -- cgit v1.2.3