-
Notifications
You must be signed in to change notification settings - Fork 10
Add the option to manually sign transactions #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
efd5358
9363466
cfd02c7
2acac91
23a2ff9
fa2313c
43fb36c
dfc526e
7cc1c89
dc328d8
8fe4ed0
7345a74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,16 +16,17 @@ module BotPlutusInterface.CardanoCLI ( | |
| queryTip, | ||
| ) where | ||
|
|
||
| import BotPlutusInterface.Effects (PABEffect, ShellArgs (..), callCommand, uploadDir) | ||
| import BotPlutusInterface.Effects (PABEffect, ShellArgs (..), callCommand, printLog, uploadDir) | ||
| import BotPlutusInterface.Files ( | ||
| DummyPrivKey (FromSKey, FromVKey), | ||
| datumJsonFilePath, | ||
| policyScriptFilePath, | ||
| redeemerJsonFilePath, | ||
| signingKeyFilePath, | ||
| txFilePath, | ||
| validatorScriptFilePath, | ||
| ) | ||
| import BotPlutusInterface.Types (PABConfig, Tip) | ||
| import BotPlutusInterface.Types (LogLevel (Warn), PABConfig, Tip) | ||
| import BotPlutusInterface.UtxoParser qualified as UtxoParser | ||
| import Cardano.Api.Shelley (NetworkId (Mainnet, Testnet), NetworkMagic (..), serialiseAddress) | ||
| import Codec.Serialise qualified as Codec | ||
|
|
@@ -54,7 +55,7 @@ import Ledger (Slot (Slot), SlotRange) | |
| import Ledger qualified | ||
| import Ledger.Ada qualified as Ada | ||
| import Ledger.Address (Address (..)) | ||
| import Ledger.Crypto (PubKey, PubKeyHash) | ||
| import Ledger.Crypto (PubKey, PubKeyHash (getPubKeyHash)) | ||
| import Ledger.Interval ( | ||
| Extended (Finite), | ||
| Interval (Interval), | ||
|
|
@@ -198,17 +199,27 @@ buildTx :: | |
| forall (w :: Type) (effs :: [Type -> Type]). | ||
| Member (PABEffect w) effs => | ||
| PABConfig -> | ||
| Map PubKeyHash DummyPrivKey -> | ||
| PubKeyHash -> | ||
| BuildMode -> | ||
| Tx -> | ||
| Eff effs () | ||
| buildTx pabConf ownPkh buildMode tx = | ||
| buildTx pabConf privKeys ownPkh buildMode tx = | ||
| callCommand @w $ ShellArgs "cardano-cli" opts (const ()) | ||
| where | ||
| ownAddr = Ledger.pubKeyHashAddress (Ledger.PaymentPubKeyHash ownPkh) Nothing | ||
| requiredSigners = | ||
| concatMap | ||
| (\pubKey -> ["--required-signer", signingKeyFilePath pabConf (Ledger.pubKeyHash pubKey)]) | ||
| ( \pubKey -> | ||
| let pkh = Ledger.pubKeyHash pubKey | ||
| in case Map.lookup pkh privKeys of | ||
| Just (FromSKey _) -> | ||
| ["--required-signer", signingKeyFilePath pabConf pkh] | ||
| Just (FromVKey _) -> | ||
| ["--required-signer-hash", encodeByteString $ fromBuiltin $ getPubKeyHash pkh] | ||
| Nothing -> | ||
| [] | ||
| ) | ||
| (Map.keys (Ledger.txSignatures tx)) | ||
| opts = | ||
| mconcat | ||
|
|
@@ -237,27 +248,39 @@ signTx :: | |
| forall (w :: Type) (effs :: [Type -> Type]). | ||
| Member (PABEffect w) effs => | ||
| PABConfig -> | ||
| Map PubKeyHash DummyPrivKey -> | ||
| Tx -> | ||
| [PubKey] -> | ||
| Eff effs () | ||
| signTx pabConf tx pubKeys = | ||
| callCommand @w $ | ||
| ShellArgs | ||
| "cardano-cli" | ||
| ( mconcat | ||
| [ ["transaction", "sign"] | ||
| , ["--tx-body-file", txFilePath pabConf "raw" tx] | ||
| , signingKeyFiles | ||
| , ["--out-file", txFilePath pabConf "signed" tx] | ||
| ] | ||
| ) | ||
| (const ()) | ||
| Eff effs (Either Text ()) | ||
| signTx pabConf privKeys tx pubKeys = | ||
| let skeys = Map.filter (\case FromSKey _ -> True; FromVKey _ -> False) privKeys | ||
| in if all ((`Map.member` skeys) . Ledger.pubKeyHash) pubKeys | ||
| then callCommand @w $ ShellArgs "cardano-cli" opts (const (Right ())) | ||
| else do | ||
| let err = | ||
| Text.unlines | ||
| [ "Not all required signatures have signing key files. Please sign and submit the tx manually:" | ||
| , "Tx file: " <> txFilePath pabConf "raw" tx | ||
| , "Signatories (pkh): " | ||
| <> Text.unwords | ||
| (map (encodeByteString . fromBuiltin . getPubKeyHash . Ledger.pubKeyHash) pubKeys) | ||
| ] | ||
| printLog @w Warn (Text.unpack err) | ||
| pure $ Left err | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For manual signing, should the transaction error?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, giving the tx here makes it pretty difficult to test contracts using this, i wonder if theres another way - perhaps a function specifically for under-signed transactions?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a bit unfinished, I just wanted to push it asap, but certainly I want to find a better way to handle this. Demanding manual intervention is pretty annoying, also if a contract contains multiple txs, the whole workflow could break.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That could certainly work! Please do keep me in the loop on this :) |
||
| where | ||
| signingKeyFiles = | ||
| concatMap | ||
| (\pubKey -> ["--signing-key-file", signingKeyFilePath pabConf (Ledger.pubKeyHash pubKey)]) | ||
| pubKeys | ||
|
|
||
| opts = | ||
| mconcat | ||
| [ ["transaction", "sign"] | ||
| , ["--tx-body-file", txFilePath pabConf "raw" tx] | ||
| , signingKeyFiles | ||
| , ["--out-file", txFilePath pabConf "signed" tx] | ||
| ] | ||
|
|
||
| -- Signs and writes a tx (uses the tx body written to disk as input) | ||
| submitTx :: | ||
| forall (w :: Type) (effs :: [Type -> Type]). | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.