From 77b7801b0a930edba2565acde4488d21352788c0 Mon Sep 17 00:00:00 2001 From: beer-1 <147697694+beer-1@users.noreply.github.com> Date: Mon, 12 Aug 2024 14:20:40 +0900 Subject: [PATCH] fix to encode intermediate sender changed packet with counterparty port consideration --- x/ibc-hooks/move-hooks/receive.go | 13 ++----------- x/ibc-hooks/move-hooks/util.go | 4 ++-- x/ibc/nft-transfer/types/packet.go | 8 ++++---- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/x/ibc-hooks/move-hooks/receive.go b/x/ibc-hooks/move-hooks/receive.go index c9362e22..10966fcf 100644 --- a/x/ibc-hooks/move-hooks/receive.go +++ b/x/ibc-hooks/move-hooks/receive.go @@ -1,7 +1,6 @@ package move_hooks import ( - "encoding/json" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -51,11 +50,7 @@ func (h MoveHooks) onRecvIcs20Packet( // // If that succeeds, we make the contract call data.Receiver = intermediateSender - bz, err := json.Marshal(data) - if err != nil { - return newEmitErrorAcknowledgement(err) - } - packet.Data = bz + packet.Data = data.GetBytes() ack := im.App.OnRecvPacket(ctx, packet, relayer) if !ack.Success() { @@ -107,11 +102,7 @@ func (h MoveHooks) onRecvIcs721Packet( // // If that succeeds, we make the contract call data.Receiver = intermediateSender - bz, err := json.Marshal(data) - if err != nil { - return newEmitErrorAcknowledgement(err) - } - packet.Data = bz + packet.Data = data.GetBytes(packet.GetDestChannel()) ack := im.App.OnRecvPacket(ctx, packet, relayer) if !ack.Success() { diff --git a/x/ibc-hooks/move-hooks/util.go b/x/ibc-hooks/move-hooks/util.go index 240cfd67..28185e20 100644 --- a/x/ibc-hooks/move-hooks/util.go +++ b/x/ibc-hooks/move-hooks/util.go @@ -38,8 +38,8 @@ func isIcs20Packet(packetData []byte) (isIcs20 bool, ics20data transfertypes.Fun return true, data } -func isIcs721Packet(packetData []byte, counterPartyPort string) (isIcs721 bool, ics721data nfttransfertypes.NonFungibleTokenPacketData) { - if data, err := nfttransfertypes.DecodePacketData(packetData, counterPartyPort); err != nil { +func isIcs721Packet(packetData []byte, counterpartyPort string) (isIcs721 bool, ics721data nfttransfertypes.NonFungibleTokenPacketData) { + if data, err := nfttransfertypes.DecodePacketData(packetData, counterpartyPort); err != nil { return false, data } else { return true, data diff --git a/x/ibc/nft-transfer/types/packet.go b/x/ibc/nft-transfer/types/packet.go index aca9c57e..236856e1 100644 --- a/x/ibc/nft-transfer/types/packet.go +++ b/x/ibc/nft-transfer/types/packet.go @@ -71,10 +71,10 @@ func (nftpd NonFungibleTokenPacketData) ValidateBasic() error { } // GetBytes is a helper for serializing -func (nftpd NonFungibleTokenPacketData) GetBytes(counterPartyPort string) []byte { +func (nftpd NonFungibleTokenPacketData) GetBytes(counterpartyPort string) []byte { var bz []byte var err error - if isWasmPacket(counterPartyPort) { + if isWasmPacket(counterpartyPort) { bz, err = json.Marshal(nftpd.ToWasmData()) } else { bz, err = json.Marshal(nftpd) @@ -87,11 +87,11 @@ func (nftpd NonFungibleTokenPacketData) GetBytes(counterPartyPort string) []byte } // decode packet data to NonFungibleTokenPacketData -func DecodePacketData(packetData []byte, counterPartyPort string) (NonFungibleTokenPacketData, error) { +func DecodePacketData(packetData []byte, counterpartyPort string) (NonFungibleTokenPacketData, error) { decoder := json.NewDecoder(strings.NewReader(string(packetData))) decoder.DisallowUnknownFields() - if isWasmPacket(counterPartyPort) { + if isWasmPacket(counterpartyPort) { var wasmData NonFungibleTokenPacketDataWasm if err := decoder.Decode(&wasmData); err != nil { return NonFungibleTokenPacketData{}, sdkerrors.ErrInvalidRequest.Wrap(err.Error())