github.com/Gessiux/neatchain@v1.3.1/utilities/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 "encoding/hex" 21 "fmt" 22 "math/big" 23 "math/rand" 24 "reflect" 25 26 "github.com/Gessiux/neatchain/utilities/common/hexutil" 27 ) 28 29 const ( 30 HashLength = 32 31 //AddressLength = 20 32 NEATAddressLength = 32 33 ) 34 35 var ( 36 hashT = reflect.TypeOf(Hash{}) 37 addressT = reflect.TypeOf(Address{}) 38 ) 39 40 // Hash represents the 32 byte Keccak256 hash of arbitrary data. 41 type Hash [HashLength]byte 42 43 func BytesToHash(b []byte) Hash { 44 var h Hash 45 h.SetBytes(b) 46 return h 47 } 48 func StringToHash(s string) Hash { return BytesToHash([]byte(s)) } 49 func BigToHash(b *big.Int) Hash { return BytesToHash(b.Bytes()) } 50 func HexToHash(s string) Hash { return BytesToHash(FromHex(s)) } 51 52 // Get the string representation of the underlying hash 53 func (h Hash) Str() string { return string(h[:]) } 54 func (h Hash) Bytes() []byte { return h[:] } 55 func (h Hash) Big() *big.Int { return new(big.Int).SetBytes(h[:]) } 56 func (h Hash) Hex() string { return hexutil.Encode(h[:]) } 57 58 // TerminalString implements log.TerminalStringer, formatting a string for console 59 // output during logging. 60 func (h Hash) TerminalString() string { 61 return fmt.Sprintf("%x…%x", h[:3], h[29:]) 62 } 63 64 // String implements the stringer interface and is used also by the logger when 65 // doing full logging into a file. 66 func (h Hash) String() string { 67 return h.Hex() 68 } 69 70 // Format implements fmt.Formatter, forcing the byte slice to be formatted as is, 71 // without going through the stringer interface used for logging. 72 func (h Hash) Format(s fmt.State, c rune) { 73 fmt.Fprintf(s, "%"+string(c), h[:]) 74 } 75 76 // UnmarshalText parses a hash in hex syntax. 77 func (h *Hash) UnmarshalText(input []byte) error { 78 return hexutil.UnmarshalFixedText("Hash", input, h[:]) 79 } 80 81 // UnmarshalJSON parses a hash in hex syntax. 82 func (h *Hash) UnmarshalJSON(input []byte) error { 83 return hexutil.UnmarshalFixedJSON(hashT, input, h[:]) 84 } 85 86 // MarshalText returns the hex representation of h. 87 func (h Hash) MarshalText() ([]byte, error) { 88 return hexutil.Bytes(h[:]).MarshalText() 89 } 90 91 // Sets the hash to the value of b. If b is larger than len(h), 'b' will be cropped (from the left). 92 func (h *Hash) SetBytes(b []byte) { 93 if len(b) > len(h) { 94 b = b[len(b)-HashLength:] 95 } 96 97 copy(h[HashLength-len(b):], b) 98 } 99 100 // Set string `s` to h. If s is larger than len(h) s will be cropped (from left) to fit. 101 func (h *Hash) SetString(s string) { h.SetBytes([]byte(s)) } 102 103 // Sets h to other 104 func (h *Hash) Set(other Hash) { 105 for i, v := range other { 106 h[i] = v 107 } 108 } 109 110 // Generate implements testing/quick.Generator. 111 func (h Hash) Generate(rand *rand.Rand, size int) reflect.Value { 112 m := rand.Intn(len(h)) 113 for i := len(h) - 1; i > m; i-- { 114 h[i] = byte(rand.Uint32()) 115 } 116 return reflect.ValueOf(h) 117 } 118 119 func EmptyHash(h Hash) bool { 120 return h == Hash{} 121 } 122 123 // UnprefixedHash allows marshaling a Hash without 0x prefix. 124 type UnprefixedHash Hash 125 126 // UnmarshalText decodes the hash from hex. The 0x prefix is optional. 127 func (h *UnprefixedHash) UnmarshalText(input []byte) error { 128 return hexutil.UnmarshalFixedUnprefixedText("UnprefixedHash", input, h[:]) 129 } 130 131 // MarshalText encodes the hash as hex. 132 func (h UnprefixedHash) MarshalText() ([]byte, error) { 133 return []byte(hex.EncodeToString(h[:])), nil 134 } 135 136 /////////// Address 137 138 // Address represents the 20 byte address of an Ethereum account. 139 //type Address [AddressLength]byte 140 141 // NEAT address 142 type Address [NEATAddressLength]byte 143 144 func BytesToAddress(b []byte) Address { 145 var a Address 146 a.SetBytes(b) 147 return a 148 } 149 func StringToAddress(s string) Address { return BytesToAddress([]byte(s)) } 150 func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) } 151 func HexToAddress(s string) Address { return BytesToAddress(FromHex(s)) } 152 153 // IsHexAddress verifies whether a string can represent a valid hex-encoded 154 // Ethereum address or not. 155 //func IsHexAddress(s string) bool { 156 // if hasHexPrefix(s) { 157 // s = s[2:] 158 // } 159 // return len(s) == 2*AddressLength && isHex(s) 160 //} 161 162 func IsHexAddress(s string) bool { 163 if hasHexPrefix(s) { 164 s = s[2:] 165 } 166 167 return len(s) == 2*NEATAddressLength && isHex(s) 168 } 169 170 // Get the string representation of the underlying address 171 func (a Address) Str() string { return string(a[:]) } 172 func (a Address) Bytes() []byte { return a[:] } 173 func (a Address) Big() *big.Int { return new(big.Int).SetBytes(a[:]) } 174 func (a Address) Hash() Hash { return BytesToHash(a[:]) } 175 176 // Hex returns an EIP55-compliant hex string representation of the address. 177 //func (a Address) Hex() string { 178 // unchecksummed := hex.EncodeToString(a[:]) 179 // sha := sha3.NewKeccak256() 180 // sha.Write([]byte(unchecksummed)) 181 // hash := sha.Sum(nil) 182 // 183 // result := []byte(unchecksummed) 184 // for i := 0; i < len(result); i++ { 185 // hashByte := hash[i/2] 186 // if i%2 == 0 { 187 // hashByte = hashByte >> 4 188 // } else { 189 // hashByte &= 0xf 190 // } 191 // if result[i] > '9' && hashByte > 7 { 192 // result[i] -= 32 193 // } 194 // } 195 // return "0x" + string(result) 196 //} 197 198 func (a Address) Hex() string { 199 unchecksummed := hex.EncodeToString(a[:]) 200 result := []byte(unchecksummed) 201 return "0x" + string(result) 202 } 203 204 //func (a Address) IsValidate() bool { 205 // addrStr := string(a[:]) 206 // return crypto.ValidateNEATAddr(addrStr) 207 //} 208 209 // String implements the stringer interface and is used also by the logger. 210 func (a Address) String() string { 211 return string(a[:]) 212 } 213 214 // Format implements fmt.Formatter, forcing the byte slice to be formatted as is, 215 // without going through the stringer interface used for logging. 216 func (a Address) Format(s fmt.State, c rune) { 217 fmt.Fprintf(s, "%"+string(c), a[:]) 218 } 219 220 // Sets the address to the value of b. If b is larger than len(a) it will panic 221 //func (a *Address) SetBytes(b []byte) { 222 // if len(b) > len(a) { 223 // b = b[len(b)-AddressLength:] 224 // } 225 // copy(a[AddressLength-len(b):], b) 226 //} 227 228 func (a *Address) SetBytes(b []byte) { 229 if len(b) > NEATAddressLength { 230 b = b[len(b)-NEATAddressLength:] 231 } 232 copy(a[NEATAddressLength-len(b):], b) 233 } 234 235 // Set string `s` to a. If s is larger than len(a) it will panic 236 func (a *Address) SetString(s string) { a.SetBytes([]byte(s)) } 237 238 // Sets a to other 239 func (a *Address) Set(other Address) { 240 for i, v := range other { 241 a[i] = v 242 } 243 } 244 245 // MarshalText returns the hex representation of a. 246 func (a Address) MarshalText() ([]byte, error) { 247 //fmt.Printf("types Address MarshalText address=%v\n", a) 248 return hexutil.Bytes(a[:]).MarshalText() 249 } 250 251 // UnmarshalText parses a hash in hex syntax. 252 func (a *Address) UnmarshalText(input []byte) error { 253 fmt.Printf("types Address UnmarshalText address=%v, input=%v\n", a, input) 254 return hexutil.UnmarshalFixedText("Address", input, a[:]) 255 } 256 257 // UnmarshalJSON parses a hash in hex syntax. 258 //func (a *Address) UnmarshalJSON(input []byte) error { 259 // fmt.Printf("types Address UnmarshalJSON address=%v, input=%v\n", a, input) 260 // return hexutil.UnmarshalFixedJSON(addressT, input, a[:]) 261 //} 262 263 // New UnmarshalJSON parses a hash in hex syntax. 264 func (a *Address) UnmarshalJSON(input []byte) error { 265 fmt.Printf("types Address UnmarshalJSON address=%v, input=%v\n", a, input) 266 return hexutil.UnmarshalAddrFixedJSON(addressT, input, a[:]) 267 } 268 269 // UnprefixedHash allows marshaling an Address without 0x prefix. 270 type UnprefixedAddress Address 271 272 // UnmarshalText decodes the address from hex. The 0x prefix is optional. 273 func (a *UnprefixedAddress) UnmarshalText(input []byte) error { 274 //fmt.Printf("types UnprefixedAddress UnmarshalText address=%v, input=%v\n", a, input) 275 return hexutil.UnmarshalFixedUnprefixedText("UnprefixedAddress", input, a[:]) 276 } 277 278 // MarshalText encodes the address as hex. 279 func (a UnprefixedAddress) MarshalText() ([]byte, error) { 280 //fmt.Printf("types UnprefixedAddress MarshalText address=%v\n", a) 281 return []byte(hex.EncodeToString(a[:])), nil 282 }