Skip to content

Commit

Permalink
fix: read bytes corresponding to the suffix number
Browse files Browse the repository at this point in the history
Signed-off-by: SamMayWork <sam.may@kaleido.io>
  • Loading branch information
SamMayWork committed Jan 17, 2024
1 parent e47e379 commit 711fd0f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
12 changes: 2 additions & 10 deletions pkg/abi/abidecode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
25 changes: 21 additions & 4 deletions pkg/abi/abidecode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,18 +1035,35 @@ 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)
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())

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
Expand Up @@ -227,7 +227,7 @@ var (
},
jsonEncodingType: JSONEncodingTypeBytes,
encodeABIData: encodeABIUnsignedInteger,
decodeABIData: decodeABIAddress,
decodeABIData: decodeABIUnsignedInt,
})
ElementaryTypeBool = registerElementaryType(elementaryTypeInfo{
name: BaseTypeBool,
Expand Down

0 comments on commit 711fd0f

Please sign in to comment.