github.com/ava-labs/subnet-evm@v0.6.4/accounts/abi/error.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2016 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package abi
    28  
    29  import (
    30  	"bytes"
    31  	"errors"
    32  	"fmt"
    33  	"strings"
    34  
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/crypto"
    37  )
    38  
    39  type Error struct {
    40  	Name   string
    41  	Inputs Arguments
    42  	str    string
    43  
    44  	// Sig contains the string signature according to the ABI spec.
    45  	// e.g. error foo(uint32 a, int b) = "foo(uint32,int256)"
    46  	// Please note that "int" is substitute for its canonical representation "int256"
    47  	Sig string
    48  
    49  	// ID returns the canonical representation of the error's signature used by the
    50  	// abi definition to identify event names and types.
    51  	ID common.Hash
    52  }
    53  
    54  func NewError(name string, inputs Arguments) Error {
    55  	// sanitize inputs to remove inputs without names
    56  	// and precompute string and sig representation.
    57  	names := make([]string, len(inputs))
    58  	types := make([]string, len(inputs))
    59  	for i, input := range inputs {
    60  		if input.Name == "" {
    61  			inputs[i] = Argument{
    62  				Name:    fmt.Sprintf("arg%d", i),
    63  				Indexed: input.Indexed,
    64  				Type:    input.Type,
    65  			}
    66  		} else {
    67  			inputs[i] = input
    68  		}
    69  		// string representation
    70  		names[i] = fmt.Sprintf("%v %v", input.Type, inputs[i].Name)
    71  		if input.Indexed {
    72  			names[i] = fmt.Sprintf("%v indexed %v", input.Type, inputs[i].Name)
    73  		}
    74  		// sig representation
    75  		types[i] = input.Type.String()
    76  	}
    77  
    78  	str := fmt.Sprintf("error %v(%v)", name, strings.Join(names, ", "))
    79  	sig := fmt.Sprintf("%v(%v)", name, strings.Join(types, ","))
    80  	id := common.BytesToHash(crypto.Keccak256([]byte(sig)))
    81  
    82  	return Error{
    83  		Name:   name,
    84  		Inputs: inputs,
    85  		str:    str,
    86  		Sig:    sig,
    87  		ID:     id,
    88  	}
    89  }
    90  
    91  func (e Error) String() string {
    92  	return e.str
    93  }
    94  
    95  func (e *Error) Unpack(data []byte) (interface{}, error) {
    96  	if len(data) < 4 {
    97  		return "", errors.New("invalid data for unpacking")
    98  	}
    99  	if !bytes.Equal(data[:4], e.ID[:4]) {
   100  		return "", errors.New("invalid data for unpacking")
   101  	}
   102  	return e.Inputs.Unpack(data[4:])
   103  }