This repository was archived by the owner on Aug 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
[GH-208] Add node-ipc for wallet. #209
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a6c8592
[GH-208] Add node-ipc for wallet.
ksaric 99e0ead
[GH-208] Fix Nix deps.
ksaric ff54336
Merge branch 'master' into ksaric/GH-208
ksaric c12e0b0
DaedalusIPC clean ups (#213)
rvl 353f13b
Merge branch 'master' into ksaric/GH-208
ksaric 9d0e741
Add DaedalusIPC nodejs test (#221)
rvl 9e2c285
Merge branch 'master' into ksaric/GH-208
ksaric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Main where | ||
|
||
import Cardano.Prelude | ||
|
||
import Cardano.BM.Configuration.Static (defaultConfigStdout) | ||
import Cardano.BM.Setup (setupTrace_) | ||
import Cardano.Shell.DaedalusIPC | ||
|
||
main :: IO () | ||
main = fmap readEither <$> getArgs >>= \case | ||
[Right port] -> do | ||
c <- defaultConfigStdout | ||
(tr, _sb) <- setupTrace_ c "daedalus-ipc" | ||
daedalusIPC tr port | ||
_ -> do | ||
putStrLn ("Usage: daedalus-ipc PORT" :: Text) | ||
exitFailure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"url": "https://github.com/input-output-hk/iohk-nix", | ||
"rev": "eec83e9bba05093c94004a001caebae252c0c83b", | ||
"date": "2019-06-26T17:23:10+00:00", | ||
"sha256": "00b11qmgr57pm6gg7a8m6fzjj6m5wmh8hnwg16jnqsmf52882y2j", | ||
"rev": "3cee475f26458ef113a494b216b1504513fa4d03", | ||
"date": "2019-07-05T14:51:43+00:00", | ||
"sha256": "04dqb2ii03y1lvyqn3v3xwardym2h6g5zbgvjljam758w768vmii", | ||
"fetchSubmodules": false | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RankNTypes #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
|
||
-- | | ||
-- Copyright: © 2018-2019 IOHK | ||
-- | ||
-- Daedalus <-> Wallet child process port discovery protocol. | ||
-- Provides a mechanism for Daedalus to discover what port the cardano-wallet | ||
-- server is listening on. | ||
-- | ||
-- See <https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options> | ||
-- for more information about the message protocol. | ||
|
||
module Cardano.Shell.DaedalusIPC | ||
( daedalusIPC | ||
) where | ||
|
||
import Cardano.Prelude | ||
|
||
import Cardano.BM.Trace (Trace, logError, logInfo, logNotice) | ||
import Cardano.Shell.NodeIPC.General (NodeChannelError (..), | ||
NodeChannelFinished (..), | ||
runNodeChannel, | ||
setupNodeChannel) | ||
import Control.Concurrent (threadDelay) | ||
import Control.Monad (forever) | ||
import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), object, | ||
withObject, (.:), (.=)) | ||
import Data.Text (Text) | ||
|
||
-- | Messages sent from Daedalus -> cardano-wallet | ||
data MsgIn = QueryPort | ||
deriving (Show, Eq) | ||
|
||
-- | Messages sent from cardano-wallet -> Daedalus | ||
data MsgOut = Started | ReplyPort Int | ParseError Text | ||
deriving (Show, Eq) | ||
|
||
instance FromJSON MsgIn where | ||
parseJSON = withObject "MsgIn" $ \v -> do | ||
(_ :: [()]) <- v .: "QueryPort" | ||
pure QueryPort | ||
|
||
instance ToJSON MsgOut where | ||
toJSON Started = object [ "Started" .= Array mempty ] | ||
toJSON (ReplyPort p) = object [ "ReplyPort" .= p ] | ||
toJSON (ParseError e) = object [ "ParseError" .= e ] | ||
|
||
-- | Start up the Daedalus IPC process. It's called 'daedalusIPC', but this | ||
-- could be any nodejs program that needs to start cardano-wallet. All it does | ||
-- is reply with a port number when asked, using a very nodejs-specific IPC | ||
-- method. | ||
-- | ||
-- If the IPC channel was successfully set up, this function won't return until | ||
-- the parent process exits. Otherwise, it will return immediately. Before | ||
-- returning, it will log an message about why it has exited. | ||
daedalusIPC | ||
:: Trace IO Text | ||
-- ^ Logging object | ||
-> Int | ||
-- ^ Port number to send to Daedalus | ||
-> IO () | ||
daedalusIPC tr port = setupNodeChannel >>= \case | ||
Right chan -> do | ||
logInfo tr "Daedalus IPC server starting" | ||
runNodeChannel (pure . msg) action chan >>= \case | ||
Left (NodeChannelFinished err) -> | ||
logNotice tr $ "Daedalus IPC finished for this reason: " <> show err | ||
Right () -> logError tr "Unreachable code" | ||
Left NodeChannelDisabled -> do | ||
logInfo tr "Daedalus IPC is not enabled." | ||
sleep | ||
Left (NodeChannelBadFD err) -> | ||
logError tr $ "Problem starting Daedalus IPC: " <> show err | ||
where | ||
-- How to respond to an incoming message, or when there is an incoming | ||
-- message that couldn't be parsed. | ||
msg (Right QueryPort) = Just (ReplyPort port) | ||
msg (Left e) = Just (ParseError e) | ||
|
||
-- What to do in context of runNodeChannel | ||
action :: (MsgOut -> IO ()) -> IO () | ||
action send = send Started >> sleep | ||
|
||
sleep = forever $ threadDelay maxBound |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.