2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2015 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05: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 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-01-27 09:19:21 -06:00
|
|
|
package abi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
IntTy byte = iota
|
|
|
|
UintTy
|
|
|
|
BoolTy
|
2016-02-09 06:57:00 -06:00
|
|
|
StringTy
|
2015-01-27 09:19:21 -06:00
|
|
|
SliceTy
|
|
|
|
AddressTy
|
2016-02-09 06:57:00 -06:00
|
|
|
FixedBytesTy
|
|
|
|
BytesTy
|
2015-11-20 06:45:37 -06:00
|
|
|
HashTy
|
2016-12-19 06:10:35 -06:00
|
|
|
FixedpointTy
|
2015-01-27 09:19:21 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// Type is the reflection of the supported argument type
|
|
|
|
type Type struct {
|
2016-04-07 04:39:22 -05:00
|
|
|
IsSlice, IsArray bool
|
|
|
|
SliceSize int
|
|
|
|
|
|
|
|
Elem *Type
|
|
|
|
|
|
|
|
Kind reflect.Kind
|
|
|
|
Type reflect.Type
|
|
|
|
Size int
|
|
|
|
T byte // Our own type checking
|
2016-03-31 16:54:47 -05:00
|
|
|
|
2015-01-27 09:19:21 -06:00
|
|
|
stringKind string // holds the unparsed string for deriving signatures
|
|
|
|
}
|
|
|
|
|
2016-03-31 16:54:47 -05:00
|
|
|
var (
|
2016-04-07 04:39:22 -05:00
|
|
|
// fullTypeRegex parses the abi types
|
|
|
|
//
|
|
|
|
// Types can be in the format of:
|
|
|
|
//
|
|
|
|
// Input = Type [ "[" [ Number ] "]" ] Name .
|
2016-12-04 16:11:03 -06:00
|
|
|
// Type = [ "u" ] "int" [ Number ] [ x ] [ Number ].
|
2016-04-07 04:39:22 -05:00
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
2016-12-04 16:11:03 -06:00
|
|
|
// string int uint fixed
|
2016-04-07 04:39:22 -05:00
|
|
|
// string32 int8 uint8 uint[]
|
2016-12-04 16:11:03 -06:00
|
|
|
// address int256 uint256 fixed128x128[2]
|
2016-12-19 06:10:35 -06:00
|
|
|
fullTypeRegex = regexp.MustCompile("([a-zA-Z0-9]+)(\\[([0-9]*)\\])?")
|
2016-04-07 04:39:22 -05:00
|
|
|
// typeRegex parses the abi sub types
|
2016-12-19 06:10:35 -06:00
|
|
|
typeRegex = regexp.MustCompile("([a-zA-Z]+)(([0-9]+)(x([0-9]+))?)?")
|
2016-03-31 16:54:47 -05:00
|
|
|
)
|
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
// NewType creates a new reflection type of abi type given in t.
|
2015-01-27 09:19:21 -06:00
|
|
|
func NewType(t string) (typ Type, err error) {
|
2016-03-31 16:54:47 -05:00
|
|
|
res := fullTypeRegex.FindAllStringSubmatch(t, -1)[0]
|
|
|
|
// check if type is slice and parse type.
|
2015-01-27 09:19:21 -06:00
|
|
|
switch {
|
|
|
|
case res[3] != "":
|
|
|
|
// err is ignored. Already checked for number through the regexp
|
2016-03-31 16:54:47 -05:00
|
|
|
typ.SliceSize, _ = strconv.Atoi(res[3])
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.IsArray = true
|
2015-01-27 09:19:21 -06:00
|
|
|
case res[2] != "":
|
2016-03-31 16:54:47 -05:00
|
|
|
typ.IsSlice, typ.SliceSize = true, -1
|
2015-01-27 09:19:21 -06:00
|
|
|
case res[0] == "":
|
2016-03-31 16:54:47 -05:00
|
|
|
return Type{}, fmt.Errorf("abi: type parse error: %s", t)
|
2015-01-27 09:19:21 -06:00
|
|
|
}
|
2016-04-07 04:39:22 -05:00
|
|
|
if typ.IsArray || typ.IsSlice {
|
|
|
|
sliceType, err := NewType(res[1])
|
|
|
|
if err != nil {
|
|
|
|
return Type{}, err
|
|
|
|
}
|
|
|
|
typ.Elem = &sliceType
|
2016-04-20 14:30:02 -05:00
|
|
|
typ.stringKind = sliceType.stringKind + t[len(res[1]):]
|
2016-04-07 04:39:22 -05:00
|
|
|
return typ, nil
|
|
|
|
}
|
2015-01-27 09:19:21 -06:00
|
|
|
|
2016-03-31 16:54:47 -05: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
|
2016-12-19 06:10:35 -06:00
|
|
|
if len(parsedType[3]) > 0 {
|
2016-03-31 16:54:47 -05:00
|
|
|
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 09:19:21 -06:00
|
|
|
}
|
2016-03-31 16:54:47 -05: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 09:19:21 -06:00
|
|
|
t += "256"
|
|
|
|
}
|
2016-04-20 14:30:02 -05:00
|
|
|
typ.stringKind = t
|
2015-01-27 09:19:21 -06:00
|
|
|
|
2016-03-31 16:54:47 -05:00
|
|
|
switch varType {
|
2016-03-30 09:22:02 -05:00
|
|
|
case "int":
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.Kind = reflectIntKind(false, varSize)
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.Type = big_t
|
2016-03-31 16:54:47 -05:00
|
|
|
typ.Size = varSize
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.T = IntTy
|
|
|
|
case "uint":
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.Kind = reflectIntKind(true, varSize)
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.Type = ubig_t
|
2016-03-31 16:54:47 -05:00
|
|
|
typ.Size = varSize
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.T = UintTy
|
|
|
|
case "bool":
|
|
|
|
typ.Kind = reflect.Bool
|
|
|
|
typ.T = BoolTy
|
|
|
|
case "address":
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.Kind = reflect.Array
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.Type = address_t
|
|
|
|
typ.Size = 20
|
|
|
|
typ.T = AddressTy
|
|
|
|
case "string":
|
|
|
|
typ.Kind = reflect.String
|
|
|
|
typ.Size = -1
|
|
|
|
typ.T = StringTy
|
|
|
|
case "bytes":
|
2016-04-07 04:39:22 -05:00
|
|
|
sliceType, _ := NewType("uint8")
|
|
|
|
typ.Elem = &sliceType
|
2016-03-31 16:54:47 -05:00
|
|
|
if varSize == 0 {
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.IsSlice = true
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.T = BytesTy
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.SliceSize = -1
|
2016-03-30 09:22:02 -05:00
|
|
|
} else {
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.IsArray = true
|
2016-03-30 09:22:02 -05:00
|
|
|
typ.T = FixedBytesTy
|
2016-04-07 04:39:22 -05:00
|
|
|
typ.SliceSize = varSize
|
2016-03-30 09:22:02 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
|
|
|
|
}
|
2015-01-27 09:19:21 -06:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
// String implements Stringer
|
2015-01-27 09:19:21 -06:00
|
|
|
func (t Type) String() (out string) {
|
|
|
|
return t.stringKind
|
|
|
|
}
|
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
func (t Type) pack(v reflect.Value) ([]byte, error) {
|
|
|
|
// dereference pointer first if it's a pointer
|
|
|
|
v = indirect(v)
|
2016-03-15 08:06:12 -05:00
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
if err := typeCheck(t, v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-27 09:19:21 -06:00
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
if (t.IsSlice || t.IsArray) && t.T != BytesTy && t.T != FixedBytesTy {
|
2015-01-27 09:19:21 -06:00
|
|
|
var packed []byte
|
2016-11-03 17:25:19 -05:00
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
for i := 0; i < v.Len(); i++ {
|
|
|
|
val, err := t.Elem.pack(v.Index(i))
|
2016-03-30 09:22:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
packed = append(packed, val...)
|
2015-01-27 09:19:21 -06:00
|
|
|
}
|
2016-11-03 17:25:19 -05:00
|
|
|
if t.IsSlice {
|
|
|
|
return packBytesSlice(packed, v.Len()), nil
|
|
|
|
} else if t.IsArray {
|
|
|
|
return packed, nil
|
|
|
|
}
|
2015-01-27 09:19:21 -06:00
|
|
|
}
|
|
|
|
|
2016-04-07 04:39:22 -05:00
|
|
|
return packElement(t, v), nil
|
2015-01-27 09:19:21 -06:00
|
|
|
}
|
2016-04-20 14:30:02 -05:00
|
|
|
|
|
|
|
// requireLengthPrefix returns whether the type requires any sort of length
|
|
|
|
// prefixing.
|
|
|
|
func (t Type) requiresLengthPrefix() bool {
|
2016-11-03 17:25:19 -05:00
|
|
|
return t.T != FixedBytesTy && (t.T == StringTy || t.T == BytesTy || t.IsSlice)
|
2016-04-20 14:30:02 -05:00
|
|
|
}
|