Cardano CLI

From Cardano Blockchain Wiki
Jump to navigation Jump to search
Cardano CLI
DevelopersIOHK
Initial release1.0.0 17 Jan 2020
Last release1.35 25 Jun 2022
Written inHaskel, Shell
LicenseApache-2.0 license
PlatformLinux, Mac, Windows
Repositoryhttps://github.com/input-output-hk/cardano-node
Documentationhttps://input-output-hk.github.io/cardano-wallet/user-guide/cli

Cardano CLI provides a collection of tools for generating keys, constructing transactions, creating certificates, and querying blockchain. Cardano CLI operates with cardano-node application through the socket and needs a path to the socket added to your bash or zsh profile pointing to the local node running.

Env variable

To add an environment variable type source ~/.zshrc or source ~/.bashrc with a line below added to the end of the file, where $HOME is the directory of the local Cardano node.

export CARDANO_NODE_SOCKET_PATH="$HOME/node.socket"

Query commands

Command Description
cardano-cli query protocol-parameters Get the node's current protocol parameters
cardano-cli query tip Get the node's current tip (slot no, hash, block no)
cardano-cli query stake-pools Get the node's current set of stake pool ids
cardano-cli query stake-distribution Get the node's current aggregated stake distribution
cardano-cli query stake-address-info Get the current delegations and reward accounts filtered by stake address.
cardano-cli query utxo Get a portion of the current UTxO: by tx in, by address or the whole.
cardano-cli query ledger-state Dump the current ledger state of the node (Ledger.NewEpochState -- advanced command)
cardano-cli query protocol-state Dump the current protocol state of the node (Ledger.ChainDepState -- advanced command)
cardano-cli query stake-snapshot Obtain the three stake snapshots for a pool, plus the total active stake (advanced command)
cardano-cli query pool-params Dump the pool parameters (Ledger.NewEpochState.esLState._delegationState._pState._pParams -- advanced command)
cardano-cli query leadership-schedule Get the slots the node is expected to mint a block in (advanced command)
cardano-cli query kes-period-info Get information about the current KES period and your node's operational certificate.

Query address ballance

cardano-cli query utxo \
    --address $(cat payment.addr) \
    --mainnet

Get blockchain state

cardano-cli query tip --mainnet

Dump protocol setting

cardano-cli query protocol-parameters \
    --mainnet \
    --out-file protocol.json

Payment address commands

Command Description
cardano-cli address key-gen Create an address key pair.
cardano-cli address key-hash Print the hash of an address key.
cardano-cli address build Build a Shelley payment address, with optional delegation to a stake address.
cardano-cli address build-script Build a Shelley script address. (deprecated; use 'build' instead with '--payment-script-file')
cardano-cli address info Print information about an address.

Stake address commands

Command Description
cardano-cli stake-address key-gen Create a stake address key pair
cardano-cli stake-address build Build a stake address
cardano-cli stake-address key-hash Print the hash of a stake address key.
cardano-cli stake-address registration-certificate Create a stake address registration certificate
cardano-cli stake-address deregistration-certificate Create a stake address deregistration certificate
cardano-cli stake-address delegation-certificate Create a stake address delegation certificate

Key utility commands

Command Description
cardano-cli key verification-key Get a verification key from a signing key. This supports all key types.
cardano-cli key non-extended-key Get a non-extended verification key from an extended verification key. This supports all extended key types.
cardano-cli key convert-byron-key Convert a Byron payment, genesis or genesis delegate key (signing or verification) to a corresponding Shelley-format key.
cardano-cli key convert-byron-genesis-vkey Convert a Base64-encoded Byron genesis verification key to a Shelley genesis verification key
cardano-cli key convert-itn-key Convert an Incentivized Testnet (ITN) non-extended (Ed25519) signing or verification key to a corresponding Shelley stake key
cardano-cli key convert-itn-extended-key Convert an Incentivized Testnet (ITN) extended (Ed25519Extended) signing key to a corresponding Shelley stake signing key
cardano-cli key convert-itn-bip32-key Convert an Incentivized Testnet (ITN) BIP32 (Ed25519Bip32) signing key to a corresponding Shelley stake signing key
cardano-cli key convert-cardano-address-key Convert a cardano-address extended signing key to a corresponding Shelley-format key.

Transaction commands

