github.com/codingfuture/orig-energi3@v0.8.4/accounts/abi/abi.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  )
    25  
    26  // The ABI holds information about a contract's context and available
    27  // invokable methods. It will allow you to type check function calls and
    28  // packs data accordingly.
    29  type ABI struct {
    30  	Constructor Method
    31  	Methods     map[string]Method
    32  	Events      map[string]Event
    33  }
    34  
    35  // JSON returns a parsed ABI interface and error if it failed.
    36  func JSON(reader io.Reader) (ABI, error) {
    37  	dec := json.NewDecoder(reader)
    38  
    39  	var abi ABI
    40  	if err := dec.Decode(&abi); err != nil {
    41  		return ABI{}, err
    42  	}
    43  
    44  	return abi, nil
    45  }
    46  
    47  // Pack the given method name to conform the ABI. Method call's data
    48  // will consist of method_id, args0, arg1, ... argN. Method id consists
    49  // of 4 bytes and arguments are all 32 bytes.
    50  // Method ids are created from the first 4 bytes of the hash of the
    51  // methods string signature. (signature = baz(uint32,string32))
    52  func (abi ABI) Pack(name string, args ...interface{}) ([]byte, error) {
    53  	// Fetch the ABI of the requested method
    54  	if name == "" {
    55  		// constructor
    56  		arguments, err := abi.Constructor.Inputs.Pack(args...)
    57  		if err != nil {
    58  			return nil, err
    59  		}
    60  		return arguments, nil
    61  	}
    62  	method, exist := abi.Methods[name]
    63  	if !exist {
    64  		return nil, fmt.Errorf("method '%s' not found", name)
    65  	}
    66  	arguments, err := method.Inputs.Pack(args...)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	// Pack up the method ID too if not a constructor and return
    71  	return append(method.Id(), arguments...), nil
    72  }
    73  
    74  // Unpack output in v according to the abi specification
    75  func (abi ABI) Unpack(v interface{}, name string, output []byte) (err error) {
    76  	if len(output) == 0 {
    77  		return fmt.Errorf("abi: unmarshalling empty output")
    78  	}
    79  	// since there can't be naming collisions with contracts and events,
    80  	// we need to decide whether we're calling a method or an event
    81  	if method, ok := abi.Methods[name]; ok {
    82  		if len(output)%32 != 0 {
    83  			return fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(output), output)
    84  		}
    85  		return method.Outputs.Unpack(v, output)
    86  	} else if event, ok := abi.Events[name]; ok {
    87  		return event.Inputs.Unpack(v, output)
    88  	}
    89  	return fmt.Errorf("abi: could not locate named method or event")
    90  }
    91  
    92  // UnmarshalJSON implements json.Unmarshaler interface
    93  func (abi *ABI) UnmarshalJSON(data []byte) error {
    94  	var fields []struct {
    95  		Type      string
    96  		Name      string
    97  		Constant  bool
    98  		Anonymous bool
    99  		Inputs    []Argument
   100  		Outputs   []Argument
   101  	}
   102  
   103  	if err := json.Unmarshal(data, &fields); err != nil {
   104  		return err
   105  	}
   106  
   107  	abi.Methods = make(map[string]Method)
   108  	abi.Events = make(map[string]Event)
   109  	for _, field := range fields {
   110  		switch field.Type {
   111  		case "constructor":
   112  			abi.Constructor = Method{
   113  				Inputs: field.Inputs,
   114  			}
   115  		// empty defaults to function according to the abi spec
   116  		case "function", "":
   117  			abi.Methods[field.Name] = Method{
   118  				Name:    field.Name,
   119  				Const:   field.Constant,
   120  				Inputs:  field.Inputs,
   121  				Outputs: field.Outputs,
   122  			}
   123  		case "event":
   124  			abi.Events[field.Name] = Event{
   125  				Name:      field.Name,
   126  				Anonymous: field.Anonymous,
   127  				Inputs:    field.Inputs,
   128  			}
   129  		}
   130  	}
   131  
   132  	return nil
   133  }
   134  
   135  // MethodById looks up a method by the 4-byte id
   136  // returns nil if none found
   137  func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
   138  	if len(sigdata) < 4 {
   139  		return nil, fmt.Errorf("data too short (% bytes) for abi method lookup", len(sigdata))
   140  	}
   141  	for _, method := range abi.Methods {
   142  		if bytes.Equal(method.Id(), sigdata[:4]) {
   143  			return &method, nil
   144  		}
   145  	}
   146  	return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
   147  }