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"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
)
|
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// reads the integer based on its kind
|
2017-06-27 11:05:33 +03:00
|
|
|
func readInteger(kind reflect.Kind, b []byte) interface{} {
|
|
|
|
switch kind {
|
|
|
|
case reflect.Uint8:
|
2017-11-10 20:06:45 +03:00
|
|
|
return b[len(b)-1]
|
2017-06-27 11:05:33 +03:00
|
|
|
case reflect.Uint16:
|
|
|
|
return binary.BigEndian.Uint16(b[len(b)-2:])
|
|
|
|
case reflect.Uint32:
|
|
|
|
return binary.BigEndian.Uint32(b[len(b)-4:])
|
|
|
|
case reflect.Uint64:
|
|
|
|
return binary.BigEndian.Uint64(b[len(b)-8:])
|
|
|
|
case reflect.Int8:
|
|
|
|
return int8(b[len(b)-1])
|
|
|
|
case reflect.Int16:
|
|
|
|
return int16(binary.BigEndian.Uint16(b[len(b)-2:]))
|
|
|
|
case reflect.Int32:
|
|
|
|
return int32(binary.BigEndian.Uint32(b[len(b)-4:]))
|
|
|
|
case reflect.Int64:
|
|
|
|
return int64(binary.BigEndian.Uint64(b[len(b)-8:]))
|
|
|
|
default:
|
|
|
|
return new(big.Int).SetBytes(b)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// 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.
|
|
|
|
// This enforces that standard by always presenting it as a 24-array (address + sig = 24 bytes)
|
|
|
|
func readFunctionType(t Type, word []byte) (funcTy [24]byte, err error) {
|
|
|
|
if t.T != FunctionTy {
|
2017-11-10 04:48:51 +03:00
|
|
|
return [24]byte{}, fmt.Errorf("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
|
|
|
}
|
|
|
|
|
2017-10-17 14:07:08 +03:00
|
|
|
// through reflection, creates a fixed array to be read from
|
|
|
|
func readFixedBytes(t Type, word []byte) (interface{}, error) {
|
|
|
|
if t.T != FixedBytesTy {
|
2017-11-10 04:48:51 +03:00
|
|
|
return nil, fmt.Errorf("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
|
|
|
|
array := reflect.New(t.Type).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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// iteratively unpack elements
|
|
|
|
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) {
|
|
|
|
return nil, fmt.Errorf("abi: cannot marshal in to 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
|
|
|
|
slice := output[start : start+size*32]
|
|
|
|
|
|
|
|
if t.T == SliceTy {
|
|
|
|
// declare our slice
|
|
|
|
refSlice = reflect.MakeSlice(t.Type, size, size)
|
|
|
|
} else if t.T == ArrayTy {
|
|
|
|
// declare our array
|
|
|
|
refSlice = reflect.New(t.Type).Elem()
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("abi: invalid type in array/slice unpacking stage")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, j := start, 0; j*32 < len(slice); i, j = i+32, j+1 {
|
|
|
|
// this corrects the arrangement so that we get all the underlying array values
|
|
|
|
if t.Elem.T == ArrayTy && j != 0 {
|
|
|
|
i = start + t.Elem.Size*32*j
|
2017-06-27 11:05:33 +03:00
|
|
|
}
|
2017-10-17 14:07:08 +03:00
|
|
|
inter, err := toGoType(i, *t.Elem, output)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-06-27 11:05:33 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
// toGoType parses the output bytes and recursively assigns the value of these bytes
|
|
|
|
// into a go type with accordance with the ABI spec.
|
|
|
|
func toGoType(index int, t Type, output []byte) (interface{}, error) {
|
|
|
|
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 (
|
|
|
|
returnOutput []byte
|
|
|
|
begin, end int
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// if we require a length prefix, find the beginning word and size returned.
|
|
|
|
if t.requiresLengthPrefix() {
|
|
|
|
begin, end, err = lengthPrefixPointsTo(index, output)
|
|
|
|
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 {
|
|
|
|
case SliceTy:
|
|
|
|
return forEachUnpack(t, output, begin, end)
|
|
|
|
case ArrayTy:
|
|
|
|
return forEachUnpack(t, output, index, t.Size)
|
|
|
|
case StringTy: // variable arrays are written at the end of the return bytes
|
|
|
|
return string(output[begin : begin+end]), nil
|
2017-06-27 11:05:33 +03:00
|
|
|
case IntTy, UintTy:
|
2017-10-17 14:07:08 +03:00
|
|
|
return readInteger(t.Kind, returnOutput), nil
|
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:
|
|
|
|
return output[begin : begin+end], nil
|
|
|
|
case FixedBytesTy:
|
|
|
|
return readFixedBytes(t, returnOutput)
|
|
|
|
case FunctionTy:
|
|
|
|
return readFunctionType(t, returnOutput)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("abi: unknown type %v", t.T)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// interprets a 32 byte slice as an offset and then determines which indice to look to decode the type.
|
|
|
|
func lengthPrefixPointsTo(index int, output []byte) (start int, length int, err error) {
|
2018-02-02 16:03:58 +03:00
|
|
|
bigOffsetEnd := big.NewInt(0).SetBytes(output[index : index+32])
|
|
|
|
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())
|
|
|
|
lengthBig := big.NewInt(0).SetBytes(output[offsetEnd-32 : offsetEnd])
|
|
|
|
|
|
|
|
totalSize := big.NewInt(0)
|
|
|
|
totalSize.Add(totalSize, bigOffsetEnd)
|
|
|
|
totalSize.Add(totalSize, lengthBig)
|
|
|
|
if totalSize.BitLen() > 63 {
|
|
|
|
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
|
|
|
}
|