2020-01-23 07:16:09 +03:00
|
|
|
{-|
|
2021-08-11 03:02:23 +03:00
|
|
|
Use L2 to access PKI information.
|
2020-01-23 07:16:09 +03:00
|
|
|
-}
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2020-09-04 21:44:26 +03:00
|
|
|
module Urbit.Vere.Dawn ( dawnVent
|
|
|
|
, dawnCometList
|
|
|
|
, renderShip
|
|
|
|
, mineComet
|
2020-09-05 00:02:11 +03:00
|
|
|
-- Used only in testing
|
|
|
|
, mix
|
|
|
|
, shas
|
|
|
|
, shaf
|
2020-10-15 20:05:54 +03:00
|
|
|
, deriveCode
|
2020-09-05 00:02:11 +03:00
|
|
|
, cometFingerprintBS
|
|
|
|
, cometFingerprint
|
2020-09-04 21:44:26 +03:00
|
|
|
) where
|
2020-01-23 07:16:09 +03:00
|
|
|
|
2020-01-24 08:28:38 +03:00
|
|
|
import Urbit.Arvo.Common
|
|
|
|
import Urbit.Arvo.Event hiding (Address)
|
2020-11-03 22:03:58 +03:00
|
|
|
import Urbit.Prelude hiding (rights, to, (.=))
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
import Prelude (read)
|
|
|
|
|
2019-10-09 23:39:11 +03:00
|
|
|
import Data.Bits (xor)
|
2019-10-04 02:39:16 +03:00
|
|
|
import Data.List (nub)
|
2019-09-26 00:26:51 +03:00
|
|
|
import Data.Text (splitOn)
|
2020-08-28 23:17:46 +03:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.HexString
|
2020-08-07 20:18:16 +03:00
|
|
|
|
2019-10-10 23:45:01 +03:00
|
|
|
import qualified Crypto.Hash.SHA256 as SHA256
|
2019-10-09 23:39:11 +03:00
|
|
|
import qualified Crypto.Hash.SHA512 as SHA512
|
|
|
|
import qualified Crypto.Sign.Ed25519 as Ed
|
|
|
|
import qualified Data.Binary as B
|
|
|
|
import qualified Data.ByteString as BS
|
|
|
|
import qualified Data.ByteString.Char8 as C
|
2020-09-04 21:44:26 +03:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
2019-10-09 23:39:11 +03:00
|
|
|
import qualified Network.HTTP.Client as C
|
|
|
|
import qualified Urbit.Ob as Ob
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2020-08-28 23:17:46 +03:00
|
|
|
import qualified Network.HTTP.Client.TLS as TLS
|
|
|
|
import qualified Network.HTTP.Types as HT
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2020-08-28 23:17:46 +03:00
|
|
|
-- Conversion Utilities --------------------------------------------------------
|
2019-09-25 03:15:00 +03:00
|
|
|
|
2021-09-01 18:27:13 +03:00
|
|
|
passFromBytes :: ByteString -> ByteString -> Int -> Pass
|
|
|
|
passFromBytes enc aut sut
|
2021-08-11 03:02:23 +03:00
|
|
|
| sut /= 1 = Pass (Ed.PublicKey mempty) (Ed.PublicKey mempty)
|
2021-09-01 18:27:13 +03:00
|
|
|
| otherwise = Pass (Ed.PublicKey aut) (Ed.PublicKey enc)
|
2019-09-20 20:41:58 +03:00
|
|
|
|
2019-09-21 02:10:03 +03:00
|
|
|
clanFromShip :: Ship -> Ob.Class
|
|
|
|
clanFromShip = Ob.clan . Ob.patp . fromIntegral
|
|
|
|
|
2019-09-24 00:42:19 +03:00
|
|
|
shipSein :: Ship -> Ship
|
|
|
|
shipSein = Ship . fromIntegral . Ob.fromPatp . Ob.sein . Ob.patp . fromIntegral
|
|
|
|
|
2019-09-26 21:14:24 +03:00
|
|
|
renderShip :: Ship -> Text
|
|
|
|
renderShip = Ob.renderPatp . Ob.patp . fromIntegral
|
2019-09-25 00:01:39 +03:00
|
|
|
|
2020-09-04 17:54:31 +03:00
|
|
|
onLeft :: (a -> b) -> Either a c -> Either b c
|
2020-09-04 21:44:26 +03:00
|
|
|
onLeft fun = bimap fun id
|
2020-09-04 17:54:31 +03:00
|
|
|
|
2019-09-21 02:10:03 +03:00
|
|
|
-- Data Validation -------------------------------------------------------------
|
|
|
|
|
2019-09-26 00:26:51 +03:00
|
|
|
-- Derive public key structure from the key derivation seed structure
|
2019-10-10 23:45:01 +03:00
|
|
|
ringToPass :: Ring -> Pass
|
|
|
|
ringToPass Ring{..} = Pass{..}
|
2019-09-25 03:15:00 +03:00
|
|
|
where
|
|
|
|
passCrypt = decode ringCrypt
|
|
|
|
passSign = decode ringSign
|
2019-09-26 00:26:51 +03:00
|
|
|
decode = fst . fromJust . Ed.createKeypairFromSeed_
|
2019-10-10 02:58:54 +03:00
|
|
|
fromJust = \case
|
|
|
|
Nothing -> error "Invalid seed passed to createKeypairFromSeed"
|
|
|
|
Just x -> x
|
2019-09-25 00:01:39 +03:00
|
|
|
|
2020-08-28 23:17:46 +03:00
|
|
|
-- JSONRPC Functions -----------------------------------------------------------
|
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
-- Our use case here is simple enough.
|
2021-09-01 18:32:59 +03:00
|
|
|
-- Network.JSONRPC appeared fragile and has no documentation.
|
2020-08-28 23:17:46 +03:00
|
|
|
-- So, like with Vere, we roll our own.
|
|
|
|
|
2020-09-04 21:44:26 +03:00
|
|
|
dawnSendHTTP :: String -> L.ByteString -> RIO e (Either Int L.ByteString)
|
2020-08-28 23:17:46 +03:00
|
|
|
dawnSendHTTP endpoint requestData = liftIO do
|
|
|
|
manager <- C.newManager TLS.tlsManagerSettings
|
|
|
|
|
|
|
|
initialRequest <- C.parseRequest endpoint
|
|
|
|
let request = initialRequest
|
|
|
|
{ C.method = "POST"
|
|
|
|
, C.requestBody = C.RequestBodyLBS $ requestData
|
2020-09-04 21:44:26 +03:00
|
|
|
, C.requestHeaders = [("Accept", "application/json"),
|
2020-08-28 23:17:46 +03:00
|
|
|
("Content-Type", "application/json"),
|
|
|
|
("Charsets", "utf-8")]
|
|
|
|
}
|
|
|
|
|
|
|
|
response <- C.httpLbs request manager
|
|
|
|
|
|
|
|
-- Return body if 200.
|
|
|
|
let code = HT.statusCode $ C.responseStatus response
|
|
|
|
case code of
|
|
|
|
200 -> pure $ Right $ C.responseBody response
|
2020-09-04 21:44:26 +03:00
|
|
|
_ -> pure $ Left code
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
class RequestMethod m where
|
|
|
|
getRequestMethod :: m -> Text
|
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
data RawResponse r = RawResponse
|
2020-08-28 23:17:46 +03:00
|
|
|
{ rrId :: Int
|
2021-08-11 03:02:23 +03:00
|
|
|
, rrResult :: r
|
2020-08-28 23:17:46 +03:00
|
|
|
}
|
|
|
|
deriving (Show)
|
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
instance FromJSON r => FromJSON (RawResponse r) where
|
2020-08-28 23:17:46 +03:00
|
|
|
parseJSON = withObject "Response" $ \v -> do
|
2021-08-11 03:02:23 +03:00
|
|
|
rawId <- v .: "id"
|
2020-08-28 23:17:46 +03:00
|
|
|
rrResult <- v .: "result"
|
2021-08-11 03:02:23 +03:00
|
|
|
let rrId = read rawId
|
2020-08-28 23:17:46 +03:00
|
|
|
pure RawResponse{..}
|
|
|
|
|
|
|
|
|
|
|
|
-- Given a list of methods and parameters, return a list of decoded responses.
|
2021-08-11 03:02:23 +03:00
|
|
|
dawnPostRequests :: forall req e resp res
|
|
|
|
. (ToJSON req, RequestMethod req, FromJSON res)
|
2020-08-28 23:17:46 +03:00
|
|
|
=> String
|
2021-08-11 03:02:23 +03:00
|
|
|
-> (req -> res -> resp)
|
2020-08-28 23:17:46 +03:00
|
|
|
-> [req]
|
|
|
|
-> RIO e [resp]
|
|
|
|
dawnPostRequests endpoint responseBuilder requests = do
|
|
|
|
-- Encode our input requests
|
|
|
|
let requestPayload =
|
2020-09-04 21:44:26 +03:00
|
|
|
encode $ Array $ fromList $ fmap toFullRequest $ zip [0..] requests
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
-- Send to the server
|
|
|
|
responses <- dawnSendHTTP endpoint requestPayload >>= \case
|
2020-10-27 21:01:59 +03:00
|
|
|
Left err -> error $ "error fetching " <> endpoint <> ": HTTP " <> (show err)
|
2020-08-28 23:17:46 +03:00
|
|
|
Right x -> pure x
|
|
|
|
|
|
|
|
-- Get a list of the result texts in the order of the submitted requests
|
|
|
|
rawSorted <- case decode responses of
|
|
|
|
Nothing -> error $ "couldn't decode json"
|
|
|
|
Just x -> pure $ map rrResult $ sortOn rrId x
|
|
|
|
|
|
|
|
-- Build the final result structure by calling the passed in builder with the
|
2020-09-04 21:44:26 +03:00
|
|
|
-- request (some outputs need data from the request structure, eitherwise,
|
2020-08-28 23:17:46 +03:00
|
|
|
-- we'd lean on FromJSON).
|
|
|
|
let results = map (uncurry responseBuilder) (zip requests rawSorted)
|
|
|
|
pure results
|
|
|
|
|
|
|
|
where
|
|
|
|
toFullRequest :: (Int, req) -> Value
|
2020-09-04 21:44:26 +03:00
|
|
|
toFullRequest (rid, req) = object [ "jsonrpc" .= ("2.0" :: Text)
|
|
|
|
, "method" .= getRequestMethod req
|
|
|
|
, "params" .= req
|
2021-08-11 03:02:23 +03:00
|
|
|
, "id" .= (show rid)
|
2020-09-04 21:44:26 +03:00
|
|
|
]
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
-- Azimuth JSON Requests -------------------------------------------------------
|
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
data PointResponse = PointResponse
|
2021-09-01 18:32:59 +03:00
|
|
|
-- NOTE also contains dominion and ownership, but not actually used here
|
2021-08-11 03:03:32 +03:00
|
|
|
{ prNetwork :: PointNetwork
|
2021-08-11 03:02:23 +03:00
|
|
|
} deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance FromJSON PointResponse where
|
2021-09-01 18:28:45 +03:00
|
|
|
parseJSON = withObject "PointResponse" $ \o -> do
|
2021-08-11 03:02:23 +03:00
|
|
|
prNetwork <- o .: "network"
|
|
|
|
pure PointResponse{..}
|
|
|
|
|
|
|
|
data PointNetwork = PointNetwork
|
|
|
|
{ pnKeys :: PointKeys
|
|
|
|
, pnSponsor :: PointSponsor
|
|
|
|
, pnRift :: ContNum
|
|
|
|
} deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance FromJSON PointNetwork where
|
2021-09-01 18:28:45 +03:00
|
|
|
parseJSON = withObject "PointNetwork" $ \o -> do
|
2021-08-11 03:02:23 +03:00
|
|
|
pnKeys <- o .: "keys"
|
|
|
|
pnSponsor <- o .: "sponsor"
|
|
|
|
pnRift <- o .: "rift"
|
|
|
|
pure PointNetwork{..}
|
|
|
|
|
|
|
|
data PointKeys = PointKeys
|
|
|
|
{ pkLife :: Life
|
|
|
|
, pkSuite :: Int
|
2021-09-01 18:27:13 +03:00
|
|
|
, pkAuth :: ByteString
|
|
|
|
, pkCrypt :: ByteString
|
2021-08-11 03:02:23 +03:00
|
|
|
} deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance FromJSON PointKeys where
|
2021-09-01 18:28:45 +03:00
|
|
|
parseJSON = withObject "PointKeys" $ \o -> do
|
2021-08-11 03:02:23 +03:00
|
|
|
pkLife <- o .: "life"
|
|
|
|
pkSuite <- o .: "suite"
|
2021-09-01 18:27:13 +03:00
|
|
|
rawAuth <- o .: "auth"
|
|
|
|
rawCrypt <- o .: "crypt"
|
|
|
|
let pkAuth = parseKey rawAuth
|
|
|
|
let pkCrypt = parseKey rawCrypt
|
2021-08-11 03:02:23 +03:00
|
|
|
pure PointKeys{..}
|
2021-09-01 18:27:13 +03:00
|
|
|
where
|
|
|
|
parseKey = reverse . toBytes . hexString . removePrefix . encodeUtf8
|
2021-08-11 03:02:23 +03:00
|
|
|
|
|
|
|
data PointSponsor = PointSponsor
|
|
|
|
{ psHas :: Bool
|
|
|
|
, psWho :: Text
|
|
|
|
} deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance FromJSON PointSponsor where
|
2021-09-01 18:28:45 +03:00
|
|
|
parseJSON = withObject "PointSponsor" $ \o -> do
|
2021-08-11 03:02:23 +03:00
|
|
|
psHas <- o .: "has"
|
|
|
|
psWho <- o .: "who"
|
|
|
|
pure PointSponsor{..}
|
|
|
|
|
|
|
|
data PointRequest = PointRequest Ship
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
instance RequestMethod PointRequest where
|
2021-09-01 18:32:59 +03:00
|
|
|
getRequestMethod PointRequest{} = "getPoint"
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
instance ToJSON PointRequest where
|
2021-08-11 03:02:23 +03:00
|
|
|
toJSON (PointRequest point) = object [ "ship" .= renderShip point ]
|
|
|
|
|
|
|
|
parseAzimuthPoint :: PointRequest -> PointResponse -> EthPoint
|
|
|
|
parseAzimuthPoint (PointRequest point) response = EthPoint{..}
|
2020-08-28 23:17:46 +03:00
|
|
|
where
|
2021-08-11 03:02:23 +03:00
|
|
|
net = prNetwork response
|
|
|
|
key = pnKeys net
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
-- Vere doesn't set ownership information, neither did the old Dawn.hs
|
|
|
|
-- implementation.
|
|
|
|
epOwn = (0, 0, 0, 0)
|
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
sponsorShip = Ob.parsePatp $ psWho $ pnSponsor net
|
2021-09-01 18:32:59 +03:00
|
|
|
epNet = if pkLife key == 0
|
2020-08-28 23:17:46 +03:00
|
|
|
then Nothing
|
2021-08-11 03:02:23 +03:00
|
|
|
else case sponsorShip of
|
|
|
|
Left _ -> Nothing
|
|
|
|
Right s -> Just
|
|
|
|
( fromIntegral $ pkLife key
|
2021-09-01 18:27:13 +03:00
|
|
|
, passFromBytes (pkCrypt key) (pkAuth key) (pkSuite key)
|
2021-08-11 03:02:23 +03:00
|
|
|
, fromIntegral $ pnRift net
|
|
|
|
, (psHas $ pnSponsor net, Ship $ fromIntegral $ Ob.fromPatp s)
|
2021-09-01 18:32:59 +03:00
|
|
|
, Nothing -- NOTE goes unused currently, so we simply put Nothing
|
2021-08-11 03:02:23 +03:00
|
|
|
)
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
-- I don't know what this is supposed to be, other than the old Dawn.hs and
|
|
|
|
-- dawn.c do the same thing.
|
2021-08-11 03:02:23 +03:00
|
|
|
-- zero-fill spawn data
|
|
|
|
epKid = case clanFromShip (Ship $ fromIntegral point) of
|
2020-08-28 23:17:46 +03:00
|
|
|
Ob.Galaxy -> Just (0, setToHoonSet mempty)
|
|
|
|
Ob.Star -> Just (0, setToHoonSet mempty)
|
|
|
|
_ -> Nothing
|
|
|
|
|
|
|
|
-- Preprocess data from a point request into the form used in the galaxy table.
|
2021-08-11 03:02:23 +03:00
|
|
|
parseGalaxyTableEntry :: PointRequest -> PointResponse -> (Ship, (Rift, Life, Pass))
|
|
|
|
parseGalaxyTableEntry (PointRequest point) response = (ship, (rift, life, pass))
|
2020-08-28 23:17:46 +03:00
|
|
|
where
|
2021-08-11 03:02:23 +03:00
|
|
|
net = prNetwork response
|
|
|
|
keys = pnKeys net
|
|
|
|
|
|
|
|
ship = Ship $ fromIntegral point
|
|
|
|
rift = fromIntegral $ pnRift net
|
|
|
|
life = fromIntegral $ pkLife keys
|
2021-09-01 18:27:13 +03:00
|
|
|
pass = passFromBytes (pkCrypt keys) (pkAuth keys) (pkSuite keys)
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
removePrefix :: ByteString -> ByteString
|
|
|
|
removePrefix withOhEx
|
|
|
|
| prefix == "0x" = suffix
|
|
|
|
| otherwise = error "not prefixed with 0x"
|
|
|
|
where
|
|
|
|
(prefix, suffix) = splitAt 2 withOhEx
|
|
|
|
|
2021-09-01 18:32:59 +03:00
|
|
|
data TurfRequest = TurfRequest
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
instance RequestMethod TurfRequest where
|
2021-09-01 18:32:59 +03:00
|
|
|
getRequestMethod TurfRequest = "getDns"
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
instance ToJSON TurfRequest where
|
2021-09-01 18:32:59 +03:00
|
|
|
toJSON TurfRequest = object [] -- NOTE getDns takes no parameters
|
2021-08-11 03:02:23 +03:00
|
|
|
|
|
|
|
parseTurfResponse :: TurfRequest -> [Text] -> [Turf]
|
2021-09-01 18:32:59 +03:00
|
|
|
parseTurfResponse TurfRequest = map turf
|
2021-08-11 03:02:23 +03:00
|
|
|
where
|
|
|
|
turf t = Turf $ fmap Cord $ reverse $ splitOn "." t
|
2020-08-28 23:17:46 +03:00
|
|
|
|
|
|
|
-- Azimuth Functions -----------------------------------------------------------
|
2019-10-10 02:58:54 +03:00
|
|
|
|
2021-08-11 03:02:23 +03:00
|
|
|
retrievePoint :: String -> Ship -> RIO e EthPoint
|
|
|
|
retrievePoint endpoint ship =
|
|
|
|
dawnPostRequests endpoint parseAzimuthPoint [PointRequest ship]
|
|
|
|
>>= \case
|
2020-08-28 23:17:46 +03:00
|
|
|
[x] -> pure x
|
|
|
|
_ -> error "JSON server returned multiple return values."
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2021-06-03 02:04:45 +03:00
|
|
|
validateFeedAndGetSponsor :: String
|
|
|
|
-> Feed
|
2021-06-04 00:38:36 +03:00
|
|
|
-> RIO e (Seed, Ship)
|
2021-08-11 03:02:23 +03:00
|
|
|
validateFeedAndGetSponsor endpoint = \case
|
2021-06-03 02:04:45 +03:00
|
|
|
Feed0 s -> do
|
2021-06-17 15:48:09 +03:00
|
|
|
r <- validateSeed s
|
2021-06-17 18:42:44 +03:00
|
|
|
case r of
|
|
|
|
Left e -> error e
|
|
|
|
Right r -> pure (s, r)
|
2021-06-17 15:48:09 +03:00
|
|
|
Feed1 s -> validateGerms s
|
2021-06-03 02:04:45 +03:00
|
|
|
|
2019-09-26 21:14:24 +03:00
|
|
|
where
|
2021-06-17 15:48:09 +03:00
|
|
|
validateGerms Germs{..} =
|
2021-06-04 00:38:36 +03:00
|
|
|
case gFeed of
|
2021-06-17 15:48:09 +03:00
|
|
|
[] -> error "no usable keys in keyfile"
|
2021-06-04 00:38:36 +03:00
|
|
|
(Germ{..}:f) -> do
|
|
|
|
let seed = Seed gShip gLife gRing Nothing
|
2021-06-17 18:42:44 +03:00
|
|
|
r :: Either String Ship
|
|
|
|
<- validateSeed seed
|
2021-06-04 00:38:36 +03:00
|
|
|
case r of
|
2021-06-17 15:48:09 +03:00
|
|
|
Left _ -> validateGerms $ Germs gShip f
|
2021-06-04 00:38:36 +03:00
|
|
|
Right r -> pure (seed, r)
|
2021-06-03 02:04:45 +03:00
|
|
|
|
2021-09-01 17:57:30 +03:00
|
|
|
validateSeed (Seed ship life ring oaf) = do
|
2021-06-03 02:04:45 +03:00
|
|
|
case clanFromShip ship of
|
2021-09-01 17:57:30 +03:00
|
|
|
Ob.Comet -> pure validateComet
|
|
|
|
Ob.Moon -> pure validateMoon
|
2021-06-03 02:04:45 +03:00
|
|
|
_ -> validateRest
|
|
|
|
where
|
2021-09-01 17:57:30 +03:00
|
|
|
cometFromPass = cometFingerprint $ ringToPass ring
|
|
|
|
validateComet
|
2021-06-03 02:04:45 +03:00
|
|
|
-- A comet address is the fingerprint of the keypair
|
2021-09-01 17:57:30 +03:00
|
|
|
| (ship /= cometFromPass) =
|
|
|
|
Left ("comet name doesn't match fingerprint " <>
|
2021-06-17 18:42:44 +03:00
|
|
|
show ship <> " vs " <>
|
2021-09-01 17:57:30 +03:00
|
|
|
show cometFromPass)
|
|
|
|
| (life /= 1) =
|
|
|
|
Left "comet can never be re-keyed"
|
|
|
|
| otherwise =
|
|
|
|
Right (shipSein ship)
|
2021-06-03 02:04:45 +03:00
|
|
|
|
2021-09-01 17:57:30 +03:00
|
|
|
validateMoon =
|
2021-06-03 02:04:45 +03:00
|
|
|
-- TODO: The current code in zuse does nothing, but we should be able
|
|
|
|
-- to try to validate the oath against the current as exists planet
|
|
|
|
-- on chain.
|
2021-09-01 17:57:30 +03:00
|
|
|
Right $ shipSein ship
|
2021-06-03 02:04:45 +03:00
|
|
|
|
|
|
|
validateRest = do
|
|
|
|
putStrLn ("boot: retrieving " <> renderShip ship <> "'s public keys")
|
|
|
|
|
|
|
|
--TODO could cache this lookup
|
2021-08-11 03:02:23 +03:00
|
|
|
whoP <- retrievePoint endpoint ship
|
2021-06-03 02:04:45 +03:00
|
|
|
case epNet whoP of
|
2021-06-17 18:42:44 +03:00
|
|
|
Nothing -> pure $ Left "ship not keyed"
|
2021-06-03 02:04:45 +03:00
|
|
|
Just (netLife, pass, contNum, (hasSponsor, who), _) -> do
|
2021-06-17 18:42:44 +03:00
|
|
|
if (netLife /= life) then
|
|
|
|
pure $ Left ("keyfile life mismatch; keyfile claims life " <>
|
|
|
|
show life <> ", but Azimuth claims life " <>
|
|
|
|
show netLife)
|
|
|
|
else if ((ringToPass ring) /= pass) then
|
2021-08-11 03:02:23 +03:00
|
|
|
pure $ Left "keyfile does not match Azimuth"
|
2021-06-03 02:04:45 +03:00
|
|
|
-- TODO: The hoon code does a breach check, but the C code never
|
|
|
|
-- supplies the data necessary for it to function.
|
2021-06-17 18:42:44 +03:00
|
|
|
else
|
|
|
|
pure $ Right who
|
2019-10-04 21:54:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- Walk through the sponsorship chain retrieving the actual sponsorship chain
|
2021-08-11 03:02:23 +03:00
|
|
|
-- as it exists on Azimuth.
|
|
|
|
getSponsorshipChain :: String -> Ship -> RIO e [(Ship,EthPoint)]
|
|
|
|
getSponsorshipChain endpoint = loop
|
2019-10-04 21:54:25 +03:00
|
|
|
where
|
|
|
|
loop ship = do
|
2020-09-04 21:44:26 +03:00
|
|
|
putStrLn ("boot: retrieving keys for sponsor " <> renderShip ship)
|
2021-08-11 03:02:23 +03:00
|
|
|
ethPoint <- retrievePoint endpoint ship
|
2019-10-04 21:54:25 +03:00
|
|
|
|
2019-10-10 02:58:54 +03:00
|
|
|
case (clanFromShip ship, epNet ethPoint) of
|
2020-10-27 15:22:33 +03:00
|
|
|
(Ob.Comet, _) -> error "Comets cannot be sponsors"
|
|
|
|
(Ob.Moon, _) -> error "Moons cannot be sponsors"
|
2019-10-10 02:58:54 +03:00
|
|
|
|
2019-10-11 01:01:50 +03:00
|
|
|
(_, Nothing) ->
|
2020-10-27 15:22:33 +03:00
|
|
|
error $ unpack ("Ship " <> renderShip ship <> " not booted")
|
2019-10-10 02:58:54 +03:00
|
|
|
|
|
|
|
(Ob.Galaxy, Just _) -> pure [(ship, ethPoint)]
|
|
|
|
|
2019-10-11 01:01:50 +03:00
|
|
|
(_, Just (_, _, _, (False, _), _)) ->
|
2020-10-27 15:22:33 +03:00
|
|
|
error $ unpack ("Ship " <> renderShip ship <> " has no sponsor")
|
2019-10-10 02:58:54 +03:00
|
|
|
|
2019-10-11 01:01:50 +03:00
|
|
|
(_, Just (_, _, _, (True, sponsor), _)) -> do
|
|
|
|
chain <- loop sponsor
|
2020-09-04 21:44:26 +03:00
|
|
|
pure $ chain <> [(ship, ethPoint)]
|
2019-09-26 21:14:24 +03:00
|
|
|
|
2019-09-20 01:40:23 +03:00
|
|
|
-- Produces either an error or a validated boot event structure.
|
2021-06-04 00:38:36 +03:00
|
|
|
dawnVent :: HasLogFunc e => String -> Feed -> RIO e (Either Text Dawn)
|
2021-06-03 02:04:45 +03:00
|
|
|
dawnVent provider feed =
|
2020-09-04 17:54:31 +03:00
|
|
|
-- The type checker can't figure this out on its own.
|
2021-06-04 00:38:36 +03:00
|
|
|
(onLeft tshow :: Either SomeException Dawn -> Either Text Dawn) <$> try do
|
2021-08-11 03:02:23 +03:00
|
|
|
putStrLn ("boot: requesting L2 Azimuth information from " <> pack provider)
|
2019-09-20 20:41:58 +03:00
|
|
|
|
2021-06-04 00:38:36 +03:00
|
|
|
(dSeed, immediateSponsor)
|
2021-08-11 03:02:23 +03:00
|
|
|
<- validateFeedAndGetSponsor provider feed
|
|
|
|
dSponsor <- getSponsorshipChain provider immediateSponsor
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2020-09-04 17:54:31 +03:00
|
|
|
putStrLn "boot: retrieving galaxy table"
|
|
|
|
dCzar <- (mapToHoonMap . mapFromList) <$>
|
2021-08-11 03:02:23 +03:00
|
|
|
(dawnPostRequests provider parseGalaxyTableEntry (map (PointRequest . Ship . fromIntegral) [0..255]))
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2020-09-04 17:54:31 +03:00
|
|
|
putStrLn "boot: retrieving network domains"
|
2021-09-01 18:32:59 +03:00
|
|
|
dTurf <- (dawnPostRequests provider parseTurfResponse [TurfRequest])
|
2021-08-11 03:02:23 +03:00
|
|
|
>>= \case
|
|
|
|
[] -> pure []
|
|
|
|
[t] -> pure (nub t)
|
|
|
|
_ -> error "too many turf responses"
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2020-09-04 17:54:31 +03:00
|
|
|
let dNode = Nothing
|
2019-09-26 01:16:48 +03:00
|
|
|
|
2021-09-01 18:32:59 +03:00
|
|
|
-- NOTE blocknum of 0 is fine because jael ignores it.
|
2021-08-11 03:02:23 +03:00
|
|
|
-- should probably be removed from dawn event.
|
|
|
|
let dBloq = 0
|
|
|
|
|
2021-06-04 00:38:36 +03:00
|
|
|
pure MkDawn{..}
|
2019-09-20 01:40:23 +03:00
|
|
|
|
2019-10-02 23:55:30 +03:00
|
|
|
|
2020-08-28 23:17:46 +03:00
|
|
|
-- Comet List ------------------------------------------------------------------
|
2019-10-02 23:55:30 +03:00
|
|
|
|
|
|
|
dawnCometList :: RIO e [Ship]
|
|
|
|
dawnCometList = do
|
|
|
|
-- Get the jamfile with the list of stars accepting comets right now.
|
2020-08-28 23:17:46 +03:00
|
|
|
manager <- io $ C.newManager TLS.tlsManagerSettings
|
2019-10-02 23:55:30 +03:00
|
|
|
request <- io $ C.parseRequest "https://bootstrap.urbit.org/comet-stars.jam"
|
|
|
|
response <- io $ C.httpLbs (C.setRequestCheckStatus request) manager
|
|
|
|
let body = toStrict $ C.responseBody response
|
|
|
|
|
|
|
|
noun <- cueBS body & either throwIO pure
|
|
|
|
fromNounErr noun & either (throwIO . uncurry ParseErr) pure
|
|
|
|
|
|
|
|
|
2019-10-09 23:39:11 +03:00
|
|
|
-- Comet Mining ----------------------------------------------------------------
|
|
|
|
|
|
|
|
mix :: BS.ByteString -> BS.ByteString -> BS.ByteString
|
2019-10-10 23:45:01 +03:00
|
|
|
mix a b = BS.pack $ loop (BS.unpack a) (BS.unpack b)
|
|
|
|
where
|
|
|
|
loop [] [] = []
|
|
|
|
loop a [] = a
|
|
|
|
loop [] b = b
|
|
|
|
loop (x:xs) (y:ys) = (xor x y) : loop xs ys
|
|
|
|
|
2020-10-15 20:05:54 +03:00
|
|
|
shax :: BS.ByteString -> BS.ByteString
|
|
|
|
shax = SHA256.hash
|
|
|
|
|
2019-10-10 23:45:01 +03:00
|
|
|
shas :: BS.ByteString -> BS.ByteString -> BS.ByteString
|
2020-10-15 20:05:54 +03:00
|
|
|
shas salt = shax . mix salt . shax
|
2019-10-09 23:39:11 +03:00
|
|
|
|
|
|
|
shaf :: BS.ByteString -> BS.ByteString -> BS.ByteString
|
|
|
|
shaf salt ruz = (mix a b)
|
|
|
|
where
|
|
|
|
haz = shas salt ruz
|
2019-10-10 23:45:01 +03:00
|
|
|
a = (take 16 haz)
|
|
|
|
b = (drop 16 haz)
|
2019-10-09 23:39:11 +03:00
|
|
|
|
2020-10-15 20:05:54 +03:00
|
|
|
-- Given a ring, derives the network login code.
|
|
|
|
--
|
|
|
|
-- Note that the network code is a patp, not a patq: the bytes have been
|
|
|
|
-- scrambled.
|
|
|
|
deriveCode :: Ring -> Ob.Patp
|
|
|
|
deriveCode Ring {..} = Ob.patp $
|
|
|
|
bytesAtom $
|
|
|
|
take 8 $
|
|
|
|
shaf (C.pack "pass") $
|
|
|
|
shax $
|
|
|
|
C.singleton 'B' <> ringSign <> ringCrypt
|
|
|
|
|
2019-10-10 23:45:01 +03:00
|
|
|
cometFingerprintBS :: Pass -> ByteString
|
|
|
|
cometFingerprintBS = (shaf $ C.pack "bfig") . passToBS
|
2019-10-09 23:39:11 +03:00
|
|
|
|
2019-10-10 23:45:01 +03:00
|
|
|
cometFingerprint :: Pass -> Ship
|
|
|
|
cometFingerprint = Ship . B.decode . fromStrict . reverse . cometFingerprintBS
|
2019-10-09 23:39:11 +03:00
|
|
|
|
|
|
|
tryMineComet :: Set Ship -> Word64 -> Maybe Seed
|
|
|
|
tryMineComet ships seed =
|
|
|
|
if member shipSponsor ships
|
|
|
|
then Just $ Seed shipName 1 ring Nothing
|
|
|
|
else Nothing
|
|
|
|
where
|
|
|
|
-- Hash the incoming seed into a 64 bytes.
|
|
|
|
baseHash = SHA512.hash $ toStrict $ B.encode seed
|
|
|
|
signSeed = (take 32 baseHash)
|
|
|
|
ringSeed = (drop 32 baseHash)
|
|
|
|
ring = Ring signSeed ringSeed
|
2019-10-10 23:45:01 +03:00
|
|
|
pass = ringToPass ring
|
2019-10-09 23:39:11 +03:00
|
|
|
shipName = cometFingerprint pass
|
|
|
|
shipSponsor = shipSein shipName
|
|
|
|
|
|
|
|
mineComet :: Set Ship -> Word64 -> Seed
|
2019-10-02 23:55:30 +03:00
|
|
|
mineComet ships = loop
|
|
|
|
where
|
|
|
|
loop eny =
|
2019-10-09 23:39:11 +03:00
|
|
|
case (tryMineComet ships eny) of
|
|
|
|
Nothing -> loop (eny + 1)
|
|
|
|
Just x -> x
|