github.com/core-coin/go-core/v2@v2.1.9/mobile/common.go (about)

     1  // Copyright 2016 by the Authors
     2  // This file is part of the go-core library.
     3  //
     4  // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Contains all the wrappers from the common package.
    18  
    19  package gocore
    20  
    21  import (
    22  	"encoding/hex"
    23  	"errors"
    24  	"fmt"
    25  	"strings"
    26  
    27  	"github.com/core-coin/go-core/v2/common"
    28  	"github.com/core-coin/go-core/v2/common/hexutil"
    29  )
    30  
    31  // Hash represents the 32 byte SHA3 hash of arbitrary data.
    32  type Hash struct {
    33  	hash common.Hash
    34  }
    35  
    36  // NewHashFromBytes converts a slice of bytes to a hash value.
    37  func NewHashFromBytes(binary []byte) (hash *Hash, _ error) {
    38  	h := new(Hash)
    39  	if err := h.SetBytes(common.CopyBytes(binary)); err != nil {
    40  		return nil, err
    41  	}
    42  	return h, nil
    43  }
    44  
    45  // NewHashFromHex converts a hex string to a hash value.
    46  func NewHashFromHex(hex string) (hash *Hash, _ error) {
    47  	h := new(Hash)
    48  	if err := h.SetHex(hex); err != nil {
    49  		return nil, err
    50  	}
    51  	return h, nil
    52  }
    53  
    54  // SetBytes sets the specified slice of bytes as the hash value.
    55  func (h *Hash) SetBytes(hash []byte) error {
    56  	if length := len(hash); length != common.HashLength {
    57  		return fmt.Errorf("invalid hash length: %v != %v", length, common.HashLength)
    58  	}
    59  	copy(h.hash[:], hash)
    60  	return nil
    61  }
    62  
    63  // GetBytes retrieves the byte representation of the hash.
    64  func (h *Hash) GetBytes() []byte {
    65  	return h.hash[:]
    66  }
    67  
    68  // SetHex sets the specified hex string as the hash value.
    69  func (h *Hash) SetHex(hash string) error {
    70  	hash = strings.ToLower(hash)
    71  	if len(hash) >= 2 && hash[:2] == "0x" {
    72  		hash = hash[2:]
    73  	}
    74  	if length := len(hash); length != 2*common.HashLength {
    75  		return fmt.Errorf("invalid hash hex length: %v != %v", length, 2*common.HashLength)
    76  	}
    77  	bin, err := hex.DecodeString(hash)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	copy(h.hash[:], bin)
    82  	return nil
    83  }
    84  
    85  // GetHex retrieves the hex string representation of the hash.
    86  func (h *Hash) GetHex() string {
    87  	return h.hash.Hex()
    88  }
    89  
    90  // String implements Stringer interface for printable representation of the hash.
    91  func (h *Hash) String() string {
    92  	return h.GetHex()
    93  }
    94  
    95  // Hashes represents a slice of hashes.
    96  type Hashes struct{ hashes []common.Hash }
    97  
    98  // NewHashes creates a slice of uninitialized Hashes.
    99  func NewHashes(size int) *Hashes {
   100  	return &Hashes{
   101  		hashes: make([]common.Hash, size),
   102  	}
   103  }
   104  
   105  // NewHashesEmpty creates an empty slice of Hashes values.
   106  func NewHashesEmpty() *Hashes {
   107  	return NewHashes(0)
   108  }
   109  
   110  // Size returns the number of hashes in the slice.
   111  func (h *Hashes) Size() int {
   112  	return len(h.hashes)
   113  }
   114  
   115  // Get returns the hash at the given index from the slice.
   116  func (h *Hashes) Get(index int) (hash *Hash, _ error) {
   117  	if index < 0 || index >= len(h.hashes) {
   118  		return nil, errors.New("index out of bounds")
   119  	}
   120  	return &Hash{h.hashes[index]}, nil
   121  }
   122  
   123  // Set sets the Hash at the given index in the slice.
   124  func (h *Hashes) Set(index int, hash *Hash) error {
   125  	if index < 0 || index >= len(h.hashes) {
   126  		return errors.New("index out of bounds")
   127  	}
   128  	h.hashes[index] = hash.hash
   129  	return nil
   130  }
   131  
   132  // Append adds a new Hash element to the end of the slice.
   133  func (h *Hashes) Append(hash *Hash) {
   134  	h.hashes = append(h.hashes, hash.hash)
   135  }
   136  
   137  // Address represents the 20 byte address of an Core account.
   138  type Address struct {
   139  	address common.Address
   140  }
   141  
   142  // NewAddressFromBytes converts a slice of bytes to a hash value.
   143  func NewAddressFromBytes(binary []byte) (address *Address, _ error) {
   144  	a := new(Address)
   145  	if err := a.SetBytes(common.CopyBytes(binary)); err != nil {
   146  		return nil, err
   147  	}
   148  	return a, nil
   149  }
   150  
   151  // NewAddressFromHex converts a hex string to a address value.
   152  func NewAddressFromHex(hex string) (address *Address, _ error) {
   153  	a := new(Address)
   154  	if err := a.SetHex(hex); err != nil {
   155  		return nil, err
   156  	}
   157  	return a, nil
   158  }
   159  
   160  // SetBytes sets the specified slice of bytes as the address value.
   161  func (a *Address) SetBytes(address []byte) error {
   162  	if length := len(address); length != common.AddressLength {
   163  		return fmt.Errorf("invalid address length: %v != %v", length, common.AddressLength)
   164  	}
   165  	copy(a.address[:], address)
   166  	return nil
   167  }
   168  
   169  // GetBytes retrieves the byte representation of the address.
   170  func (a *Address) GetBytes() []byte {
   171  	return a.address[:]
   172  }
   173  
   174  // SetHex sets the specified hex string as the address value.
   175  func (a *Address) SetHex(address string) error {
   176  	address = strings.ToLower(address)
   177  	if len(address) >= 2 && address[:2] == "0x" {
   178  		address = address[2:]
   179  	}
   180  	if length := len(address); length != 2*common.AddressLength {
   181  		return fmt.Errorf("invalid address hex length: %v != %v", length, 2*common.AddressLength)
   182  	}
   183  	bin, err := hex.DecodeString(address)
   184  	if err != nil {
   185  		return err
   186  	}
   187  	copy(a.address[:], bin)
   188  	return nil
   189  }
   190  
   191  // GetHex retrieves the hex string representation of the address.
   192  func (a *Address) GetHex() string {
   193  	return a.address.Hex()
   194  }
   195  
   196  // String returns a printable representation of the address.
   197  func (a *Address) String() string {
   198  	return a.GetHex()
   199  }
   200  
   201  // Addresses represents a slice of addresses.
   202  type Addresses struct{ addresses []common.Address }
   203  
   204  // NewAddresses creates a slice of uninitialized addresses.
   205  func NewAddresses(size int) *Addresses {
   206  	return &Addresses{
   207  		addresses: make([]common.Address, size),
   208  	}
   209  }
   210  
   211  // NewAddressesEmpty creates an empty slice of Addresses values.
   212  func NewAddressesEmpty() *Addresses {
   213  	return NewAddresses(0)
   214  }
   215  
   216  // Size returns the number of addresses in the slice.
   217  func (a *Addresses) Size() int {
   218  	return len(a.addresses)
   219  }
   220  
   221  // Get returns the address at the given index from the slice.
   222  func (a *Addresses) Get(index int) (address *Address, _ error) {
   223  	if index < 0 || index >= len(a.addresses) {
   224  		return nil, errors.New("index out of bounds")
   225  	}
   226  	return &Address{a.addresses[index]}, nil
   227  }
   228  
   229  // Set sets the address at the given index in the slice.
   230  func (a *Addresses) Set(index int, address *Address) error {
   231  	if index < 0 || index >= len(a.addresses) {
   232  		return errors.New("index out of bounds")
   233  	}
   234  	a.addresses[index] = address.address
   235  	return nil
   236  }
   237  
   238  // Append adds a new address element to the end of the slice.
   239  func (a *Addresses) Append(address *Address) {
   240  	a.addresses = append(a.addresses, address.address)
   241  }
   242  
   243  // EncodeToHex encodes b as a hex string with 0x prefix.
   244  func EncodeToHex(b []byte) string {
   245  	return hexutil.Encode(b)
   246  }
   247  
   248  // DecodeFromHex decodes a hex string with 0x prefix.
   249  func DecodeFromHex(s string) ([]byte, error) {
   250  	return hexutil.Decode(s)
   251  }