From e47e37997cfee6fad0551df7cfe25c64ce9731cd Mon Sep 17 00:00:00 2001 From: SamMayWork Date: Wed, 10 Jan 2024 17:40:57 +0000 Subject: [PATCH 1/2] fix: when decoding addresses restrict to 20 bytes Signed-off-by: SamMayWork --- pkg/abi/abidecode.go | 12 +++++++++++- pkg/abi/abidecode_test.go | 25 ++++++++++++++++++++++++- pkg/abi/typecomponents.go | 4 ++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/pkg/abi/abidecode.go b/pkg/abi/abidecode.go index 12db5fbe..f4852e2e 100644 --- a/pkg/abi/abidecode.go +++ b/pkg/abi/abidecode.go @@ -1,4 +1,4 @@ -// Copyright © 2023 Kaleido, Inc. +// Copyright © 2024 Kaleido, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -131,6 +131,16 @@ func decodeABIUnsignedInt(ctx context.Context, desc string, block []byte, _, hea return cv, err } +func decodeABIAddress(ctx context.Context, desc string, block []byte, _, headPosition int, component *typeComponent) (cv *ComponentValue, err error) { + // Addresses are 20 bytes, but are represented as a 32 bit unsigned integer so we remove the proceeding 12 bytes and then read the rest as the address + cv = &ComponentValue{Component: component} + if headPosition+32 > len(block) { + return nil, i18n.NewError(ctx, signermsgs.MsgNotEnoughBytesABIValue, component, desc) + } + cv.Value = new(big.Int).SetBytes(block[headPosition+12 : headPosition+32]) + return cv, err +} + func intToFixed(ctx context.Context, component *typeComponent, cv *ComponentValue) (*ComponentValue, error) { if component.n == 0 { return nil, i18n.NewError(ctx, signermsgs.MsgBadABITypeComponent, component) diff --git a/pkg/abi/abidecode_test.go b/pkg/abi/abidecode_test.go index 1f419415..eb174e01 100644 --- a/pkg/abi/abidecode_test.go +++ b/pkg/abi/abidecode_test.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Kaleido, Inc. +// Copyright © 2024 Kaleido, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -1027,3 +1027,26 @@ func TestDecodeABIElementInsufficientDataTuple(t *testing.T) { _, _, err = decodeABIElement(context.Background(), "", block, 0, 0, tc.(*typeComponent).tupleChildren[0]) assert.Regexp(t, "FF22045", err) } + +func TestDecodeAddressWithNonZeroPadding(t *testing.T) { + + f := &Entry{ + Name: "approve", + Inputs: ParameterArray{ + {Type: "address"}, + {Type: "uint256"}, + }, + } + + d, err := hex.DecodeString("095ea7b3" + + "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025" + // Valid address padded with non-zero bytes + "0000000000000000000000000000000000000000000000000000000000000064") // 100 + assert.NoError(t, err) + + cv, err := f.DecodeCallData(d) + assert.NoError(t, err) + + address, _ := cv.Children[0].JSON() + assert.Equal(t, "\"ab0974bbed8afc5212e951c8498873319d02d025\"", string(address)) + assert.Equal(t, "100", cv.Children[1].Value.(*big.Int).String()) +} diff --git a/pkg/abi/typecomponents.go b/pkg/abi/typecomponents.go index 79c4dc42..b2e4441d 100644 --- a/pkg/abi/typecomponents.go +++ b/pkg/abi/typecomponents.go @@ -1,4 +1,4 @@ -// Copyright © 2023 Kaleido, Inc. +// Copyright © 2024 Kaleido, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -227,7 +227,7 @@ var ( }, jsonEncodingType: JSONEncodingTypeBytes, encodeABIData: encodeABIUnsignedInteger, - decodeABIData: decodeABIUnsignedInt, + decodeABIData: decodeABIAddress, }) ElementaryTypeBool = registerElementaryType(elementaryTypeInfo{ name: BaseTypeBool, From 711fd0f9bd4bfbe676189a25746cfe4e28ab3424 Mon Sep 17 00:00:00 2001 From: SamMayWork Date: Wed, 17 Jan 2024 15:45:06 +0000 Subject: [PATCH 2/2] fix: read bytes corresponding to the suffix number Signed-off-by: SamMayWork --- pkg/abi/abidecode.go | 12 ++---------- pkg/abi/abidecode_test.go | 25 +++++++++++++++++++++---- pkg/abi/typecomponents.go | 2 +- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/pkg/abi/abidecode.go b/pkg/abi/abidecode.go index f4852e2e..72325b26 100644 --- a/pkg/abi/abidecode.go +++ b/pkg/abi/abidecode.go @@ -127,17 +127,9 @@ func decodeABIUnsignedInt(ctx context.Context, desc string, block []byte, _, hea if headPosition+32 > len(block) { return nil, i18n.NewError(ctx, signermsgs.MsgNotEnoughBytesABIValue, component, desc) } - cv.Value = new(big.Int).SetBytes(block[headPosition : headPosition+32]) - return cv, err -} -func decodeABIAddress(ctx context.Context, desc string, block []byte, _, headPosition int, component *typeComponent) (cv *ComponentValue, err error) { - // Addresses are 20 bytes, but are represented as a 32 bit unsigned integer so we remove the proceeding 12 bytes and then read the rest as the address - cv = &ComponentValue{Component: component} - if headPosition+32 > len(block) { - return nil, i18n.NewError(ctx, signermsgs.MsgNotEnoughBytesABIValue, component, desc) - } - cv.Value = new(big.Int).SetBytes(block[headPosition+12 : headPosition+32]) + // When we're reading bytes, need to make sure we're reading the correct size number of bytes for the uint size + cv.Value = new(big.Int).SetBytes(block[headPosition+(32-(int(component.m/8))) : headPosition+32]) return cv, err } diff --git a/pkg/abi/abidecode_test.go b/pkg/abi/abidecode_test.go index eb174e01..0fe4cc2d 100644 --- a/pkg/abi/abidecode_test.go +++ b/pkg/abi/abidecode_test.go @@ -1035,12 +1035,18 @@ func TestDecodeAddressWithNonZeroPadding(t *testing.T) { Inputs: ParameterArray{ {Type: "address"}, {Type: "uint256"}, + {Type: "uint160"}, + {Type: "uint64"}, + {Type: "uint8"}, }, } - d, err := hex.DecodeString("095ea7b3" + - "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025" + // Valid address padded with non-zero bytes - "0000000000000000000000000000000000000000000000000000000000000064") // 100 + d, err := hex.DecodeString("9028b841" + + "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025" + // (address) 0xab0974bbed8afc5212e951c8498873319d02d025 + "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025" + // (uint256) 0xffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025 + "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025" + // (uint160) 0xab0974bbed8afc5212e951c8498873319d02d025 + "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025" + // ( uint64) 0x498873319d02d025 + "ffffffffffffffffffffffffab0974bbed8afc5212e951c8498873319d02d025") // ( uint8) 0x25 assert.NoError(t, err) cv, err := f.DecodeCallData(d) @@ -1048,5 +1054,16 @@ func TestDecodeAddressWithNonZeroPadding(t *testing.T) { address, _ := cv.Children[0].JSON() assert.Equal(t, "\"ab0974bbed8afc5212e951c8498873319d02d025\"", string(address)) - assert.Equal(t, "100", cv.Children[1].Value.(*big.Int).String()) + + value, _ := cv.Children[1].JSON() + assert.Equal(t, "\"115792089237316195423570985008202854513430464469730409417913252338120266010661\"", string(value)) + + value, _ = cv.Children[2].JSON() + assert.Equal(t, "\"976448297491382722293530211171951349863068913701\"", string(value)) + + value, _ = cv.Children[3].JSON() + assert.Equal(t, "\"5298611618526187557\"", string(value)) + + value, _ = cv.Children[4].JSON() + assert.Equal(t, "\"37\"", string(value)) } diff --git a/pkg/abi/typecomponents.go b/pkg/abi/typecomponents.go index b2e4441d..5ba52ce5 100644 --- a/pkg/abi/typecomponents.go +++ b/pkg/abi/typecomponents.go @@ -227,7 +227,7 @@ var ( }, jsonEncodingType: JSONEncodingTypeBytes, encodeABIData: encodeABIUnsignedInteger, - decodeABIData: decodeABIAddress, + decodeABIData: decodeABIUnsignedInt, }) ElementaryTypeBool = registerElementaryType(elementaryTypeInfo{ name: BaseTypeBool,