common.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(binary []byte) (hash *Hash, _ error) {
  31. h := new(Hash)
  32. if err := h.SetBytes(common.CopyBytes(binary)); 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(hex string) (hash *Hash, _ error) {
  39. h := new(Hash)
  40. if err := h.SetHex(hex); 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. // NewHashes creates a slice of uninitialized Hashes.
  80. func NewHashes(size int) *Hashes {
  81. return &Hashes{
  82. hashes: make([]common.Hash, size),
  83. }
  84. }
  85. // NewHashesEmpty creates an empty slice of Hashes values.
  86. func NewHashesEmpty() *Hashes {
  87. return NewHashes(0)
  88. }
  89. // Size returns the number of hashes in the slice.
  90. func (h *Hashes) Size() int {
  91. return len(h.hashes)
  92. }
  93. // Get returns the hash at the given index from the slice.
  94. func (h *Hashes) Get(index int) (hash *Hash, _ error) {
  95. if index < 0 || index >= len(h.hashes) {
  96. return nil, errors.New("index out of bounds")
  97. }
  98. return &Hash{h.hashes[index]}, nil
  99. }
  100. // Set sets the Hash at the given index in the slice.
  101. func (h *Hashes) Set(index int, hash *Hash) error {
  102. if index < 0 || index >= len(h.hashes) {
  103. return errors.New("index out of bounds")
  104. }
  105. h.hashes[index] = hash.hash
  106. return nil
  107. }
  108. // Append adds a new Hash element to the end of the slice.
  109. func (h *Hashes) Append(hash *Hash) {
  110. h.hashes = append(h.hashes, hash.hash)
  111. }
  112. // Address represents the 20 byte address of an Ethereum account.
  113. type Address struct {
  114. address common.Address
  115. }
  116. // NewAddressFromBytes converts a slice of bytes to a hash value.
  117. func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
  118. a := new(Address)
  119. if err := a.SetBytes(common.CopyBytes(binary)); err != nil {
  120. return nil, err
  121. }
  122. return a, nil
  123. }
  124. // NewAddressFromHex converts a hex string to a address value.
  125. func NewAddressFromHex(hex string) (address *Address, _ error) {
  126. a := new(Address)
  127. if err := a.SetHex(hex); err != nil {
  128. return nil, err
  129. }
  130. return a, nil
  131. }
  132. // SetBytes sets the specified slice of bytes as the address value.
  133. func (a *Address) SetBytes(address []byte) error {
  134. if length := len(address); length != common.AddressLength {
  135. return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength)
  136. }
  137. copy(a.address[:], address)
  138. return nil
  139. }
  140. // GetBytes retrieves the byte representation of the address.
  141. func (a *Address) GetBytes() []byte {
  142. return a.address[:]
  143. }
  144. // SetHex sets the specified hex string as the address value.
  145. func (a *Address) SetHex(address string) error {
  146. address = strings.ToLower(address)
  147. if len(address) >= 2 && address[:2] == "0x" {
  148. address = address[2:]
  149. }
  150. if length := len(address); length != 2*common.AddressLength {
  151. return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength)
  152. }
  153. bin, err := hex.DecodeString(address)
  154. if err != nil {
  155. return err
  156. }
  157. copy(a.address[:], bin)
  158. return nil
  159. }
  160. // GetHex retrieves the hex string representation of the address.
  161. func (a *Address) GetHex() string {
  162. return a.address.Hex()
  163. }
  164. // Addresses represents a slice of addresses.
  165. type Addresses struct{ addresses []common.Address }
  166. // NewAddresses creates a slice of uninitialized addresses.
  167. func NewAddresses(size int) *Addresses {
  168. return &Addresses{
  169. addresses: make([]common.Address, size),
  170. }
  171. }
  172. // NewAddressesEmpty creates an empty slice of Addresses values.
  173. func NewAddressesEmpty() *Addresses {
  174. return NewAddresses(0)
  175. }
  176. // Size returns the number of addresses in the slice.
  177. func (a *Addresses) Size() int {
  178. return len(a.addresses)
  179. }
  180. // Get returns the address at the given index from the slice.
  181. func (a *Addresses) Get(index int) (address *Address, _ error) {
  182. if index < 0 || index >= len(a.addresses) {
  183. return nil, errors.New("index out of bounds")
  184. }
  185. return &Address{a.addresses[index]}, nil
  186. }
  187. // Set sets the address at the given index in the slice.
  188. func (a *Addresses) Set(index int, address *Address) error {
  189. if index < 0 || index >= len(a.addresses) {
  190. return errors.New("index out of bounds")
  191. }
  192. a.addresses[index] = address.address
  193. return nil
  194. }
  195. // Append adds a new address element to the end of the slice.
  196. func (a *Addresses) Append(address *Address) {
  197. a.addresses = append(a.addresses, address.address)
  198. }