Skip to content

Commit

Permalink
Merge pull request #56 from SamMayWork/weird-padding
Browse files Browse the repository at this point in the history
fix: when decoding addresses restrict to 20 bytes
  • Loading branch information
peterbroadhurst authored Jan 17, 2024
2 parents 322b6b4 + 711fd0f commit ede49b9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
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
}

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

0 comments on commit ede49b9

Please sign in to comment.