mirror of
https://github.com/luxfi/sdk.git
synced 2026-10-30 15:19:23 +00:00
Lux Partners Limited is not the entity behind this work. Every place the old name stood — copyright lines, site footers, brand config, legal and compliance text — now names the company that actually holds it. lux.fund is the exception and is not renamed to match: that site belongs to Lux Assets Management LLC, a different company, and it says so. Two kinds of line were left for a person rather than rewritten. A dated news item recording that Lux Partners Limited was established in the Isle of Man is a historical fact, and renaming it would falsify the record. And a sentence asserting that the company is domiciled in the Isle of Man is a claim about a jurisdiction, not a name — an Inc or an LLC is not an Isle of Man company, so swapping the name there would assert something untrue. Co-authored-by: Hanzo Dev <[email protected]>
222 lines
4.8 KiB
Go
222 lines
4.8 KiB
Go
// Copyright (C) 2022, Lux Industries Inc All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package prompts
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"math/big"
|
|
"net/mail"
|
|
"net/url"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/luxfi/address"
|
|
"github.com/luxfi/constants"
|
|
"github.com/luxfi/geth/common"
|
|
"github.com/luxfi/ids"
|
|
"github.com/luxfi/sdk/models"
|
|
"github.com/luxfi/sdk/ux"
|
|
)
|
|
|
|
func validateEmail(input string) error {
|
|
_, err := mail.ParseAddress(input)
|
|
return err
|
|
}
|
|
|
|
func ValidateURLFormat(input string) error {
|
|
if input == "" {
|
|
return errors.New("URL cannot be empty")
|
|
}
|
|
_, err := url.Parse(input)
|
|
return err
|
|
}
|
|
|
|
func validatePositiveBigInt(input string) error {
|
|
n := new(big.Int)
|
|
n, ok := n.SetString(input, 10)
|
|
if !ok {
|
|
return errors.New("invalid number")
|
|
}
|
|
if n.Cmp(big.NewInt(0)) == -1 {
|
|
return errors.New("invalid number")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateStakingDuration(input string) error {
|
|
d, err := time.ParseDuration(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d > constants.MaxStakeDuration {
|
|
return fmt.Errorf("exceeds maximum staking duration of %s", ux.FormatDuration(constants.MaxStakeDuration))
|
|
}
|
|
if d < constants.MinStakeDuration {
|
|
return fmt.Errorf("below the minimum staking duration of %s", ux.FormatDuration(constants.MinStakeDuration))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateTime(input string) error {
|
|
t, err := time.Parse(constants.TimeParseLayout, input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if t.Before(time.Now().Add(constants.StakingStartLeadTime)) {
|
|
return fmt.Errorf("time should be at least start from now + %s", constants.StakingStartLeadTime)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func validateNodeID(input string) error {
|
|
_, err := ids.NodeIDFromString(input)
|
|
return err
|
|
}
|
|
|
|
func validateAddress(input string) error {
|
|
if !common.IsHexAddress(input) {
|
|
return errors.New("invalid address")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateExistingFilepath(input string) error {
|
|
if fileInfo, err := os.Stat(input); err == nil && !fileInfo.IsDir() {
|
|
return nil
|
|
}
|
|
return errors.New("file doesn't exist")
|
|
}
|
|
|
|
func validateWeight(input string) error {
|
|
val, err := strconv.ParseUint(input, 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if val < constants.MinStakeWeight {
|
|
return errors.New("the weight must be an integer between 1 and 100")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateBiggerThanZero(input string) error {
|
|
val, err := strconv.ParseUint(input, 0, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if val == 0 {
|
|
return errors.New("the value must be bigger than zero")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validateURL(input string) error {
|
|
_, err := url.ParseRequestURI(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validatePChainAddress(input string) (string, error) {
|
|
chainID, hrp, _, err := address.Parse(input)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if chainID != "P" {
|
|
return "", errors.New("this is not a PChain address")
|
|
}
|
|
return hrp, nil
|
|
}
|
|
|
|
func validatePChainTestnetAddress(input string) error {
|
|
hrp, err := validatePChainAddress(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hrp != constants.TestnetHRP {
|
|
return errors.New("this is not a testnet address")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validatePChainMainAddress(input string) error {
|
|
hrp, err := validatePChainAddress(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hrp != constants.MainnetHRP {
|
|
return errors.New("this is not a mainnet address")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func validatePChainLocalAddress(input string) error {
|
|
hrp, err := validatePChainAddress(input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if hrp != constants.CustomHRP {
|
|
return errors.New("this is not a custom address")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getPChainValidationFunc(network models.Network) func(string) error {
|
|
switch network {
|
|
case models.Testnet:
|
|
return validatePChainTestnetAddress
|
|
case models.Mainnet:
|
|
return validatePChainMainAddress
|
|
case models.Local:
|
|
return validatePChainLocalAddress
|
|
default:
|
|
return func(string) error {
|
|
return errors.New("unsupported network")
|
|
}
|
|
}
|
|
}
|
|
|
|
func validateID(input string) error {
|
|
_, err := ids.FromString(input)
|
|
return err
|
|
}
|
|
|
|
func validateNewFilepath(input string) error {
|
|
if _, err := os.Stat(input); err != nil && os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return errors.New("file already exists")
|
|
}
|
|
|
|
// ValidateHexa validates hexadecimal string format
|
|
func ValidateHexa(input string) error {
|
|
if input == "" {
|
|
return errors.New("string cannot be empty")
|
|
}
|
|
if len(input) < 2 || strings.ToLower(input[:2]) != "0x" {
|
|
return errors.New("hexa string has not 0x prefix")
|
|
}
|
|
if len(input) == 2 {
|
|
return errors.New("no hexa digits in string")
|
|
}
|
|
_, err := hex.DecodeString(input[2:])
|
|
if err != nil {
|
|
return errors.New("string not in hexa format")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidatePositiveInt validates that an integer is positive
|
|
func ValidatePositiveInt(val int) error {
|
|
if val <= 0 {
|
|
return fmt.Errorf("value must be greater than zero")
|
|
}
|
|
return nil
|
|
}
|