2018-02-14 15:49:11 +03:00
|
|
|
// Copyright 2017 The go-ethereum Authors
|
2017-06-27 11:05:33 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package abi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2023-05-25 15:25:58 +03:00
|
|
|
"errors"
|
2017-06-27 11:05:33 +03:00
|
|
|
"fmt"
|
2023-02-07 16:32:27 +03:00
|
|
|
"math"
|
2017-06-27 11:05:33 +03:00
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2018-09-04 18:53:28 +03:00
|
|
|
var (
|
2020-09-20 11:43:57 +03:00
|
|
|
// MaxUint256 is the maximum value that can be represented by a uint256.
|
2020-04-01 19:46:53 +03:00
|
|
|
MaxUint256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 256), common.Big1)
|
2020-09-20 11:43:57 +03:00
|
|
|
// MaxInt256 is the maximum value that can be represented by a int256.
|
2020-04-01 19:46:53 +03:00
|
|
|
MaxInt256 = new(big.Int).Sub(new(big.Int).Lsh(common.Big1, 255), common.Big1)
|
2018-09-04 18:53:28 +03:00
|
|
|
)
|
|
|
|
|
2020-09-20 11:43:57 +03:00
|
|
|
// ReadInteger reads the integer based on its kind and returns the appropriate value.
|
2023-02-07 16:32:27 +03:00
|
|
|
func ReadInteger(typ Type, b []byte) (interface{}, error) {
|
|
|
|
ret := new(big.Int).SetBytes(b)
|
|
|
|
|
2020-05-05 17:30:43 +03:00
|
|
|
if typ.T == UintTy {
|
2023-02-07 16:32:27 +03:00
|
|
|
u64, isu64 := ret.Uint64(), ret.IsUint64()
|
2020-05-05 17:30:43 +03:00
|
|
|
switch typ.Size {
|
|
|
|
case 8:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isu64 || u64 > math.MaxUint8 {
|
|
|
|
return nil, errBadUint8
|
|
|
|
}
|
|
|
|
return byte(u64), nil
|
2020-05-05 17:30:43 +03:00
|
|
|
case 16:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isu64 || u64 > math.MaxUint16 {
|
|
|
|
return nil, errBadUint16
|
|
|
|
}
|
|
|
|
return uint16(u64), nil
|
2020-05-05 17:30:43 +03:00
|
|
|
case 32:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isu64 || u64 > math.MaxUint32 {
|
|
|
|
return nil, errBadUint32
|
|
|
|
}
|
|
|
|
return uint32(u64), nil
|
2020-05-05 17:30:43 +03:00
|
|
|
case 64:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isu64 {
|
|
|
|
return nil, errBadUint64
|
|
|
|
}
|
|
|
|
return u64, nil
|
2020-05-05 17:30:43 +03:00
|
|
|
default:
|
|
|
|
// the only case left for unsigned integer is uint256.
|
2023-02-07 16:32:27 +03:00
|
|
|
return ret, nil
|
2020-05-05 17:30:43 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-07 16:32:27 +03:00
|
|
|
|
|
|
|
// big.SetBytes can't tell if a number is negative or positive in itself.
|
|
|
|
// On EVM, if the returned number > max int256, it is negative.
|
|
|
|
// A number is > max int256 if the bit at position 255 is set.
|
|
|
|
if ret.Bit(255) == 1 {
|
|
|
|
ret.Add(MaxUint256, new(big.Int).Neg(ret))
|
|
|
|
ret.Add(ret, common.Big1)
|
|
|
|
ret.Neg(ret)
|
|
|
|
}
|
|
|
|
i64, isi64 := ret.Int64(), ret.IsInt64()
|
2020-05-05 17:30:43 +03:00
|
|
|
switch typ.Size {
|
|
|
|
case 8:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isi64 || i64 < math.MinInt8 || i64 > math.MaxInt8 {
|
|
|
|
return nil, errBadInt8
|
|
|
|
}
|
|
|
|
return int8(i64), nil
|
2020-05-05 17:30:43 +03:00
|
|
|
case 16:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isi64 || i64 < math.MinInt16 || i64 > math.MaxInt16 {
|
|
|
|
return nil, errBadInt16
|
|
|
|
}
|
|
|
|
return int16(i64), nil
|
2020-05-05 17:30:43 +03:00
|
|
|
case 32:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isi64 || i64 < math.MinInt32 || i64 > math.MaxInt32 {
|
|
|
|
return nil, errBadInt32
|
|
|
|
}
|
|
|
|
return int32(i64), nil
|
2020-05-05 17:30:43 +03:00
|
|
|
case 64:
|
2023-02-07 16:32:27 +03:00
|
|
|
if !isi64 {
|
|
|
|
return nil, errBadInt64
|
|
|
|
}
|
|
|
|
return i64, nil
|
2017-06-27 11:05:33 +03:00
|
|
|
default:
|
2020-05-05 17:30:43 +03:00
|
|
|
// the only case left for integer is int256
|
2023-02-07 16:32:27 +03:00
|
|
|
|
|
|
|
return ret, nil
|
2017-06-27 11:05:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:43:57 +03:00
|
|
|
// readBool reads a bool.
|
2017-06-27 11:05:33 +03:00
|
|
|
func readBool(word []byte) (bool, error) {
|
2017-10-17 14:07:08 +03:00
|
|
|
for _, b := range word[:31] {
|
|
|
|
if b != 0 {
|
2017-06-27 11:05:33 +03:00
|
|
|
return false, errBadBool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch word[31] {
|
|
|
|
case 0:
|
|
|
|
return false, nil
|
|
|
|
case 1:
|
|
|
|
return true, nil
|
|
|
|
default:
|
|
|
|
return false, errBadBool
|
|
|
|
}
|
2017-10-17 14:07:08 +03:00
|
|
|
}
|
2017-06-27 11:05:33 +03:00
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// A function type is simply the address with the function selection signature at the end.
|
2020-09-20 11:43:57 +03:00
|
|
|
//
|
|
|
|
// readFunctionType enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
|
2017-10-17 14:07:08 +03:00
|
|
|
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
|
|
|
|
if t.T != FunctionTy {
|
2023-05-25 15:25:58 +03:00
|
|
|
return [24]byte{}, errors.New("abi: invalid type in call to make function type byte array")
|
2017-10-17 14:07:08 +03:00
|
|
|
}
|
|
|
|
if garbage := binary.BigEndian.Uint64(word[24:32]); garbage != 0 {
|
|
|
|
err = fmt.Errorf("abi: got improperly encoded function type, got %v", word)
|
|
|
|
} else {
|
|
|
|
copy(funcTy[:], word[0:24])
|
|
|
|
}
|
|
|
|
return
|
2017-06-27 11:05:33 +03:00
|
|
|
}
|
|
|
|
|
2020-09-20 11:43:57 +03:00
|
|
|
// ReadFixedBytes uses reflection to create a fixed array to be read from.
|
2019-12-18 13:16:07 +03:00
|
|
|
func ReadFixedBytes(t Type, word []byte) (interface{}, error) {
|
2017-10-17 14:07:08 +03:00
|
|
|
if t.T != FixedBytesTy {
|
2023-05-25 15:25:58 +03:00
|
|
|
return nil, errors.New("abi: invalid type in call to make fixed byte array")
|
2017-06-27 11:05:33 +03:00
|
|
|
}
|
2017-10-17 14:07:08 +03:00
|
|
|
// convert
|
2020-06-09 11:26:56 +03:00
|
|
|
array := reflect.New(t.GetType()).Elem()
|
2017-06-27 11:05:33 +03:00
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
reflect.Copy(array, reflect.ValueOf(word[0:t.Size]))
|
|
|
|
return array.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:43:57 +03:00
|
|
|
// forEachUnpack iteratively unpack elements.
|
2017-10-17 14:07:08 +03:00
|
|
|
func forEachUnpack(t Type, output []byte, start, size int) (interface{}, error) {
|
2018-01-13 18:03:24 +03:00
|
|
|
if size < 0 {
|
|
|
|
return nil, fmt.Errorf("cannot marshal input to array, size is negative (%d)", size)
|
|
|
|
}
|
2017-10-17 14:07:08 +03:00
|
|
|
if start+32*size > len(output) {
|
2022-09-13 15:02:34 +03:00
|
|
|
return nil, fmt.Errorf("abi: cannot marshal into go array: offset %d would go over slice boundary (len=%d)", len(output), start+32*size)
|
2017-06-27 11:05:33 +03:00
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// this value will become our slice or our array, depending on the type
|
|
|
|
var refSlice reflect.Value
|
|
|
|
|
2023-08-23 16:53:38 +03:00
|
|
|
switch t.T {
|
|
|
|
case SliceTy:
|
2017-10-17 14:07:08 +03:00
|
|
|
// declare our slice
|
2020-06-09 11:26:56 +03:00
|
|
|
refSlice = reflect.MakeSlice(t.GetType(), size, size)
|
2023-08-23 16:53:38 +03:00
|
|
|
case ArrayTy:
|
2017-10-17 14:07:08 +03:00
|
|
|
// declare our array
|
2020-06-09 11:26:56 +03:00
|
|
|
refSlice = reflect.New(t.GetType()).Elem()
|
2023-08-23 16:53:38 +03:00
|
|
|
default:
|
2023-05-25 15:25:58 +03:00
|
|
|
return nil, errors.New("abi: invalid type in array/slice unpacking stage")
|
2017-10-17 14:07:08 +03:00
|
|
|
}
|
|
|
|
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-05 01:24:17 +03:00
|
|
|
// Arrays have packed elements, resulting in longer unpack steps.
|
|
|
|
// Slices have just 32 bytes per element (pointing to the contents).
|
2019-01-10 11:59:37 +03:00
|
|
|
elemSize := getTypeSize(*t.Elem)
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-05 01:24:17 +03:00
|
|
|
|
|
|
|
for i, j := start, 0; j < size; i, j = i+elemSize, j+1 {
|
2020-05-12 13:21:40 +03:00
|
|
|
inter, err := toGoType(i, *t.Elem, output)
|
2017-10-17 14:07:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-06-27 11:05:33 +03:00
|
|
|
}
|
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676)
* accounts/abi/bind: support for multi-dim arrays
Also:
- reduce usage of regexes a bit.
- fix minor Java syntax problems
Fixes #15648
* accounts/abi/bind: Add some more documentation
* accounts/abi/bind: Improve code readability
* accounts/abi: bugfix for unpacking nested arrays
The code previously assumed the arrays/slices were always 1 level
deep. While the packing supports nested arrays (!!!).
The current code for unpacking doesn't return the "consumed" length, so
this fix had to work around that by calculating it (i.e. packing and
getting resulting length) after the unpacking of the array element.
It's far from ideal, but unpacking behaviour is fixed now.
* accounts/abi: Fix unpacking of nested arrays
Removed the temporary workaround of packing to calculate size, which was
incorrect for slice-like types anyway.
Full size of nested arrays is used now.
* accounts/abi: deeply nested array unpack test
Test unpacking of an array nested more than one level.
* accounts/abi: Add deeply nested array pack test
Same as the deep nested array unpack test, but the other way around.
* accounts/abi/bind: deeply nested arrays bind test
Test the usage of bindings that were generated
for methods with multi-dimensional (and not
just a single extra dimension, like foo[2][3])
array arguments and returns.
edit: trigger rebuild, CI failed to fetch linter module.
* accounts/abi/bind: improve array binding
wrapArray uses a regex now, and arrayBindingJava is improved.
* accounts/abi: Improve naming of element size func
The full step size for unpacking an array
is now retrieved with "getFullElemSize".
* accounts/abi: support nested nested array args
Previously, the code only considered the outer-size of the array,
ignoring the size of the contents. This was fine for most types,
but nested arrays are packed directly into it, and count towards
the total size. This resulted in arguments following a nested
array to replicate some of the binary contents of the array.
The fix: for arrays, calculate their complete contents size:
count the arg.Type.Elem.Size when Elem is an Array, and
repeat when their child is an array too, etc.
The count is the number of 32 byte elements, similar to how it
previously counted, but nested.
* accounts/abi: Test deep nested arr multi-arguments
Arguments with a deeply nested array should not cause the next arguments
to be read from the wrong position.
2018-03-05 01:24:17 +03:00
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// append the item to our reflect slice
|
|
|
|
refSlice.Index(j).Set(reflect.ValueOf(inter))
|
|
|
|
}
|
2017-06-27 11:05:33 +03:00
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// return the interface
|
|
|
|
return refSlice.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2019-01-10 11:59:37 +03:00
|
|
|
func forTupleUnpack(t Type, output []byte) (interface{}, error) {
|
2020-06-09 11:26:56 +03:00
|
|
|
retval := reflect.New(t.GetType()).Elem()
|
2019-01-10 11:59:37 +03:00
|
|
|
virtualArgs := 0
|
|
|
|
for index, elem := range t.TupleElems {
|
2020-05-12 13:21:40 +03:00
|
|
|
marshalledValue, err := toGoType((index+virtualArgs)*32, *elem, output)
|
2022-09-29 11:47:12 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-10 11:59:37 +03:00
|
|
|
if elem.T == ArrayTy && !isDynamicType(*elem) {
|
|
|
|
// If we have a static array, like [3]uint256, these are coded as
|
|
|
|
// just like uint256,uint256,uint256.
|
|
|
|
// This means that we need to add two 'virtual' arguments when
|
|
|
|
// we count the index from now on.
|
|
|
|
//
|
|
|
|
// Array values nested multiple levels deep are also encoded inline:
|
|
|
|
// [2][3]uint256: uint256,uint256,uint256,uint256,uint256,uint256
|
|
|
|
//
|
|
|
|
// Calculate the full array size to get the correct offset for the next argument.
|
|
|
|
// Decrement it by 1, as the normal index increment is still applied.
|
|
|
|
virtualArgs += getTypeSize(*elem)/32 - 1
|
|
|
|
} else if elem.T == TupleTy && !isDynamicType(*elem) {
|
|
|
|
// If we have a static tuple, like (uint256, bool, uint256), these are
|
|
|
|
// coded as just like uint256,bool,uint256
|
|
|
|
virtualArgs += getTypeSize(*elem)/32 - 1
|
|
|
|
}
|
|
|
|
retval.Field(index).Set(reflect.ValueOf(marshalledValue))
|
|
|
|
}
|
|
|
|
return retval.Interface(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 13:21:40 +03:00
|
|
|
// toGoType parses the output bytes and recursively assigns the value of these bytes
|
2017-10-17 14:07:08 +03:00
|
|
|
// into a go type with accordance with the ABI spec.
|
2020-05-12 13:21:40 +03:00
|
|
|
func toGoType(index int, t Type, output []byte) (interface{}, error) {
|
2017-10-17 14:07:08 +03:00
|
|
|
if index+32 > len(output) {
|
|
|
|
return nil, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %d require %d", len(output), index+32)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-01-10 11:59:37 +03:00
|
|
|
returnOutput []byte
|
|
|
|
begin, length int
|
|
|
|
err error
|
2017-10-17 14:07:08 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// if we require a length prefix, find the beginning word and size returned.
|
|
|
|
if t.requiresLengthPrefix() {
|
2019-01-10 11:59:37 +03:00
|
|
|
begin, length, err = lengthPrefixPointsTo(index, output)
|
2017-10-17 14:07:08 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-27 11:05:33 +03:00
|
|
|
returnOutput = output[index : index+32]
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
switch t.T {
|
2019-01-10 11:59:37 +03:00
|
|
|
case TupleTy:
|
|
|
|
if isDynamicType(t) {
|
|
|
|
begin, err := tuplePointsTo(index, output)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return forTupleUnpack(t, output[begin:])
|
2018-12-28 10:43:55 +03:00
|
|
|
}
|
2020-05-12 13:21:40 +03:00
|
|
|
return forTupleUnpack(t, output[index:])
|
2019-01-10 11:59:37 +03:00
|
|
|
case SliceTy:
|
|
|
|
return forEachUnpack(t, output[begin:], 0, length)
|
2017-10-17 14:07:08 +03:00
|
|
|
case ArrayTy:
|
2019-01-10 11:59:37 +03:00
|
|
|
if isDynamicType(*t.Elem) {
|
2020-09-16 14:15:22 +03:00
|
|
|
offset := binary.BigEndian.Uint64(returnOutput[len(returnOutput)-8:])
|
|
|
|
if offset > uint64(len(output)) {
|
|
|
|
return nil, fmt.Errorf("abi: toGoType offset greater than output length: offset: %d, len(output): %d", offset, len(output))
|
|
|
|
}
|
2018-12-28 10:43:55 +03:00
|
|
|
return forEachUnpack(t, output[offset:], 0, t.Size)
|
|
|
|
}
|
2019-01-10 11:59:37 +03:00
|
|
|
return forEachUnpack(t, output[index:], 0, t.Size)
|
2017-10-17 14:07:08 +03:00
|
|
|
case StringTy: // variable arrays are written at the end of the return bytes
|
2019-01-10 11:59:37 +03:00
|
|
|
return string(output[begin : begin+length]), nil
|
2017-06-27 11:05:33 +03:00
|
|
|
case IntTy, UintTy:
|
2023-02-07 16:32:27 +03:00
|
|
|
return ReadInteger(t, returnOutput)
|
2017-06-27 11:05:33 +03:00
|
|
|
case BoolTy:
|
|
|
|
return readBool(returnOutput)
|
|
|
|
case AddressTy:
|
|
|
|
return common.BytesToAddress(returnOutput), nil
|
|
|
|
case HashTy:
|
|
|
|
return common.BytesToHash(returnOutput), nil
|
2017-10-17 14:07:08 +03:00
|
|
|
case BytesTy:
|
2019-01-10 11:59:37 +03:00
|
|
|
return output[begin : begin+length], nil
|
2017-10-17 14:07:08 +03:00
|
|
|
case FixedBytesTy:
|
2019-12-18 13:16:07 +03:00
|
|
|
return ReadFixedBytes(t, returnOutput)
|
2017-10-17 14:07:08 +03:00
|
|
|
case FunctionTy:
|
|
|
|
return readFunctionType(t, returnOutput)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("abi: unknown type %v", t.T)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 11:43:57 +03:00
|
|
|
// lengthPrefixPointsTo interprets a 32 byte slice as an offset and then determines which indices to look to decode the type.
|
2017-10-17 14:07:08 +03:00
|
|
|
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
|
2022-06-14 15:09:48 +03:00
|
|
|
bigOffsetEnd := new(big.Int).SetBytes(output[index : index+32])
|
2018-02-02 16:03:58 +03:00
|
|
|
bigOffsetEnd.Add(bigOffsetEnd, common.Big32)
|
|
|
|
outputLength := big.NewInt(int64(len(output)))
|
|
|
|
|
|
|
|
if bigOffsetEnd.Cmp(outputLength) > 0 {
|
|
|
|
return 0, 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", bigOffsetEnd, outputLength)
|
2018-01-13 18:03:24 +03:00
|
|
|
}
|
2018-02-02 16:03:58 +03:00
|
|
|
|
|
|
|
if bigOffsetEnd.BitLen() > 63 {
|
|
|
|
return 0, 0, fmt.Errorf("abi offset larger than int64: %v", bigOffsetEnd)
|
2017-10-17 14:07:08 +03:00
|
|
|
}
|
2018-02-02 16:03:58 +03:00
|
|
|
|
|
|
|
offsetEnd := int(bigOffsetEnd.Uint64())
|
2022-06-14 15:09:48 +03:00
|
|
|
lengthBig := new(big.Int).SetBytes(output[offsetEnd-32 : offsetEnd])
|
2018-02-02 16:03:58 +03:00
|
|
|
|
2022-06-14 15:09:48 +03:00
|
|
|
totalSize := new(big.Int).Add(bigOffsetEnd, lengthBig)
|
2018-02-02 16:03:58 +03:00
|
|
|
if totalSize.BitLen() > 63 {
|
2019-04-01 16:42:59 +03:00
|
|
|
return 0, 0, fmt.Errorf("abi: length larger than int64: %v", totalSize)
|
2018-01-13 18:03:24 +03:00
|
|
|
}
|
2018-02-02 16:03:58 +03:00
|
|
|
|
|
|
|
if totalSize.Cmp(outputLength) > 0 {
|
|
|
|
return 0, 0, fmt.Errorf("abi: cannot marshal in to go type: length insufficient %v require %v", outputLength, totalSize)
|
2017-10-17 14:07:08 +03:00
|
|
|
}
|
2018-02-02 16:03:58 +03:00
|
|
|
start = int(bigOffsetEnd.Uint64())
|
|
|
|
length = int(lengthBig.Uint64())
|
2017-10-17 14:07:08 +03:00
|
|
|
return
|
2017-12-02 00:32:04 +03:00
|
|
|
}
|
2019-01-10 11:59:37 +03:00
|
|
|
|
|
|
|
// tuplePointsTo resolves the location reference for dynamic tuple.
|
|
|
|
func tuplePointsTo(index int, output []byte) (start int, err error) {
|
2022-06-14 15:09:48 +03:00
|
|
|
offset := new(big.Int).SetBytes(output[index : index+32])
|
2019-01-10 11:59:37 +03:00
|
|
|
outputLen := big.NewInt(int64(len(output)))
|
|
|
|
|
2021-12-20 12:25:46 +03:00
|
|
|
if offset.Cmp(outputLen) > 0 {
|
2019-01-10 11:59:37 +03:00
|
|
|
return 0, fmt.Errorf("abi: cannot marshal in to go slice: offset %v would go over slice boundary (len=%v)", offset, outputLen)
|
|
|
|
}
|
|
|
|
if offset.BitLen() > 63 {
|
|
|
|
return 0, fmt.Errorf("abi offset larger than int64: %v", offset)
|
|
|
|
}
|
|
|
|
return int(offset.Uint64()), nil
|
|
|
|
}
|