Skip to content

Commit dfc526e

Browse files
author
gege251
committed
Refactor
1 parent 43fb36c commit dfc526e

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

src/BotPlutusInterface/CardanoCLI.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,17 @@ buildTx pabConf privKeys ownPkh buildMode tx =
207207
callCommand @w $ ShellArgs "cardano-cli" opts (const ())
208208
where
209209
ownAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing
210-
skeys = Map.filter (\case FromSKey _ -> True; FromVKey _ -> False) privKeys
211210
requiredSigners =
212211
concatMap
213212
( \pubKey ->
214213
let pkh = Ledger.pubKeyHash pubKey
215-
in if Map.member pkh skeys
216-
then ["--required-signer", signingKeyFilePath pabConf pkh]
217-
else ["--required-signer-hash", encodeByteString $ fromBuiltin $ getPubKeyHash pkh]
214+
in case Map.lookup pkh privKeys of
215+
Just (FromSKey _) ->
216+
["--required-signer", signingKeyFilePath pabConf pkh]
217+
Just (FromVKey _) ->
218+
["--required-signer-hash", encodeByteString $ fromBuiltin $ getPubKeyHash pkh]
219+
Nothing ->
220+
[]
218221
)
219222
(Map.keys (Ledger.txSignatures tx))
220223
opts =

