github.com/benorgera/go-ethereum@v1.10.18-0.20220401011646-b3f57b1a73ba/accounts/abi/error.go (about)

     1  // Copyright 2021 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  	"errors"
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/ethereum/go-ethereum/common"
    26  	"github.com/ethereum/go-ethereum/crypto"
    27  )
    28  
    29  type Error struct {
    30  	Name   string
    31  	Inputs Arguments
    32  	str    string
    33  	// Sig contains the string signature according to the ABI spec.
    34  	// e.g.	 event foo(uint32 a, int b) = "foo(uint32,int256)"
    35  	// Please note that "int" is substitute for its canonical representation "int256"
    36  	Sig string
    37  	// ID returns the canonical representation of the event's signature used by the
    38  	// abi definition to identify event names and types.
    39  	ID common.Hash
    40  }
    41  
    42  func NewError(name string, inputs Arguments) Error {
    43  	// sanitize inputs to remove inputs without names
    44  	// and precompute string and sig representation.
    45  	names := make([]string, len(inputs))
    46  	types := make([]string, len(inputs))
    47  	for i, input := range inputs {
    48  		if input.Name == "" {
    49  			inputs[i] = Argument{
    50  				Name:    fmt.Sprintf("arg%d", i),
    51  				Indexed: input.Indexed,
    52  				Type:    input.Type,
    53  			}
    54  		} else {
    55  			inputs[i] = input
    56  		}
    57  		// string representation
    58  		names[i] = fmt.Sprintf("%v %v", input.Type, inputs[i].Name)
    59  		if input.Indexed {
    60  			names[i] = fmt.Sprintf("%v indexed %v", input.Type, inputs[i].Name)
    61  		}
    62  		// sig representation
    63  		types[i] = input.Type.String()
    64  	}
    65  
    66  	str := fmt.Sprintf("error %v(%v)", name, strings.Join(names, ", "))
    67  	sig := fmt.Sprintf("%v(%v)", name, strings.Join(types, ","))
    68  	id := common.BytesToHash(crypto.Keccak256([]byte(sig)))
    69  
    70  	return Error{
    71  		Name:   name,
    72  		Inputs: inputs,
    73  		str:    str,
    74  		Sig:    sig,
    75  		ID:     id,
    76  	}
    77  }
    78  
    79  func (e *Error) String() string {
    80  	return e.str
    81  }
    82  
    83  func (e *Error) Unpack(data []byte) (interface{}, error) {
    84  	if len(data) < 4 {
    85  		return "", errors.New("invalid data for unpacking")
    86  	}
    87  	if !bytes.Equal(data[:4], e.ID[:4]) {
    88  		return "", errors.New("invalid data for unpacking")
    89  	}
    90  	return e.Inputs.Unpack(data[4:])
    91  }