Validate aliases, addresses and phone numbers in the send menu #176

Merged
kamikazechaser merged 14 commits from alias-address-validation into master 2024-11-26 07:24:57 +01:00
Showing only changes of commit 1a3426e729 - Show all commits

48
common/recipient.go Normal file
View 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}))$`
Review

I think phone regex should be check formatting but not specifics; that is, country code should be filtered elsewhere.

I think phone regex should be check formatting but not specifics; that is, country code should be filtered elsewhere.
addressRegex = `^0x[a-fA-F0-9]{40}$`
aliasRegex = `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+$`
kamikazechaser marked this conversation as resolved
Review

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/
Review

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

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`
Review
@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")
}