github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/mobile/common.go (about)

     1  // Contains all the wrappers from the common package.
     2  
     3  package geth
     4  
     5  import (
     6  	"encoding/hex"
     7  	"errors"
     8  	"fmt"
     9  	"strings"
    10  
    11  	"github.com/quickchainproject/quickchain/common"
    12  )
    13  
    14  // Hash represents the 32 byte Keccak256 hash of arbitrary data.
    15  type Hash struct {
    16  	hash common.Hash
    17  }
    18  
    19  // NewHashFromBytes converts a slice of bytes to a hash value.
    20  func NewHashFromBytes(binary []byte) (hash *Hash, _ error) {
    21  	h := new(Hash)
    22  	if err := h.SetBytes(common.CopyBytes(binary)); err != nil {
    23  		return nil, err
    24  	}
    25  	return h, nil
    26  }
    27  
    28  // NewHashFromHex converts a hex string to a hash value.
    29  func NewHashFromHex(hex string) (hash *Hash, _ error) {
    30  	h := new(Hash)
    31  	if err := h.SetHex(hex); err != nil {
    32  		return nil, err
    33  	}
    34  	return h, nil
    35  }
    36  
    37  // SetBytes sets the specified slice of bytes as the hash value.
    38  func (h *Hash) SetBytes(hash []byte) error {
    39  	if length := len(hash); length != common.HashLength {
    40  		return fmt.Errorf("invalid hash length: %v != %v", length, common.HashLength)
    41  	}
    42  	copy(h.hash[:], hash)
    43  	return nil
    44  }
    45  
    46  // GetBytes retrieves the byte representation of the hash.
    47  func (h *Hash) GetBytes() []byte {
    48  	return h.hash[:]
    49  }
    50  
    51  // SetHex sets the specified hex string as the hash value.
    52  func (h *Hash) SetHex(hash string) error {
    53  	hash = strings.ToLower(hash)
    54  	if len(hash) >= 2 && hash[:2] == "0x" {
    55  		hash = hash[2:]
    56  	}
    57  	if length := len(hash); length != 2*common.HashLength {
    58  		return fmt.Errorf("invalid hash hex length: %v != %v", length, 2*common.HashLength)
    59  	}
    60  	bin, err := hex.DecodeString(hash)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	copy(h.hash[:], bin)
    65  	return nil
    66  }
    67  
    68  // GetHex retrieves the hex string representation of the hash.
    69  func (h *Hash) GetHex() string {
    70  	return h.hash.Hex()
    71  }
    72  
    73  // Hashes represents a slice of hashes.
    74  type Hashes struct{ hashes []common.Hash }
    75  
    76  // NewHashes creates a slice of uninitialized Hashes.
    77  func NewHashes(size int) *Hashes {
    78  	return &Hashes{
    79  		hashes: make([]common.Hash, size),
    80  	}
    81  }
    82  
    83  // NewHashesEmpty creates an empty slice of Hashes values.
    84  func NewHashesEmpty() *Hashes {
    85  	return NewHashes(0)
    86  }
    87  
    88  // Size returns the number of hashes in the slice.
    89  func (h *Hashes) Size() int {
    90  	return len(h.hashes)
    91  }
    92  
    93  // Get returns the hash at the given index from the slice.
    94  func (h *Hashes) Get(index int) (hash *Hash, _ error) {
    95  	if index < 0 || index >= len(h.hashes) {
    96  		return nil, errors.New("index out of bounds")
    97  	}
    98  	return &Hash{h.hashes[index]}, nil
    99  }
   100  
   101  // Set sets the Hash at the given index in the slice.
   102  func (h *Hashes) Set(index int, hash *Hash) error {
   103  	if index < 0 || index >= len(h.hashes) {
   104  		return errors.New("index out of bounds")
   105  	}
   106  	h.hashes[index] = hash.hash
   107  	return nil
   108  }
   109  
   110  // Append adds a new Hash element to the end of the slice.
   111  func (h *Hashes) Append(hash *Hash) {
   112  	h.hashes = append(h.hashes, hash.hash)
   113  }
   114  
   115  // Address represents the 20 byte address of an Ethereum account.
   116  type Address struct {
   117  	address common.Address
   118  }
   119  
   120  // NewAddressFromBytes converts a slice of bytes to a hash value.
   121  func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
   122  	a := new(Address)
   123  	if err := a.SetBytes(common.CopyBytes(binary)); err != nil {
   124  		return nil, err
   125  	}
   126  	return a, nil
   127  }
   128  
   129  // NewAddressFromHex converts a hex string to a address value.
   130  func NewAddressFromHex(hex string) (address *Address, _ error) {
   131  	a := new(Address)
   132  	if err := a.SetHex(hex); err != nil {
   133  		return nil, err
   134  	}
   135  	return a, nil
   136  }
   137  
   138  // SetBytes sets the specified slice of bytes as the address value.
   139  func (a *Address) SetBytes(address []byte) error {
   140  	if length := len(address); length != common.AddressLength {
   141  		return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength)
   142  	}
   143  	copy(a.address[:], address)
   144  	return nil
   145  }
   146  
   147  // GetBytes retrieves the byte representation of the address.
   148  func (a *Address) GetBytes() []byte {
   149  	return a.address[:]
   150  }
   151  
   152  // SetHex sets the specified hex string as the address value.
   153  func (a *Address) SetHex(address string) error {
   154  	address = strings.ToLower(address)
   155  	if len(address) >= 2 && address[:2] == "0x" {
   156  		address = address[2:]
   157  	}
   158  	if length := len(address); length != 2*common.AddressLength {
   159  		return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength)
   160  	}
   161  	bin, err := hex.DecodeString(address)
   162  	if err != nil {
   163  		return err
   164  	}
   165  	copy(a.address[:], bin)
   166  	return nil
   167  }
   168  
   169  // GetHex retrieves the hex string representation of the address.
   170  func (a *Address) GetHex() string {
   171  	return a.address.Hex()
   172  }
   173  
   174  // Addresses represents a slice of addresses.
   175  type Addresses struct{ addresses []common.Address }
   176  
   177  // NewAddresses creates a slice of uninitialized addresses.
   178  func NewAddresses(size int) *Addresses {
   179  	return &Addresses{
   180  		addresses: make([]common.Address, size),
   181  	}
   182  }
   183  
   184  // NewAddressesEmpty creates an empty slice of Addresses values.
   185  func NewAddressesEmpty() *Addresses {
   186  	return NewAddresses(0)
   187  }
   188  
   189  // Size returns the number of addresses in the slice.
   190  func (a *Addresses) Size() int {
   191  	return len(a.addresses)
   192  }
   193  
   194  // Get returns the address at the given index from the slice.
   195  func (a *Addresses) Get(index int) (address *Address, _ error) {
   196  	if index < 0 || index >= len(a.addresses) {
   197  		return nil, errors.New("index out of bounds")
   198  	}
   199  	return &Address{a.addresses[index]}, nil
   200  }
   201  
   202  // Set sets the address at the given index in the slice.
   203  func (a *Addresses) Set(index int, address *Address) error {
   204  	if index < 0 || index >= len(a.addresses) {
   205  		return errors.New("index out of bounds")
   206  	}
   207  	a.addresses[index] = address.address
   208  	return nil
   209  }
   210  
   211  // Append adds a new address element to the end of the slice.
   212  func (a *Addresses) Append(address *Address) {
   213  	a.addresses = append(a.addresses, address.address)
   214  }