resolve alias address based on UseApi field

This commit is contained in:
Carlosokumu 2025-01-22 17:19:46 +03:00
parent bc7afa50a4
commit 3f9dc89a40
Signed by: carlos
GPG Key ID: 7BD6BC8160A5C953

View File

@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
@ -24,7 +25,8 @@ var (
)
type HTTPAccountService struct {
ss storage.StorageService
SS storage.StorageService
UseApi bool
}
// Parameters:
@ -211,25 +213,38 @@ func (as *HTTPAccountService) TokenTransfer(ctx context.Context, amount, from, t
// Parameters:
// - alias: The alias of the user.
func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
if as.SS == nil {
return nil, fmt.Errorf("The storage service cannot be nil")
}
svc := dev.NewDevAccountService(ctx, as.SS)
if as.UseApi {
return resolveAliasAddress(ctx, alias)
} else {
return svc.CheckAliasAddress(ctx, alias)
}
}
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
var r models.AliasAddress
ep, err := url.JoinPath(config.CheckAliasURL, alias)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", ep, nil)
if err != nil {
return nil, 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) {
svc := dev.NewDevAccountService(ctx, as.ss)
if as.SS == nil {
return nil, fmt.Errorf("The storage service cannot be nil")
}
svc := dev.NewDevAccountService(ctx, as.SS)
return svc.RequestAlias(ctx, publicKey, hint)
}