Validate aliases, addresses and phone numbers in the send menu #176
48
common/recipient.go
Normal file
48
common/recipient.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Define the regex patterns as constants
|
||||||
|
const (
|
||||||
|
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$`
|
||||||
|
|||||||
|
addressRegex = `^0x[a-fA-F0-9]{40}$`
|
||||||
|
aliasRegex = `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+$`
|
||||||
kamikazechaser marked this conversation as resolved
lash
commented
alias is same format as email? May want to have a look at https://emailregex.com/ alias is same format as email? May want to have a look at https://emailregex.com/
Alfred-mk
commented
No, it is not. This is the format that I picked from Sarafu's website
No, it is not. This is the format that I picked from Sarafu's website
`Your shortcode is a unique identifier that can be used by others to send you vouchers. It must be in the following format name@area`
kamikazechaser
commented
@Alfred-mk See https://github.com/grassrootseconomics/sarafu.network/issues/174 for Alias requirements @Alfred-mk See https://github.com/grassrootseconomics/sarafu.network/issues/174 for Alias requirements
|
|||||||
|
)
|
||||||
|
|
||||||
|
// IsValidPhoneNumber checks if the given number is a valid phone number
|
||||||
|
func IsValidPhoneNumber(phonenumber string) bool {
|
||||||
|
match, _ := regexp.MatchString(phoneRegex, phonenumber)
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValidAddress checks if the given address is a valid Ethereum address
|
||||||
|
func IsValidAddress(address string) bool {
|
||||||
|
match, _ := regexp.MatchString(addressRegex, address)
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValidAlias checks if the alias is a valid alias format
|
||||||
|
func IsValidAlias(alias string) bool {
|
||||||
|
match, _ := regexp.MatchString(aliasRegex, alias)
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
// CheckRecipient validates the recipient format based on the criteria
|
||||||
|
func CheckRecipient(recipient string) (string, error) {
|
||||||
|
if IsValidPhoneNumber(recipient) {
|
||||||
|
return "phone number", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsValidAddress(recipient) {
|
||||||
|
return "address", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if IsValidAlias(recipient) {
|
||||||
|
return "alias", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return "", fmt.Errorf("invalid recipient: must be a phone number, address or alias")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user
I think phone regex should be check formatting but not specifics; that is, country code should be filtered elsewhere.