From ded01e8dd0d1f46db473afe323b64c850d232dc4 Mon Sep 17 00:00:00 2001 From: lash Date: Sun, 12 Jan 2025 08:17:29 +0000 Subject: [PATCH] Initial commit --- go.mod | 3 +++ identity/address.go | 15 +++++++++++++++ identity/alias.go | 15 +++++++++++++++ identity/identity.go | 24 ++++++++++++++++++++++++ phone/phone.go | 16 ++++++++++++++++ 5 files changed, 73 insertions(+) create mode 100644 go.mod create mode 100644 identity/address.go create mode 100644 identity/alias.go create mode 100644 identity/identity.go create mode 100644 phone/phone.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5081e27 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.grassecon.net/grassrootseconomics/common + +go 1.23.4 diff --git a/identity/address.go b/identity/address.go new file mode 100644 index 0000000..4e1a2bf --- /dev/null +++ b/identity/address.go @@ -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 +} diff --git a/identity/alias.go b/identity/alias.go new file mode 100644 index 0000000..195bcd5 --- /dev/null +++ b/identity/alias.go @@ -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 +} diff --git a/identity/identity.go b/identity/identity.go new file mode 100644 index 0000000..eac61aa --- /dev/null +++ b/identity/identity.go @@ -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") +} diff --git a/phone/phone.go b/phone/phone.go new file mode 100644 index 0000000..f1e5510 --- /dev/null +++ b/phone/phone.go @@ -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 +}