common.go 6.8 KB

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