Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when decoding addresses restrict to 20 bytes #56

Merged
merged 2 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/abi/abidecode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -127,7 +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])

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

Expand Down
42 changes: 41 additions & 1 deletion pkg/abi/abidecode_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -1027,3 +1027,43 @@ 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"},
{Type: "uint160"},
{Type: "uint64"},
{Type: "uint8"},
},
}

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)
assert.NoError(t, err)

address, _ := cv.Children[0].JSON()
assert.Equal(t, "\"ab0974bbed8afc5212e951c8498873319d02d025\"", string(address))

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))
}
2 changes: 1 addition & 1 deletion pkg/abi/typecomponents.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down
Loading