Compare commits

..

No commits in common. "master" and "dev-pool-swap" have entirely different histories.

9 changed files with 11 additions and 233 deletions

View File

@ -16,17 +16,12 @@ const (
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
voucherDataPathPrefix = "/api/v1/token"
AliasPrefix = "api/v1/alias"
SendSMSPrefix = "api/v1/external/upsell"
AliasEnsPrefix = "/api/v1/bypass"
ExternalSMSPrefix = "/api/v1/external"
)
var (
custodialURLBase string
dataURLBase string
BearerToken string
aliasEnsURLBase string
externalSMSBase string
)
var (
@ -39,9 +34,6 @@ var (
VoucherTransfersURL string
VoucherDataURL string
CheckAliasURL string
SendSMSURL string
AliasEnsURL string
ExternalSMSURL string
)
func setBase() error {
@ -49,8 +41,6 @@ func setBase() error {
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
aliasEnsURLBase = env.GetEnv("ALIAS_ENS_BASE", "http://localhost:5015")
externalSMSBase = env.GetEnv("EXTERNAL_SMS_BASE", "http://localhost:5035")
BearerToken = env.GetEnv("BEARER_TOKEN", "")
_, err = url.Parse(custodialURLBase)
@ -79,9 +69,5 @@ func LoadConfig() error {
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
SendSMSURL, _ = url.JoinPath(dataURLBase, SendSMSPrefix)
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
ExternalSMSURL, _ = url.JoinPath(externalSMSBase, ExternalSMSPrefix)
return nil
}

View File

@ -653,18 +653,3 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
Alias: alias,
}, nil
}
func (das *DevAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
logg.DebugCtxf(ctx, "sent an SMS", "inviterPhone", inviterPhone, "inviteePhone", inviteePhone)
return &models.SendSMSResponse{
Invitee: inviteePhone,
}, nil
}
func (das *DevAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
return fmt.Errorf("unimplemented")
}
func (das *DevAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
return fmt.Errorf("unimplemented")
}

View File

@ -7,13 +7,3 @@ type RequestAliasResult struct {
type AliasAddress struct {
Address string
}
type AliasEnsResult struct {
Address string `json:"address"`
AutoChoose bool `json:"autoChoose"`
Name string `json:"name"`
}
type AliasEnsAddressResult struct {
Address string `json:"address"`
}

View File

@ -1,5 +0,0 @@
package models
type SendSMSResponse struct {
Invitee string `json:"invitee"`
}

View File

@ -17,7 +17,4 @@ type AccountService interface {
TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error)
CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error)
RequestAlias(ctx context.Context, hint string, publicKey string) (*models.RequestAliasResult, error)
SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error)
SendAddressSMS(ctx context.Context, publicKey, originPhone string) error
SendPINResetSMS(ctx context.Context, admin, phone string) error
}

View File