src/BotPlutusInterface/Files.hs

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE AllowAmbiguousTypes #-}
2+
{-# LANGUAGE MultiWayIf #-}
23

34
module BotPlutusInterface.Files (
45
policyScriptFilePath,
@@ -55,9 +56,10 @@ import Data.ByteString.Lazy qualified as LazyByteString
5556
import Data.ByteString.Short qualified as ShortByteString
5657
import Data.Either.Combinators (mapLeft)
5758
import Data.Kind (Type)
59+
import Data.List (sortOn)
5860
import Data.Map (Map)
5961
import Data.Map qualified as Map
60-
import Data.Maybe (mapMaybe)
62+
import Data.Maybe (catMaybes, mapMaybe)
6163
import Data.Set qualified as Set
6264
import Data.Text (Text)
6365
import Data.Text qualified as Text
@@ -82,7 +84,7 @@ import Plutus.V1.Ledger.Api (
8284
)
8385
import PlutusTx (ToData, toData)
8486
import PlutusTx.Builtins (fromBuiltin)
85-
import System.FilePath (isExtensionOf)
87+
import System.FilePath (isExtensionOf, (</>))
8688
import Prelude
8789

8890
-- | Filename of a minting policy script
@@ -176,15 +178,21 @@ readPrivateKeys ::
176178
Eff effs (Either Text (Map PubKeyHash DummyPrivKey))
177179
readPrivateKeys pabConf = do
178180
files <- listDirectory @w $ Text.unpack pabConf.pcSigningKeyFileDir
179-
let vKeyFiles =
180-
map (\filename -> Text.unpack pabConf.pcSigningKeyFileDir ++ "/" ++ filename) $
181-
filter ("vkey" `isExtensionOf`) files
182-
let sKeyFiles =
183-
map (\filename -> Text.unpack pabConf.pcSigningKeyFileDir ++ "/" ++ filename) $
184-
filter ("skey" `isExtensionOf`) files
185-
privKeys <- mapM (readVerificationKey @w) vKeyFiles
186-
privKeys' <- mapM (readSigningKey @w) sKeyFiles
187-
pure $ toPrivKeyMap <$> sequence (privKeys <> privKeys')
181+
182+
privKeys <-
183+
catMaybes
184+
<$> mapM
185+
( \filename ->
186+
let fullPath = Text.unpack pabConf.pcSigningKeyFileDir </> filename
187+
in case filename of
188+
_
189+
| "vkey" `isExtensionOf` filename -> Just <$> readVerificationKey @w fullPath
190+
| "skey" `isExtensionOf` filename -> Just <$> readSigningKey @w fullPath
191+
| otherwise -> pure Nothing
192+
)
193+
files
194+
195+
pure $ toPrivKeyMap <$> sequence privKeys
188196
where
189197
toPrivKeyMap :: [DummyPrivKey] -> Map PubKeyHash DummyPrivKey
190198
toPrivKeyMap =
@@ -194,6 +202,11 @@ readPrivateKeys pabConf = do
194202
in Map.insert pkh pKey pKeyMap
195203
)
196204
Map.empty
205+
. sortOn keyPriority
206+
207+
keyPriority :: DummyPrivKey -> Int
208+
keyPriority (FromSKey _) = 1
209+
keyPriority (FromVKey _) = 0
197210

198211
data DummyPrivKey
199212
= FromSKey PrivateKey

src/BotPlutusInterface/PreBalance.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module BotPlutusInterface.PreBalance (
77

88
import BotPlutusInterface.CardanoCLI qualified as CardanoCLI
99
import BotPlutusInterface.Effects (PABEffect, createDirectoryIfMissing, printLog)
10-
import BotPlutusInterface.Files (DummyPrivKey (FromSKey, FromVKey))
10+
import BotPlutusInterface.Files (DummyPrivKey, unDummyPrivateKey)
1111
import BotPlutusInterface.Files qualified as Files
1212
import BotPlutusInterface.Types (LogLevel (Debug), PABConfig)
1313
import Cardano.Api.Shelley (Lovelace (Lovelace), ProtocolParameters (protocolParamUTxOCostPerWord))
@@ -267,8 +267,7 @@ addSignatories ownPkh privKeys pkhs tx =
267267
foldM
268268
( \tx' pkh ->
269269
case Map.lookup pkh privKeys of
270-
Just (FromSKey privKey) -> Right $ Tx.addSignature' privKey tx'
271-
Just (FromVKey privKey) -> Right $ Tx.addSignature' privKey tx'
270+
Just privKey -> Right $ Tx.addSignature' (unDummyPrivateKey privKey) tx'
272271
Nothing -> Left "Signing key not found."
273272
)
274273
tx

test/Spec/BotPlutusInterface/Contract.hs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,13 @@ import Spec.MockContract (
5656
pkh3',
5757
pkhAddr1,
5858
runContractPure,
59+
signingKey1,
5960
tip,
61+
toSigningKeyFile,
6062
toVerificationKeyFile,
6163
utxos,
6264
verificationKey1,
65+
verificationKey3,
6366
)
6467
import Test.Tasty (TestTree, testGroup)
6568
import Test.Tasty.HUnit (Assertion, assertBool, assertFailure, testCase, (@?=))
@@ -312,12 +315,18 @@ withoutSigning = do
312315
def
313316
& utxos .~ [(txOutRef, txOut)]
314317
& files
315-
.~ Map.fromList [toVerificationKeyFile "./signing-keys" verificationKey1]
318+
.~ Map.fromList
319+
[ toSigningKeyFile "./signing-keys" signingKey1
320+
, toVerificationKeyFile "./signing-keys" verificationKey1
321+
, toVerificationKeyFile "./signing-keys" verificationKey3
322+
]
316323
inTxId = encodeByteString $ fromBuiltin $ TxId.getTxId $ Tx.txOutRefId txOutRef
317324

318325
contract :: Contract Text (Endpoint "SendAda" ()) Text CardanoTx
319326
contract = do
320-
let constraints = Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 1000)
327+
let constraints =
328+
Constraints.mustPayToPubKey paymentPkh2 (Ada.lovelaceValueOf 1000)
329+
<> Constraints.mustBeSignedBy paymentPkh3
321330
submitTx constraints
322331

323332
-- Building and siging the tx should include both signing keys
@@ -331,7 +340,8 @@ withoutSigning = do
331340
--tx-in ${inTxId}#0
332341
--tx-in-collateral ${inTxId}#0
333342
--tx-out ${addr2}+1000
334-
--required-signer-hash ${pkh1'}
343+
--required-signer ./signing-keys/signing-key-${pkh1'}.skey
344+
--required-signer-hash ${pkh3'}
335345
--change-address ${addr1}
336346
--mainnet --protocol-params-file ./protocol.json --out-file ./txs/tx-${outTxId}.raw
337347
|]

test/Spec/MockContract.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import Data.Aeson qualified as JSON
8585
import Data.Aeson.Extras (encodeByteString)
8686
import Data.ByteString qualified as ByteString
8787
import Data.Default (Default (def))
88-
import Data.Either.Combinators (fromRight, mapLeft)
88+
import Data.Either.Combinators (mapLeft)
8989
import Data.Hex (hex)
9090
import Data.Kind (Type)
9191
import Data.List (isPrefixOf)
@@ -164,14 +164,14 @@ skeyToPubKey :: SigningKey PaymentKey -> PubKey
164164
skeyToPubKey =
165165
Ledger.toPublicKey
166166
. Files.unDummyPrivateKey
167-
. fromRight (error "Impossible happened")
167+
. either (error . Text.unpack) id
168168
. Files.skeyToDummyPrivKey
169169

170170
vkeyToPubKey :: VerificationKey PaymentKey -> PubKey
171171
vkeyToPubKey =
172172
Ledger.toPublicKey
173173
. Files.unDummyPrivateKey
174-
. fromRight (error "Impossible happened")
174+
. either (error . Text.unpack) id
175175
. Files.vkeyToDummyPrivKey
176176

177177
toSigningKeyFile :: FilePath -> SigningKey PaymentKey -> (FilePath, MockFile)

0 commit comments

Comments
 (0)