github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/validate/transvalidate.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package validate
     6  
     7  import (
     8  	"strconv"
     9  	"strings"
    10  
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    12  )
    13  
    14  func IsTransHash(str string) bool {
    15  	str = clearDirectional(str)
    16  
    17  	if !strings.HasPrefix(str, "0x") {
    18  		return false
    19  	}
    20  
    21  	if len(str) != 66 {
    22  		return false
    23  	}
    24  
    25  	if !base.IsHex(str) {
    26  		return false
    27  	}
    28  
    29  	return true
    30  }
    31  
    32  func IsTransIndex(str string) bool {
    33  	str = clearDirectional(str)
    34  
    35  	base := 10
    36  	source := str
    37  
    38  	if strings.HasPrefix(str, "0x") {
    39  		base = 16
    40  		source = str[2:]
    41  	}
    42  
    43  	_, err := strconv.ParseUint(source, base, 64)
    44  	return err == nil
    45  }
    46  
    47  func IsTransBlockNumAndId(strIn string) bool {
    48  	str := clearDirectional(strIn)
    49  
    50  	parts := strings.Split(str, ".")
    51  	if len(parts) != 2 {
    52  		return false
    53  	}
    54  
    55  	validBlockNumber, _ := IsBlockNumber(parts[0])
    56  	if parts[1] == "*" {
    57  		return validBlockNumber
    58  	}
    59  	return validBlockNumber && IsTransIndex(parts[1])
    60  }
    61  
    62  func IsTransBlockHashAndId(str string) bool {
    63  	str = clearDirectional(str)
    64  
    65  	parts := strings.Split(str, ".")
    66  	if len(parts) != 2 {
    67  		return false
    68  	}
    69  
    70  	validBlockHash := IsBlockHash(parts[0])
    71  	if parts[1] == "*" {
    72  		return validBlockHash
    73  	}
    74  	return validBlockHash && IsTransIndex(parts[1])
    75  }
    76  
    77  func IsValidTransId(chain string, ids []string, validTypes ValidArgumentType) (bool, error) {
    78  	err := ValidateIdentifiers(chain, ids, validTypes, 1, nil)
    79  	return err == nil, err
    80  }
    81  
    82  func clearDirectional(str string) string {
    83  	if !strings.Contains(str, ":") {
    84  		return str
    85  	}
    86  	// Note that this leaves invalid directional signals which will cause the check to fail
    87  	str = strings.Replace(str, ":next", "", -1)
    88  	str = strings.Replace(str, ":prev", "", -1)
    89  	return str
    90  }