当编排一笔跨链账户交易时,如果该交易由多个以 Any 类型表示的 sdk.Msg 对象组成,则这些交易必须在 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;
}
data 字段必须编码为 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;
}
CosmosTx 的编码方式在通道握手过程中确定。如果通道版本元数据的 encoding 字段标记为 proto3,则 CosmosTx 会进行 protobuf 编码。相反,如果该字段设置为 proto3json,则会进行 proto3 json 编码,从而生成该 protobuf 消息的 JSON 表示。

Protobuf 编码

Protobuf 编码是 CosmosTx 的标准编码流程。如果通道握手以空的通道版本元数据开始,或者 encoding 字段显式指定为 proto3,就会采用这种方式。在 Golang 中,protobuf 编码过程使用 proto.Marshal 函数。每个 protobuf 自动生成的 Golang 类型都自带一个 Marshal 方法,可用于对消息进行编码。

(Protobuf)JSON 编码

proto3 JSON 编码为 CosmosTx 提供了另一种编码方式。如果通道握手以通道版本元数据中 encoding 字段标记为 proto3json 开始,则会选择这种方式。在 Golang 中,JSON 形式的 Proto3 规范编码由 "github.com/cosmos/gogoproto/jsonpb" 包实现。在 Cosmos SDK 中,ProtoCodec 结构实现了 JSONCodec 接口,并使用 jsonpb 包。该方法会生成如下所示的 JSON 格式:
{
  "messages": [
    {
      "@type": "/cosmos.bank.v1beta1.MsgSend",
      "from_address": "cosmos1...",
      "to_address": "cosmos1...",
      "amount": [
        {
          "denom": "uatom",
          "amount": "1000000"
        }
      ]
    }
  ]
}
这里,"messages" 数组用于填充交易。每笔交易都表示为一个 JSON 对象,其中 @type 字段表示交易类型,其余字段表示交易的属性。
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.