-
Notifications
You must be signed in to change notification settings - Fork 563
/
metadata.go
177 lines (142 loc) · 6.3 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package types
import (
"slices"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
connectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
)
const (
// EncodingProtobuf defines the protocol buffers proto3 encoding format
EncodingProtobuf = "proto3"
// EncodingProto3JSON defines the proto3 JSON encoding format
EncodingProto3JSON = "proto3json"
// TxTypeSDKMultiMsg defines the multi message transaction type supported by the Cosmos SDK
TxTypeSDKMultiMsg = "sdk_multi_msg"
)
// NewMetadata creates and returns a new ICS27 Metadata instance
func NewMetadata(version, controllerConnectionID, hostConnectionID, accAddress, encoding, txType string) Metadata {
return Metadata{
Version: version,
ControllerConnectionId: controllerConnectionID,
HostConnectionId: hostConnectionID,
Address: accAddress,
Encoding: encoding,
TxType: txType,
}
}
// NewDefaultMetadata creates and returns a new ICS27 Metadata instance containing the default ICS27 Metadata values
// with the provided controller and host connection identifiers
func NewDefaultMetadata(controllerConnectionID, hostConnectionID string) Metadata {
metadata := Metadata{
ControllerConnectionId: controllerConnectionID,
HostConnectionId: hostConnectionID,
Encoding: EncodingProtobuf,
TxType: TxTypeSDKMultiMsg,
Version: Version,
}
return metadata
}
// NewDefaultMetadataString creates and returns a new JSON encoded version string containing the default ICS27 Metadata values
// with the provided controller and host connection identifiers
func NewDefaultMetadataString(controllerConnectionID, hostConnectionID string) string {
metadata := NewDefaultMetadata(controllerConnectionID, hostConnectionID)
return string(ModuleCdc.MustMarshalJSON(&metadata))
}
// MetadataFromVersion parses Metadata from a json encoded version string.
func MetadataFromVersion(versionString string) (Metadata, error) {
var metadata Metadata
if err := ModuleCdc.UnmarshalJSON([]byte(versionString), &metadata); err != nil {
return Metadata{}, errorsmod.Wrapf(ErrUnknownDataType, "cannot unmarshal ICS-27 interchain accounts metadata")
}
return metadata, nil
}
// IsPreviousMetadataEqual compares a metadata to a previous version string set in a channel struct.
// It ensures all fields are equal except the Address string
func IsPreviousMetadataEqual(previousVersion string, metadata Metadata) bool {
previousMetadata, err := MetadataFromVersion(previousVersion)
if err != nil {
return false
}
return (previousMetadata.Version == metadata.Version &&
previousMetadata.ControllerConnectionId == metadata.ControllerConnectionId &&
previousMetadata.HostConnectionId == metadata.HostConnectionId &&
previousMetadata.Encoding == metadata.Encoding &&
previousMetadata.TxType == metadata.TxType)
}
// ValidateControllerMetadata performs validation of the provided ICS27 controller metadata parameters as well
// as the connection params against the provided metadata
func ValidateControllerMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connectionHops []string, metadata Metadata) error {
if !isSupportedEncoding(metadata.Encoding) {
return errorsmod.Wrapf(ErrInvalidCodec, "unsupported encoding format %s", metadata.Encoding)
}
if !isSupportedTxType(metadata.TxType) {
return errorsmod.Wrapf(ErrUnknownDataType, "unsupported transaction type %s", metadata.TxType)
}
connection, err := channelKeeper.GetConnection(ctx, connectionHops[0])
if err != nil {
return err
}
if err := validateConnectionParams(metadata, connectionHops[0], connection.GetCounterparty().GetConnectionID()); err != nil {
return err
}
if metadata.Address != "" {
if err := ValidateAccountAddress(metadata.Address); err != nil {
return err
}
}
if metadata.Version != Version {
return errorsmod.Wrapf(ErrInvalidVersion, "expected %s, got %s", Version, metadata.Version)
}
return nil
}
// ValidateHostMetadata performs validation of the provided ICS27 host metadata parameters
func ValidateHostMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connectionHops []string, metadata Metadata) error {
if !isSupportedEncoding(metadata.Encoding) {
return errorsmod.Wrapf(ErrInvalidCodec, "unsupported encoding format %s", metadata.Encoding)
}
if !isSupportedTxType(metadata.TxType) {
return errorsmod.Wrapf(ErrUnknownDataType, "unsupported transaction type %s", metadata.TxType)
}
connection, err := channelKeeper.GetConnection(ctx, connectionHops[0])
if err != nil {
return err
}
if err := validateConnectionParams(metadata, connection.GetCounterparty().GetConnectionID(), connectionHops[0]); err != nil {
return err
}
if metadata.Address != "" {
if err := ValidateAccountAddress(metadata.Address); err != nil {
return err
}
}
if metadata.Version != Version {
return errorsmod.Wrapf(ErrInvalidVersion, "expected %s, got %s", Version, metadata.Version)
}
return nil
}
// isSupportedEncoding returns true if the provided encoding is supported, otherwise false
func isSupportedEncoding(encoding string) bool {
return slices.Contains(getSupportedEncoding(), encoding)
}
// getSupportedEncoding returns a string slice of supported encoding formats
func getSupportedEncoding() []string {
return []string{EncodingProtobuf, EncodingProto3JSON}
}
// isSupportedTxType returns true if the provided transaction type is supported, otherwise false
func isSupportedTxType(txType string) bool {
return slices.Contains(getSupportedTxTypes(), txType)
}
// getSupportedTxTypes returns a string slice of supported transaction types
func getSupportedTxTypes() []string {
return []string{TxTypeSDKMultiMsg}
}
// validateConnectionParams compares the given the controller and host connection IDs to those set in the provided ICS27 Metadata
func validateConnectionParams(metadata Metadata, controllerConnectionID, hostConnectionID string) error {
if metadata.ControllerConnectionId != controllerConnectionID {
return errorsmod.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", controllerConnectionID, metadata.ControllerConnectionId)
}
if metadata.HostConnectionId != hostConnectionID {
return errorsmod.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", hostConnectionID, metadata.HostConnectionId)
}
return nil
}