-
Notifications
You must be signed in to change notification settings - Fork 568
/
metadata.go
174 lines (140 loc) · 5.88 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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
connectiontypes "github.com/cosmos/ibc-go/v5/modules/core/03-connection/types"
)
const (
// EncodingProtobuf defines the protocol buffers proto3 encoding format
EncodingProtobuf = "proto3"
// 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))
}
// 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 {
var previousMetadata Metadata
if err := ModuleCdc.UnmarshalJSON([]byte(previousVersion), &previousMetadata); 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
func ValidateControllerMetadata(ctx sdk.Context, channelKeeper ChannelKeeper, connectionHops []string, metadata Metadata) error {
if !isSupportedEncoding(metadata.Encoding) {
return sdkerrors.Wrapf(ErrInvalidCodec, "unsupported encoding format %s", metadata.Encoding)
}
if !isSupportedTxType(metadata.TxType) {
return sdkerrors.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 sdkerrors.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 sdkerrors.Wrapf(ErrInvalidCodec, "unsupported encoding format %s", metadata.Encoding)
}
if !isSupportedTxType(metadata.TxType) {
return sdkerrors.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 sdkerrors.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 {
for _, enc := range getSupportedEncoding() {
if enc == encoding {
return true
}
}
return false
}
// getSupportedEncoding returns a string slice of supported encoding formats
func getSupportedEncoding() []string {
return []string{EncodingProtobuf}
}
// isSupportedTxType returns true if the provided transaction type is supported, otherwise false
func isSupportedTxType(txType string) bool {
for _, t := range getSupportedTxTypes() {
if t == txType {
return true
}
}
return false
}
// 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 sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", controllerConnectionID, metadata.ControllerConnectionId)
}
if metadata.HostConnectionId != hostConnectionID {
return sdkerrors.Wrapf(connectiontypes.ErrInvalidConnection, "expected %s, got %s", hostConnectionID, metadata.HostConnectionId)
}
return nil
}