aboutsummaryrefslogtreecommitdiff
path: root/src/Ssb/Peer.hs
diff options
context:
space:
mode:
authorHaskell Guy <haskell.guy@localhost>2020-05-26 13:07:50 +0200
committerHaskell Guy <haskell.guy@localhost>2020-05-26 13:37:29 +0200
commit41cde99ec6189dbecca6803a5aa4f6f18142e8ba (patch)
tree7a0ceab0d516b8c3b7b49313100ae50c97e875c3 /src/Ssb/Peer.hs
downloadssb-haskell-41cde99ec6189dbecca6803a5aa4f6f18142e8ba.tar.xz
initial commit
Diffstat (limited to 'src/Ssb/Peer.hs')
-rw-r--r--src/Ssb/Peer.hs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/Ssb/Peer.hs b/src/Ssb/Peer.hs
new file mode 100644
index 0000000..75078ff
--- /dev/null
+++ b/src/Ssb/Peer.hs
@@ -0,0 +1,60 @@
+-- | This module implements Scuttlebutt Peers
+--
+-- https://ssbc.github.io/scuttlebutt-protocol-guide/#peer-connections
+
+module Ssb.Peer where
+
+import Protolude hiding ( Identity )
+import Control.Arrow ((***))
+import qualified Data.ByteString.Base64 as Base64
+import qualified Network.Simple.TCP as TCP
+import qualified Data.Text as Text
+
+import Ssb.Identity
+import Ssb.Network
+import qualified Ssb.Peer.SecretHandshake as SH
+import qualified Ssb.Peer.RPC as RPC
+
+-- | TODO: Is there a standard way to re-export types?
+type NetworkIdentifier = SH.NetworkIdentifier
+
+-- | mainNet is the default Network where one will find most scuttlebutt
+-- traffic.
+mainNet :: NetworkIdentifier
+mainNet = Base64.decodeLenient "1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s="
+
+-- TODO: Perfect MultiAddress implementation
+
+data MultiAddress = MultiAddress
+ { protocol :: Text
+ , host :: Host
+ , port :: Port
+ , key :: PublicKey
+ } deriving (Eq, Show)
+
+id :: MultiAddress -> Identity
+id ma = Identity Nothing (key ma)
+
+parseMultiAddress :: Text -> Either Text MultiAddress
+parseMultiAddress txt = do
+ let (protocol, postProtocol) = split ":" txt
+ let (addressPort, postAddress) = split "~" postProtocol
+ let (address, port) = splitOnEnd ":" addressPort
+ let (hashType, key) = split ":" postAddress
+ return MultiAddress
+ { protocol = protocol
+ , 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
+
+-- Example string "net:some.ho.st:8008~shs:SomeActuallyValidPubKey="
+formatMultiAddress :: MultiAddress -> Text
+formatMultiAddress addr =
+ protocol addr <> ":"
+ <> host addr <> ":" <> port addr
+ <> "~shs:"
+ <> formatPublicKey (key addr)