use RPC to talk to namecoind
This commit is contained in:
parent
16c7de690d
commit
bfe521cce8
@ -28,8 +28,8 @@ executable rosa
|
|||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
other-extensions: RecordWildCards, OverloadedStrings
|
other-extensions: RecordWildCards, OverloadedStrings
|
||||||
build-depends: base >=4.8 && <5.0, aeson, text,
|
build-depends: base >=4.8 && <5.0 , aeson, text,
|
||||||
vector, unordered-containers,
|
vector, unordered-containers,
|
||||||
wreq, lens >=4.4, bytestring,
|
wreq, lens, bytestring, directory,
|
||||||
optparse-applicative, process
|
optparse-applicative, namecoin-update
|
||||||
ghc-options: -O2
|
ghc-options: -O2
|
||||||
|
@ -16,7 +16,7 @@ obj |. key = case parse (.: key) obj of
|
|||||||
Error err -> toJSON err
|
Error err -> toJSON err
|
||||||
|
|
||||||
|
|
||||||
-- | Get the String value of a key
|
-- | Get the 'String' value of a key
|
||||||
(|:) :: Object -> Text -> String
|
(|:) :: Object -> Text -> String
|
||||||
obj |: key = repr (obj |. key)
|
obj |: key = repr (obj |. key)
|
||||||
|
|
||||||
|
55
src/Main.hs
55
src/Main.hs
@ -4,17 +4,19 @@ module Main where
|
|||||||
|
|
||||||
import Json
|
import Json
|
||||||
|
|
||||||
import Control.Lens
|
import Control.Lens (view)
|
||||||
import Control.Monad (when)
|
import Control.Monad (when)
|
||||||
|
import Namecoin (Error, rpcRequest, uri)
|
||||||
import Network.Wreq (get, responseBody)
|
import Network.Wreq (get, responseBody)
|
||||||
import Data.Monoid
|
import System.Directory (XdgDirectory(..), getXdgDirectory)
|
||||||
import Data.Aeson (decode, toJSON)
|
import Data.Aeson (Value(..), decode, toJSON)
|
||||||
import Data.Maybe (fromJust)
|
import Data.Maybe (fromJust, fromMaybe)
|
||||||
import Data.HashMap.Strict (delete)
|
import Data.HashMap.Strict (delete)
|
||||||
import Data.ByteString.Lazy.Char8 (pack)
|
import Data.ByteString.Lazy.Char8 (pack)
|
||||||
|
import Data.Monoid
|
||||||
import Options.Applicative
|
import Options.Applicative
|
||||||
import System.Process
|
|
||||||
|
|
||||||
|
import qualified Data.Text.IO as T
|
||||||
|
|
||||||
-- * CLI interface
|
-- * CLI interface
|
||||||
|
|
||||||
@ -22,6 +24,7 @@ import System.Process
|
|||||||
data Options = Options
|
data Options = Options
|
||||||
{ name :: String
|
{ name :: String
|
||||||
, url :: String
|
, url :: String
|
||||||
|
, conf :: Maybe FilePath
|
||||||
, dnschain :: Bool
|
, dnschain :: Bool
|
||||||
, block :: Bool
|
, block :: Bool
|
||||||
, raw :: Bool
|
, raw :: Bool
|
||||||
@ -36,9 +39,14 @@ options = Options
|
|||||||
<*> strOption
|
<*> strOption
|
||||||
( long "url"
|
( long "url"
|
||||||
<> short 'u'
|
<> short 'u'
|
||||||
<> value "http://dns.dnschain.net/"
|
<> value "http://namecoin.dns"
|
||||||
<> metavar "URL"
|
<> metavar "URL"
|
||||||
<> help "Use custom api url" )
|
<> help "Use custom api url" )
|
||||||
|
<*> (optional $ strOption $
|
||||||
|
long "conf"
|
||||||
|
<> short 'c'
|
||||||
|
<> metavar "FILE"
|
||||||
|
<> help "Use custom api url" )
|
||||||
<*> switch
|
<*> switch
|
||||||
( long "dnschain"
|
( long "dnschain"
|
||||||
<> short 'd'
|
<> short 'd'
|
||||||
@ -70,27 +78,38 @@ main = execParser description >>= exec where
|
|||||||
exec Options{..} =
|
exec Options{..} =
|
||||||
if dnschain
|
if dnschain
|
||||||
then doDnschain url name raw
|
then doDnschain url name raw
|
||||||
else doLocal name block
|
else doLocal name block conf
|
||||||
|
|
||||||
|
|
||||||
|
-- | Load namecoin configuration
|
||||||
|
apiURI :: Maybe FilePath -> IO String
|
||||||
|
apiURI path = do
|
||||||
|
path <- flip fromMaybe path <$> getXdgDirectory XdgConfig "namecoin"
|
||||||
|
res <- uri <$> T.readFile path
|
||||||
|
case res of
|
||||||
|
Left err -> fail ("Couldn't load the configuration: " ++ err)
|
||||||
|
Right uri -> return uri
|
||||||
|
|
||||||
|
|
||||||
-- | Connect to local namecoin node
|
-- | Connect to local namecoin node
|
||||||
doLocal :: String -> Bool -> IO ()
|
doLocal :: String -> Bool -> Maybe FilePath -> IO ()
|
||||||
doLocal name block = do
|
doLocal name block conf = do
|
||||||
out <- readProcess "namecoin-cli" ["name_show", name] ""
|
uri <- apiURI conf
|
||||||
case decode (pack out) of
|
req <- rpcRequest uri "name_show" [name]
|
||||||
Nothing -> putStrLn "Error parsing data"
|
case req of
|
||||||
Just res -> do
|
Left err -> putStrLn ("The lookup failed: " ++ err)
|
||||||
pprint $ reparse (res |: "value")
|
Right (Object res) -> do
|
||||||
when block (pprint extra)
|
pprint $ tryParse (res |: "value")
|
||||||
|
when block (pprint blockInfo)
|
||||||
where
|
where
|
||||||
reparse = fromJust . decode . pack
|
tryParse = fromMaybe (res |. "value") . decode . pack
|
||||||
extra = toJSON (delete "value" res)
|
blockInfo = toJSON (delete "value" res)
|
||||||
|
|
||||||
|
|
||||||
-- | Connect to dnschain api endpoint
|
-- | Connect to dnschain api endpoint
|
||||||
doDnschain :: String -> String -> Bool -> IO ()
|
doDnschain :: String -> String -> Bool -> IO ()
|
||||||
doDnschain url name raw = do
|
doDnschain url name raw = do
|
||||||
body <- (^. responseBody) <$> get (url ++ name)
|
body <- view responseBody <$> get (url++"/v1/namecoin/key/"++name)
|
||||||
if raw
|
if raw
|
||||||
then print body
|
then print body
|
||||||
else putStrLn $
|
else putStrLn $
|
||||||
|
Loading…
Reference in New Issue
Block a user