summaryrefslogtreecommitdiff
path: root/examples/testcookie.hs
blob: 5f5d4e834b74a85444997434872ba52cda3ac247 (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


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

--
-- 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 exitSuccess else exitFailure