Skip to content

Commit dda3bf3

Browse files
committed
Merge pull request #1943 from obscuren/abi-fixes
accounts/abi: ABI fixes & added types
2 parents 6dfbbc3 + 1f72952 commit dda3bf3

File tree

3 files changed

+80
-21
lines changed

3 files changed

+80
-21
lines changed

accounts/abi/abi.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import (
3636
type Method struct {
3737
Name string
3838
Const bool
39-
Input []Argument
39+
Inputs []Argument
4040
Return Type // not yet implemented
4141
}
4242

@@ -49,9 +49,9 @@ type Method struct {
4949
// Please note that "int" is substitute for its canonical representation "int256"
5050
func (m Method) String() (out string) {
5151
out += m.Name
52-
types := make([]string, len(m.Input))
52+
types := make([]string, len(m.Inputs))
5353
i := 0
54-
for _, input := range m.Input {
54+
for _, input := range m.Inputs {
5555
types[i] = input.Type.String()
5656
i++
5757
}
@@ -104,7 +104,7 @@ func (abi ABI) pack(name string, args ...interface{}) ([]byte, error) {
104104

105105
var ret []byte
106106
for i, a := range args {
107-
input := method.Input[i]
107+
input := method.Inputs[i]
108108

109109
packed, err := input.Type.pack(a)
110110
if err != nil {
@@ -129,8 +129,8 @@ func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
129129
}
130130

131131
// start with argument count match
132-
if len(args) != len(method.Input) {
133-
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Input))
132+
if len(args) != len(method.Inputs) {
133+
return nil, fmt.Errorf("argument count mismatch: %d for %d", len(args), len(method.Inputs))
134134
}
135135

136136
arguments, err := abi.pack(name, args...)

accounts/abi/abi_test.go

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,38 @@ package abi
1818

1919
import (
2020
"bytes"
21+
"fmt"
22+
"log"
2123
"math/big"
2224
"reflect"
2325
"strings"
2426
"testing"
2527

28+
"github.com/ethereum/go-ethereum/common"
2629
"github.com/ethereum/go-ethereum/crypto"
2730
)
2831

2932
const jsondata = `
3033
[
3134
{ "name" : "balance", "const" : true },
32-
{ "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] }
35+
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
3336
]`
3437

3538
const jsondata2 = `
3639
[
3740
{ "name" : "balance", "const" : true },
38-
{ "name" : "send", "const" : false, "input" : [ { "name" : "amount", "type" : "uint256" } ] },
39-
{ "name" : "test", "const" : false, "input" : [ { "name" : "number", "type" : "uint32" } ] },
40-
{ "name" : "string", "const" : false, "input" : [ { "name" : "input", "type" : "string" } ] },
41-
{ "name" : "bool", "const" : false, "input" : [ { "name" : "input", "type" : "bool" } ] },
42-
{ "name" : "address", "const" : false, "input" : [ { "name" : "input", "type" : "address" } ] },
43-
{ "name" : "string32", "const" : false, "input" : [ { "name" : "input", "type" : "string32" } ] },
44-
{ "name" : "uint64[2]", "const" : false, "input" : [ { "name" : "input", "type" : "uint64[2]" } ] },
45-
{ "name" : "uint64[]", "const" : false, "input" : [ { "name" : "input", "type" : "uint64[]" } ] },
46-
{ "name" : "foo", "const" : false, "input" : [ { "name" : "input", "type" : "uint32" } ] },
47-
{ "name" : "bar", "const" : false, "input" : [ { "name" : "input", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
48-
{ "name" : "slice", "const" : false, "input" : [ { "name" : "input", "type" : "uint32[2]" } ] },
49-
{ "name" : "slice256", "const" : false, "input" : [ { "name" : "input", "type" : "uint256[2]" } ] }
41+
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] },
42+
{ "name" : "test", "const" : false, "inputs" : [ { "name" : "number", "type" : "uint32" } ] },
43+
{ "name" : "string", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string" } ] },
44+
{ "name" : "bool", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "bool" } ] },
45+
{ "name" : "address", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "address" } ] },
46+
{ "name" : "string32", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "string32" } ] },
47+
{ "name" : "uint64[2]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[2]" } ] },
48+
{ "name" : "uint64[]", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint64[]" } ] },
49+
{ "name" : "foo", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" } ] },
50+
{ "name" : "bar", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32" }, { "name" : "string", "type" : "uint16" } ] },
51+
{ "name" : "slice", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint32[2]" } ] },
52+
{ "name" : "slice256", "const" : false, "inputs" : [ { "name" : "inputs", "type" : "uint256[2]" } ] }
5053
]`
5154

5255
func TestType(t *testing.T) {
@@ -344,3 +347,49 @@ func TestPackSliceBig(t *testing.T) {
344347
t.Errorf("expected %x got %x", sig, packed)
345348
}
346349
}
350+
351+
func ExampleJSON() {
352+
const definition = `[{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"isBar","outputs":[{"name":"","type":"bool"}],"type":"function"}]`
353+
354+
abi, err := JSON(strings.NewReader(definition))
355+
if err != nil {
356+
log.Fatalln(err)
357+
}
358+
out, err := abi.Pack("isBar", common.HexToAddress("01"))
359+
if err != nil {
360+
log.Fatalln(err)
361+
}
362+
363+
fmt.Printf("%x\n", out)
364+
// Output:
365+
// 1f2c40920000000000000000000000000000000000000000000000000000000000000001
366+
}
367+
368+
func TestBytes(t *testing.T) {
369+
const definition = `[
370+
{ "name" : "balance", "const" : true, "inputs" : [ { "name" : "address", "type" : "bytes20" } ] },
371+
{ "name" : "send", "const" : false, "inputs" : [ { "name" : "amount", "type" : "uint256" } ] }
372+
]`
373+
374+
abi, err := JSON(strings.NewReader(definition))
375+
if err != nil {
376+
t.Fatal(err)
377+
}
378+
ok := make([]byte, 20)
379+
_, err = abi.Pack("balance", ok)
380+
if err != nil {
381+
t.Error(err)
382+
}
383+
384+
toosmall := make([]byte, 19)
385+
_, err = abi.Pack("balance", toosmall)
386+
if err != nil {
387+
t.Error(err)
388+
}
389+
390+
toobig := make([]byte, 21)
391+
_, err = abi.Pack("balance", toobig)
392+
if err == nil {
393+
t.Error("expected error")
394+
}
395+
}

accounts/abi/type.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type Type struct {
4343
stringKind string // holds the unparsed string for deriving signatures
4444
}
4545

46-
// New type returns a fully parsed Type given by the input string or an error if it can't be parsed.
46+
// NewType returns a fully parsed Type given by the input string or an error if it can't be parsed.
4747
//
4848
// Strings can be in the format of:
4949
//
@@ -130,6 +130,10 @@ func NewType(t string) (typ Type, err error) {
130130
if vsize > 0 {
131131
typ.Size = 32
132132
}
133+
case "bytes":
134+
typ.Kind = reflect.Slice
135+
typ.Type = byte_ts
136+
typ.Size = vsize
133137
default:
134138
return Type{}, fmt.Errorf("unsupported arg type: %s", t)
135139
}
@@ -200,7 +204,13 @@ func (t Type) pack(v interface{}) ([]byte, error) {
200204
} else {
201205
return common.LeftPadBytes(common.Big0.Bytes(), 32), nil
202206
}
207+
case reflect.Array:
208+
if v, ok := value.Interface().(common.Address); ok {
209+
return t.pack(v[:])
210+
} else if v, ok := value.Interface().(common.Hash); ok {
211+
return t.pack(v[:])
212+
}
203213
}
204214

205-
panic("unreached")
215+
return nil, fmt.Errorf("ABI: bad input given %T", value.Kind())
206216
}

0 commit comments

Comments
 (0)