aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi
diff options
context:
space:
mode:
authorMartin Holst Swende <martin@swende.se>2017-12-28 18:17:45 +0800
committerMartin Holst Swende <martin@swende.se>2018-02-21 19:27:50 +0800
commit1ede68355db6adbf468d198a8d1ecb0ad1a3ea31 (patch)
tree288e71dbf81a19030f36f507d6b91bd42f3ba979 /accounts/abi
parent5603715c06998ff9ed30eb134c4398ad1439ef48 (diff)
downloaddexon-1ede68355db6adbf468d198a8d1ecb0ad1a3ea31.tar.gz
dexon-1ede68355db6adbf468d198a8d1ecb0ad1a3ea31.tar.zst
dexon-1ede68355db6adbf468d198a8d1ecb0ad1a3ea31.zip
accounts/abi: add another unpack interface
Diffstat (limited to 'accounts/abi')
-rw-r--r--accounts/abi/abi.go6
-rw-r--r--accounts/abi/abi_test.go6
-rw-r--r--accounts/abi/argument.go59
-rw-r--r--accounts/abi/unpack_test.go2
-rw-r--r--accounts/abi/unpackv2_test.go336
5 files changed, 395 insertions, 14 deletions
diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go
index abcb403db..32f041890 100644
--- a/accounts/abi/abi.go
+++ b/accounts/abi/abi.go
@@ -136,11 +136,11 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
// MethodById looks up a method by the 4-byte id
// returns nil if none found
-func (abi *ABI) MethodById(sigdata []byte) *Method {
+func (abi *ABI) MethodById(sigdata []byte) (*Method, error){
for _, method := range abi.Methods {
if bytes.Equal(method.Id(), sigdata[:4]) {
- return &method
+ return &method, nil
}
}
- return nil
+ return nil, fmt.Errorf("ABI spec does not contain method signature in data: 0x%x", sigdata[:4])
}
diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go
index 2d43b631c..3bef6add5 100644
--- a/accounts/abi/abi_test.go
+++ b/accounts/abi/abi_test.go
@@ -689,7 +689,11 @@ func TestABI_MethodById(t *testing.T) {
}
for name, m := range abi.Methods {
a := fmt.Sprintf("%v", m)
- b := fmt.Sprintf("%v", abi.MethodById(m.Id()))
+ m2,err := abi.MethodById(m.Id())
+ if err != nil {
+ t.Fatal(err)
+ }
+ b := fmt.Sprintf("%v", m2)
if a != b {
t.Errorf("Method %v (id %v) not 'findable' by id in ABI", name, common.ToHex(m.Id()))
}
diff --git a/accounts/abi/argument.go b/accounts/abi/argument.go
index 04ca6150a..b9b537121 100644
--- a/accounts/abi/argument.go
+++ b/accounts/abi/argument.go
@@ -67,6 +67,16 @@ func (arguments Arguments) LengthNonIndexed() int {
return out
}
+func (arguments Arguments) NonIndexed() Arguments{
+ var ret []Argument
+ for _,arg := range arguments{
+ if !arg.Indexed{
+ ret = append(ret, arg)
+ }
+ }
+ return ret
+}
+
// isTuple returns true for non-atomic constructs, like (uint,uint) or uint[]
func (arguments Arguments) isTuple() bool {
return len(arguments) > 1
@@ -114,14 +124,9 @@ func (arguments Arguments) unpackTuple(v interface{}, output []byte) error {
// `j` counts the number of complex types.
// both `i` and `j` are used to to correctly compute `data` offset.
- i, j := -1, 0
- for _, arg := range arguments {
+ j := 0
+ for i, arg := range arguments.NonIndexed() {
- if arg.Indexed {
- // can't read, continue
- continue
- }
- i++
marshalledValue, err := toGoType((i+j)*32, arg.Type, output)
if err != nil {
return err
@@ -178,7 +183,6 @@ func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
}
value := valueOf.Elem()
-
marshalledValue, err := toGoType(0, arg.Type, output)
if err != nil {
return err
@@ -186,7 +190,44 @@ func (arguments Arguments) unpackAtomic(v interface{}, output []byte) error {
return set(value, reflect.ValueOf(marshalledValue), arg)
}
-// Unpack performs the operation Go format -> Hexdata
+// UnpackValues can be used to unpack ABI-encoded hexdata according to the ABI-specification,
+// without supplying a struct to unpack into. Instead, this method returns a list containing the
+// values. An atomic argument will be a list with one element.
+func (arguments Arguments) UnpackValues(data []byte) ([]interface{}, error){
+
+ retval := make([]interface{},0,arguments.LengthNonIndexed())
+
+ virtualArgs := 0
+
+ for index,arg:= range arguments.NonIndexed(){
+
+ marshalledValue, err := toGoType((index + virtualArgs) * 32, arg.Type, data)
+
+ if arg.Type.T == ArrayTy {
+ //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
+
+ virtualArgs += arg.Type.Size - 1
+ }
+
+ if err != nil{
+ return nil, err
+ }
+ retval = append(retval, marshalledValue)
+ }
+ return retval, nil
+}
+
+// UnpackValues performs the operation Go format -> Hexdata
+// It is the semantic opposite of UnpackValues
+func (arguments Arguments) PackValues(args []interface{}) ([]byte, error) {
+ return arguments.Pack(args...)
+}
+
+
+// Pack performs the operation Go format -> Hexdata
func (arguments Arguments) Pack(args ...interface{}) ([]byte, error) {
// Make sure arguments match up and pack them
abiArgs := arguments
diff --git a/accounts/abi/unpack_test.go b/accounts/abi/unpack_test.go
index a65426a30..e9f910812 100644
--- a/accounts/abi/unpack_test.go
+++ b/accounts/abi/unpack_test.go
@@ -130,7 +130,7 @@ var unpackTests = []unpackTest{
{
def: `[{"type": "bytes32"}]`,
enc: "0100000000000000000000000000000000000000000000000000000000000000",
- want: common.HexToHash("0100000000000000000000000000000000000000000000000000000000000000"),
+ want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
{
def: `[{"type": "function"}]`,
diff --git a/accounts/abi/unpackv2_test.go b/accounts/abi/unpackv2_test.go
new file mode 100644
index 000000000..d0074ff7b
--- /dev/null
+++ b/accounts/abi/unpackv2_test.go
@@ -0,0 +1,336 @@
+// Copyright 2015 The go-ethereum Authors
+// 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 (
+ "bytes"
+ "encoding/hex"
+ "fmt"
+ "math/big"
+ "reflect"
+ "strconv"
+ "strings"
+ "testing"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+func TestUnpackV2(t *testing.T) {
+ for i, test := range unpackTests {
+ t.Run(strconv.Itoa(i), func(t *testing.T) {
+ def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
+ abi, err := JSON(strings.NewReader(def))
+ if err != nil {
+ t.Fatalf("invalid ABI definition %s: %v", def, err)
+ }
+ encb, err := hex.DecodeString(test.enc)
+ if err != nil {
+ t.Fatalf("invalid hex: %s" + test.enc)
+ }
+ out, err := abi.Methods["method"].Outputs.UnpackValues(encb)
+
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(test.err) != 0 {
+ // The new stuff doesn't have these types of errors
+ return
+ }
+ if !reflect.DeepEqual(test.want, out[0]) {
+ t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out[0])
+ }
+ })
+ }
+}
+
+
+func TestMultiReturnWithArrayV2(t *testing.T) {
+ const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
+ abi, err := JSON(strings.NewReader(definition))
+ if err != nil {
+ t.Fatal(err)
+ }
+ buff := new(bytes.Buffer)
+ buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000007"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006"))
+
+ out, err := abi.Methods["multi"].Outputs.UnpackValues(buff.Bytes())
+
+ ret1Exp := [3]uint64{9, 8, 7}
+ ret2Exp := uint64(6)
+
+ if !reflect.DeepEqual(out[0], ret1Exp) {
+ t.Error("array result", out[0], "!= Expected", ret1Exp)
+ }
+ if out[1] != ret2Exp {
+ t.Error("int result", out[1], "!= Expected", ret2Exp)
+ }
+}
+
+func TestUnmarshalV2(t *testing.T) {
+ const definition = `[
+ { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
+ { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
+ { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
+ { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
+ { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
+ { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
+ { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
+ { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
+ { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
+
+ abi, err := JSON(strings.NewReader(definition))
+ if err != nil {
+ t.Fatal(err)
+ }
+ buff := new(bytes.Buffer)
+
+ // marshall mixed bytes (mixedBytes)
+ p0Exp := common.Hex2Bytes("01020000000000000000")
+ p1Exp := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
+
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
+ buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
+ buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
+
+ mixedBytes, err := abi.Methods["mixedBytes"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ } else {
+ p0 := mixedBytes[0].([]byte)
+ p1 := mixedBytes[1].([32]byte)
+ if !bytes.Equal(p0, p0Exp) {
+ t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
+ }
+
+ if !bytes.Equal(p1[:], p1Exp) {
+ t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
+ }
+ }
+
+ // marshal int
+ integer, err := abi.Methods["int"].Outputs.UnpackValues(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
+ if err != nil {
+ t.Error(err)
+ }
+ if len(integer) == 0 {
+ t.Error("Expected one integer")
+ }
+ intval := integer[0].(*big.Int)
+ if intval == nil || intval.Cmp(big.NewInt(1)) != 0 {
+ t.Error("expected Int to be 1 got", intval)
+ }
+
+ // marshal bool
+ boolreturns, err := abi.Methods["bool"].Outputs.UnpackValues(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
+ if err != nil {
+ t.Error(err)
+ }
+ boolval := boolreturns[0].(bool)
+ if !boolval {
+ t.Error("expected Bool to be true")
+ }
+
+ // marshal dynamic bytes max length 32
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ bytesOut := common.RightPadBytes([]byte("hello"), 32)
+ buff.Write(bytesOut)
+
+ bytesreturns, err := abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
+
+ if err != nil {
+ t.Error(err)
+ }
+ bytesval := bytesreturns[0].([]byte)
+ if !bytes.Equal(bytesval, bytesOut) {
+ t.Errorf("expected %x got %x", bytesOut, bytesval)
+ }
+
+ // marshall dynamic bytes max length 64
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
+ bytesOut = common.RightPadBytes([]byte("hello"), 64)
+ buff.Write(bytesOut)
+
+ bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+ bytesval = bytesreturns[0].([]byte)
+ if !bytes.Equal(bytesval, bytesOut) {
+ t.Errorf("expected %x got %x", bytesOut, bytesval)
+ }
+
+ // marshall dynamic bytes max length 64
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
+ bytesOut = common.RightPadBytes([]byte("hello"), 64)
+ buff.Write(bytesOut)
+
+ bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+ bytesval = bytesreturns[0].([]byte)
+
+ if !bytes.Equal(bytesval, bytesOut[:len(bytesOut)-1]) {
+ t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], bytesval)
+ }
+ // marshal dynamic bytes output empty (nil)
+ bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(nil)
+ if err == nil {
+ t.Error("expected error")
+ }
+ // marshal dynamic bytes output empty
+ buff.Reset()
+ bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
+ if err == nil {
+ t.Error("expected error")
+ }
+
+ // marshal dynamic bytes length 5
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
+ buff.Write(common.RightPadBytes([]byte("hello"), 32))
+
+ bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+ bytesval = bytesreturns[0].([]byte)
+
+ if !bytes.Equal(bytesval, []byte("hello")) {
+ t.Errorf("expected %x got %x", bytesOut, bytesval)
+ }
+
+ // marshal dynamic bytes length 5
+ buff.Reset()
+ buff.Write(common.RightPadBytes([]byte("hello"), 32))
+
+ hashreturns, err := abi.Methods["fixed"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+ hashval := hashreturns[0].([32]byte)
+
+ helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
+ if common.Hash(hashval) != helloHash {
+ t.Errorf("Expected %x to equal %x", hashval, helloHash)
+ }
+
+ // marshal error
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+
+ bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
+ if err == nil {
+ // Error abi: cannot marshal in to go slice: offset 32 would go over slice boundary (len=64)
+ t.Error("expected error")
+ }
+ bytesreturns, err = abi.Methods["multi"].Outputs.UnpackValues(make([]byte, 64))
+
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
+ // marshal int array
+
+ intArrayReturns, err := abi.Methods["intArraySingle"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+ intArray := intArrayReturns[0].([3]*big.Int)
+
+ var testAgainstIntArray = [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
+
+ for i, intval := range intArray {
+ if intval.Cmp(testAgainstIntArray[i]) != 0 {
+ t.Errorf("expected %v, got %v", testAgainstIntArray[i], intval)
+ }
+ }
+ // marshal address slice
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
+ buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
+
+ outAddrReturns, err := abi.Methods["addressSliceSingle"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Fatal("didn't expect error:", err)
+ }
+ outAddr := outAddrReturns[0].([]common.Address)
+ if len(outAddr) != 1 {
+ t.Fatal("expected 1 item, got", len(outAddr))
+ }
+
+ if outAddr[0] != (common.Address{1}) {
+ t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
+ }
+
+ // marshal multiple address slice
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
+ buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
+ buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
+
+ outAddrStructReturns, err := abi.Methods["addressSliceDouble"].Outputs.UnpackValues(buff.Bytes())
+ if err != nil {
+ t.Fatal("didn't expect error:", err)
+ }
+ A := outAddrStructReturns[0].([]common.Address)
+ B := outAddrStructReturns[1].([]common.Address)
+
+ if len(A) != 1 {
+ t.Fatal("expected 1 item, got", len(A))
+ }
+
+ if A[0] != (common.Address{1}) {
+ t.Errorf("expected %x, got %x", common.Address{1}, A[0])
+ }
+
+ if len(B) != 2 {
+ t.Fatal("expected 1 item, got", len(B))
+ }
+
+ if B[0] != (common.Address{2}) {
+ t.Errorf("expected %x, got %x", common.Address{2}, B[0])
+ }
+ if B[1] != (common.Address{3}) {
+ t.Errorf("expected %x, got %x", common.Address{3}, B[1])
+ }
+
+ // marshal invalid address slice
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
+
+ err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
+ _, err = abi.Methods["addressSliceSingle"].Outputs.UnpackValues(buff.Bytes())
+ if err == nil {
+ t.Fatal("expected error:", err)
+ }
+
+}