github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/validate/validate.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 "strings" 9 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" 12 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/usage" 13 ) 14 15 func Usage(msg string, values ...string) error { 16 return usage.Usage(msg, values...) 17 } 18 19 func IsValidHash(hash string) bool { 20 ok, err := base.IsValidHex("hash", hash, 32) 21 return ok && err == nil 22 } 23 24 func IsValidFourByteE(val string) (bool, error) { 25 return base.IsValidHex("fourbyte", val, 4) 26 } 27 28 func IsValidFourByte(val string) bool { 29 ok, _ := base.IsValidHex("fourbyte", val, 4) 30 return ok 31 } 32 33 func IsValidTopicE(val string) (bool, error) { 34 return base.IsValidHex("topic", val, 32) 35 } 36 37 func IsValidTopic(val string) bool { 38 ok, _ := base.IsValidHex("topic", val, 32) 39 return ok 40 } 41 42 func ValidateAddresses(args []string) error { 43 for _, arg := range args { 44 if !base.IsValidAddress(arg) { 45 return Usage("The value {0} is not a valid address.", arg) 46 } 47 } 48 return nil 49 } 50 51 func ValidateExactlyOneAddr(args []string) error { 52 if err := ValidateAtLeastOneAddr(args); err != nil { 53 return Usage("Please specify a valid Ethereum address.") 54 } 55 56 if len(args) > 1 { 57 return Usage("Please specify only a single Ethereum address.") 58 } 59 60 if !base.IsValidAddress(args[0]) { 61 return Usage("The value {0} is not a valid address.", args[0]) 62 } 63 64 return nil 65 } 66 67 func ValidateAtLeastOneNonSentinal(args []string) error { 68 if err := ValidateAtLeastOneAddr(args); err != nil { 69 return err 70 } 71 for _, arg := range args { 72 if arg == base.NotAMonitor.Hex() { 73 return Usage("The address {0} is reserved for system use.", arg) 74 } 75 } 76 return nil 77 } 78 79 func ValidateAtLeastOneAddr(args []string) error { 80 hasOne := false 81 for _, arg := range args { 82 if hasOne { 83 break 84 } 85 hasOne = base.IsValidAddress(arg) 86 } 87 if hasOne { 88 return nil 89 } 90 return Usage("Please specify at least one {0}.", "valid Ethereum address") 91 } 92 93 func ValidateEnumRequired(field, value, valid string) error { 94 if len(value) == 0 { 95 return Usage("Please choose at least one of {0}.", valid) 96 } 97 return ValidateEnum(field, value, valid) 98 } 99 100 func ValidateEnum(field, value, valid string) error { 101 if len(value) == 0 { 102 return nil 103 } 104 105 valid = strings.Replace(valid, "[", "|", 1) 106 valid = strings.Replace(valid, "]", "|", 1) 107 if strings.Contains(valid, "|"+value+"|") { 108 return nil 109 } 110 parts := strings.Split(strings.Trim(valid, "|"), "|") 111 list := "" 112 for _, part := range parts { 113 if len(list) > 0 { 114 list += " | " 115 } 116 list += part 117 } 118 return Usage("The {0} option ({1}) must be one of [ {2} ]", field, value, list) 119 } 120 121 func ValidateEnumSlice(field string, values []string, valid string) error { 122 for _, value := range values { 123 err := ValidateEnum(field, value, valid) 124 if err != nil { 125 return err 126 } 127 } 128 return nil 129 } 130 131 // HasArticulationKey returns true if the articulation key is present 132 func HasArticulationKey(on bool) bool { 133 if !on { 134 return true 135 } 136 return len(config.GetKey("etherscan").ApiKey) > 0 137 }