common.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. "github.com/ethereum/go-ethereum/common/hexutil"
  25. )
  26. // Hash represents the 32 byte Keccak256 hash of arbitrary data.
  27. type Hash struct {
  28. hash common.Hash
  29. }
  30. // NewHashFromBytes converts a slice of bytes to a hash value.
  31. func NewHashFromBytes(binary []byte) (hash *Hash, _ error) {
  32. h := new(Hash)
  33. if err := h.SetBytes(common.CopyBytes(binary)); err != nil {
  34. return nil, err
  35. }
  36. return h, nil
  37. }
  38. // NewHashFromHex converts a hex string to a hash value.
  39. func NewHashFromHex(hex string) (hash *Hash, _ error) {
  40. h := new(Hash)
  41. if err := h.SetHex(hex); err != nil {
  42. return nil, err
  43. }
  44. return h, nil
  45. }
  46. // SetBytes sets the specified slice of bytes as the hash value.
  47. func (h *Hash) SetBytes(hash []byte) error {
  48. if length := len(hash); length != common.HashLength {
  49. return fmt.Errorf("invalid hash length: %v != %v", length, common.HashLength)
  50. }
  51. copy(h.hash[:], hash)
  52. return nil
  53. }
  54. // GetBytes retrieves the byte representation of the hash.
  55. func (h *Hash) GetBytes() []byte {
  56. return h.hash[:]
  57. }
  58. // SetHex sets the specified hex string as the hash value.
  59. func (h *Hash) SetHex(hash string) error {
  60. hash = strings.ToLower(hash)
  61. if len(hash) >= 2 && hash[:2] == "0x" {
  62. hash = hash[2:]
  63. }
  64. if length := len(hash); length != 2*common.HashLength {
  65. return fmt.Errorf("invalid hash hex length: %v != %v", length, 2*common.HashLength)
  66. }
  67. bin, err := hex.DecodeString(hash)
  68. if err != nil {
  69. return err
  70. }
  71. copy(h.hash[:], bin)
  72. return nil
  73. }
  74. // GetHex retrieves the hex string representation of the hash.
  75. func (h *Hash) GetHex() string {
  76. return h.hash.Hex()
  77. }
  78. // Hashes represents a slice of hashes.
  79. type Hashes struct{ hashes []common.Hash }
  80. // NewHashes creates a slice of uninitialized Hashes.
  81. func NewHashes(size int) *Hashes {
  82. return &Hashes{
  83. hashes: make([]common.Hash, size),
  84. }
  85. }
  86. // NewHashesEmpty creates an empty slice of Hashes values.
  87. func NewHashesEmpty() *Hashes {
  88. return NewHashes(0)
  89. }
  90. // Size returns the number of hashes in the slice.
  91. func (h *Hashes) Size() int {
  92. return len(h.hashes)
  93. }
  94. // Get returns the hash at the given index from the slice.
  95. func (h *Hashes) Get(index int) (hash *Hash, _ error) {
  96. if index < 0 || index >= len(h.hashes) {
  97. return nil, errors.New("index out of bounds")
  98. }
  99. return &Hash{h.hashes[index]}, nil
  100. }
  101. // Set sets the Hash at the given index in the slice.
  102. func (h *Hashes) Set(index int, hash *Hash) error {
  103. if index < 0 || index >= len(h.hashes) {
  104. return errors.New("index out of bounds")
  105. }
  106. h.hashes[index] = hash.hash
  107. return nil
  108. }
  109. // Append adds a new Hash element to the end of the slice.
  110. func (h *Hashes) Append(hash *Hash) {
  111. h.hashes = append(h.hashes, hash.hash)
  112. }
  113. // Address represents the 20 byte address of an Ethereum account.
  114. type Address struct {
  115. address common.Address
  116. }
  117. // NewAddressFromBytes converts a slice of bytes to a hash value.
  118. func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
  119. a := new(Address)
  120. if err := a.SetBytes(common.CopyBytes(binary)); err != nil {
  121. return nil, err
  122. }
  123. return a, nil
  124. }
  125. // NewAddressFromHex converts a hex string to a address value.
  126. func NewAddressFromHex(hex string) (address *Address, _ error) {
  127. a := new(Address)
  128. if err := a.SetHex(hex); err != nil {
  129. return nil, err
  130. }
  131. return a, nil
  132. }
  133. // SetBytes sets the specified slice of bytes as the address value.
  134. func (a *Address) SetBytes(address []byte) error {
  135. if length := len(address); length != common.AddressLength {
  136. return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength)
  137. }
  138. copy(a.address[:], address)
  139. return nil
  140. }
  141. // GetBytes retrieves the byte representation of the address.
  142. func (a *Address) GetBytes() []byte {
  143. return a.address[:]
  144. }
  145. // SetHex sets the specified hex string as the address value.
  146. func (a *Address) SetHex(address string) error {
  147. address = strings.ToLower(address)
  148. if len(address) >= 2 && address[:2] == "0x" {
  149. address = address[2:]
  150. }
  151. if length := len(address); length != 2*common.AddressLength {
  152. return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength)
  153. }
  154. bin, err := hex.DecodeString(address)
  155. if err != nil {
  156. return err
  157. }
  158. copy(a.address[:], bin)
  159. return nil
  160. }
  161. // GetHex retrieves the hex string representation of the address.
  162. func (a *Address) GetHex() string {
  163. return a.address.Hex()
  164. }
  165. // Addresses represents a slice of addresses.
  166. type Addresses struct{ addresses []common.Address }
  167. // NewAddresses creates a slice of uninitialized addresses.
  168. func NewAddresses(size int) *Addresses {
  169. return &Addresses{
  170. addresses: make([]common.Address, size),
  171. }
  172. }
  173. // NewAddressesEmpty creates an empty slice of Addresses values.
  174. func NewAddressesEmpty() *Addresses {
  175. return NewAddresses(0)
  176. }
  177. // Size returns the number of addresses in the slice.
  178. func (a *Addresses) Size() int {
  179. return len(a.addresses)
  180. }
  181. // Get returns the address at the given index from the slice.
  182. func (a *Addresses) Get(index int) (address *Address, _ error) {
  183. if index < 0 || index >= len(a.addresses) {
  184. return nil, errors.New("index out of bounds")
  185. }
  186. return &Address{a.addresses[index]}, nil
  187. }
  188. // Set sets the address at the given index in the slice.
  189. func (a *Addresses) Set(index int, address *Address) error {
  190. if index < 0 || index >= len(a.addresses) {
  191. return errors.New("index out of bounds")
  192. }
  193. a.addresses[index] = address.address
  194. return nil
  195. }
  196. // Append adds a new address element to the end of the slice.
  197. func (a *Addresses) Append(address *Address) {
  198. a.addresses = append(a.addresses, address.address)
  199. }
  200. // EncodeToHex encodes b as a hex string with 0x prefix.
  201. func EncodeToHex(b []byte) string {
  202. return hexutil.Encode(b)
  203. }
  204. // DecodeFromHex decodes a hex string with 0x prefix.
  205. func DecodeFromHex(s string) ([]byte, error) {
  206. return hexutil.Decode(s)
  207. }