Merge branch 'master' into pool-swap-endpoints
This commit is contained in:
commit
064e749b04
@ -28,6 +28,7 @@ var (
|
|||||||
custodialURLBase string
|
custodialURLBase string
|
||||||
dataURLBase string
|
dataURLBase string
|
||||||
BearerToken string
|
BearerToken string
|
||||||
|
aliasEnsURLBase string
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -53,6 +54,7 @@ func setBase() error {
|
|||||||
|
|
||||||
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
custodialURLBase = env.GetEnv("CUSTODIAL_URL_BASE", "http://localhost:5003")
|
||||||
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
dataURLBase = env.GetEnv("DATA_URL_BASE", "http://localhost:5006")
|
||||||
|
aliasEnsURLBase = env.GetEnv("ALIAS_ENS_BASE", "http://localhost:5015")
|
||||||
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
BearerToken = env.GetEnv("BEARER_TOKEN", "")
|
||||||
|
|
||||||
_, err = url.Parse(custodialURLBase)
|
_, err = url.Parse(custodialURLBase)
|
||||||
|
@ -7,3 +7,13 @@ type RequestAliasResult struct {
|
|||||||
type AliasAddress struct {
|
type AliasAddress struct {
|
||||||
Address string
|
Address string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AliasEnsResult struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
AutoChoose bool `json:"autoChoose"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AliasEnsAddressResult struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
}
|
||||||
|
@ -11,7 +11,9 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.defalsify.org/vise.git/logging"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/config"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/dev"
|
||||||
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
"git.grassecon.net/grassrootseconomics/sarafu-api/models"
|
||||||
@ -22,6 +24,7 @@ import (
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
aliasRegex = regexp.MustCompile("^\\+?[a-zA-Z0-9\\-_]+$")
|
||||||
|
logg = logging.NewVanilla().WithDomain("sarafu-api.devapi")
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPAccountService struct {
|
type HTTPAccountService struct {
|
||||||
@ -59,6 +62,10 @@ func (as *HTTPAccountService) TrackAccountStatus(ctx context.Context, publicKey
|
|||||||
return &r, nil
|
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.
|
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
||||||
// Parameters:
|
// Parameters:
|
||||||
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
||||||
@ -216,8 +223,10 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
|
|||||||
if as.SS == nil {
|
if as.SS == nil {
|
||||||
return nil, fmt.Errorf("The storage service cannot be 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)
|
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||||
if as.UseApi {
|
if as.UseApi {
|
||||||
|
logg.InfoCtxf(ctx, "resolving alias to address", "alias", alias)
|
||||||
return resolveAliasAddress(ctx, alias)
|
return resolveAliasAddress(ctx, alias)
|
||||||
} else {
|
} else {
|
||||||
return svc.CheckAliasAddress(ctx, alias)
|
return svc.CheckAliasAddress(ctx, alias)
|
||||||
@ -225,18 +234,33 @@ func (as *HTTPAccountService) CheckAliasAddress(ctx context.Context, alias strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
func resolveAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||||
var r models.AliasAddress
|
var (
|
||||||
|
aliasEnsResult models.AliasEnsAddressResult
|
||||||
|
)
|
||||||
|
|
||||||
ep, err := url.JoinPath(config.CheckAliasURL, alias)
|
ep, err := url.JoinPath(config.CheckAliasURL, "/resolve")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
req, err := http.NewRequest("GET", ep, nil)
|
|
||||||
|
u, err := url.Parse(ep)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
_, err = doRequest(ctx, req, &r)
|
|
||||||
return &r, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
|
func (as *HTTPAccountService) FetchTopPools(ctx context.Context) ([]dataserviceapi.PoolDetails, error) {
|
||||||
@ -450,8 +474,50 @@ func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
if as.SS == nil {
|
if as.SS == nil {
|
||||||
return nil, fmt.Errorf("The storage service cannot be nil")
|
return nil, fmt.Errorf("The storage service cannot be nil")
|
||||||
}
|
}
|
||||||
svc := dev.NewDevAccountService(ctx, as.SS)
|
if as.UseApi {
|
||||||
return svc.RequestAlias(ctx, publicKey, hint)
|
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.CheckAliasURL, "/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
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove eth-custodial api dependency
|
// TODO: remove eth-custodial api dependency
|
||||||
|
Loading…
Reference in New Issue
Block a user