Compare commits
No commits in common. "master" and "dev-api-aliases-v2" have entirely different histories.
master
...
dev-api-al
@ -16,14 +16,12 @@ const (
|
|||||||
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
voucherTransfersPathPrefix = "/api/v1/transfers/last10"
|
||||||
voucherDataPathPrefix = "/api/v1/token"
|
voucherDataPathPrefix = "/api/v1/token"
|
||||||
AliasPrefix = "api/v1/alias"
|
AliasPrefix = "api/v1/alias"
|
||||||
AliasEnsPrefix = "/api/v1/bypass"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
custodialURLBase string
|
custodialURLBase string
|
||||||
dataURLBase string
|
dataURLBase string
|
||||||
BearerToken string
|
BearerToken string
|
||||||
aliasEnsURLBase string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -36,7 +34,6 @@ var (
|
|||||||
VoucherTransfersURL string
|
VoucherTransfersURL string
|
||||||
VoucherDataURL string
|
VoucherDataURL string
|
||||||
CheckAliasURL string
|
CheckAliasURL string
|
||||||
AliasEnsURL string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func setBase() error {
|
func setBase() error {
|
||||||
@ -44,7 +41,6 @@ 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)
|
||||||
@ -73,6 +69,5 @@ func LoadConfig() error {
|
|||||||
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
VoucherTransfersURL, _ = url.JoinPath(dataURLBase, voucherTransfersPathPrefix)
|
||||||
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
VoucherDataURL, _ = url.JoinPath(dataURLBase, voucherDataPathPrefix)
|
||||||
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
CheckAliasURL, _ = url.JoinPath(dataURLBase, AliasPrefix)
|
||||||
AliasEnsURL, _ = url.JoinPath(aliasEnsURLBase, AliasEnsPrefix)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -202,13 +202,10 @@ func (das *DevAccountService) loadItem(ctx context.Context, k []byte, v []byte)
|
|||||||
}
|
}
|
||||||
if ss[0] == "account" {
|
if ss[0] == "account" {
|
||||||
err = das.loadAccount(ctx, ss[1], v)
|
err = das.loadAccount(ctx, ss[1], v)
|
||||||
logg.ErrorCtxf(ctx, "loading saved account failed", "error_load_account", err)
|
|
||||||
} else if ss[0] == "tx" {
|
} else if ss[0] == "tx" {
|
||||||
err = das.loadTx(ctx, ss[1], v)
|
err = das.loadTx(ctx, ss[1], v)
|
||||||
logg.ErrorCtxf(ctx, "loading transactions failed", "error_load_txs", err)
|
|
||||||
} else if ss[0] == "alias" {
|
} else if ss[0] == "alias" {
|
||||||
err = das.loadAlias(ctx, ss[1], k)
|
err = das.loadAlias(ctx, ss[1], k)
|
||||||
logg.ErrorCtxf(ctx, "loading aliases failed", "error_load_aliases", err)
|
|
||||||
} else {
|
} else {
|
||||||
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
logg.ErrorCtxf(ctx, "unknown double underscore key", "key", ss[0])
|
||||||
}
|
}
|
||||||
@ -577,12 +574,10 @@ func (das *DevAccountService) TokenTransfer(ctx context.Context, amount, from, t
|
|||||||
func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
func (das *DevAccountService) CheckAliasAddress(ctx context.Context, alias string) (*models.AliasAddress, error) {
|
||||||
addr, ok := das.accountsAlias[alias]
|
addr, ok := das.accountsAlias[alias]
|
||||||
if !ok {
|
if !ok {
|
||||||
logg.ErrorCtxf(ctx, "alias check failed", "alias", alias)
|
|
||||||
return nil, fmt.Errorf("alias %s not found", alias)
|
return nil, fmt.Errorf("alias %s not found", alias)
|
||||||
}
|
}
|
||||||
acc, ok := das.accounts[addr]
|
acc, ok := das.accounts[addr]
|
||||||
if !ok {
|
if !ok {
|
||||||
logg.ErrorCtxf(ctx, "failed to resolve alias", "alias", alias)
|
|
||||||
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
return nil, fmt.Errorf("alias %s found but does not resolve", alias)
|
||||||
}
|
}
|
||||||
return &models.AliasAddress{
|
return &models.AliasAddress{
|
||||||
@ -605,7 +600,6 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
var alias string
|
var alias string
|
||||||
uid, err := uuid.NewV4()
|
uid, err := uuid.NewV4()
|
||||||
if !aliasRegex.MatchString(hint) {
|
if !aliasRegex.MatchString(hint) {
|
||||||
logg.ErrorCtxf(ctx, "alias hint does not match", "key", publicKey, "hint", hint)
|
|
||||||
return nil, fmt.Errorf("alias hint does not match: %s", publicKey)
|
return nil, fmt.Errorf("alias hint does not match: %s", publicKey)
|
||||||
}
|
}
|
||||||
acc, ok := das.accounts[publicKey]
|
acc, ok := das.accounts[publicKey]
|
||||||
@ -617,7 +611,6 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
}
|
}
|
||||||
err = das.saveAccount(ctx, acc)
|
err = das.saveAccount(ctx, acc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "account save failed with", "account", acc, "account_save_error", err)
|
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
das.accounts[publicKey] = acc
|
das.accounts[publicKey] = acc
|
||||||
@ -625,7 +618,6 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
alias = hint
|
alias = hint
|
||||||
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
isPhone, err := das.applyPhoneAlias(ctx, publicKey, alias)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "failed to apply phone alias", "public key", publicKey, "alias", alias, "error", err)
|
|
||||||
return nil, fmt.Errorf("phone parser error: %v", err)
|
return nil, fmt.Errorf("phone parser error: %v", err)
|
||||||
}
|
}
|
||||||
if !isPhone {
|
if !isPhone {
|
||||||
@ -644,7 +636,6 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string
|
|||||||
das.accountsAlias[alias] = publicKey
|
das.accountsAlias[alias] = publicKey
|
||||||
err := das.saveAlias(ctx, map[string]string{alias: publicKey})
|
err := das.saveAlias(ctx, map[string]string{alias: publicKey})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logg.ErrorCtxf(ctx, "account save error", "public key", publicKey, "alias", alias, "alias_save_error", err)
|
|
||||||
return nil, fmt.Errorf("Failed to save the account alias with error: %s", err.Error())
|
return nil, fmt.Errorf("Failed to save the account alias with error: %s", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,3 @@ 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,9 +11,7 @@ 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"
|
||||||
@ -24,7 +22,6 @@ 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 {
|
||||||
@ -62,10 +59,6 @@ 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.
|
||||||
@ -223,10 +216,8 @@ 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)
|
||||||
@ -234,83 +225,27 @@ 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 (
|
var r models.AliasAddress
|
||||||
aliasEnsResult models.AliasEnsAddressResult
|
|
||||||
)
|
|
||||||
|
|
||||||
ep, err := url.JoinPath(config.AliasEnsURL, "/resolve")
|
ep, err := url.JoinPath(config.CheckAliasURL, alias)
|
||||||
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)
|
||||||
query := u.Query()
|
return &r, err
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Use actual custodial api to request available alias
|
||||||
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
func (as *HTTPAccountService) RequestAlias(ctx context.Context, publicKey string, hint string) (*models.RequestAliasResult, error) {
|
||||||
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")
|
||||||
}
|
}
|
||||||
if as.UseApi {
|
svc := dev.NewDevAccountService(ctx, as.SS)
|
||||||
if !strings.Contains(hint, ".") {
|
return svc.RequestAlias(ctx, publicKey, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove eth-custodial api dependency
|
// TODO: remove eth-custodial api dependency
|
||||||
|
Loading…
Reference in New Issue
Block a user