Initial commit

This commit is contained in:
lash 2025-01-12 08:17:29 +00:00
commit ded01e8dd0
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 73 additions and 0 deletions

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.grassecon.net/grassrootseconomics/common
go 1.23.4

15
identity/address.go Normal file
View File

@ -0,0 +1,15 @@
package identity
import (
"regexp"
)
// Define the regex patterns as constants
const (
addressRegex = `^0x[a-fA-F0-9]{40}$`
)
// IsValidAddress checks if the given address is a valid Ethereum address
func IsValidAddress(address string) bool {
match, _ := regexp.MatchString(addressRegex, address)
return match
}

15
identity/alias.go Normal file
View File

@ -0,0 +1,15 @@
package identity
import (
"regexp"
)
const (
aliasRegex = `^[a-zA-Z0-9]+$`
)
// IsValidAlias checks if the alias is a valid alias format
func IsValidAlias(alias string) bool {
match, _ := regexp.MatchString(aliasRegex, alias)
return match
}

24
identity/identity.go Normal file
View File

@ -0,0 +1,24 @@
package identity
import (
"fmt"
"git.grassecon.net/grassrootseconomics/common/phone"
)
// CheckRecipient validates the recipient format based on the criteria
func CheckRecipient(recipient string) (string, error) {
if phone.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")
}

16
phone/phone.go Normal file
View File

@ -0,0 +1,16 @@
package phone
import (
"regexp"
)
// Define the regex patterns as constants
const (
phoneRegex = `^(?:\+254|254|0)?((?:7[0-9]{8})|(?:1[01][0-9]{7}))$`
)
// IsValidPhoneNumber checks if the given number is a valid phone number
func IsValidPhoneNumber(phonenumber string) bool {
match, _ := regexp.MatchString(phoneRegex, phonenumber)
return match
}