github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/common/types.go (about) 1 // Copyright 2015 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 package common 18 19 import ( 20 "database/sql/driver" 21 "encoding/hex" 22 "encoding/json" 23 "fmt" 24 "math/big" 25 "math/rand" 26 "reflect" 27 "strings" 28 29 "github.com/ethereum/go-ethereum/common/hexutil" 30 "golang.org/x/crypto/sha3" 31 ) 32 33 // Lengths of hashes and addresses in bytes. 34 const ( 35 // HashLength is the expected length of the hash 36 HashLength = 32 37 // AddressLength is the expected length of the address 38 AddressLength = 20 39 ) 40 41 var ( 42 hashT = reflect.TypeOf(Hash{}) 43 addressT = reflect.TypeOf(Address{}) 44 ) 45 46 // Hash represents the 32 byte Keccak256 hash of arbitrary data. 47 type Hash [HashLength]byte 48 49 // BytesToHash sets b to hash. 50 // If b is larger than len(h), b will be cropped from the left. 51 func BytesToHash(b []byte) Hash { 52 var h Hash 53 h.SetBytes(b) 54 return h 55 } 56 57 // BigToHash sets byte representation of b to hash. 58 // If b is larger than len(h), b will be cropped from the left. 59 func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) } 60 61 // HexToHash sets byte representation of s to hash. 62 // If b is larger than len(h), b will be cropped from the left. 63 func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) } 64 65 // Bytes gets the byte representation of the underlying hash. 66 func (h Hash) Bytes() []byte { return h[:] } 67 68 // Big converts a hash to a big integer. 69 func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) } 70 71 // Hex converts a hash to a hex string. 72 func (h Hash) Hex() string { return hexutil.Encode(h[:]) } 73 74 // TerminalString implements log.TerminalStringer, formatting a string for console 75 // output during logging. 76 func (h Hash) TerminalString() string { 77 return fmt.Sprintf("%x…%x", h[:3], h[29:]) 78 } 79 80 // String implements the stringer interface and is used also by the logger when 81 // doing full logging into a file. 82 func (h Hash) String() string { 83 return h.Hex() 84 } 85 86 // Format implements fmt.Formatter, forcing the byte slice to be formatted as is, 87 // without going through the stringer interface used for logging. 88 func (h Hash) Format(s fmt.State, c rune) { 89 fmt.Fprintf(s, "%"+string(c), h[:]) 90 } 91 92 // UnmarshalText parses a hash in hex syntax. 93 func (h *Hash) UnmarshalText(input []byte) error { 94 return hexutil.UnmarshalFixedText("Hash", input, h[:]) 95 } 96 97 // UnmarshalJSON parses a hash in hex syntax. 98 func (h *Hash) UnmarshalJSON(input []byte) error { 99 return hexutil.UnmarshalFixedJSON(hashT, input, h[:]) 100 } 101 102 // MarshalText returns the hex representation of h. 103 func (h Hash) MarshalText() ([]byte, error) { 104 return hexutil.Bytes(h[:]).MarshalText() 105 } 106 107 // SetBytes sets the hash to the value of b. 108 // If b is larger than len(h), b will be cropped from the left. 109 func (h *Hash) SetBytes(b []byte) { 110 if len(b) > len(h) { 111 b = b[len(b)-HashLength:] 112 } 113 114 copy(h[HashLength-len(b):], b) 115 } 116 117 // Generate implements testing/quick.Generator. 118 func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { 119 m := rand.Intn(len(h)) 120 for i := len(h) - 1; i > m; i-- { 121 h[i] = byte(rand.Uint32()) 122 } 123 return reflect.ValueOf(h) 124 } 125 126 // Scan implements Scanner for database/sql. 127 func (h *Hash) Scan(src interface{}) error { 128 srcB, ok := src.([]byte) 129 if !ok { 130 return fmt.Errorf("can't scan %T into Hash", src) 131 } 132 if len(srcB) != HashLength { 133 return fmt.Errorf("can't scan []byte of len %d into Hash, want %d", len(srcB), HashLength) 134 } 135 copy(h[:], srcB) 136 return nil 137 } 138 139 // Value implements valuer for database/sql. 140 func (h Hash) Value() (driver.Value, error) { 141 return h[:], nil 142 } 143 144 // ImplementsGraphQLType returns true if Hash implements the specified GraphQL type. 145 func (_ Hash) ImplementsGraphQLType(name string) bool { return name == "Bytes32" } 146 147 // UnmarshalGraphQL unmarshals the provided GraphQL query data. 148 func (h *Hash) UnmarshalGraphQL(input interface{}) error { 149 var err error 150 switch input := input.(type) { 151 case string: 152 *h = HexToHash(input) 153 default: 154 err = fmt.Errorf("Unexpected type for Bytes32: %v", input) 155 } 156 return err 157 } 158 159 // UnprefixedHash allows marshaling a Hash without 0x prefix. 160 type UnprefixedHash Hash 161 162 // UnmarshalText decodes the hash from hex. The 0x prefix is optional. 163 func (h *UnprefixedHash) UnmarshalText(input []byte) error { 164 return hexutil.UnmarshalFixedUnprefixedText("UnprefixedHash", input, h[:]) 165 } 166 167 // MarshalText encodes the hash as hex. 168 func (h UnprefixedHash) MarshalText() ([]byte, error) { 169 return []byte(hex.EncodeToString(h[:])), nil 170 } 171 172 /////////// Address 173 174 // Address represents the 20 byte address of an Ethereum account. 175 type Address [AddressLength]byte 176 177 // BytesToAddress returns Address with value b. 178 // If b is larger than len(h), b will be cropped from the left. 179 func BytesToAddress(b []byte) Address { 180 var a Address 181 a.SetBytes(b) 182 return a 183 } 184 185 // BigToAddress returns Address with byte values of b. 186 // If b is larger than len(h), b will be cropped from the left. 187 func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) } 188 189 // HexToAddress returns Address with byte values of s. 190 // If s is larger than len(h), s will be cropped from the left. 191 func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } 192 193 // IsHexAddress verifies whether a string can represent a valid hex-encoded 194 // Ethereum address or not. 195 func IsHexAddress(s string) bool { 196 if hasHexPrefix(s) { 197 s = s[2:] 198 } 199 return len(s) == 2*AddressLength && isHex(s) 200 } 201 202 // Bytes gets the string representation of the underlying address. 203 func (a Address) Bytes() []byte { return a[:] } 204 205 // Big converts an address to a big integer. 206 func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) } 207 208 // Hash converts an address to a hash by left-padding it with zeros. 209 func (a Address) Hash() Hash { return BytesToHash(a[:]) } 210 211 // Hex returns an EIP55-compliant hex string representation of the address. 212 func (a Address) Hex() string { 213 unchecksummed := hex.EncodeToString(a[:]) 214 sha := sha3.NewLegacyKeccak256() 215 sha.Write([]byte(unchecksummed)) 216 hash := sha.Sum(nil) 217 218 result := []byte(unchecksummed) 219 for i := 0; i < len(result); i++ { 220 hashByte := hash[i/2] 221 if i%2 == 0 { 222 hashByte = hashByte >> 4 223 } else { 224 hashByte &= 0xf 225 } 226 if result[i] > '9' && hashByte > 7 { 227 result[i] -= 32 228 } 229 } 230 return "0x" + string(result) 231 } 232 233 // String implements fmt.Stringer. 234 func (a Address) String() string { 235 return a.Hex() 236 } 237 238 // Format implements fmt.Formatter, forcing the byte slice to be formatted as is, 239 // without going through the stringer interface used for logging. 240 func (a Address) Format(s fmt.State, c rune) { 241 fmt.Fprintf(s, "%"+string(c), a[:]) 242 } 243 244 // SetBytes sets the address to the value of b. 245 // If b is larger than len(a) it will panic. 246 func (a *Address) SetBytes(b []byte) { 247 if len(b) > len(a) { 248 b = b[len(b)-AddressLength:] 249 } 250 copy(a[AddressLength-len(b):], b) 251 } 252 253 // MarshalText returns the hex representation of a. 254 func (a Address) MarshalText() ([]byte, error) { 255 return hexutil.Bytes(a[:]).MarshalText() 256 } 257 258 // UnmarshalText parses a hash in hex syntax. 259 func (a *Address) UnmarshalText(input []byte) error { 260 return hexutil.UnmarshalFixedText("Address", input, a[:]) 261 } 262 263 // UnmarshalJSON parses a hash in hex syntax. 264 func (a *Address) UnmarshalJSON(input []byte) error { 265 return hexutil.UnmarshalFixedJSON(addressT, input, a[:]) 266 } 267 268 // Scan implements Scanner for database/sql. 269 func (a *Address) Scan(src interface{}) error { 270 srcB, ok := src.([]byte) 271 if !ok { 272 return fmt.Errorf("can't scan %T into Address", src) 273 } 274 if len(srcB) != AddressLength { 275 return fmt.Errorf("can't scan []byte of len %d into Address, want %d", len(srcB), AddressLength) 276 } 277 copy(a[:], srcB) 278 return nil 279 } 280 281 // Value implements valuer for database/sql. 282 func (a Address) Value() (driver.Value, error) { 283 return a[:], nil 284 } 285 286 // ImplementsGraphQLType returns true if Hash implements the specified GraphQL type. 287 func (a Address) ImplementsGraphQLType(name string) bool { return name == "Address" } 288 289 // UnmarshalGraphQL unmarshals the provided GraphQL query data. 290 func (a *Address) UnmarshalGraphQL(input interface{}) error { 291 var err error 292 switch input := input.(type) { 293 case string: 294 *a = HexToAddress(input) 295 default: 296 err = fmt.Errorf("Unexpected type for Address: %v", input) 297 } 298 return err 299 } 300 301 // UnprefixedAddress allows marshaling an Address without 0x prefix. 302 type UnprefixedAddress Address 303 304 // UnmarshalText decodes the address from hex. The 0x prefix is optional. 305 func (a *UnprefixedAddress) UnmarshalText(input []byte) error { 306 return hexutil.UnmarshalFixedUnprefixedText("UnprefixedAddress", input, a[:]) 307 } 308 309 // MarshalText encodes the address as hex. 310 func (a UnprefixedAddress) MarshalText() ([]byte, error) { 311 return []byte(hex.EncodeToString(a[:])), nil 312 } 313 314 // MixedcaseAddress retains the original string, which may or may not be 315 // correctly checksummed 316 type MixedcaseAddress struct { 317 addr Address 318 original string 319 } 320 321 // NewMixedcaseAddress constructor (mainly for testing) 322 func NewMixedcaseAddress(addr Address) MixedcaseAddress { 323 return MixedcaseAddress{addr: addr, original: addr.Hex()} 324 } 325 326 // NewMixedcaseAddressFromString is mainly meant for unit-testing 327 func NewMixedcaseAddressFromString(hexaddr string) (*MixedcaseAddress, error) { 328 if !IsHexAddress(hexaddr) { 329 return nil, fmt.Errorf("Invalid address") 330 } 331 a := FromHex(hexaddr) 332 return &MixedcaseAddress{addr: BytesToAddress(a), original: hexaddr}, nil 333 } 334 335 // UnmarshalJSON parses MixedcaseAddress 336 func (ma *MixedcaseAddress) UnmarshalJSON(input []byte) error { 337 if err := hexutil.UnmarshalFixedJSON(addressT, input, ma.addr[:]); err != nil { 338 return err 339 } 340 return json.Unmarshal(input, &ma.original) 341 } 342 343 // MarshalJSON marshals the original value 344 func (ma *MixedcaseAddress) MarshalJSON() ([]byte, error) { 345 if strings.HasPrefix(ma.original, "0x") || strings.HasPrefix(ma.original, "0X") { 346 return json.Marshal(fmt.Sprintf("0x%s", ma.original[2:])) 347 } 348 return json.Marshal(fmt.Sprintf("0x%s", ma.original)) 349 } 350 351 // Address returns the address 352 func (ma *MixedcaseAddress) Address() Address { 353 return ma.addr 354 } 355 356 // String implements fmt.Stringer 357 func (ma *MixedcaseAddress) String() string { 358 if ma.ValidChecksum() { 359 return fmt.Sprintf("%s [chksum ok]", ma.original) 360 } 361 return fmt.Sprintf("%s [chksum INVALID]", ma.original) 362 } 363 364 // ValidChecksum returns true if the address has valid checksum 365 func (ma *MixedcaseAddress) ValidChecksum() bool { 366 return ma.original == ma.addr.Hex() 367 } 368 369 // Original returns the mixed-case input string 370 func (ma *MixedcaseAddress) Original() string { 371 return ma.original 372 }