go-ethereum/accounts/abi/abi.go

140 lines
3.8 KiB
Go
Raw Normal View History

2015-07-07 03:54:22 +03:00
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
2015-07-07 03:54:22 +03:00
//
// The go-ethereum library is free software: you can redistribute it and/or modify
2015-07-07 03:54:22 +03: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.
//
// The go-ethereum library is distributed in the hope that it will be useful,
2015-07-07 03:54:22 +03:00
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-07-07 03:54:22 +03:00
// 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/>.
2015-07-07 03:54:22 +03:00
2015-01-27 17:19:21 +02:00
package abi
import (
"encoding/json"
"fmt"
"io"
)
// The ABI holds information about a contract's context and available
// invokable methods. It will allow you to type check function calls and
// packs data accordingly.
type ABI struct {
Constructor Method
Methods map[string]Method
Events map[string]Event
2015-01-27 17:19:21 +02:00
}
// JSON returns a parsed ABI interface and error if it failed.
func JSON(reader io.Reader) (ABI, error) {
dec := json.NewDecoder(reader)
var abi ABI
if err := dec.Decode(&abi); err != nil {
return ABI{}, err
}
return abi, nil
}
2015-01-27 17:19:21 +02:00
// Pack the given method name to conform the ABI. Method call's data
// will consist of method_id, args0, arg1, ... argN. Method id consists
// of 4 bytes and arguments are all 32 bytes.
// Method ids are created from the first 4 bytes of the hash of the
// 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
2015-01-27 17:19:21 +02:00
if name == "" {
method = abi.Constructor
} else {
m, exist := abi.Methods[name]
if !exist {
return nil, fmt.Errorf("method '%s' not found", name)
}
method = m
}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 11:05:33 +03:00
arguments, err := method.pack(args...)
2015-01-27 17:19:21 +02:00
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
2015-01-27 17:19:21 +02:00
}
// 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
}
// 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
} 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 unpack.singleUnpack(v, output)
}
2015-01-27 17:19:21 +02:00
func (abi *ABI) UnmarshalJSON(data []byte) error {
var fields []struct {
Type string
Name string
Constant bool
Indexed bool
Anonymous bool
Inputs []Argument
Outputs []Argument
}
if err := json.Unmarshal(data, &fields); err != nil {
2015-01-27 17:19:21 +02:00
return err
}
abi.Methods = make(map[string]Method)
abi.Events = make(map[string]Event)
for _, field := range fields {
switch field.Type {
case "constructor":
abi.Constructor = Method{
Inputs: field.Inputs,
}
// empty defaults to function according to the abi spec
case "function", "":
abi.Methods[field.Name] = Method{
Name: field.Name,
Const: field.Constant,
Inputs: field.Inputs,
Outputs: field.Outputs,
}
case "event":
abi.Events[field.Name] = Event{
Name: field.Name,
Anonymous: field.Anonymous,
Inputs: field.Inputs,
}
}
2015-01-27 17:19:21 +02:00
}
return nil
}