summaryrefslogtreecommitdiff
path: root/src/Data/Tox.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Tox.hs')
-rw-r--r--src/Data/Tox.hs18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/Data/Tox.hs b/src/Data/Tox.hs
index 888ca3b6..b9df7582 100644
--- a/src/Data/Tox.hs
+++ b/src/Data/Tox.hs
@@ -1,3 +1,6 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE StandaloneDeriving #-}
3{-# LANGUAGE FlexibleInstances #-}
1{-# LANGUAGE DeriveDataTypeable #-} 4{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveFunctor #-} 5{-# LANGUAGE DeriveFunctor #-}
3{-# LANGUAGE DeriveGeneric #-} 6{-# LANGUAGE DeriveGeneric #-}
@@ -10,23 +13,23 @@
10{-# LANGUAGE UnboxedTuples #-} 13{-# LANGUAGE UnboxedTuples #-}
11module Data.Tox where 14module Data.Tox where
12 15
16import Data.Bits
13import Data.ByteString (ByteString) 17import Data.ByteString (ByteString)
14import Data.Data (Data) 18import Data.Data (Data)
15import Data.Word 19import Data.Word
16import Data.LargeWord 20import Data.LargeWord
17import Data.IP 21import Data.IP
18import Data.Serialize 22import Data.Serialize
19import Network.BitTorrent.Address (NodeInfo(..)) -- Serialize IP 23-- import Network.BitTorrent.Address (NodeInfo(..)) -- Serialize IP
20import GHC.Generics (Generic) 24import GHC.Generics (Generic)
21import Network.Socket 25import Network.Socket
22import Network.RPC hiding (NodeId) 26import Network.RPC
23import qualified Network.RPC as Envelope (NodeId) 27import qualified Network.RPC as Envelope (NodeId)
24import Crypto.PubKey.ECC.Types 28import Crypto.PubKey.ECC.Types
25 29
26type Key32 = Word256 -- 32 byte key 30type Key32 = Word256 -- 32 byte key
27type Nonce8 = Word64 -- 8 bytes 31type Nonce8 = Word64 -- 8 bytes
28type Nonce24 = Word192 -- 24 bytes 32type Nonce24 = Word192 -- 24 bytes
29type NodeId = Word256 -- 32 bytes (mainline uses only 20-byte node IDs)
30 33
31 34
32data NodeFormat = NodeFormat 35data NodeFormat = NodeFormat
@@ -91,12 +94,14 @@ instance (Serialize a, Serialize b) => Serialize (LargeKey a b) where
91-- | Use with 'PingPayload', 'GetNodesPayload', or 'SendNodesPayload' 94-- | Use with 'PingPayload', 'GetNodesPayload', or 'SendNodesPayload'
92data Message a = Message 95data Message a = Message
93 { msgType :: MessageType 96 { msgType :: MessageType
94 , msgClient :: NodeId 97 , msgClient :: NodeId Message
95 , msgNonce :: Nonce24 98 , msgNonce :: Nonce24
96 , msgPayload :: a 99 , msgPayload :: a
97 } 100 }
98 deriving (Show, Generic, Functor, Foldable, Traversable) 101 deriving (Show, Generic, Functor, Foldable, Traversable)
99 102
103deriving instance Show (NodeId Message) -- TODO: print as hex
104
100isQuery :: Message a -> Bool 105isQuery :: Message a -> Bool
101isQuery (Message { msgType = SendNodes }) = False 106isQuery (Message { msgType = SendNodes }) = False
102isQuery (Message { msgType = MessageType typ }) | even typ = True 107isQuery (Message { msgType = MessageType typ }) | even typ = True
@@ -114,7 +119,7 @@ data PingPayload = PingPayload
114 } 119 }
115 120
116data GetNodesPayload = GetNodesPayload 121data GetNodesPayload = GetNodesPayload
117 { nodesForWho :: NodeId 122 { nodesForWho :: NodeId Message
118 , nodesNonce :: Nonce8 123 , nodesNonce :: Nonce8
119 } 124 }
120 125
@@ -249,7 +254,8 @@ curve25519 = CurveFP (CurvePrime prime curvecommon)
249 254
250instance Envelope Message where 255instance Envelope Message where
251 type TransactionID Message = Nonce24 256 type TransactionID Message = Nonce24
252 type NodeId Message = NodeId 257 newtype NodeId Message = NodeId Word256
258 deriving (Serialize, Eq, Ord, Bits, FiniteBits)
253 259
254 envelopePayload = msgPayload 260 envelopePayload = msgPayload
255 261