Skip to content

Commit

Permalink
Update cli's witness creation command to allow multisig scripts as input
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 authored and intricate committed Sep 14, 2020
1 parent a7cf6a5 commit 4c0a76a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 47 deletions.
15 changes: 7 additions & 8 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ data AddressCmd
= AddressKeyGen AddressKeyType VerificationKeyFile SigningKeyFile
| AddressKeyHash VerificationKeyFile (Maybe OutputFile)
| AddressBuild VerificationKeyFile (Maybe VerificationKeyFile) NetworkId (Maybe OutputFile)
| AddressBuildMultiSig --TODO
| AddressBuildScript ScriptFile NetworkId (Maybe OutputFile)
| AddressBuildMultiSig ScriptFile NetworkId (Maybe OutputFile)
| AddressInfo Text (Maybe OutputFile)
deriving (Eq, Show)

Expand All @@ -105,9 +104,8 @@ renderAddressCmd cmd =
AddressKeyGen {} -> "address key-gen"
AddressKeyHash {} -> "address key-hash"
AddressBuild {} -> "address build"
AddressBuildMultiSig {} -> "address build-multisig"
AddressBuildMultiSig {} -> "address build-script"
AddressInfo {} -> "address info"
AddressBuildScript {} -> "address build-script"

data StakeAddressCmd
= StakeAddressKeyGen VerificationKeyFile SigningKeyFile
Expand Down Expand Up @@ -160,9 +158,10 @@ data TransactionCmd
[MetaDataFile]
(Maybe UpdateProposalFile)
TxBodyFile
| TxBuildMultiSig MultiSigScriptObject (Maybe OutputFile)
| TxSign TxBodyFile [SigningKeyFile] (Maybe NetworkId) TxFile
| TxWitness TxBodyFile SigningKeyFile (Maybe NetworkId) OutputFile
| TxSignWitness TxBodyFile [WitnessFile] OutputFile
| TxCreateWitness TxBodyFile SigningKeyOrScriptFile (Maybe NetworkId) OutputFile
| TxAssembleTxBodyWitness TxBodyFile [WitnessFile] OutputFile
| TxSubmit Protocol NetworkId FilePath
| TxCalculateMinFee
TxBodyFile
Expand All @@ -180,8 +179,8 @@ renderTransactionCmd cmd =
case cmd of
TxBuildRaw {} -> "transaction build-raw"
TxSign {} -> "transaction sign"
TxWitness {} -> "transaction witness"
TxSignWitness {} -> "transaction sign-witness"
TxCreateWitness {} -> "transaction witness"
TxAssembleTxBodyWitness {} -> "transaction sign-witness"
TxSubmit {} -> "transaction submit"
TxCalculateMinFee {} -> "transaction calculate-min-fee"
TxGetTxId {} -> "transaction txid"
Expand Down
95 changes: 68 additions & 27 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module Cardano.CLI.Shelley.Parsers
, renderTxIn
) where

import Cardano.Prelude hiding (option)
import Cardano.Prelude hiding (All, Any, option)
import Prelude (String)

import Control.Monad.Fail (fail)
Expand Down Expand Up @@ -123,8 +123,6 @@ pAddressCmd =
(Opt.info pAddressKeyHash $ Opt.progDesc "Print the hash of an address key.")
, Opt.command "build"
(Opt.info pAddressBuild $ Opt.progDesc "Build a Shelley payment address, with optional delegation to a stake address.")
, Opt.command "build-multisig"
(Opt.info pAddressBuildMultiSig $ Opt.progDesc "Build a Shelley payment multi-sig address.")
, Opt.command "build-script"
(Opt.info pAddressBuildScript $ Opt.progDesc "Build a Shelley script address.")
, Opt.command "info"
Expand All @@ -147,12 +145,8 @@ pAddressCmd =
<*> pNetworkId
<*> pMaybeOutputFile


pAddressBuildMultiSig :: Parser AddressCmd
pAddressBuildMultiSig = pure AddressBuildMultiSig

pAddressBuildScript :: Parser AddressCmd
pAddressBuildScript = AddressBuildScript
pAddressBuildScript = AddressBuildMultiSig
<$> pScript
<*> pNetworkId
<*> pMaybeOutputFile
Expand Down Expand Up @@ -180,10 +174,24 @@ pScript :: Parser ScriptFile
pScript = ScriptFile <$> Opt.strOption
( Opt.long "script-file"
<> Opt.metavar "FILE"
<> Opt.help "Filepath of the script."
<> Opt.help "Filepath of the multisig script."
<> Opt.completer (Opt.bashCompleter "file")
)

