common.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Contains all the wrappers from the common package.
  17. package geth
  18. import (
  19. "encoding/hex"
  20. "errors"
  21. "fmt"
  22. "strings"
  23. "github.com/ethereum/go-ethereum/common"
  24. )
  25. // Hash represents the 32 byte Keccak256 hash of arbitrary data.
  26. type Hash struct {
  27. hash common.Hash
  28. }
  29. // NewHashFromBytes converts a slice of bytes to a hash value.
  30. func NewHashFromBytes(hash []byte) (*Hash, error) {
  31. h := new(Hash)
  32. if err := h.SetBytes(hash); err != nil {
  33. return nil, err
  34. }
  35. return h, nil
  36. }
  37. // NewHashFromHex converts a hex string to a hash value.
  38. func NewHashFromHex(hash string) (*Hash, error) {
  39. h := new(Hash)
  40. if err := h.SetHex(hash); err != nil {
  41. return nil, err
  42. }
  43. return h, nil
  44. }
  45. // SetBytes sets the specified slice of bytes as the hash value.
  46. func (h *Hash) SetBytes(hash []byte) error {
  47. if length := len(hash); length != common.HashLength {
  48. return fmt.Errorf("invalid hash length: %v != %v", length, common.HashLength)
  49. }
  50. copy(h.hash[:], hash)
  51. return nil
  52. }
  53. // GetBytes retrieves the byte representation of the hash.
  54. func (h *Hash) GetBytes() []byte {
  55. return h.hash[:]
  56. }
  57. // SetHex sets the specified hex string as the hash value.
  58. func (h *Hash) SetHex(hash string) error {
  59. hash = strings.ToLower(hash)
  60. if len(hash) >= 2 && hash[:2] == "0x" {
  61. hash = hash[2:]
  62. }
  63. if length := len(hash); length != 2*common.HashLength {
  64. return fmt.Errorf("invalid hash hex length: %v != %v", length, 2*common.HashLength)
  65. }
  66. bin, err := hex.DecodeString(hash)
  67. if err != nil {
  68. return err
  69. }
  70. copy(h.hash[:], bin)
  71. return nil
  72. }
  73. // GetHex retrieves the hex string representation of the hash.
  74. func (h *Hash) GetHex() string {
  75. return h.hash.Hex()
  76. }
  77. // Hashes represents a slice of hashes.
  78. type Hashes struct{ hashes []common.Hash }
  79. // Size returns the number of hashes in the slice.
  80. func (h *Hashes) Size() int {
  81. return len(h.hashes)
  82. }
  83. // Get returns the hash at the given index from the slice.
  84. func (h *Hashes) Get(index int) (*Hash, error) {
  85. if index < 0 || index >= len(h.hashes) {
  86. return nil, errors.New("index out of bounds")
  87. }
  88. return &Hash{h.hashes[index]}, nil
  89. }
  90. // Address represents the 20 byte address of an Ethereum account.
  91. type Address struct {
  92. address common.Address
  93. }
  94. // NewAddressFromBytes converts a slice of bytes to a hash value.
  95. func NewAddressFromBytes(address []byte) (*Address, error) {
  96. a := new(Address)
  97. if err := a.SetBytes(address); err != nil {
  98. return nil, err
  99. }
  100. return a, nil
  101. }
  102. // NewAddressFromHex converts a hex string to a address value.
  103. func NewAddressFromHex(address string) (*Address, error) {
  104. a := new(Address)
  105. if err := a.SetHex(address); err != nil {
  106. return nil, err
  107. }
  108. return a, nil
  109. }
  110. // SetBytes sets the specified slice of bytes as the address value.
  111. func (a *Address) SetBytes(address []byte) error {
  112. if length := len(address); length != common.AddressLength {
  113. return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength)
  114. }
  115. copy(a.address[:], address)
  116. return nil
  117. }
  118. // GetBytes retrieves the byte representation of the address.
  119. func (a *Address) GetBytes() []byte {
  120. return a.address[:]
  121. }
  122. // SetHex sets the specified hex string as the address value.
  123. func (a *Address) SetHex(address string) error {
  124. address = strings.ToLower(address)
  125. if len(address) >= 2 && address[:2] == "0x" {
  126. address = address[2:]
  127. }
  128. if length := len(address); length != 2*common.AddressLength {
  129. return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength)
  130. }
  131. bin, err := hex.DecodeString(address)
  132. if err != nil {
  133. return err
  134. }
  135. copy(a.address[:], bin)
  136. return nil
  137. }
  138. // GetHex retrieves the hex string representation of the address.
  139. func (a *Address) GetHex() string {
  140. return a.address.Hex()
  141. }
  142. // Addresses represents a slice of addresses.
  143. type Addresses struct{ addresses []common.Address }
  144. // Size returns the number of addresses in the slice.
  145. func (a *Addresses) Size() int {
  146. return len(a.addresses)
  147. }
  148. // Get returns the address at the given index from the slice.
  149. func (a *Addresses) Get(index int) (*Address, error) {
  150. if index < 0 || index >= len(a.addresses) {
  151. return nil, errors.New("index out of bounds")
  152. }
  153. return &Address{a.addresses[index]}, nil
  154. }
  155. // Set sets the address at the given index in the slice.
  156. func (a *Addresses) Set(index int, address *Address) error {
  157. if index < 0 || index >= len(a.addresses) {
  158. return errors.New("index out of bounds")
  159. }
  160. a.addresses[index] = address.address
  161. return nil
  162. }