# Transact

## Using the Marsd CLI​

The easiest way to send transactions is using the CLI. For example, running the following command:

_1marsd tx bank send $MY_VALIDATOR_ADDRESS$RECIPIENT 1000stake --chain-id my-test-chain --keyring-backend test

will run the following steps:

• generate a transaction with one Msg (x/bank's MsgSend), and print the generated transaction to the console.
• ask the user for confirmation to send the transaction from the $MY_VALIDATOR_ADDRESS account. • fetch $MY_NODE_ADDRESS from the keyring. This is possible if you have set you have set up the CLI's keyring (from a separate tutorial).
• sign the generated transaction with the keyring's account.
• broadcast the signed transaction to the network. This is possible because the CLI connects to the node's Tendermint RPC endpoint.

The CLI bundles all the necessary steps into a simple-to-use user experience. However, it's possible to run all the steps individually too.

## Generating a Transaction​

Generating a transaction can simply be done by appending the --generate-only flag on any tx command, e.g.:

_1marsd tx bank send $MY_NODE_ADDRESS$RECIPIENT 1000stake --chain-id my-test-chain --generate-only

This will output the unsigned transaction as JSON in the console. We can also save the unsigned transaction to a file (to be passed around between signers more easily) by appending > unsigned_tx.json to the above command.

## Signing a Transaction​

Signing a transaction using the CLI requires the unsigned transaction to be saved in a file. Let's assume the unsigned transaction is in a file called unsigned_tx.json in the current directory (see previous paragraph on how to do that). Then, simply run the following command:

_1marsd tx sign unsigned_tx.json --chain-id my-test-chain --keyring-backend test --from $MY_NODE_ADDRESS This command will decode the unsigned transaction and sign it with SIGN_MODE_DIRECT with $MY_VALIDATOR_ADDRESS's key, which we already set up in the keyring. The signed transaction will be output as JSON to the console, and, as above, we can save it to a file by appending > signed_tx.json.

Some useful flags to consider in the tx sign command:

• --sign-mode: you may use amino-json to sign the transaction using SIGN_MODE_LEGACY_AMINO_JSON,
• --offline: sign in offline mode. This means that the tx sign command doesn't connect to the node to retrieve the signer's account number and sequence, both needed for signing. In this case, you must manually supply the --account-number and --sequence flags. This is useful for offline signing, i.e. signing in a secure environment which doesn't have access to the internet.

## Signing with Multiple Signers​

Please note that signing a transaction with multiple signers or with a multisig account, where at least one signer uses SIGN_MODE_DIRECT, is not yet possible. You may follow this Github issue for more info.

Signing with multiple signers is done with the tx multisign command. This command assumes that all signers use SIGN_MODE_LEGACY_AMINO_JSON. The flow is similar to the tx sign command flow, but instead of signing an unsigned transaction file, each signer signs the file signed by previous signer(s). The tx multisign command will append signatures to the existing transactions. It is important that signers sign the transaction in the same order as given by the transaction, which is retrievable using the GetSigners() method.

For example, starting with the unsigned_tx.json, and assuming the transaction has 4 signers, we would run:

_9# Let signer1 sign the unsigned tx._9marsd tx multisign unsigned_tx.json signer_key_1 --chain-id my-test-chain --keyring-backend test > partial_tx_1.json_9_9# Now signer1 will send the partial_tx_1.json to the signer2._9# Signer2 appends their signature:_9marsd tx multisign partial_tx_1.json signer_key_2 --chain-id my-test-chain --keyring-backend test > partial_tx_2.json_9_9# Signer2 sends the partial_tx_2.json file to signer3, and signer3 can append his signature:_9marsd tx multisign partial_tx_2.json signer_key_3 --chain-id my-test-chain --keyring-backend test > partial_tx_3.json

Broadcasting a transaction is done using the following command:

_1marsd tx broadcast tx_signed.json

You may optionally pass the --broadcast-mode flag to specify which response to receive from the node:

• block: the CLI waits for the tx to be committed in a block.
• sync: the CLI waits for a CheckTx execution response only.
• async: the CLI returns immediately (transaction might fail).

## Encoding a Transaction​

In order to broadcast a transaction using the gRPC or REST endpoints, the transaction will need to be encoded first. This can be done using the CLI.

Encoding a transaction is done using the following command:

_1marsd tx encode tx_signed.json

This will read the transaction from the file, serialize it using Protobuf, and output the transaction bytes as base64 in the console.

## Decoding a Transaction​

The CLI can also be used to decode transaction bytes.

Decoding a transaction is done using the following command:

_1marsd tx decode [protobuf-byte-string]

This will decode the transaction bytes and output the transaction as JSON in the console. You can also save the transaction to a file by appending > tx.json to the above command.