github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/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 "fmt" 21 "math/big" 22 "math/rand" 23 "reflect" 24 ) 25 26 const ( 27 hashLength = 32 28 addressLength = 20 29 ) 30 31 type ( 32 Hash [hashLength]byte 33 Address [addressLength]byte 34 ) 35 36 func BytesToHash(b []byte) Hash { 37 var h Hash 38 h.SetBytes(b) 39 return h 40 } 41 func StringToHash(s string) Hash { return BytesToHash([]byte(s)) } 42 func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) } 43 func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) } 44 45 // Don't use the default 'String' method in case we want to overwrite 46 47 // Get the string representation of the underlying hash 48 func (h Hash) Str() string { return string(h[:]) } 49 func (h Hash) Bytes() []byte { return h[:] } 50 func (h Hash) Big() *big.Int { return Bytes2Big(h[:]) } 51 func (h Hash) Hex() string { return "0x" + Bytes2Hex(h[:]) } 52 53 // Sets the hash to the value of b. If b is larger than len(h) it will panic 54 func (h *Hash) SetBytes(b []byte) { 55 if len(b) > len(h) { 56 b = b[len(b)-hashLength:] 57 } 58 59 copy(h[hashLength-len(b):], b) 60 } 61 62 // Set string `s` to h. If s is larger than len(h) it will panic 63 func (h *Hash) SetString(s string) { h.SetBytes([]byte(s)) } 64 65 // Sets h to other 66 func (h *Hash) Set(other Hash) { 67 for i, v := range other { 68 h[i] = v 69 } 70 } 71 72 // Generate implements testing/quick.Generator. 73 func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { 74 m := rand.Intn(len(h)) 75 for i := len(h) - 1; i > m; i-- { 76 h[i] = byte(rand.Uint32()) 77 } 78 return reflect.ValueOf(h) 79 } 80 81 func EmptyHash(h Hash) bool { 82 return h == Hash{} 83 } 84 85 /////////// Address 86 func BytesToAddress(b []byte) Address { 87 var a Address 88 a.SetBytes(b) 89 return a 90 } 91 func StringToAddress(s string) Address { return BytesToAddress([]byte(s)) } 92 func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) } 93 func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } 94 95 // Get the string representation of the underlying address 96 func (a Address) Str() string { return string(a[:]) } 97 func (a Address) Bytes() []byte { return a[:] } 98 func (a Address) Big() *big.Int { return Bytes2Big(a[:]) } 99 func (a Address) Hash() Hash { return BytesToHash(a[:]) } 100 func (a Address) Hex() string { return "0x" + Bytes2Hex(a[:]) } 101 102 // Sets the address to the value of b. If b is larger than len(a) it will panic 103 func (a *Address) SetBytes(b []byte) { 104 if len(b) > len(a) { 105 b = b[len(b)-addressLength:] 106 } 107 copy(a[addressLength-len(b):], b) 108 } 109 110 // Set string `s` to a. If s is larger than len(a) it will panic 111 func (a *Address) SetString(s string) { a.SetBytes([]byte(s)) } 112 113 // Sets a to other 114 func (a *Address) Set(other Address) { 115 for i, v := range other { 116 a[i] = v 117 } 118 } 119 120 // PP Pretty Prints a byte slice in the following format: 121 // hex(value[:4])...(hex[len(value)-4:]) 122 func PP(value []byte) string { 123 if len(value) <= 8 { 124 return Bytes2Hex(value) 125 } 126 127 return fmt.Sprintf("%x...%x", value[:4], value[len(value)-4]) 128 }