error.go 2.6 KB

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