github.com/gochain-io/gochain/v3@v3.2.8/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 geth
    20  
    21  import (
    22  	"encoding/hex"
    23  	"errors"
    24  	"fmt"
    25  	"strings"
    26  
    27  	"github.com/gochain-io/gochain/v3/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(common.CopyBytes(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  // NewHashes creates a slice of uninitialized Hashes.
    93  func NewHashes(size int) *Hashes {
    94  	return &Hashes{
    95  		hashes: make([]common.Hash, size),
    96  	}
    97  }
    98  
    99  // NewHashesEmpty creates an empty slice of Hashes values.
   100  func NewHashesEmpty() *Hashes {
   101  	return NewHashes(0)
   102  }
   103  
   104  // Size returns the number of hashes in the slice.
   105  func (h *Hashes) Size() int {
   106  	return len(h.hashes)
   107  }
   108  
   109  // Get returns the hash at the given index from the slice.
   110  func (h *Hashes) Get(index int) (hash *Hash, _ error) {
   111  	if index < 0 || index >= len(h.hashes) {
   112  		return nil, errors.New("index out of bounds")
   113  	}
   114  	return &Hash{h.hashes[index]}, nil
   115  }
   116  
   117  // Set sets the Hash at the given index in the slice.
   118  func (h *Hashes) Set(index int, hash *Hash) error {
   119  	if index < 0 || index >= len(h.hashes) {
   120  		return errors.New("index out of bounds")
   121  	}
   122  	h.hashes[index] = hash.hash
   123  	return nil
   124  }
   125  
   126  // Append adds a new Hash element to the end of the slice.
   127  func (h *Hashes) Append(hash *Hash) {
   128  	h.hashes = append(h.hashes, hash.hash)
   129  }
   130  
   131  // Address represents the 20 byte address of an Ethereum account.
   132  type Address struct {
   133  	address common.Address
   134  }
   135  
   136  // NewAddressFromBytes converts a slice of bytes to a hash value.
   137  func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
   138  	a := new(Address)
   139  	if err := a.SetBytes(common.CopyBytes(binary)); err != nil {
   140  		return nil, err
   141  	}
   142  	return a, nil
   143  }
   144  
   145  // NewAddressFromHex converts a hex string to a address value.
   146  func NewAddressFromHex(hex string) (address *Address, _ error) {
   147  	a := new(Address)
   148  	if err := a.SetHex(hex); err != nil {
   149  		return nil, err
   150  	}
   151  	return a, nil
   152  }
   153  
   154  // SetBytes sets the specified slice of bytes as the address value.
   155  func (a *Address) SetBytes(address []byte) error {
   156  	if length := len(address); length != common.AddressLength {
   157  		return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength)
   158  	}
   159  	copy(a.address[:], address)
   160  	return nil
   161  }
   162  
   163  // GetBytes retrieves the byte representation of the address.
   164  func (a *Address) GetBytes() []byte {
   165  	return a.address[:]
   166  }
   167  
   168  // SetHex sets the specified hex string as the address value.
   169  func (a *Address) SetHex(address string) error {
   170  	address = strings.ToLower(address)
   171  	if len(address) >= 2 && address[:2] == "0x" {
   172  		address = address[2:]
   173  	}
   174  	if length := len(address); length != 2*common.AddressLength {
   175  		return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength)
   176  	}
   177  	bin, err := hex.DecodeString(address)
   178  	if err != nil {
   179  		return err
   180  	}
   181  	copy(a.address[:], bin)
   182  	return nil
   183  }
   184  
   185  // GetHex retrieves the hex string representation of the address.
   186  func (a *Address) GetHex() string {
   187  	return a.address.Hex()
   188  }
   189  
   190  // Addresses represents a slice of addresses.
   191  type Addresses struct{ addresses []common.Address }
   192  
   193  // NewAddresses creates a slice of uninitialized addresses.
   194  func NewAddresses(size int) *Addresses {
   195  	return &Addresses{
   196  		addresses: make([]common.Address, size),
   197  	}
   198  }
   199  
   200  // NewAddressesEmpty creates an empty slice of Addresses values.
   201  func NewAddressesEmpty() *Addresses {
   202  	return NewAddresses(0)
   203  }
   204  
   205  // Size returns the number of addresses in the slice.
   206  func (a *Addresses) Size() int {
   207  	return len(a.addresses)
   208  }
   209  
   210  // Get returns the address at the given index from the slice.
   211  func (a *Addresses) Get(index int) (address *Address, _ error) {
   212  	if index < 0 || index >= len(a.addresses) {
   213  		return nil, errors.New("index out of bounds")
   214  	}
   215  	return &Address{a.addresses[index]}, nil
   216  }
   217  
   218  // Set sets the address at the given index in the slice.
   219  func (a *Addresses) Set(index int, address *Address) error {
   220  	if index < 0 || index >= len(a.addresses) {
   221  		return errors.New("index out of bounds")
   222  	}
   223  	a.addresses[index] = address.address
   224  	return nil
   225  }
   226  
   227  // Append adds a new address element to the end of the slice.
   228  func (a *Addresses) Append(address *Address) {
   229  	a.addresses = append(a.addresses, address.address)
   230  }