github.com/annchain/OG@v0.0.9/common/address.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  	"github.com/annchain/OG/arefactor/common/utilfuncs"
    22  	"math/big"
    23  	"math/rand"
    24  	"reflect"
    25  
    26  	"github.com/annchain/OG/common/hexutil"
    27  )
    28  
    29  //go:generate msgp
    30  //msgp:tuple Address
    31  
    32  // Length of Addresses in bytes.
    33  const (
    34  	AddressLength = 20
    35  )
    36  
    37  var (
    38  	addressT = reflect.TypeOf(Address{})
    39  )
    40  
    41  // Address represents the 20 byte of address.
    42  //msgp:tuple Address
    43  type Address struct {
    44  	Bytes [AddressLength]byte `msgp:"bytes"`
    45  }
    46  
    47  // BytesToAddress sets b to hash.
    48  // If b is larger than len(h), b will be cropped from the left.
    49  func BytesToAddress(b []byte) Address {
    50  	var h Address
    51  	h.MustSetBytes(b)
    52  	return h
    53  }
    54  
    55  func StringToAddress(s string) (add Address, err error) {
    56  	var h Address
    57  	bytes, err := FromHex(s)
    58  	if err != nil {
    59  		return
    60  	}
    61  	err = h.SetBytes(bytes)
    62  	return h, err
    63  }
    64  
    65  // BigToAddress sets byte representation of b to Address.
    66  // If b is larger than len(h), b will be cropped from the left.
    67  func BigToAddress(b *big.Int) Address { return BytesToAddress(b.Bytes()) }
    68  
    69  // HexToAddress sets byte representation of s to Address.
    70  // If b is larger than len(h), b will be cropped from the left.
    71  func HexToAddress(s string) (address Address, err error) {
    72  	bytes, err := FromHex(s)
    73  	if err != nil {
    74  		return
    75  	}
    76  	address = BytesToAddress(bytes)
    77  	return
    78  }
    79  
    80  func HexToAddressNoError(s string) (address Address) {
    81  	address, err := HexToAddress(s)
    82  	utilfuncs.PanicIfError(err, "hexToAddress")
    83  	return
    84  }
    85  
    86  // ToBytes convers Address to []byte.
    87  func (h Address) ToBytes() []byte { return h.Bytes[:] }
    88  
    89  // Big converts an Address to a big integer.
    90  func (h Address) Big() *big.Int { return new(big.Int).SetBytes(h.Bytes[:]) }
    91  
    92  // Hex converts a Address to a hex string.
    93  func (h Address) Hex() string { return hexutil.Encode(h.Bytes[:]) }
    94  
    95  // TerminalString implements log.TerminalStringer, formatting a string for console
    96  // output during logging.
    97  func (h Address) TerminalString() string {
    98  	return fmt.Sprintf("%x…%x", h.Bytes[:3], h.Bytes[len(h.Bytes)-3:])
    99  }
   100  
   101  func (h Address) ShortString() string {
   102  	return hexutil.Encode(h.Bytes[:8])
   103  }
   104  
   105  // String implements the stringer interface and is used also by the logger when
   106  // doing full logging into a file.
   107  func (h Address) String() string {
   108  	return h.Hex()
   109  }
   110  
   111  // Format implements fmt.Formatter, forcing the byte slice to be formatted as is,
   112  // without going through the stringer interface used for logging.
   113  func (h Address) Format(s fmt.State, c rune) {
   114  	fmt.Fprintf(s, "%"+string(c), h.Bytes)
   115  }
   116  
   117  // UnmarshalText parses an Address in hex syntax.
   118  func (h *Address) UnmarshalText(input []byte) error {
   119  	return hexutil.UnmarshalFixedText("Address", input, h.Bytes[:])
   120  }
   121  
   122  // UnmarshalJSON parses an Address in hex syntax.
   123  func (h *Address) UnmarshalJSON(input []byte) error {
   124  	return hexutil.UnmarshalFixedJSON(addressT, input, h.Bytes[:])
   125  }
   126  
   127  // MarshalText returns the hex representation of h.
   128  func (h Address) MarshalText() ([]byte, error) {
   129  	return hexutil.Bytes(h.Bytes[:]).MarshalText()
   130  }
   131  
   132  // SetBytes sets the Address to the value of b.
   133  // If b is larger than len(h), panic. It usually indicates a logic error.
   134  func (h *Address) MustSetBytes(b []byte) {
   135  	if len(b) > AddressLength {
   136  		panic(fmt.Sprintf("byte to set is longer than expected length: %d > %d", len(b), AddressLength))
   137  	}
   138  	h.Bytes = [AddressLength]byte{}
   139  	copy(h.Bytes[:], b)
   140  }
   141  
   142  func (h *Address) SetBytes(b []byte) error {
   143  	if len(b) > AddressLength {
   144  		return fmt.Errorf("byte to set is longer than expected length: %d > %d", len(b), AddressLength)
   145  	}
   146  	h.Bytes = [AddressLength]byte{}
   147  	copy(h.Bytes[:], b)
   148  	return nil
   149  }
   150  
   151  // Generate implements testing/quick.Generator.
   152  func (h Address) Generate(rand *rand.Rand, size int) reflect.Value {
   153  	m := rand.Intn(AddressLength)
   154  	for i := AddressLength - 1; i > m; i-- {
   155  		h.Bytes[i] = byte(rand.Uint32())
   156  	}
   157  	return reflect.ValueOf(h)
   158  }