summaryrefslogtreecommitdiff
path: root/src/Network/Tox
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2018-11-25 22:26:24 -0500
committerJoe Crayne <joe@jerkface.net>2018-12-16 14:08:26 -0500
commit1177dc760f2d12c551859054323580b93f2d38d9 (patch)
tree2dcd7d5fb26f0af4594ebd484f2d3aa33bd1badc /src/Network/Tox
parentdfcab14e4d593f6a51db3fa5cf61f0358dc0f280 (diff)
ChatID is a type alias for an Ed25519 signature key.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r--src/Network/Tox/NodeId.hs14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/Network/Tox/NodeId.hs b/src/Network/Tox/NodeId.hs
index 97faa942..56ddf03c 100644
--- a/src/Network/Tox/NodeId.hs
+++ b/src/Network/Tox/NodeId.hs
@@ -37,6 +37,8 @@ module Network.Tox.NodeId
37 , verifyChecksum 37 , verifyChecksum
38 , ToxContact(..) 38 , ToxContact(..)
39 , ToxProgress(..) 39 , ToxProgress(..)
40 , parseToken32
41 , showToken32
40 ) where 42 ) where
41 43
42import Control.Applicative 44import Control.Applicative
@@ -166,18 +168,26 @@ nmtoken64 True '+' = '.'
166nmtoken64 True '/' = '-' 168nmtoken64 True '/' = '-'
167nmtoken64 _ c = c 169nmtoken64 _ c = c
168 170
171-- | Parse 43-digit base64 token into 32-byte bytestring.
172parseToken32 :: String -> Either String ByteString
173parseToken32 str = fmap (BA.drop 1) $ Base64.decode $ C8.pack $ 'A':map (nmtoken64 False) (take 43 str)
174
175-- | Encode 32-byte bytestring as 43-digit base64 token.
176showToken32 :: ByteArrayAccess bin => bin -> String
177showToken32 bs = map (nmtoken64 True) $ C8.unpack $ BA.drop 1 $ Base64.encode $ BA.cons 0 $ BA.convert bs
178
169instance Read NodeId where 179instance Read NodeId where
170 readsPrec _ str 180 readsPrec _ str
171 | (bs,_) <- Base16.decode (C8.pack $ take 64 str) 181 | (bs,_) <- Base16.decode (C8.pack $ take 64 str)
172 , CryptoPassed pub <- publicKey bs -- B.length bs == 32 182 , CryptoPassed pub <- publicKey bs -- B.length bs == 32
173 = [ (key2id pub, drop (2 * B.length bs) str) ] 183 = [ (key2id pub, drop (2 * B.length bs) str) ]
174 | Right bs <- fmap (BA.drop 1) $ Base64.decode $ C8.pack $ 'A':map (nmtoken64 False) (take 43 str) 184 | Right bs <- parseToken32 str
175 , CryptoPassed pub <- publicKey bs -- B.length bs == 32 185 , CryptoPassed pub <- publicKey bs -- B.length bs == 32
176 = [ (key2id pub, drop 43 str) ] 186 = [ (key2id pub, drop 43 str) ]
177 | otherwise = [] 187 | otherwise = []
178 188
179instance Show NodeId where 189instance Show NodeId where
180 show nid = map (nmtoken64 True) $ C8.unpack $ BA.drop 1 $ Base64.encode $ BA.cons 0 $ BA.convert $ id2key nid 190 show nid = showToken32 $ id2key nid
181 191
182instance S.Serialize NodeId where 192instance S.Serialize NodeId where
183 get = key2id <$> getPublicKey 193 get = key2id <$> getPublicKey