pScriptOrSigningKey :: Parser SigningKeyOrScriptFile
pScriptOrSigningKey = pScript' <|> pWitnessSigningKeyFile

pScript' :: Parser SigningKeyOrScriptFile
pScript' =
ScriptFileForWitness <$>
Opt.strOption
( Opt.long "script-file"
<> Opt.metavar "FILE"
<> Opt.help "Filepath of the multisig script to be used in witness construction."
<> Opt.completer (Opt.bashCompleter "file")
)


pStakeAddress :: Parser StakeAddressCmd
pStakeAddress =
Opt.subparser $
Expand Down Expand Up @@ -417,12 +425,15 @@ pTransaction =
mconcat
[ Opt.command "build-raw"
(Opt.info pTransactionBuild $ Opt.progDesc "Build a transaction (low-level, inconvenient)")
, Opt.command "build-multisig"
(Opt.info pMultiSigBuild $ Opt.progDesc "Build a multisig script.")
, Opt.command "sign"
(Opt.info pTransactionSign $ Opt.progDesc "Sign a transaction")
, Opt.command "witness"
(Opt.info pTransactionWitness $ Opt.progDesc "Witness a transaction")
(Opt.info pTransactionCreateWitness $ Opt.progDesc "Create a transaction witness")
, Opt.command "sign-witness"
(Opt.info pTransactionSignWit $ Opt.progDesc "Sign and witness a transaction")
(Opt.info pTransactionAssembleTxBodyWit
$ Opt.progDesc "Assemble a tx body and witness(es) to form a transaction")
, Opt.command "submit"
(Opt.info pTransactionSubmit . Opt.progDesc $
mconcat
Expand All @@ -447,22 +458,26 @@ pTransaction =
<*> optional pUpdateProposalFile
<*> pTxBodyFile Output

pMultiSigBuild :: Parser TransactionCmd
pMultiSigBuild = TxBuildMultiSig <$> pMultiSigScriptObject <*> pMaybeOutputFile

pTransactionSign :: Parser TransactionCmd
pTransactionSign = TxSign <$> pTxBodyFile Input
<*> pSomeSigningKeyFiles
<*> optional pNetworkId
<*> pTxFile Output

pTransactionWitness :: Parser TransactionCmd
pTransactionWitness = TxWitness <$> pTxBodyFile Input
<*> pWitnessSigningKeyFile
<*> optional pNetworkId
<*> pOutputFile

pTransactionSignWit :: Parser TransactionCmd
pTransactionSignWit = TxSignWitness <$> pTxBodyFile Input
<*> some pWitnessFile
<*> pOutputFile
pTransactionCreateWitness :: Parser TransactionCmd
pTransactionCreateWitness = TxCreateWitness
<$> pTxBodyFile Input
<*> pScriptOrSigningKey
<*> optional pNetworkId
<*> pOutputFile

pTransactionAssembleTxBodyWit :: Parser TransactionCmd
pTransactionAssembleTxBodyWit = TxAssembleTxBodyWitness
<$> pTxBodyFile Input
<*> some pWitnessFile
<*> pOutputFile

pTransactionSubmit :: Parser TransactionCmd
pTransactionSubmit = TxSubmit <$> pProtocol
Expand Down Expand Up @@ -906,6 +921,32 @@ pMetaDataFile =
<> Opt.completer (Opt.bashCompleter "file")
)


pMultiSigScriptObject :: Parser MultiSigScriptObject
pMultiSigScriptObject = pAny <|> pAll <|> pAtLeast
where
pAny :: Parser MultiSigScriptObject
pAny = Opt.flag' () ( Opt.long "any"
<> Opt.help "Build an \"any\" multi-signature script.")
*> (Any <$> some pPaymentVerificationKeyFile)

pAll :: Parser MultiSigScriptObject
pAll = Opt.flag' () ( Opt.long "all"
<> Opt.help "Build an \"all\" multi-signature script.")
*> (All <$> some pPaymentVerificationKeyFile)

pAtLeast :: Parser MultiSigScriptObject
pAtLeast = Opt.flag' () ( Opt.long "at-least"
<> Opt.help "Build an \"atLeast\" multi-signature script.")
*> (AtLeast <$> pRequired <*> some pPaymentVerificationKeyFile)

pRequired :: Parser Int
pRequired = Opt.option Opt.auto
( Opt.long "required"
<> Opt.metavar "INT"
<> Opt.help "The minimum number of signatures required."
)

