github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/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, data []byte) (err error) {
    76  	if len(data) == 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(data)%32 != 0 {
    83  			return fmt.Errorf("abi: improperly formatted output: %s - Bytes: [%+v]", string(data), data)
    84  		}
    85  		return method.Outputs.Unpack(v, data)
    86  	}
    87  	if event, ok := abi.Events[name]; ok {
    88  		return event.Inputs.Unpack(v, data)
    89  	}
    90  	return fmt.Errorf("abi: could not locate named method or event")
    91  }
    92  
    93  // UnpackIntoMap unpacks a log into the provided map[string]interface{}
    94  func (abi ABI) UnpackIntoMap(v map[string]interface{}, name string, data []byte) (err error) {
    95  	if len(data) == 0 {
    96  		return fmt.Errorf("abi: unmarshalling empty output")
    97  	}
    98  	// since there can't be naming collisions with contracts and events,
    99  	// we need to decide whether we're calling a method or an event
   100  	if method, ok := abi.Methods[name]; ok {
   101  		if len(data)%32 != 0 {
   102  			return fmt.Errorf("abi: improperly formatted output")
   103  		}
   104  		return method.Outputs.UnpackIntoMap(v, data)
   105  	}
   106  	if event, ok := abi.Events[name]; ok {
   107  		return event.Inputs.UnpackIntoMap(v, data)
   108  	}
   109  	return fmt.Errorf("abi: could not locate named method or event")
   110  }
   111  
   112  // UnmarshalJSON implements json.Unmarshaler interface
   113  func (abi *ABI) UnmarshalJSON(data []byte) error {
   114  	var fields []struct {
   115  		Type      string
   116  		Name      string
   117  		Constant  bool
   118  		Anonymous bool
   119  		Inputs    []Argument
   120  		Outputs   []Argument
   121  	}
   122  
   123  	if err := json.Unmarshal(data, &fields); err != nil {
   124  		return err
   125  	}
   126  
   127  	abi.Methods = make(map[string]Method)
   128  	abi.Events = make(map[string]Event)
   129  	for _, field := range fields {
   130  		switch field.Type {
   131  		case "constructor":
   132  			abi.Constructor = Method{
   133  				Inputs: field.Inputs,
   134  			}
   135  		// empty defaults to function according to the abi spec
   136  		case "function", "":
   137  			abi.Methods[field.Name] = Method{
   138  				Name:    field.Name,
   139  				Const:   field.Constant,
   140  				Inputs:  field.Inputs,
   141  				Outputs: field.Outputs,
   142  			}
   143  		case "event":
   144  			abi.Events[field.Name] = Event{
   145  				Name:      field.Name,
   146  				Anonymous: field.Anonymous,
   147  				Inputs:    field.Inputs,
   148  			}
   149  		}
   150  	}
   151  
   152  	return nil
   153  }
   154  
   155  // MethodById looks up a method by the 4-byte id
   156  // returns nil if none found
   157  func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
   158  	if len(sigdata) < 4 {
   159  		return nil, fmt.Errorf("data too short (%d bytes) for abi method lookup", len(sigdata))
   160  	}
   161  	for _, method := range abi.Methods {
   162  		if bytes.Equal(method.Id(), sigdata[:4]) {
   163  			return &method, nil
   164  		}
   165  	}
   166  	return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
   167  }