summaryrefslogtreecommitdiff
path: root/dht/examples/testcookie.hs
blob: 4302ad35c5c66db676e415271f2369aca77af2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65


import System.Exit

import Control.Concurrent.STM
import Crypto.Tox
import Network.Tox
import Network.Tox.DHT.Handlers
import Network.Tox.Crypto.Handlers
import Network.Tox.DHT.Transport (Cookie(..))

import Data.Serialize as S

import Network.Tox.Crypto.Transport
import Data.Word

--
-- Some relevant functions:
--
-- src/Network/Tox.hs
-- newCrypto :: IO TransportCrypto
--
-- src/Network/Tox/DHT/Handlers.hs
-- createCookie :: TransportCrypto -> NodeInfo -> PublicKey -> IO Cookie
--
-- src/Crypto/Tox.hs
-- decryptSymmetric :: SymmetricKey -> Nonce24 -> Encrypted a -> Either String (Plain s a)
--
--src/Network/Tox/NodeId.hs
--nodeInfo :: NodeId -> SockAddr -> Either String NodeInfo
--

main = do
    crypto <- newCrypto
    secUser <- generateSecretKey
    let pubUser = toPublic secUser
        node = read "Ivr3mkGriCmv5FeF91UPZbkirDfpIagXcfvo6ozUCRp@92.99.99.99:33412"
    ecookie@(Cookie cookieNonce eCookieData) <- createCookie crypto node pubUser

    let bs = encode ecookie
    print $ (decode bs :: Either String Cookie)


    sym <- atomically $ transportSymmetric crypto
    print $ decryptSymmetric sym cookieNonce eCookieData >>= decodePlain

    n24 <- atomically $ transportNewNonce crypto
    putStrLn $ "n24 = " ++ show n24
    let e24 = S.encode n24
    case (S.decode e24) of
        Left e -> do
            putStrLn $ "serialize Failure:" ++ show e
            exitFailure
        Right  n24' -> do
          putStrLn $ "n24' = " ++ show n24'
          if n24' == n24 then doContinue else exitFailure

doContinue = do
    let allmsgids = [Padding .. Messenger255]
        allgrpmsgids = [Ping .. MessageName0xff]
        allmsgs = map Msg allmsgids 
               ++ concatMap (\x -> [GrpMsg KnownLossless x,GrpMsg KnownLossy x]) allgrpmsgids
        typmap :: [(MessageType,Word64)]
        typmap =  map (\x -> (x, toWord64 x)) allmsgs
    mapM_ print typmap