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