diff options
-rw-r--r-- | DHTTransport.hs | 18 | ||||
-rw-r--r-- | ToxCrypto.hs | 5 |
2 files changed, 21 insertions, 2 deletions
diff --git a/DHTTransport.hs b/DHTTransport.hs index ac263cdc..49d3034d 100644 --- a/DHTTransport.hs +++ b/DHTTransport.hs | |||
@@ -2,8 +2,10 @@ | |||
2 | {-# LANGUAGE KindSignatures #-} | 2 | {-# LANGUAGE KindSignatures #-} |
3 | {-# LANGUAGE LambdaCase #-} | 3 | {-# LANGUAGE LambdaCase #-} |
4 | {-# LANGUAGE RankNTypes #-} | 4 | {-# LANGUAGE RankNTypes #-} |
5 | {-# LANGUAGE StandaloneDeriving #-} | ||
5 | {-# LANGUAGE TupleSections #-} | 6 | {-# LANGUAGE TupleSections #-} |
6 | {-# LANGUAGE TypeOperators #-} | 7 | {-# LANGUAGE TypeOperators #-} |
8 | {-# LANGUAGE UndecidableInstances #-} | ||
7 | module DHTTransport | 9 | module DHTTransport |
8 | ( parseDHTAddr | 10 | ( parseDHTAddr |
9 | , encodeDHTAddr | 11 | , encodeDHTAddr |
@@ -52,6 +54,15 @@ data DHTMessage (f :: * -> *) | |||
52 | | DHTCookie Nonce24 (f Cookie) | 54 | | DHTCookie Nonce24 (f Cookie) |
53 | | DHTDHTRequest PublicKey (Assym (f DHTRequest)) | 55 | | DHTDHTRequest PublicKey (Assym (f DHTRequest)) |
54 | 56 | ||
57 | deriving instance ( Show (f Cookie) | ||
58 | , Show (Assym (f Ping)) | ||
59 | , Show (Assym (f Pong)) | ||
60 | , Show (Assym (f GetNodes)) | ||
61 | , Show (Assym (f SendNodes)) | ||
62 | , Show (Assym (f CookieRequest)) | ||
63 | , Show (Assym (f DHTRequest)) | ||
64 | ) => Show (DHTMessage f) | ||
65 | |||
55 | mapMessage :: forall f b. (forall a. Nonce24 -> f a -> b) -> DHTMessage f -> b | 66 | mapMessage :: forall f b. (forall a. Nonce24 -> f a -> b) -> DHTMessage f -> b |
56 | mapMessage f (DHTPing a) = f (assymNonce a) (assymData a) | 67 | mapMessage f (DHTPing a) = f (assymNonce a) (assymData a) |
57 | mapMessage f (DHTPong a) = f (assymNonce a) (assymData a) | 68 | mapMessage f (DHTPong a) = f (assymNonce a) (assymData a) |
@@ -152,6 +163,7 @@ data DHTRequest | |||
152 | -- `8` `uint64_t` random number (the same that was received in request) | 163 | -- `8` `uint64_t` random number (the same that was received in request) |
153 | | NATPong Nonce8 | 164 | | NATPong Nonce8 |
154 | | DHTPK LongTermKeyWrap | 165 | | DHTPK LongTermKeyWrap |
166 | deriving Show | ||
155 | 167 | ||
156 | instance Sized DHTRequest where | 168 | instance Sized DHTRequest where |
157 | size = VarSize $ \case | 169 | size = VarSize $ \case |
@@ -204,6 +216,7 @@ data LongTermKeyWrap = LongTermKeyWrap | |||
204 | , wrapNonce :: Nonce24 | 216 | , wrapNonce :: Nonce24 |
205 | , wrapData :: Encrypted DHTPublicKey | 217 | , wrapData :: Encrypted DHTPublicKey |
206 | } | 218 | } |
219 | deriving Show | ||
207 | 220 | ||
208 | instance Serialize LongTermKeyWrap where | 221 | instance Serialize LongTermKeyWrap where |
209 | get = LongTermKeyWrap <$> getPublicKey <*> get <*> get | 222 | get = LongTermKeyWrap <$> getPublicKey <*> get <*> get |
@@ -268,9 +281,12 @@ instance S.Serialize Pong where | |||
268 | put Pong = S.put (1 :: Word8) | 281 | put Pong = S.put (1 :: Word8) |
269 | 282 | ||
270 | newtype CookieRequest = CookieRequest PublicKey | 283 | newtype CookieRequest = CookieRequest PublicKey |
284 | deriving (Eq, Show) | ||
271 | newtype CookieResponse = CookieResponse Cookie | 285 | newtype CookieResponse = CookieResponse Cookie |
286 | deriving (Eq, Show) | ||
272 | 287 | ||
273 | data Cookie = Cookie Nonce24 (Encrypted CookieData) | 288 | data Cookie = Cookie Nonce24 (Encrypted CookieData) |
289 | deriving (Eq, Ord, Show) | ||
274 | 290 | ||
275 | instance Sized Cookie where size = ConstSize 112 -- 24 byte nonce + 88 byte cookie data | 291 | instance Sized Cookie where size = ConstSize 112 -- 24 byte nonce + 88 byte cookie data |
276 | 292 | ||
diff --git a/ToxCrypto.hs b/ToxCrypto.hs index 5e602fc9..4a466357 100644 --- a/ToxCrypto.hs +++ b/ToxCrypto.hs | |||
@@ -92,6 +92,9 @@ instance Serialize Auth where | |||
92 | get = Auth . Poly1305.Auth . BA.convert <$> getBytes 16 | 92 | get = Auth . Poly1305.Auth . BA.convert <$> getBytes 16 |
93 | put (Auth (Poly1305.Auth bs)) = putByteString $ BA.convert bs | 93 | put (Auth (Poly1305.Auth bs)) = putByteString $ BA.convert bs |
94 | 94 | ||
95 | instance Typeable a => Show (Encrypted a) where | ||
96 | show (Encrypted _) = "Encrypted "++show (typeOf (undefined :: a)) | ||
97 | |||
95 | encryptedAuth :: Encrypted a -> Auth | 98 | encryptedAuth :: Encrypted a -> Auth |
96 | encryptedAuth (Encrypted bs) | 99 | encryptedAuth (Encrypted bs) |
97 | | Right auth <- decode (B.take 16 bs) = auth | 100 | | Right auth <- decode (B.take 16 bs) = auth |
@@ -279,7 +282,7 @@ data Assym a = Assym | |||
279 | , assymNonce :: Nonce24 | 282 | , assymNonce :: Nonce24 |
280 | , assymData :: a | 283 | , assymData :: a |
281 | } | 284 | } |
282 | deriving (Functor,Foldable,Traversable) | 285 | deriving (Functor,Foldable,Traversable, Show) |
283 | 286 | ||
284 | instance Sized a => Sized (Assym a) where | 287 | instance Sized a => Sized (Assym a) where |
285 | size = case size of | 288 | size = case size of |