github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/validator/validator.go (about)

     1  // Copyright (c) 2019 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package validator
     7  
     8  import (
     9  	"math/big"
    10  
    11  	"github.com/pkg/errors"
    12  
    13  	"github.com/iotexproject/iotex-address/address"
    14  )
    15  
    16  // Errors
    17  var (
    18  	// ErrInvalidAddr indicates error for an invalid address
    19  	ErrInvalidAddr = errors.New("invalid IoTeX address")
    20  	// ErrLongAlias indicates error for a long alias more than 40 characters
    21  	ErrLongAlias = errors.New("invalid long alias that is more than 40 characters")
    22  	// ErrNonPositiveNumber indicates error for a non-positive number
    23  	ErrNonPositiveNumber = errors.New("invalid number that is not positive")
    24  	// ErrInvalidStakeDuration indicates error for invalid stake duration
    25  	ErrInvalidStakeDuration = errors.New("stake duration must be within 0 and 1050 and in multiples of 7")
    26  )
    27  
    28  const (
    29  	// IoAddrLen defines length of IoTeX address
    30  	IoAddrLen = 41
    31  )
    32  
    33  // ValidateAddress validates IoTeX address
    34  func ValidateAddress(addr string) error {
    35  	if _, err := address.FromString(addr); err != nil {
    36  		return ErrInvalidAddr
    37  	}
    38  
    39  	return nil
    40  }
    41  
    42  // ValidateAlias validates alias for account
    43  func ValidateAlias(alias string) error {
    44  	if len(alias) > 40 {
    45  		return ErrLongAlias
    46  	}
    47  
    48  	return nil
    49  }
    50  
    51  // ValidatePositiveNumber validates positive Number for action
    52  func ValidatePositiveNumber(number int64) error {
    53  	if number <= 0 {
    54  		return ErrNonPositiveNumber
    55  	}
    56  
    57  	return nil
    58  }
    59  
    60  // ValidateStakeDuration validates stake duration for native staking
    61  func ValidateStakeDuration(stakeDuration *big.Int) error {
    62  	stakeDurationInt := stakeDuration.Int64()
    63  	if stakeDurationInt%7 != 0 || stakeDurationInt < 0 || stakeDurationInt > 1050 {
    64  		return ErrInvalidStakeDuration
    65  	}
    66  
    67  	return nil
    68  }