Skip to content

Commit 992b1dc

Browse files
author
gege251
committed
Move signing key validation step to Contract
1 parent a4c72a0 commit 992b1dc

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module BotPlutusInterface.CardanoCLI (
1616
queryTip,
1717
) where
1818

19-
import BotPlutusInterface.Effects (PABEffect, ShellArgs (..), callCommand, printLog, uploadDir)
19+
import BotPlutusInterface.Effects (PABEffect, ShellArgs (..), callCommand, uploadDir)
2020
import BotPlutusInterface.Files (
2121
DummyPrivKey (FromSKey, FromVKey),
2222
datumJsonFilePath,
@@ -26,7 +26,7 @@ import BotPlutusInterface.Files (
2626
txFilePath,
2727
validatorScriptFilePath,
2828
)
29-
import BotPlutusInterface.Types (LogLevel (Warn), PABConfig, Tip)
29+
import BotPlutusInterface.Types (PABConfig, Tip)
3030
import BotPlutusInterface.UtxoParser qualified as UtxoParser
3131
import Cardano.Api.Shelley (NetworkId (Mainnet, Testnet), NetworkMagic (..), serialiseAddress)
3232
import Codec.Serialise qualified as Codec
@@ -251,25 +251,11 @@ signTx ::
251251
forall (w :: Type) (effs :: [Type -> Type]).
252252
Member (PABEffect w) effs =>
253253
PABConfig ->
254-
Map PubKeyHash DummyPrivKey ->
255254
Tx ->
256255
[PubKey] ->
257256
Eff effs (Either Text ())
258-
signTx pabConf privKeys tx pubKeys =
259-
let skeys = Map.filter (\case FromSKey _ -> True; FromVKey _ -> False) privKeys
260-
in if all ((`Map.member` skeys) . Ledger.pubKeyHash) pubKeys
261-
then callCommand @w $ ShellArgs "cardano-cli" opts (const ())
262-
else do
263-
let err =
264-
Text.unlines
265-
[ "Not all required signatures have signing key files. Please sign and submit the tx manually:"
266-
, "Tx file: " <> txFilePath pabConf "raw" tx
267-
, "Signatories (pkh): "
268-
<> Text.unwords
269-
(map (encodeByteString . fromBuiltin . getPubKeyHash . Ledger.pubKeyHash) pubKeys)
270-
]
271-
printLog @w Warn (Text.unpack err)
272-
pure $ Left err
257+
signTx pabConf tx pubKeys =
258+
callCommand @w $ ShellArgs "cardano-cli" opts (const ())
273259
where
274260
signingKeyFiles =
275261
concatMap

src/BotPlutusInterface/Contract.hs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import BotPlutusInterface.Effects (
1313
queryChainIndex,
1414
threadDelay,
1515
)
16+
import BotPlutusInterface.Files (DummyPrivKey (FromSKey, FromVKey))
1617
import BotPlutusInterface.Files qualified as Files
1718
import BotPlutusInterface.PreBalance qualified as PreBalance
18-
import BotPlutusInterface.Types (ContractEnvironment (..), LogLevel (Debug), Tip (slot))
19+
import BotPlutusInterface.Types (ContractEnvironment (..), LogLevel (Debug, Warn), Tip (slot))
1920
import Control.Lens ((^.))
2021
import Control.Monad (void)
2122
import Control.Monad.Freer (Eff, Member, interpret, reinterpret, runM, subsume, type (~>))
@@ -26,11 +27,14 @@ import Control.Monad.Freer.Writer (Writer (Tell))
2627
import Control.Monad.Trans.Class (lift)
2728
import Control.Monad.Trans.Either (eitherT, firstEitherT, newEitherT, secondEitherT)
2829
import Data.Aeson (ToJSON, Value)
30+
import Data.Aeson.Extras (encodeByteString)
2931
import Data.Kind (Type)
3032
import Data.Map qualified as Map
3133
import Data.Row (Row)
34+
import Data.Text (Text)
3235
import Data.Text qualified as Text
3336
import Ledger (POSIXTime)
37+
import Ledger qualified
3438
import Ledger.Address (PaymentPubKeyHash (PaymentPubKeyHash))
3539
import Ledger.Constraints.OffChain (UnbalancedTx (..))
3640
import Ledger.Slot (Slot (Slot))
@@ -47,6 +51,7 @@ import Plutus.Contract.Effects (
4751
)
4852
import Plutus.Contract.Resumable (Resumable (..))
4953
import Plutus.Contract.Types (Contract (..), ContractEffs)
54+
import PlutusTx.Builtins (fromBuiltin)
5055
import Wallet.Emulator.Error (WalletAPIError (..))
5156
import Prelude
5257

@@ -182,23 +187,37 @@ writeBalancedTx ::
182187
Eff effs WriteBalancedTxResponse
183188
writeBalancedTx _ (Left _) = error "Cannot handle cardano api tx"
184189
writeBalancedTx contractEnv (Right tx) = do
185-
createDirectoryIfMissing @w False (Text.unpack contractEnv.cePABConfig.pcScriptFileDir)
190+
let pabConf = contractEnv.cePABConfig
191+
createDirectoryIfMissing @w False (Text.unpack pabConf.pcScriptFileDir)
186192

187193
eitherT (pure . WriteBalancedTxFailed . OtherError) (pure . WriteBalancedTxSuccess . Right) $ do
188-
void $ firstEitherT (Text.pack . show) $ newEitherT $ Files.writeAll @w contractEnv.cePABConfig tx
189-
privKeys <- newEitherT $ Files.readPrivateKeys @w contractEnv.cePABConfig
194+
void $ firstEitherT (Text.pack . show) $ newEitherT $ Files.writeAll @w pabConf tx
195+
privKeys <- newEitherT $ Files.readPrivateKeys @w pabConf
190196

191-
let ownPkh = contractEnv.cePABConfig.pcOwnPubKeyHash
197+
let ownPkh = pabConf.pcOwnPubKeyHash
192198
let requiredSigners = Map.keys $ tx ^. Tx.signatures
199+
let skeys = Map.filter (\case FromSKey _ -> True; FromVKey _ -> False) privKeys
200+
let signable = all ((`Map.member` skeys) . Ledger.pubKeyHash) requiredSigners
193201

194-
lift $ CardanoCLI.uploadFiles @w contractEnv.cePABConfig
202+
lift $ CardanoCLI.uploadFiles @w pabConf
195203

196-
newEitherT $ CardanoCLI.buildTx @w contractEnv.cePABConfig privKeys ownPkh CardanoCLI.BuildAuto tx
197-
newEitherT $ CardanoCLI.signTx @w contractEnv.cePABConfig privKeys tx requiredSigners
204+
newEitherT $ CardanoCLI.buildTx @w pabConf privKeys ownPkh CardanoCLI.BuildAuto tx
198205

199-
if contractEnv.cePABConfig.pcDryRun
200-
then pure tx
201-
else secondEitherT (const tx) $ newEitherT $ CardanoCLI.submitTx @w contractEnv.cePABConfig tx
206+
if signable
207+
then newEitherT $ CardanoCLI.signTx @w pabConf tx requiredSigners
208+
else
209+
lift . printLog @w Warn . Text.unpack . Text.unlines $
210+
[ "Not all required signatures have signing key files. Please sign and submit the tx manually:"
211+
, "Tx file: " <> Files.txFilePath pabConf "raw" tx
212+
, "Signatories (pkh): " <> Text.unwords (map pkhToText requiredSigners)
213+
]
214+
215+
if not pabConf.pcDryRun && signable
216+
then secondEitherT (const tx) $ newEitherT $ CardanoCLI.submitTx @w pabConf tx
217+
else pure tx
218+
219+
pkhToText :: Ledger.PubKey -> Text
220+
pkhToText = encodeByteString . fromBuiltin . Ledger.getPubKeyHash . Ledger.pubKeyHash
202221

203222
{- | Wait at least until the given slot. The slot number only changes when a new block is appended
204223
to the chain so it waits for at least one block

0 commit comments

Comments
 (0)