github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/mobile/common.go (about)

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