You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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`](https://github.com/cosmos/ibc-go/blob/v7.2.0/proto/ibc/applications/interchain_accounts/v1/packet.proto#L21-L26).
11
+
12
+
```protobuf
13
+
// InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field.
14
+
message InterchainAccountPacketData {
15
+
Type type = 1;
16
+
bytes data = 2;
17
+
string memo = 3;
18
+
}
19
+
```
20
+
21
+
The `data` field must be encoded as a [`CosmosTx`](https://github.com/cosmos/ibc-go/blob/v7.2.0/proto/ibc/applications/interchain_accounts/v1/packet.proto#L28-L31).
22
+
23
+
```protobuf
24
+
// CosmosTx contains a list of sdk.Msg's. It should be used when sending transactions to an SDK host chain.
25
+
message CosmosTx {
26
+
repeated google.protobuf.Any messages = 1;
27
+
}
28
+
```
29
+
30
+
The encoding method for `CosmosTx` is determined during the channel handshake process. If the channel version [metadata's `encoding` field](https://github.com/cosmos/ibc-go/blob/v7.2.0/proto/ibc/applications/interchain_accounts/v1/metadata.proto#L22) is marked as `proto3`, then `CosmosTx` undergoes protobuf encoding. Conversely, if the field is set to `proto3json`, then [proto3 json](https://protobuf.dev/programming-guides/proto3/#json) encoding takes place, which generates a JSON representation of the protobuf message.
31
+
32
+
## Protobuf Encoding
33
+
34
+
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.
35
+
36
+
## (Protobuf) JSON Encoding
37
+
38
+
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:
39
+
40
+
```json
41
+
{
42
+
"messages": [
43
+
{
44
+
"@type": "/cosmos.bank.v1beta1.MsgSend",
45
+
"from_address": "cosmos1...",
46
+
"to_address": "cosmos1...",
47
+
"amount": [
48
+
{
49
+
"denom": "uatom",
50
+
"amount": "1000000"
51
+
}
52
+
]
53
+
}
54
+
]
55
+
}
56
+
```
57
+
58
+
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.
59
+
60
+
:::warning
61
+
When utilizing proto3 JSON encoding, we have extra validations to ensure the integrity of the encoded data. Specifically, after decoding the `CosmosTx` from JSON, we re-encode it back to JSON and compare it with the original input. If non-formatting discrepancies arise, an error is returned. This validation step ensures that the JSON representation remains consistent throughout the encoding and decoding processes. This additional check means that all optional fields must be explicitly set in the JSON input, all enums and integers must be represented as strings.
0 commit comments