pWithdrawal :: Parser (StakeAddress, Lovelace)
pWithdrawal =
Opt.option (readerFromAttoParser parseWithdrawal)
Expand Down Expand Up @@ -975,11 +1016,11 @@ pSigningKeyFile fdir =
<> Opt.completer (Opt.bashCompleter "file")
)

pWitnessSigningKeyFile :: Parser SigningKeyFile
pWitnessSigningKeyFile = SigningKeyFile <$> Opt.strOption
( Opt.long "witness-signing-key-file"
pWitnessSigningKeyFile :: Parser SigningKeyOrScriptFile
pWitnessSigningKeyFile = SigningKeyFileForWitness <$> Opt.strOption
( Opt.long "signing-key-file"
<> Opt.metavar "FILE"
<> Opt.help "Filepath of the witness signing key."
<> Opt.help "Filepath of the signing key to be used in witness construction."
<> Opt.completer (Opt.bashCompleter "file")
)

Expand Down Expand Up @@ -1314,7 +1355,7 @@ pWitnessFile =
Opt.strOption
( Opt.long "witness-file"
<> Opt.metavar "FILE"
<> Opt.help "Filepath of the witness."
<> Opt.help "Filepath of the witness"
<> Opt.completer (Opt.bashCompleter "file")
)

Expand Down
13 changes: 2 additions & 11 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Cardano.CLI.Shelley.Run.Address
) where

import Cardano.Prelude hiding (putStrLn)
import Prelude (String, putStrLn)
import Prelude (String)

import Data.Aeson
import qualified Data.ByteString.Char8 as BS
Expand Down Expand Up @@ -50,8 +50,7 @@ runAddressCmd cmd =
AddressKeyGen kt vkf skf -> runAddressKeyGen kt vkf skf
AddressKeyHash vkf mOFp -> runAddressKeyHash vkf mOFp
AddressBuild payVk stkVk nw mOutFp -> runAddressBuild payVk stkVk nw mOutFp
AddressBuildMultiSig {} -> runAddressBuildMultiSig
AddressBuildScript sFp nId mOutFp -> runAddressBuildScript sFp nId mOutFp
AddressBuildMultiSig sFp nId mOutFp -> runAddressBuildScript sFp nId mOutFp
AddressInfo txt mOFp -> firstExceptT ShelleyAddressCmdAddressInfoError $ runAddressInfo txt mOFp

runAddressKeyGen :: AddressKeyType
Expand Down Expand Up @@ -180,18 +179,10 @@ readAddressVerificationKeyFile (VerificationKeyFile vkfile) =
AGenesisUTxOVerificationKey
]


--
-- Multisig addresses
--

runAddressBuildMultiSig :: ExceptT ShelleyAddressCmdError IO ()
runAddressBuildMultiSig = liftIO $ putStrLn "runAddressBuildMultiSig: TODO"

--
-- Script addresses
--

runAddressBuildScript
:: ScriptFile
-> NetworkId
Expand Down
16 changes: 15 additions & 1 deletion cardano-cli/src/Cardano/CLI/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ module Cardano.CLI.Types
( CBORObject (..)
, CertificateFile (..)
, GenesisFile (..)
, MultiSigScriptObject (..)
, OutputFormat (..)
, QueryFilter (..)
, SigningKeyFile (..)
, SigningKeyOrScriptFile (..)
, SocketPath (..)
, StakePoolVerificationKeyHashOrFile (..)
, ScriptFile (..)
Expand Down Expand Up @@ -77,8 +79,20 @@ newtype UpdateProposalFile = UpdateProposalFile { unUpdateProposalFile :: FilePa
deriving newtype (Eq, Show)

newtype VerificationKeyFile
= VerificationKeyFile FilePath
= VerificationKeyFile { unVerificationKeyFile :: FilePath }
deriving (Eq, Show)

-- | Specify what type of multisig script to create
-- i.e any, all or atleast
data MultiSigScriptObject
= Any [VerificationKeyFile]
| All [VerificationKeyFile]
| AtLeast Int [VerificationKeyFile]
deriving (Eq, Show)

newtype ScriptFile = ScriptFile { unScriptFile :: FilePath }
deriving (Eq, Show)

data SigningKeyOrScriptFile = ScriptFileForWitness FilePath
| SigningKeyFileForWitness FilePath
deriving (Eq, Show)

0 comments on commit 4c0a76a

Please sign in to comment.