~haskell-guy/ssb-haskell

ref: 41cde99ec6189dbecca6803a5aa4f6f18142e8ba ssb-haskell/src/Ssb/Pub.hs -rw-r--r-- 1.4 KiB
41cde99e — Haskell Guy initial commit 10 months ago
                                                                                
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
-- | This module defines SSB pubs used as a gathering point for peers not sharing a local
-- network.
--
-- https://ssbc.github.io/scuttlebutt-protocol-guide/#pubs
module Ssb.Pub where

import           Protolude
import           Control.Arrow ((***))
import qualified Data.Text as Text
import qualified Data.ByteString.Base64        as Base64

import           Ssb.Identity
import           Ssb.Network

data Invite = Invite
    {
    } deriving (Eq)

data PubAddress = PubAddress
    { host :: Host
    , port :: Port
    , key  :: PublicKey
    } deriving (Eq, Show)

-- | Create a Pub Address from the given text.
-- Example string "net:some.ho.st:8008~shs:SomeActuallyValidPubKey="
parsePub :: Text -> Either Text PubAddress
parsePub txt = do
    let (protocol, postProtocol) = split ":" txt
    let (addressPort, postAddress) = split "~" postProtocol
    let (address, port) = splitOnEnd ":" addressPort
    let (hashType, key) = split ":" postAddress
    return PubAddress
      { host = address
      , port = port
      , key  = PublicKey $ Base64.decodeLenient $ toS key
      }
  where
    split c arg = (identity *** Text.drop 1) $ Text.breakOn c arg
    splitOnEnd c arg = (Text.dropEnd 1 *** identity) $ Text.breakOnEnd c arg

-- TODO: Complete formatPub
formatPub :: PubAddress -> Text
formatPub pub = undefined

newtype PubMessage = PubMessage PubAddress
    deriving (Eq, Show)