From d9dc4b6732cd49a91c3299831f9e34ed177dbf35 Mon Sep 17 00:00:00 2001 From: xiaodino Date: Mon, 11 Mar 2024 02:55:58 -0700 Subject: [PATCH] fix(relayer): fix decodeMessageSentData (#16391) Co-authored-by: David --- packages/relayer/types.go | 31 +++++++++++++++++++++---------- packages/relayer/types_test.go | 7 +++---- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/relayer/types.go b/packages/relayer/types.go index 3166dc66ceb..8c00d8ae347 100644 --- a/packages/relayer/types.go +++ b/packages/relayer/types.go @@ -137,14 +137,20 @@ func splitByteArray(data []byte, chunkSize int) [][]byte { func decodeDataAsERC20(decodedData []byte) (CanonicalToken, *big.Int, error) { var token CanonicalERC20 + canonicalTokenDataStartingindex := int64(2) chunks := splitByteArray(decodedData, 32) - offset, err := strconv.ParseInt(common.Bytes2Hex((chunks[0])), 16, 64) + + if len(chunks) < 4 { + return token, big.NewInt(0), fmt.Errorf("data too short") + } + + offset, err := strconv.ParseInt(common.Bytes2Hex((chunks[canonicalTokenDataStartingindex])), 16, 64) if err != nil { return token, big.NewInt(0), err } - canonicalTokenData := decodedData[offset:] + canonicalTokenData := decodedData[offset+canonicalTokenDataStartingindex*32:] types := []string{"uint64", "address", "uint8", "string", "string"} values, err := decodeABI(types, canonicalTokenData) @@ -159,7 +165,7 @@ func decodeDataAsERC20(decodedData []byte) (CanonicalToken, *big.Int, error) { token.Symbol = values[3].(string) token.Name = values[4].(string) - amount, err := strconv.ParseInt(common.Bytes2Hex((chunks[3])), 16, 64) + amount, err := strconv.ParseInt(common.Bytes2Hex((chunks[canonicalTokenDataStartingindex+3])), 16, 64) if err != nil { return token, big.NewInt(0), err } @@ -170,19 +176,23 @@ func decodeDataAsERC20(decodedData []byte) (CanonicalToken, *big.Int, error) { func decodeDataAsNFT(decodedData []byte) (EventType, CanonicalToken, *big.Int, error) { var token CanonicalNFT + canonicalTokenDataStartingindex := int64(2) chunks := splitByteArray(decodedData, 32) - offset, err := strconv.ParseInt(common.Bytes2Hex((chunks[0])), 16, 64) + offset, err := strconv.ParseInt(common.Bytes2Hex((chunks[canonicalTokenDataStartingindex])), 16, 64) if err != nil || offset%32 != 0 { return EventTypeSendETH, token, big.NewInt(0), err } - canonicalTokenData := decodedData[offset:] + canonicalTokenData := decodedData[offset+canonicalTokenDataStartingindex*32:] types := []string{"uint64", "address", "string", "string"} values, err := decodeABI(types, canonicalTokenData) + slog.Info("decodeDataAsNFT after decodeABI") + fmt.Println(len(values)) + if err != nil && len(values) != 4 { return EventTypeSendETH, token, big.NewInt(0), err } @@ -197,12 +207,12 @@ func decodeDataAsNFT(decodedData []byte) (EventType, CanonicalToken, *big.Int, e return EventTypeSendERC721, token, amount, nil } else if offset == 160 { - offset, err := strconv.ParseInt(common.Bytes2Hex((chunks[4])), 16, 64) + offset, err := strconv.ParseInt(common.Bytes2Hex((chunks[canonicalTokenDataStartingindex+4])), 16, 64) if err != nil || offset%32 != 0 { return EventTypeSendETH, token, big.NewInt(0), err } - indexOffset := int64(offset / 32) + indexOffset := canonicalTokenDataStartingindex + int64(offset/32) length, err := strconv.ParseInt(common.Bytes2Hex((chunks[indexOffset])), 16, 64) @@ -257,10 +267,11 @@ func DecodeMessageData(eventData []byte, value *big.Int) (EventType, CanonicalTo onMessageInvocationFunctionSig := "7f07c947" + slog.Info("DecodeMessageData") + slog.Info(common.Bytes2Hex(eventData)) + // Check if eventData is valid - if eventData != nil && - common.BytesToHash(eventData) != ZeroHash && - len(eventData) > 3 && + if len(eventData) > 3 && common.Bytes2Hex(eventData[:4]) == onMessageInvocationFunctionSig { // Try to decode data as ERC20 canonicalToken, amount, err := decodeDataAsERC20(eventData[4:]) diff --git a/packages/relayer/types_test.go b/packages/relayer/types_test.go index dc1e5d61feb..084bfb36e67 100644 --- a/packages/relayer/types_test.go +++ b/packages/relayer/types_test.go @@ -158,7 +158,7 @@ func Test_DecodeMessageSentData(t *testing.T) { { "onMessageInvocationERC20Vault", // nolint lll - common.Hex2Bytes("7f07c94700000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000"), + common.Hex2Bytes("7f07c947000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000"), nil, EventTypeSendERC20, CanonicalERC20{ @@ -171,11 +171,10 @@ func Test_DecodeMessageSentData(t *testing.T) { big.NewInt(1), nil, }, - { "onMessageInvocationERC721Vault", // nolint lll - common.Hex2Bytes("7f07c94700000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), + common.Hex2Bytes("7f07c947000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002"), nil, EventTypeSendERC721, CanonicalNFT{ @@ -190,7 +189,7 @@ func Test_DecodeMessageSentData(t *testing.T) { { "onMessageInvocationERC1155Vault", // nolint lll - common.Hex2Bytes("7f07c94700000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004"), + common.Hex2Bytes("7f07c947000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004"), nil, EventTypeSendERC1155, CanonicalNFT{