github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/mobile/common.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 // Contains all the wrappers from the common package. 13 14 package geth 15 16 import ( 17 "encoding/hex" 18 "errors" 19 "fmt" 20 "strings" 21 22 "github.com/Sberex/go-sberex/common" 23 ) 24 25 // Hash represents the 32 byte Keccak256 hash of arbitrary data. 26 type Hash struct { 27 hash common.Hash 28 } 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 39 // NewHashFromHex converts a hex string to a hash value. 40 func NewHashFromHex(hex string) (hash *Hash, _ error) { 41 h := new(Hash) 42 if err := h.SetHex(hex); err != nil { 43 return nil, err 44 } 45 return h, nil 46 } 47 48 // SetBytes sets the specified slice of bytes as the hash value. 49 func (h *Hash) SetBytes(hash []byte) error { 50 if length := len(hash); length != common.HashLength { 51 return fmt.Errorf("invalid hash length: %v != %v", length, common.HashLength) 52 } 53 copy(h.hash[:], hash) 54 return nil 55 } 56 57 // GetBytes retrieves the byte representation of the hash. 58 func (h *Hash) GetBytes() []byte { 59 return h.hash[:] 60 } 61 62 // SetHex sets the specified hex string as the hash value. 63 func (h *Hash) SetHex(hash string) error { 64 hash = strings.ToLower(hash) 65 if len(hash) >= 2 && hash[:2] == "0x" { 66 hash = hash[2:] 67 } 68 if length := len(hash); length != 2*common.HashLength { 69 return fmt.Errorf("invalid hash hex length: %v != %v", length, 2*common.HashLength) 70 } 71 bin, err := hex.DecodeString(hash) 72 if err != nil { 73 return err 74 } 75 copy(h.hash[:], bin) 76 return nil 77 } 78 79 // GetHex retrieves the hex string representation of the hash. 80 func (h *Hash) GetHex() string { 81 return h.hash.Hex() 82 } 83 84 // Hashes represents a slice of hashes. 85 type Hashes struct{ hashes []common.Hash } 86 87 // NewHashes creates a slice of uninitialized Hashes. 88 func NewHashes(size int) *Hashes { 89 return &Hashes{ 90 hashes: make([]common.Hash, size), 91 } 92 } 93 94 // NewHashesEmpty creates an empty slice of Hashes values. 95 func NewHashesEmpty() *Hashes { 96 return NewHashes(0) 97 } 98 99 // Size returns the number of hashes in the slice. 100 func (h *Hashes) Size() int { 101 return len(h.hashes) 102 } 103 104 // Get returns the hash at the given index from the slice. 105 func (h *Hashes) Get(index int) (hash *Hash, _ error) { 106 if index < 0 || index >= len(h.hashes) { 107 return nil, errors.New("index out of bounds") 108 } 109 return &Hash{h.hashes[index]}, nil 110 } 111 112 // Set sets the Hash at the given index in the slice. 113 func (h *Hashes) Set(index int, hash *Hash) error { 114 if index < 0 || index >= len(h.hashes) { 115 return errors.New("index out of bounds") 116 } 117 h.hashes[index] = hash.hash 118 return nil 119 } 120 121 // Append adds a new Hash element to the end of the slice. 122 func (h *Hashes) Append(hash *Hash) { 123 h.hashes = append(h.hashes, hash.hash) 124 } 125 126 // Address represents the 20 byte address of an Sberex account. 127 type Address struct { 128 address common.Address 129 } 130 131 // NewAddressFromBytes converts a slice of bytes to a hash value. 132 func NewAddressFromBytes(binary []byte) (address *Address, _ error) { 133 a := new(Address) 134 if err := a.SetBytes(common.CopyBytes(binary)); err != nil { 135 return nil, err 136 } 137 return a, nil 138 } 139 140 // NewAddressFromHex converts a hex string to a address value. 141 func NewAddressFromHex(hex string) (address *Address, _ error) { 142 a := new(Address) 143 if err := a.SetHex(hex); err != nil { 144 return nil, err 145 } 146 return a, nil 147 } 148 149 // SetBytes sets the specified slice of bytes as the address value. 150 func (a *Address) SetBytes(address []byte) error { 151 if length := len(address); length != common.AddressLength { 152 return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength) 153 } 154 copy(a.address[:], address) 155 return nil 156 } 157 158 // GetBytes retrieves the byte representation of the address. 159 func (a *Address) GetBytes() []byte { 160 return a.address[:] 161 } 162 163 // SetHex sets the specified hex string as the address value. 164 func (a *Address) SetHex(address string) error { 165 address = strings.ToLower(address) 166 if len(address) >= 2 && address[:2] == "0x" { 167 address = address[2:] 168 } 169 if length := len(address); length != 2*common.AddressLength { 170 return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength) 171 } 172 bin, err := hex.DecodeString(address) 173 if err != nil { 174 return err 175 } 176 copy(a.address[:], bin) 177 return nil 178 } 179 180 // GetHex retrieves the hex string representation of the address. 181 func (a *Address) GetHex() string { 182 return a.address.Hex() 183 } 184 185 // Addresses represents a slice of addresses. 186 type Addresses struct{ addresses []common.Address } 187 188 // NewAddresses creates a slice of uninitialized addresses. 189 func NewAddresses(size int) *Addresses { 190 return &Addresses{ 191 addresses: make([]common.Address, size), 192 } 193 } 194 195 // NewAddressesEmpty creates an empty slice of Addresses values. 196 func NewAddressesEmpty() *Addresses { 197 return NewAddresses(0) 198 } 199 200 // Size returns the number of addresses in the slice. 201 func (a *Addresses) Size() int { 202 return len(a.addresses) 203 } 204 205 // Get returns the address at the given index from the slice. 206 func (a *Addresses) Get(index int) (address *Address, _ error) { 207 if index < 0 || index >= len(a.addresses) { 208 return nil, errors.New("index out of bounds") 209 } 210 return &Address{a.addresses[index]}, nil 211 } 212 213 // Set sets the address at the given index in the slice. 214 func (a *Addresses) Set(index int, address *Address) error { 215 if index < 0 || index >= len(a.addresses) { 216 return errors.New("index out of bounds") 217 } 218 a.addresses[index] = address.address 219 return nil 220 } 221 222 // Append adds a new address element to the end of the slice. 223 func (a *Addresses) Append(address *Address) { 224 a.addresses = append(a.addresses, address.address) 225 }