@ -11,9 +11,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"
"git.defalsify.org/vise.git/logging"
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
@ -24,7 +22,6 @@ import (
var (
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
)
type HTTPAccountService struct {
@ -62,10 +59,6 @@ func (as *HTTPAccountService) TrackAccountStatus(ctx context.Context, publicKey
return &r, nil
}
func (as *HTTPAccountService) ToFqdn(alias string) string {
return alias + ".sarafu.eth"
}
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
// Parameters:
// - publicKey: The public key associated with the account whose balance needs to be checked.
@ -223,10 +216,8 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
if as.SS == nil {
return nil, fmt.Errorf("The storage service cannot be nil")
}
logg.InfoCtxf(ctx, "resolving alias before formatting", "alias", alias)
svc := dev.NewDevAccountService(ctx, as.SS)
if as.UseApi {
logg.InfoCtxf(ctx, "resolving alias to address", "alias", alias)
return resolveAliasAddress(ctx, alias)
} else {
return svc.CheckAliasAddress(ctx, alias)
@ -234,165 +225,28 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
}
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
var (
aliasEnsResult models.AliasEnsAddressResult
)
var r models.AliasAddress
ep, err := url.JoinPath(config.AliasEnsURL, "/resolve")
ep, err := url.JoinPath(config.CheckAliasURL, alias)
if err != nil {
return nil, err
}
u, err := url.Parse(ep)
req, err := http.NewRequest("GET", ep, nil)
if err != nil {
return nil, err
}
query := u.Query()
query.Set("name", alias)
u.RawQuery = query.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &aliasEnsResult)
if err != nil {
return nil, err
}
return &models.AliasAddress{Address: aliasEnsResult.Address}, err
_, err = doRequest(ctx, req, &r)
return &r, err
}
// TODO: Use actual custodial api to request available alias
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
if as.SS == nil {
return nil, fmt.Errorf("The storage service cannot be nil")
}
if as.UseApi {
if !strings.Contains(hint, ".") {
hint = as.ToFqdn(hint)
}
enr, err := requestEnsAlias(ctx, publicKey, hint)
if err != nil {
return nil, err
}
return &models.RequestAliasResult{Alias: enr.Name}, nil
} else {
svc := dev.NewDevAccountService(ctx, as.SS)
return svc.RequestAlias(ctx, publicKey, hint)
}
}
func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*models.AliasEnsResult, error) {
var r models.AliasEnsResult
ep, err := url.JoinPath(config.AliasEnsURL, "/register")
if err != nil {
return nil, err
}
logg.InfoCtxf(ctx, "requesting alias", "endpoint", ep, "hint", hint)
//Payload with the address and hint to derive an ENS name
payload := map[string]string{
"address": publicKey,
"hint": hint,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
// Log the request body
logg.InfoCtxf(ctx, "request body", "payload", string(payloadBytes))
_, err = doRequest(ctx, req, &r)
if err != nil {
return nil, err
}
logg.InfoCtxf(ctx, "alias successfully assigned", "alias", r.Name)
return &r, nil
}
// SendSMS calls the API to send out an SMS.
// Parameters:
// - inviterPhone: The user initiating the SMS.
// - inviteePhone: The number being invited to Sarafu.
func (as *HTTPAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
var r models.SendSMSResponse
// Create request payload
payload := map[string]string{
"inviterPhone": inviterPhone,
"inviteePhone": inviteePhone,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, err
}
// Create a new request
req, err := http.NewRequest("POST", config.SendSMSURL, bytes.NewBuffer(payloadBytes))
if err != nil {
return nil, err
}
_, err = doRequest(ctx, req, &r)
if err != nil {
return nil, err
}
return &r, nil
}
func (as *HTTPAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
ep, err := url.JoinPath(config.ExternalSMSURL, "address")
if err != nil {
return err
}
logg.InfoCtxf(ctx, "sending an address sms", "endpoint", ep, "address", publicKey, "origin-phone", originPhone)
payload := map[string]string{
"address": publicKey,
"originPhone": originPhone,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
if err != nil {
return err
}
_, err = doRequest(ctx, req, nil)
if err != nil {
return err
}
return nil
}
func (as *HTTPAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
ep, err := url.JoinPath(config.ExternalSMSURL, "pinreset")
if err != nil {
return err
}
logg.InfoCtxf(ctx, "sending pin reset sms", "endpoint", ep, "admin", admin, "phone", phone)
payload := map[string]string{
"admin": admin,
"phone": phone,
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequest("POST", ep, bytes.NewBuffer(payloadBytes))
if err != nil {
return err
}
_, err = doRequest(ctx, req, nil)
if err != nil {
return err
}
return nil
}
// TODO: remove eth-custodial api dependency
func doRequest(ctx context.Context, req *http.Request, rcpt any) (*api.OKResponse, error) {

View File

@ -61,7 +61,3 @@ func (m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string
func (m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) {
return nil, nil
}
func (m MockApi) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
return nil, nil
}

View File

@ -53,20 +53,7 @@ func (m *MockAccountService) CheckAliasAddress(ctx context.Context, alias string
return args.Get(0).(*models.AliasAddress), args.Error(1)
}
func (m *MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
func (m MockAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
args := m.Called(publicKey, hint)
return args.Get(0).(*models.RequestAliasResult), args.Error(1)
}
func (m *MockAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
args := m.Called(inviterPhone, inviteePhone)
return args.Get(0).(*models.SendSMSResponse), args.Error(1)
}
func (m *MockAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
return nil
}
func (m *MockAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
return nil
}

View File

@ -57,22 +57,10 @@ func (tas *TestAccountService) TokenTransfer(ctx context.Context, amount, from,
}, nil
}
func (m *TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
func (m TestAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
return &models.AliasAddress{}, nil
}
func (m *TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
func (m TestAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
return &models.RequestAliasResult{}, nil
}
func (m *TestAccountService) SendUpsellSMS(ctx context.Context, inviterPhone, inviteePhone string) (*models.SendSMSResponse, error) {
return &models.SendSMSResponse{}, nil
}
func (m *TestAccountService) SendAddressSMS(ctx context.Context, publicKey, originPhone string) error {
return nil
}
func (m *TestAccountService) SendPINResetSMS(ctx context.Context, admin, phone string) error {
return nil
}