Skip to content

Commit

Permalink
fix(relayer): fix decodeMessageSentData (#16391)
Browse files Browse the repository at this point in the history
Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
xiaodino and davidtaikocha authored Mar 11, 2024
1 parent 627bf01 commit d9dc4b6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
31 changes: 21 additions & 10 deletions packages/relayer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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)

Expand Down Expand Up @@ -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:])
Expand Down
7 changes: 3 additions & 4 deletions packages/relayer/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func Test_DecodeMessageSentData(t *testing.T) {
{
"onMessageInvocationERC20Vault",
// nolint lll
common.Hex2Bytes("7f07c94700000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000"),
common.Hex2Bytes("7f07c947000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000"),
nil,
EventTypeSendERC20,
CanonicalERC20{
Expand All @@ -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{
Expand All @@ -190,7 +189,7 @@ func Test_DecodeMessageSentData(t *testing.T) {
{
"onMessageInvocationERC1155Vault",
// nolint lll
common.Hex2Bytes("7f07c94700000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004"),
common.Hex2Bytes("7f07c947000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000123000000000000000000000000000000000000000000000000000000000000012400000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000028c590000000000000000000000006e7cfe3fd749f6e1c53bc1065fc702eb6c22f600000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000004544553540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000095465737445524332300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004"),
nil,
EventTypeSendERC1155,
CanonicalNFT{
Expand Down

0 comments on commit d9dc4b6

Please sign in to comment.