Skip to main content
Version: v9.0.x

Transaction Encoding

When orchestrating an interchain account transaction, which comprises multiple sdk.Msg objects represented as Any types, the transactions must be encoded as bytes within InterchainAccountPacketData.

// InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field.
message InterchainAccountPacketData {
Type type = 1;
bytes data = 2;
string memo = 3;
}

The data field must be encoded as a CosmosTx.

// CosmosTx contains a list of sdk.Msg's. It should be used when sending transactions to an SDK host chain.
message CosmosTx {
repeated google.protobuf.Any messages = 1;
}

The encoding method for CosmosTx is determined during the channel handshake process. If the channel version metadata's encoding field is marked as proto3, then CosmosTx undergoes protobuf encoding. Conversely, if the field is set to proto3json, then proto3 json encoding takes place, which generates a JSON representation of the protobuf message.

Protobuf Encoding

Protobuf encoding serves as the standard encoding process for CosmosTx. This occurs if the channel handshake initiates with an empty channel version metadata or if the encoding field explicitly denotes proto3. In Golang, the protobuf encoding procedure utilizes the proto.Marshal function. Every protobuf autogenerated Golang type comes equipped with a Marshal method that can be employed to encode the message.

(Protobuf) JSON Encoding

The proto3 JSON encoding presents an alternative encoding technique for CosmosTx. It is selected if the channel handshake begins with the channel version metadata encoding field labeled as proto3json. In Golang, the Proto3 canonical encoding in JSON is implemented by the "github.com/cosmos/gogoproto/jsonpb" package. Within Cosmos SDK, the ProtoCodec structure implements the JSONCodec interface, leveraging the jsonpb package. This method generates a JSON format as follows:

{
"messages": [
{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": "cosmos1...",
"to_address": "cosmos1...",
"amount": [
{
"denom": "uatom",
"amount": "1000000"
}
]
}
]
}

Here, the "messages" array is populated with transactions. Each transaction is represented as a JSON object with the @type field denoting the transaction type and the remaining fields representing the transaction's attributes.