aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/abi.go
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/abi/abi.go')
-rw-r--r--accounts/abi/abi.go59
1 files changed, 33 insertions, 26 deletions
diff --git a/accounts/abi/abi.go b/accounts/abi/abi.go
index 205dc300b..abcb403db 100644
--- a/accounts/abi/abi.go
+++ b/accounts/abi/abi.go
@@ -17,6 +17,7 @@
package abi
import (
+ "bytes"
"encoding/json"
"fmt"
"io"
@@ -50,57 +51,52 @@ func JSON(reader io.Reader) (ABI, error) {
// methods string signature. (signature = baz(uint32,string32))
func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
// Fetch the ABI of the requested method
- var method Method
-
if name == "" {
- method = abi.Constructor
- } else {
- m, exist := abi.Methods[name]
- if !exist {
- return nil, fmt.Errorf("method '%s' not found", name)
+ // constructor
+ arguments, err := abi.Constructor.Inputs.Pack(args...)
+ if err != nil {
+ return nil, err
}
- method = m
+ return arguments, nil
+
}
- arguments, err := method.pack(args...)
+ method, exist := abi.Methods[name]
+ if !exist {
+ return nil, fmt.Errorf("method '%s' not found", name)
+ }
+
+ arguments, err := method.Inputs.Pack(args...)
if err != nil {
return nil, err
}
// Pack up the method ID too if not a constructor and return
- if name == "" {
- return arguments, nil
- }
return append(method.Id(), arguments...), nil
}
// Unpack output in v according to the abi specification
func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
- if err = bytesAreProper(output); err != nil {
- return err
+ if len(output) == 0 {
+ return fmt.Errorf("abi: unmarshalling empty output")
}
// since there can't be naming collisions with contracts and events,
// we need to decide whether we're calling a method or an event
- var unpack unpacker
if method, ok := abi.Methods[name]; ok {
- unpack = method
+ if len(output)%32 != 0 {
+ return fmt.Errorf("abi: improperly formatted output")
+ }
+ return method.Outputs.Unpack(v, output)
} else if event, ok := abi.Events[name]; ok {
- unpack = event
- } else {
- return fmt.Errorf("abi: could not locate named method or event.")
- }
-
- // requires a struct to unpack into for a tuple return...
- if unpack.isTupleReturn() {
- return unpack.tupleUnpack(v, output)
+ return event.Inputs.Unpack(v, output)
}
- return unpack.singleUnpack(v, output)
+ return fmt.Errorf("abi: could not locate named method or event")
}
+// UnmarshalJSON implements json.Unmarshaler interface
func (abi *ABI) UnmarshalJSON(data []byte) error {
var fields []struct {
Type string
Name string
Constant bool
- Indexed bool
Anonymous bool
Inputs []Argument
Outputs []Argument
@@ -137,3 +133,14 @@ func (abi *ABI) UnmarshalJSON(data []byte) error {
return nil
}
+
+// MethodById looks up a method by the 4-byte id
+// returns nil if none found
+func (abi *ABI) MethodById(sigdata []byte) *Method {
+ for _, method := range abi.Methods {
+ if bytes.Equal(method.Id(), sigdata[:4]) {
+ return &method
+ }
+ }
+ return nil
+}