22
33module BotPlutusInterface.Files (
44 policyScriptFilePath ,
5+ DummyPrivKey (FromSKey , FromVKey ),
6+ unDummyPrivateKey ,
57 validatorScriptFilePath ,
68 readPrivateKeys ,
79 signingKeyFilePath ,
810 txFilePath ,
9- readPrivateKey ,
1011 writeAll ,
1112 writePolicyScriptFile ,
1213 redeemerJsonFilePath ,
14+ mkDummyPrivateKey ,
1315 writeRedeemerJsonFile ,
1416 writeValidatorScriptFile ,
1517 datumJsonFilePath ,
16- fromCardanoPaymentKey ,
18+ skeyToDummyPrivKey ,
19+ vkeyToDummyPrivKey ,
1720 writeDatumJsonFile ,
1821) where
1922
@@ -27,8 +30,9 @@ import BotPlutusInterface.Effects (
2730 )
2831import BotPlutusInterface.Types (PABConfig )
2932import Cardano.Api (
30- AsType (AsPaymentKey , AsSigningKey ),
33+ AsType (AsPaymentKey , AsSigningKey , AsVerificationKey ),
3134 FileError ,
35+ Key (VerificationKey ),
3236 PaymentKey ,
3337 SigningKey ,
3438 getVerificationKey ,
@@ -51,14 +55,15 @@ import Data.ByteString.Lazy qualified as LazyByteString
5155import Data.ByteString.Short qualified as ShortByteString
5256import Data.Either.Combinators (mapLeft )
5357import Data.Kind (Type )
58+ import Data.List (sortOn )
5459import Data.Map (Map )
5560import Data.Map qualified as Map
56- import Data.Maybe (mapMaybe )
61+ import Data.Maybe (catMaybes , mapMaybe )
5762import Data.Set qualified as Set
5863import Data.Text (Text )
5964import Data.Text qualified as Text
6065import Ledger qualified
61- import Ledger.Crypto (PrivateKey , PubKeyHash (PubKeyHash ))
66+ import Ledger.Crypto (PrivateKey , PubKey ( PubKey ), PubKeyHash (PubKeyHash ))
6267import Ledger.Tx (Tx )
6368import Ledger.Tx qualified as Tx
6469import Ledger.TxId qualified as TxId
@@ -67,16 +72,18 @@ import Plutus.V1.Ledger.Api (
6772 CurrencySymbol ,
6873 Datum (getDatum ),
6974 DatumHash (.. ),
75+ LedgerBytes (LedgerBytes ),
7076 MintingPolicy ,
7177 Redeemer (getRedeemer ),
7278 RedeemerHash (.. ),
7379 Script ,
7480 Validator ,
7581 ValidatorHash (.. ),
82+ toBuiltin ,
7683 )
7784import PlutusTx (ToData , toData )
7885import PlutusTx.Builtins (fromBuiltin )
79- import System.FilePath (isExtensionOf )
86+ import System.FilePath (takeExtension , (</>) )
8087import Prelude
8188
8289-- | Filename of a minting policy script
@@ -167,50 +174,92 @@ readPrivateKeys ::
167174 forall (w :: Type ) (effs :: [Type -> Type ]).
168175 Member (PABEffect w ) effs =>
169176 PABConfig ->
170- Eff effs (Either Text (Map PubKeyHash PrivateKey ))
177+ Eff effs (Either Text (Map PubKeyHash DummyPrivKey ))
171178readPrivateKeys pabConf = do
172179 files <- listDirectory @ w $ Text. unpack pabConf. pcSigningKeyFileDir
173- let sKeyFiles =
174- map (\ filename -> Text. unpack pabConf. pcSigningKeyFileDir ++ " /" ++ filename) $
175- filter (" skey" `isExtensionOf` ) files
176- privKeys <- mapM (readPrivateKey @ w ) sKeyFiles
180+
181+ privKeys <-
182+ catMaybes
183+ <$> mapM
184+ ( \ filename ->
185+ let fullPath = Text. unpack pabConf. pcSigningKeyFileDir </> filename
186+ in case takeExtension filename of
187+ " .vkey" -> Just <$> readVerificationKey @ w fullPath
188+ " .skey" -> Just <$> readSigningKey @ w fullPath
189+ _ -> pure Nothing
190+ )
191+ files
192+
177193 pure $ toPrivKeyMap <$> sequence privKeys
178194 where
179- toPrivKeyMap :: [PrivateKey ] -> Map PubKeyHash PrivateKey
195+ toPrivKeyMap :: [DummyPrivKey ] -> Map PubKeyHash DummyPrivKey
180196 toPrivKeyMap =
181197 foldl
182198 ( \ pKeyMap pKey ->
183- let pkh = Ledger. pubKeyHash $ Ledger. toPublicKey pKey
199+ let pkh = Ledger. pubKeyHash $ Ledger. toPublicKey $ unDummyPrivateKey pKey
184200 in Map. insert pkh pKey pKeyMap
185201 )
186202 Map. empty
203+ . sortOn keyPriority
204+
205+ keyPriority :: DummyPrivKey -> Int
206+ keyPriority (FromSKey _) = 1
207+ keyPriority (FromVKey _) = 0
187208
188- readPrivateKey ::
209+ data DummyPrivKey
210+ = FromSKey PrivateKey
211+ | FromVKey PrivateKey
212+
213+ unDummyPrivateKey :: DummyPrivKey -> PrivateKey
214+ unDummyPrivateKey (FromSKey key) = key
215+ unDummyPrivateKey (FromVKey key) = key
216+
217+ readSigningKey ::
189218 forall (w :: Type ) (effs :: [Type -> Type ]).
190219 Member (PABEffect w ) effs =>
191220 FilePath ->
192- Eff effs (Either Text PrivateKey )
193- readPrivateKey filePath = do
221+ Eff effs (Either Text DummyPrivKey )
222+ readSigningKey filePath = do
194223 pKey <- mapLeft (Text. pack . show ) <$> readFileTextEnvelope @ w (AsSigningKey AsPaymentKey ) filePath
195- pure $ fromCardanoPaymentKey =<< pKey
224+ pure $ skeyToDummyPrivKey =<< pKey
225+
226+ readVerificationKey ::
227+ forall (w :: Type ) (effs :: [Type -> Type ]).
228+ Member (PABEffect w ) effs =>
229+ FilePath ->
230+ Eff effs (Either Text DummyPrivKey )
231+ readVerificationKey filePath = do
232+ pKey <- mapLeft (Text. pack . show ) <$> readFileTextEnvelope @ w (AsVerificationKey AsPaymentKey ) filePath
233+ pure $ vkeyToDummyPrivKey =<< pKey
234+
235+ vkeyToDummyPrivKey :: VerificationKey PaymentKey -> Either Text DummyPrivKey
236+ vkeyToDummyPrivKey =
237+ fmap FromVKey . vkeyToDummyPrivKey'
238+
239+ skeyToDummyPrivKey :: SigningKey PaymentKey -> Either Text DummyPrivKey
240+ skeyToDummyPrivKey =
241+ fmap FromSKey . vkeyToDummyPrivKey' . getVerificationKey
196242
197243{- | Warning! This implementation is not correct!
198244 This private key is derived from a normal signing key which uses a 32 byte private key compared
199245 to the extended key which is 64 bytes. Also, the extended key includes a chain index value
200246
201- This keys sole purpose is to be able to derive a public key from it, which is then used for
247+ This key's sole purpose is to be able to derive a public key from it, which is then used for
202248 mapping to a signing key file for the CLI
203249-}
204- fromCardanoPaymentKey :: SigningKey PaymentKey -> Either Text PrivateKey
205- fromCardanoPaymentKey sKey =
206- let dummyPrivKeySuffix = ByteString. replicate 32 0
250+ vkeyToDummyPrivKey' :: VerificationKey PaymentKey -> Either Text PrivateKey
251+ vkeyToDummyPrivKey' =
252+ mkDummyPrivateKey . PubKey . LedgerBytes . toBuiltin . serialiseToRawBytes
253+
254+ mkDummyPrivateKey :: PubKey -> Either Text PrivateKey
255+ mkDummyPrivateKey (PubKey (LedgerBytes pubkey)) =
256+ let dummyPrivKey = ByteString. replicate 32 0
257+ dummyPrivKeySuffix = ByteString. replicate 32 0
207258 dummyChainCode = ByteString. replicate 32 1
208- vKey = getVerificationKey sKey
209- privkeyBS = serialiseToRawBytes sKey
210- pubkeyBS = serialiseToRawBytes vKey
259+ pubkeyBS = fromBuiltin pubkey
211260 in mapLeft Text. pack $
212261 Crypto. xprv $
213- mconcat [privkeyBS , dummyPrivKeySuffix, pubkeyBS, dummyChainCode]
262+ mconcat [dummyPrivKey , dummyPrivKeySuffix, pubkeyBS, dummyChainCode]
214263
215264serialiseScript :: Script -> PlutusScript PlutusScriptV1
216265serialiseScript =
0 commit comments