2015-07-07 00:54:22 +00:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 16:48:40 +00:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 00:54:22 +00:00
|
|
|
//
|
2015-07-23 16:35:11 +00:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 00:54:22 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 16:48:40 +00:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 00:54:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 16:48:40 +00:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 00:54:22 +00:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 16:48:40 +00:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 00:54:22 +00:00
|
|
|
|
2015-01-27 15:19:21 +00:00
|
|
|
package abi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
|
2015-03-16 10:27:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-01-27 15:19:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IntTy byte = iota
|
|
|
|
UintTy
|
|
|
|
BoolTy
|
2016-02-09 12:57:00 +00:00
|
|
|
StringTy
|
2015-01-27 15:19:21 +00:00
|
|
|
SliceTy
|
|
|
|
AddressTy
|
2016-02-09 12:57:00 +00:00
|
|
|
FixedBytesTy
|
|
|
|
BytesTy
|
2015-11-20 12:45:37 +00:00
|
|
|
HashTy
|
2015-01-27 15:19:21 +00:00
|
|
|
RealTy
|
|
|
|
)
|
|
|
|
|
|
|
|
// Type is the reflection of the supported argument type
|
|
|
|
type Type struct {
|
2016-03-31 21:54:47 +00:00
|
|
|
IsSlice bool
|
|
|
|
SliceSize int
|
|
|
|
|
2015-01-27 15:19:21 +00:00
|
|
|
Kind reflect.Kind
|
|
|
|
Type reflect.Type
|
|
|
|
Size int
|
|
|
|
T byte // Our own type checking
|
|
|
|
stringKind string // holds the unparsed string for deriving signatures
|
|
|
|
}
|
|
|
|
|
2016-03-31 21:54:47 +00:00
|
|
|
var (
|
|
|
|
fullTypeRegex = regexp.MustCompile("([a-zA-Z0-9]+)(\\[([0-9]*)?\\])?")
|
|
|
|
typeRegex = regexp.MustCompile("([a-zA-Z]+)([0-9]*)?")
|
|
|
|
)
|
|
|
|
|
2015-10-28 21:57:21 +00:00
|
|
|
// NewType returns a fully parsed Type given by the input string or an error if it can't be parsed.
|
2015-01-27 15:19:21 +00:00
|
|
|
//
|
|
|
|
// Strings can be in the format of:
|
|
|
|
//
|
|
|
|
// Input = Type [ "[" [ Number ] "]" ] Name .
|
|
|
|
// Type = [ "u" ] "int" [ Number ] .
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// string int uint real
|
|
|
|
// string32 int8 uint8 uint[]
|
|
|
|
// address int256 uint256 real[2]
|
|
|
|
func NewType(t string) (typ Type, err error) {
|
|
|
|
// 1. full string 2. type 3. (opt.) is slice 4. (opt.) size
|
2016-03-31 21:54:47 +00:00
|
|
|
// parse the full representation of the abi-type definition; including:
|
|
|
|
// * full string
|
|
|
|
// * type
|
|
|
|
// * is slice
|
|
|
|
// * slice size
|
|
|
|
res := fullTypeRegex.FindAllStringSubmatch(t, -1)[0]
|
|
|
|
|
|
|
|
// check if type is slice and parse type.
|
2015-01-27 15:19:21 +00:00
|
|
|
switch {
|
|
|
|
case res[3] != "":
|
|
|
|
// err is ignored. Already checked for number through the regexp
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.SliceSize, _ = strconv.Atoi(res[3])
|
|
|
|
typ.IsSlice = true
|
2015-01-27 15:19:21 +00:00
|
|
|
case res[2] != "":
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.IsSlice, typ.SliceSize = true, -1
|
2015-01-27 15:19:21 +00:00
|
|
|
case res[0] == "":
|
2016-03-31 21:54:47 +00:00
|
|
|
return Type{}, fmt.Errorf("abi: type parse error: %s", t)
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
2016-03-31 21:54:47 +00:00
|
|
|
// parse the type and size of the abi-type.
|
|
|
|
parsedType := typeRegex.FindAllStringSubmatch(res[1], -1)[0]
|
|
|
|
// varSize is the size of the variable
|
|
|
|
var varSize int
|
|
|
|
if len(parsedType[2]) > 0 {
|
|
|
|
var err error
|
|
|
|
varSize, err = strconv.Atoi(parsedType[2])
|
|
|
|
if err != nil {
|
|
|
|
return Type{}, fmt.Errorf("abi: error parsing variable size: %v", err)
|
|
|
|
}
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
2016-03-31 21:54:47 +00:00
|
|
|
// varType is the parsed abi type
|
|
|
|
varType := parsedType[1]
|
|
|
|
// substitute canonical integer
|
|
|
|
if varSize == 0 && (varType == "int" || varType == "uint") {
|
|
|
|
varSize = 256
|
2015-01-27 15:19:21 +00:00
|
|
|
t += "256"
|
|
|
|
}
|
|
|
|
|
2016-03-31 21:54:47 +00:00
|
|
|
switch varType {
|
2016-03-30 14:22:02 +00:00
|
|
|
case "int":
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.Kind = reflect.Int
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.Type = big_t
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.Size = varSize
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.T = IntTy
|
|
|
|
case "uint":
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.Kind = reflect.Uint
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.Type = ubig_t
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.Size = varSize
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.T = UintTy
|
|
|
|
case "bool":
|
|
|
|
typ.Kind = reflect.Bool
|
|
|
|
typ.T = BoolTy
|
|
|
|
case "real": // TODO
|
|
|
|
typ.Kind = reflect.Invalid
|
|
|
|
case "address":
|
|
|
|
typ.Type = address_t
|
|
|
|
typ.Size = 20
|
|
|
|
typ.T = AddressTy
|
|
|
|
case "string":
|
|
|
|
typ.Kind = reflect.String
|
|
|
|
typ.Size = -1
|
|
|
|
typ.T = StringTy
|
2016-03-31 21:54:47 +00:00
|
|
|
if varSize > 0 {
|
2015-11-20 12:45:37 +00:00
|
|
|
typ.Size = 32
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
2016-03-30 14:22:02 +00:00
|
|
|
case "hash":
|
2016-03-31 09:38:31 +00:00
|
|
|
typ.Kind = reflect.Array
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.Size = 32
|
|
|
|
typ.Type = hash_t
|
|
|
|
typ.T = HashTy
|
|
|
|
case "bytes":
|
2016-03-31 09:38:31 +00:00
|
|
|
typ.Kind = reflect.Array
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.Type = byte_ts
|
2016-03-31 21:54:47 +00:00
|
|
|
typ.Size = varSize
|
|
|
|
if varSize == 0 {
|
2016-03-30 14:22:02 +00:00
|
|
|
typ.T = BytesTy
|
|
|
|
} else {
|
|
|
|
typ.T = FixedBytesTy
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
|
|
|
|
}
|
2015-01-27 15:19:21 +00:00
|
|
|
typ.stringKind = t
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Type) String() (out string) {
|
|
|
|
return t.stringKind
|
|
|
|
}
|
|
|
|
|
2016-03-15 13:06:12 +00:00
|
|
|
// packBytesSlice packs the given bytes as [L, V] as the canonical representation
|
|
|
|
// bytes slice
|
|
|
|
func packBytesSlice(bytes []byte, l int) []byte {
|
|
|
|
len := packNum(reflect.ValueOf(l), UintTy)
|
|
|
|
return append(len, common.RightPadBytes(bytes, (l+31)/32*32)...)
|
|
|
|
}
|
|
|
|
|
2015-01-27 15:19:21 +00:00
|
|
|
// Test the given input parameter `v` and checks if it matches certain
|
|
|
|
// criteria
|
|
|
|
// * Big integers are checks for ptr types and if the given value is
|
|
|
|
// assignable
|
|
|
|
// * Integer are checked for size
|
|
|
|
// * Strings, addresses and bytes are checks for type and size
|
|
|
|
func (t Type) pack(v interface{}) ([]byte, error) {
|
|
|
|
value := reflect.ValueOf(v)
|
|
|
|
switch kind := value.Kind(); kind {
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
2016-03-31 21:54:47 +00:00
|
|
|
// check input is unsigned
|
2015-01-27 15:19:21 +00:00
|
|
|
if t.Type != ubig_t {
|
2016-03-31 21:54:47 +00:00
|
|
|
return nil, fmt.Errorf("abi: type mismatch: %s for %T", t.Type, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// no implicit type casting
|
|
|
|
if int(value.Type().Size()*8) != t.Size {
|
|
|
|
return nil, fmt.Errorf("abi: cannot use type %T as type uint%d", v, t.Size)
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
2016-03-31 21:54:47 +00:00
|
|
|
|
2015-01-27 15:19:21 +00:00
|
|
|
return packNum(value, t.T), nil
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
if t.Type != ubig_t {
|
|
|
|
return nil, fmt.Errorf("type mismatch: %s for %T", t.Type, v)
|
|
|
|
}
|
2016-03-31 21:54:47 +00:00
|
|
|
|
|
|
|
// no implicit type casting
|
|
|
|
if int(value.Type().Size()*8) != t.Size {
|
|
|
|
return nil, fmt.Errorf("abi: cannot use type %T as type uint%d", v, t.Size)
|
|
|
|
}
|
2015-01-27 15:19:21 +00:00
|
|
|
return packNum(value, t.T), nil
|
|
|
|
case reflect.Ptr:
|
|
|
|
// If the value is a ptr do a assign check (only used by
|
|
|
|
// big.Int for now)
|
|
|
|
if t.Type == ubig_t && value.Type() != ubig_t {
|
|
|
|
return nil, fmt.Errorf("type mismatch: %s for %T", t.Type, v)
|
|
|
|
}
|
|
|
|
return packNum(value, t.T), nil
|
|
|
|
case reflect.String:
|
|
|
|
if t.Size > -1 && value.Len() > t.Size {
|
|
|
|
return nil, fmt.Errorf("%v out of bound. %d for %d", value.Kind(), value.Len(), t.Size)
|
|
|
|
}
|
2016-03-15 13:06:12 +00:00
|
|
|
|
|
|
|
return packBytesSlice([]byte(value.String()), value.Len()), nil
|
2015-01-27 15:19:21 +00:00
|
|
|
case reflect.Slice:
|
2016-03-30 14:22:02 +00:00
|
|
|
// Byte slice is a special case, it gets treated as a single value
|
2016-03-15 13:06:12 +00:00
|
|
|
if t.T == BytesTy {
|
|
|
|
return packBytesSlice(value.Bytes(), value.Len()), nil
|
|
|
|
}
|
|
|
|
|
2016-03-31 21:54:47 +00:00
|
|
|
if t.SliceSize > -1 && value.Len() > t.SliceSize {
|
2015-01-27 15:19:21 +00:00
|
|
|
return nil, fmt.Errorf("%v out of bound. %d for %d", value.Kind(), value.Len(), t.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Signed / Unsigned check
|
2016-03-30 14:22:02 +00:00
|
|
|
if value.Type() == big_t && (t.T != IntTy && isSigned(value)) || (t.T == UintTy && isSigned(value)) {
|
2015-01-27 15:19:21 +00:00
|
|
|
return nil, fmt.Errorf("slice of incompatible types.")
|
|
|
|
}
|
|
|
|
|
|
|
|
var packed []byte
|
|
|
|
for i := 0; i < value.Len(); i++ {
|
2016-03-30 14:22:02 +00:00
|
|
|
val, err := t.pack(value.Index(i).Interface())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
packed = append(packed, val...)
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
2016-03-30 14:22:02 +00:00
|
|
|
return packBytesSlice(packed, value.Len()), nil
|
2015-01-27 15:19:21 +00:00
|
|
|
case reflect.Bool:
|
|
|
|
if value.Bool() {
|
2015-03-16 10:27:38 +00:00
|
|
|
return common.LeftPadBytes(common.Big1.Bytes(), 32), nil
|
2015-01-27 15:19:21 +00:00
|
|
|
} else {
|
2015-03-16 10:27:38 +00:00
|
|
|
return common.LeftPadBytes(common.Big0.Bytes(), 32), nil
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
2015-10-28 21:57:21 +00:00
|
|
|
case reflect.Array:
|
|
|
|
if v, ok := value.Interface().(common.Address); ok {
|
2015-11-20 12:45:37 +00:00
|
|
|
return common.LeftPadBytes(v[:], 32), nil
|
2015-10-28 21:57:21 +00:00
|
|
|
} else if v, ok := value.Interface().(common.Hash); ok {
|
2015-11-20 12:45:37 +00:00
|
|
|
return v[:], nil
|
2015-10-28 21:57:21 +00:00
|
|
|
}
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 07:38:53 +00:00
|
|
|
return nil, fmt.Errorf("ABI: bad input given %v", value.Kind())
|
2015-01-27 15:19:21 +00:00
|
|
|
}
|