|
4 | 4 | "github.com/cosmos/cosmos-sdk/codec"
|
5 | 5 | codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
6 | 6 | sdk "github.com/cosmos/cosmos-sdk/types"
|
| 7 | + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" |
7 | 8 | "github.com/cosmos/cosmos-sdk/types/msgservice"
|
| 9 | + proto "github.com/gogo/protobuf/proto" |
8 | 10 |
|
9 | 11 | "github.com/cosmos/ibc-go/v3/modules/core/exported"
|
10 | 12 | )
|
@@ -50,3 +52,35 @@ func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
|
50 | 52 | // The actual codec used for serialization should be provided to x/ibc/core/04-channel and
|
51 | 53 | // defined at the application level.
|
52 | 54 | var SubModuleCdc = codec.NewProtoCodec(codectypes.NewInterfaceRegistry())
|
| 55 | + |
| 56 | +// UnpackAcknowledgement unpacks an Any into an Acknowledgement. It returns an error if the |
| 57 | +// Any can't be unpacked into an Acknowledgement. |
| 58 | +func UnpackAcknowledgement(any *codectypes.Any) (exported.Acknowledgement, error) { |
| 59 | + if any == nil { |
| 60 | + return nil, sdkerrors.Wrap(sdkerrors.ErrUnpackAny, "protobuf Any message cannot be nil") |
| 61 | + } |
| 62 | + |
| 63 | + ack, ok := any.GetCachedValue().(exported.Acknowledgement) |
| 64 | + if !ok { |
| 65 | + return nil, sdkerrors.Wrapf(sdkerrors.ErrUnpackAny, "cannot unpack Any into Acknowledgement %T", any) |
| 66 | + } |
| 67 | + |
| 68 | + return ack, nil |
| 69 | +} |
| 70 | + |
| 71 | +// PackAcknowledgement constructs a new Any packed with the given acknowledgement value. It returns |
| 72 | +// an error if the acknowledgement can't be casted to a protobuf message or if the concrete |
| 73 | +// implemention is not registered to the protobuf codec. |
| 74 | +func PackAcknowledgement(acknowledgement exported.Acknowledgement) (*codectypes.Any, error) { |
| 75 | + msg, ok := acknowledgement.(proto.Message) |
| 76 | + if !ok { |
| 77 | + return nil, sdkerrors.Wrapf(sdkerrors.ErrPackAny, "cannot proto marshal %T", acknowledgement) |
| 78 | + } |
| 79 | + |
| 80 | + anyAcknowledgement, err := codectypes.NewAnyWithValue(msg) |
| 81 | + if err != nil { |
| 82 | + return nil, sdkerrors.Wrap(sdkerrors.ErrPackAny, err.Error()) |
| 83 | + } |
| 84 | + |
| 85 | + return anyAcknowledgement, nil |
| 86 | +} |
0 commit comments