Skip to content

Commit 74e9d95

Browse files
authored
Merge pull request #154 from mlabs-haskell/karol/staking-keys
Support for addresses with staking keys [Plutip's #103].
2 parents d6cf1e3 + 4a6ba87 commit 74e9d95

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ cardano-cli query protocol-parameters --testnet-magic 42 --out-file protocol.jso
140140
```
141141

142142
5. Create a `signing-keys` folder under your projects root with the necessary signig key file(s).
143-
The files should be named in the following format: `signing-key-PUBKEYHASH.skey`
143+
The files should be named in the following format: `signing-key-PUBKEYHASHHEX.skey` and `verification-key-PUBKEYHASHHEX.vkey`
144144

145145
Use the cardano-cli to find out the pub key hash for your key:
146146

src/BotPlutusInterface/Collateral.hs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import BotPlutusInterface.Types (
1010
ContractEnvironment (ceCollateral),
1111
PABConfig (pcOwnPubKeyHash),
1212
collateralValue,
13+
pcOwnStakePubKeyHash,
1314
unCollateralVar,
1415
)
1516
import Cardano.Prelude (Void)
@@ -54,7 +55,13 @@ mkCollateralTx :: PABConfig -> Either Constraints.MkTxError Constraints.Unbalanc
5455
mkCollateralTx pabConf = Constraints.mkTx @Void mempty txc
5556
where
5657
txc :: Constraints.TxConstraints Void Void
57-
txc = Constraints.mustPayToPubKey (PaymentPubKeyHash $ pcOwnPubKeyHash pabConf) (collateralValue pabConf)
58+
txc =
59+
maybe -- pay to either a base or enterprise address
60+
Constraints.mustPayToPubKey
61+
(flip Constraints.mustPayToPubKeyAddress)
62+
(pcOwnStakePubKeyHash pabConf)
63+
(PaymentPubKeyHash $ pcOwnPubKeyHash pabConf)
64+
(collateralValue pabConf)
5865

5966
-- | Middleware to run `chain-index` queries and filter out collateral output from response.
6067
withCollateralHandling ::

src/BotPlutusInterface/Files.hs

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import Data.ByteString.Lazy qualified as LazyByteString
5959
import Data.ByteString.Short qualified as ShortByteString
6060
import Data.Either.Combinators (mapLeft)
6161
import Data.Kind (Type)
62-
import Data.List (sortOn, unzip4)
62+
import Data.List (isPrefixOf, sortOn, unzip4)
6363
import Data.Map (Map)
6464
import Data.Map qualified as Map
6565
import Data.Maybe (catMaybes, mapMaybe)
@@ -211,21 +211,30 @@ readPrivateKeys ::
211211
Eff effs (Either Text (Map PubKeyHash DummyPrivKey))
212212
readPrivateKeys pabConf = do
213213
files <- listDirectory @w $ Text.unpack pabConf.pcSigningKeyFileDir
214-
215-
privKeys <-
216-
catMaybes
217-
<$> mapM
218-
( \filename ->
219-
let fullPath = Text.unpack pabConf.pcSigningKeyFileDir </> filename
220-
in case takeExtension filename of
221-
".vkey" -> Just <$> readVerificationKey @w fullPath
222-
".skey" -> Just <$> readSigningKey @w fullPath
223-
_ -> pure Nothing
224-
)
225-
files
226-
214+
privKeys <- catMaybes <$> mapM readKey files
227215
pure $ toPrivKeyMap <$> sequence privKeys
228216
where
217+
readKey filename =
218+
let fullPath = Text.unpack pabConf.pcSigningKeyFileDir </> filename
219+
in case takeExtension filename of
220+
".vkey" ->
221+
guardPaymentKey paymentVKeyPrefix filename
222+
<$> readVerificationKey @w fullPath
223+
".skey" ->
224+
guardPaymentKey paymentSKeyPrefix filename
225+
<$> readSigningKey @w fullPath
226+
_ -> pure Nothing
227+
228+
paymentVKeyPrefix = "verification-key"
229+
paymentSKeyPrefix = "signing-key"
230+
231+
{- this filtering ensures that only payment keys are read,
232+
it allows to store other types of keys in the same drirectory if required
233+
by altering filename prefix
234+
-}
235+
guardPaymentKey prefix filename =
236+
if prefix `isPrefixOf` filename then Just else const Nothing
237+
229238
toPrivKeyMap :: [DummyPrivKey] -> Map PubKeyHash DummyPrivKey
230239
toPrivKeyMap =
231240
foldl

test/Spec/MockContract.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ toSigningKeyFile signingKeyFileDir sKey =
250250

251251
toVerificationKeyFile :: FilePath -> VerificationKey PaymentKey -> (FilePath, MockFile)
252252
toVerificationKeyFile signingKeyFileDir vKey =
253-
( signingKeyFileDir ++ "/signing-key-" ++ show (Ledger.pubKeyHash (vkeyToPubKey vKey)) ++ ".vkey"
253+
( signingKeyFileDir ++ "/verification-key-" ++ show (Ledger.pubKeyHash (vkeyToPubKey vKey)) ++ ".vkey"
254254
, TextEnvelopeFile $ serialiseToTextEnvelope Nothing vKey
255255
)
256256

0 commit comments

Comments
 (0)