Skip to content

Commit 7d78b3d

Browse files
Merge pull request #59 from mlabs-haskell/sam/fix-remote-cli
Fix signing keys and txs folder on remote
2 parents 860ba3c + 28aaf6e commit 7d78b3d

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

src/BotPlutusInterface/Balance.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module BotPlutusInterface.Balance (
77
) where
88

99
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
10-
import BotPlutusInterface.Effects (PABEffect, createDirectoryIfMissing, printLog)
10+
import BotPlutusInterface.Effects (PABEffect, createDirectoryIfMissingCLI, printLog)
1111
import BotPlutusInterface.Files (DummyPrivKey, unDummyPrivateKey)
1212
import BotPlutusInterface.Files qualified as Files
1313
import BotPlutusInterface.Types (LogLevel (Debug), PABConfig)
@@ -82,6 +82,9 @@ balanceTxIO pabConf ownPkh unbalancedTx =
8282

8383
lift $ printLog @w Debug $ show utxoIndex
8484

85+
-- We need this folder on the CLI machine, which may not be the local machine
86+
lift $ createDirectoryIfMissingCLI @w False (Text.unpack pabConf.pcTxFileDir)
87+
8588
-- Adds required collaterals, only needs to happen once
8689
-- Also adds signatures for fee calculation
8790
preBalancedTx <- hoistEither $ addTxCollaterals utxoIndex tx >>= addSignatories ownPkh privKeys requiredSigs
@@ -125,7 +128,6 @@ balanceTxIO pabConf ownPkh unbalancedTx =
125128
txWithoutFees <-
126129
hoistEither $ balanceTxStep minUtxos utxoIndex ownPkh $ tx `withFee` 0
127130

128-
lift $ createDirectoryIfMissing @w False (Text.unpack pabConf.pcTxFileDir)
129131
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys txWithoutFees
130132
fees <- newEitherT $ CardanoCLI.calculateMinFee @w pabConf txWithoutFees
131133

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ module BotPlutusInterface.CardanoCLI (
77
calculateMinFee,
88
buildTx,
99
signTx,
10-
uploadFiles,
1110
validatorScriptFilePath,
1211
unsafeSerialiseAddress,
1312
policyScriptFilePath,
1413
utxosAt,
1514
queryTip,
1615
) where
1716