Command Description
cardano-cli transaction build-raw Build a transaction (low-level, inconvenient) Please note the order of some cmd options is crucial. If used incorrectly may produce undesired tx body. See nested [] notation above for details.
cardano-cli transaction build Build a balanced transaction (automatically calculates fees). Please note the order of some cmd options is crucial. If used incorrectly may produce undesired tx body. See nested [] notation above for details.
cardano-cli transaction sign Sign a transaction
cardano-cli transaction witness Create a transaction witness
cardano-cli transaction assemble Assemble a tx body and witness(es) to form a transaction
cardano-cli transaction submit Submit a transaction to the local node whose Unix domain socket is obtained from the CARDANO_NODE_SOCKET_PATH enviromnent variable.
cardano-cli transaction policyid Calculate the PolicyId from the monetary policy script.
cardano-cli transaction calculate-min-fee Calculate the minimum fee for a transaction.
cardano-cli transaction calculate-min-required-utxo Calculate the minimum required UTxO for a transaction output.
cardano-cli transaction hash-script-data Calculate the hash of script data.
cardano-cli transaction txid Print a transaction identifier.
cardano-cli transaction view Print a transaction.

Transaction build simple

https://developers.cardano.org/docs/stake-pool-course/handbook/create-simple-transaction

Create Simple Transaction

cardano-cli query utxo \
    --address $(cat payment.addr) \
    --mainnet

cardano-cli transaction build-raw \
    --tx-in <TxHash>#<TxIx> \
    --tx-out <Address>+<Amount> \
    --change-address $(cat payment.addr) \
    --invalid-hereafter 0 \
    --fee 0 \
    --out-file tx.draft

cardano-cli transaction calculate-min-fee \
    --tx-body-file tx.draft \
    --tx-in-count 1 \
    --tx-out-count 2(equal to output transations) \
    --witness-count 1 \
    --byron-witness-count 0 \
    --mainnet \
    --protocol-params-file protocol.json

fee = <UTXO BALANCE> - <AMOUNT TO SEND> - <TRANSACTION FEE>

cardano-cli query tip --mainnet
{
    "block": 7761484,
    "epoch": 363,
    "era": "Alonzo",
    "hash": "18f469fc1870b5848866fdb0e7f18f0c9992021b36a8139870005dd9c0cef2db",
    "slot": 71700477,
    "syncProgress": "100.00"
}

cardano-cli transaction build-raw \
    --tx-in <TxHash>#<TxIx> \
    --tx-out <Address>+<Amount>\
    --tx-out $(cat payment.addr)+<Change> \
    --invalid-hereafter <slot + 400> \
    --fee <Fee> \
    --out-file tx.raw

cardano-cli transaction sign \
    --tx-body-file tx.raw \
    --signing-key-file payment.skey \
    --mainnet \
    --out-file tx.signed

cardano-cli transaction submit \
    --tx-file tx.signed \
    --mainnet


Transaction build with change address

https://cardano-foundation.gitbook.io/stake-pool-course/stake-pool-guide/stake-pool-operations/building-transactions-the-easy-way-with-the-build-command#cardano-cli-transaction-build

Build command automatically calculates fees and the change to send to the specified change-address.

Note that the build command overestimates the fees slightly compared to build-raw.‎

cardano-cli query tip --mainnet

cardano-cli query utxo --address $(cat payment.addr) --mainnet

cardano-cli query protocol-parameters \
   --mainnet \
   --out-file protocol.json

cardano-cli transaction build \
--tx-in <TxHash>#<TxIx> \
--tx-out <Address>+<Amount>\
--change-address $(cat payment.addr) \
--protocol-params-file protocol.json \
--mainnet \
--out-file tx.raw

cardano-cli transaction sign \
--tx-body-file tx.raw \
--signing-key-file payment.skey \
--mainnet \
--out-file tx.signed

cardano-cli transaction submit \
--tx-file tx.signed \
--mainnet

Node commands

Command Description
cardano-cli node key-gen Create a key pair for a node operator's offline key and a new certificate issue counter
cardano-cli node key-gen-KES Create a key pair for a node KES operational key
cardano-cli node key-gen-VRF Create a key pair for a node VRF operational key
cardano-cli node key-hash-VRF Print hash of a node's operational VRF key
cardano-cli node new-counter Create a new certificate issue counter
cardano-cli node issue-op-cert Issue a node operational certificate

Stake pool commands

Command Description
cardano-cli stake-pool registration-certificate Create a stake pool registration certificate
cardano-cli stake-pool deregistration-certificate Create a stake pool deregistration certificate
cardano-cli stake-pool id Build pool id from the offline key
cardano-cli stake-pool metadata-hash Print the hash of pool metadata.

Governance commands

Command Description
cardano-cli governance create-mir-certificate Create a MIR (Move Instantaneous Rewards) certificate
cardano-cli governance create-genesis-key-delegation-certificate Create a genesis key delegation certificate
cardano-cli governance create-update-proposal Create an update proposal