18-
import BotPlutusInterface.Effects (PABEffect, ShellArgs (..), callCommand, uploadDir)
17+
import BotPlutusInterface.Effects (PABEffect, ShellArgs (..), callCommand)
1918
import BotPlutusInterface.Files (
2019
DummyPrivKey (FromSKey, FromVKey),
2120
datumJsonFilePath,
@@ -93,19 +92,6 @@ import Plutus.V1.Ledger.Api qualified as Plutus
9392
import PlutusTx.Builtins (fromBuiltin)
9493
import Prelude
9594

96-
-- | Upload script files to remote server
97-
uploadFiles ::
98-
forall (w :: Type) (effs :: [Type -> Type]).
99-
Member (PABEffect w) effs =>
100-
PABConfig ->
101-
Eff effs ()
102-
uploadFiles pabConf =
103-
mapM_
104-
(uploadDir @w)
105-
[ pabConf.pcScriptFileDir
106-
, pabConf.pcSigningKeyFileDir
107-
]
108-
10995
-- | Getting information of the latest block
11096
queryTip ::
11197
forall (w :: Type) (effs :: [Type -> Type]).

src/BotPlutusInterface/Contract.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import BotPlutusInterface.Effects (
1313
printLog,
1414
queryChainIndex,
1515
threadDelay,
16+
uploadDir,
1617
)
1718
import BotPlutusInterface.Files (DummyPrivKey (FromSKey, FromVKey))
1819
import BotPlutusInterface.Files qualified as Files
@@ -170,10 +171,12 @@ balanceTx ::
170171
UnbalancedTx ->
171172
Eff effs BalanceTxResponse
172173
balanceTx contractEnv unbalancedTx = do
174+
let pabConf = contractEnv.cePABConfig
175+
uploadDir @w pabConf.pcSigningKeyFileDir
173176
eitherPreBalancedTx <-
174177
PreBalance.balanceTxIO @w
175-
contractEnv.cePABConfig
176-
(contractEnv.cePABConfig.pcOwnPubKeyHash)
178+
pabConf
179+
pabConf.pcOwnPubKeyHash
177180
unbalancedTx
178181

179182
pure $ either (BalanceTxFailed . InsufficientFunds) (BalanceTxSuccess . Right) eitherPreBalancedTx
@@ -188,18 +191,19 @@ writeBalancedTx ::
188191
writeBalancedTx _ (Left _) = error "Cannot handle cardano api tx"
189192
writeBalancedTx contractEnv (Right tx) = do
190193
let pabConf = contractEnv.cePABConfig
194+
uploadDir @w pabConf.pcSigningKeyFileDir
191195
createDirectoryIfMissing @w False (Text.unpack pabConf.pcScriptFileDir)
192196

193197
eitherT (pure . WriteBalancedTxFailed . OtherError) (pure . WriteBalancedTxSuccess . Right) $ do
194198
void $ firstEitherT (Text.pack . show) $ newEitherT $ Files.writeAll @w pabConf tx
199+
lift $ uploadDir @w pabConf.pcScriptFileDir
200+
195201
privKeys <- newEitherT $ Files.readPrivateKeys @w pabConf
196202

197203
let requiredSigners = Map.keys $ tx ^. Tx.signatures
198204
skeys = Map.filter (\case FromSKey _ -> True; FromVKey _ -> False) privKeys
199205
signable = all ((`Map.member` skeys) . Ledger.pubKeyHash) requiredSigners
200206

201-
lift $ CardanoCLI.uploadFiles @w pabConf
202-
203207
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys tx
204208

205209
if signable

src/BotPlutusInterface/Effects.hs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module BotPlutusInterface.Effects (
77
ShellArgs (..),
88
handlePABEffect,
99
createDirectoryIfMissing,
10+
createDirectoryIfMissingCLI,
1011
queryChainIndex,
1112
listDirectory,
1213
threadDelay,
@@ -37,6 +38,8 @@ import Data.Aeson (ToJSON)
3738
import Data.Aeson qualified as JSON
3839
import Data.Bifunctor (second)
3940
import Data.Kind (Type)
41+
import Data.Maybe (catMaybes)
42+
import Data.String (IsString, fromString)
4043
import Data.Text (Text)
4144
import Data.Text qualified as Text
4245
import Plutus.Contract.Effects (ChainIndexQuery, ChainIndexResponse)
@@ -58,6 +61,8 @@ instance Show (ShellArgs a) where
5861
data PABEffect (w :: Type) (r :: Type) where
5962
CallCommand :: ShellArgs a -> PABEffect w (Either Text a)
6063
CreateDirectoryIfMissing :: Bool -> FilePath -> PABEffect w ()
64+
-- Same as above but creates folder on the CLI machine, be that local or remote.
65+
CreateDirectoryIfMissingCLI :: Bool -> FilePath -> PABEffect w ()
6166
PrintLog :: LogLevel -> String -> PABEffect w ()
6267
UpdateInstanceState :: Activity -> PABEffect w ()
6368
LogToContract :: (ToJSON w, Monoid w) => w -> PABEffect w ()
@@ -93,6 +98,10 @@ handlePABEffect contractEnv =
9398
Remote ipAddr -> callRemoteCommand ipAddr shellArgs
9499
CreateDirectoryIfMissing createParents filePath ->
95100
Directory.createDirectoryIfMissing createParents filePath
101+
CreateDirectoryIfMissingCLI createParents filePath ->
102+
case contractEnv.cePABConfig.pcCliLocation of
103+
Local -> Directory.createDirectoryIfMissing createParents filePath
104+
Remote ipAddr -> createDirectoryIfMissingRemote ipAddr createParents filePath
96105
PrintLog logLevel txt -> printLog' contractEnv.cePABConfig.pcLogLevel logLevel txt
97106
UpdateInstanceState s -> do
98107
atomically $
@@ -132,8 +141,15 @@ callRemoteCommand ipAddr ShellArgs {cmdName, cmdArgs, cmdOutParser} =
132141
"ssh"
133142
(map Text.unpack [ipAddr, Text.unwords $ "source ~/.bash_profile;" : cmdName : map quotes cmdArgs])
134143

135-
quotes :: Text -> Text
136-
quotes str = "\"" <> str <> "\""
144+
createDirectoryIfMissingRemote :: Text -> Bool -> FilePath -> IO ()
145+
createDirectoryIfMissingRemote ipAddr createParents path =
146+
void $ readProcessEither "ssh" $ catMaybes [Just $ Text.unpack ipAddr, Just "mkdir", pFlag, Just $ quotes path]
147+
where
148+
pFlag :: Maybe String
149+
pFlag = if createParents then Just "-p" else Nothing
150+
151+
quotes :: forall (a :: Type). (IsString a, Semigroup a) => a -> a
152+
quotes str = fromString "\"" <> str <> fromString "\""
137153

138154
readProcessEither :: FilePath -> [String] -> IO (Either Text String)
139155
readProcessEither path args =
@@ -162,6 +178,14 @@ createDirectoryIfMissing ::
162178
Eff effs ()
163179
createDirectoryIfMissing createParents path = send @(PABEffect w) $ CreateDirectoryIfMissing createParents path
164180

181+
createDirectoryIfMissingCLI ::
182+
forall (w :: Type) (effs :: [Type -> Type]).
183+
Member (PABEffect w) effs =>
184+
Bool ->
185+
FilePath ->
186+
Eff effs ()
187+
createDirectoryIfMissingCLI createParents path = send @(PABEffect w) $ CreateDirectoryIfMissingCLI createParents path
188+
165189
printLog ::
166190
forall (w :: Type) (effs :: [Type -> Type]).
167191
Member (PABEffect w) effs =>

test/Spec/MockContract.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ runPABEffectPure initState req =
279279
go (CallCommand args) = mockCallCommand args
280280
go (CreateDirectoryIfMissing createParents filePath) =
281281
mockCreateDirectoryIfMissing createParents filePath
282+
go (CreateDirectoryIfMissingCLI createParents filePath) =
283+
mockCreateDirectoryIfMissing createParents filePath
282284
go (PrintLog logLevel msg) = mockPrintLog logLevel msg
283285
go (UpdateInstanceState msg) = mockUpdateInstanceState msg
284286
go (LogToContract msg) = mockLogToContract msg

0 commit comments

Comments
 (0)