From ea3a6d8382658e7c109c81a66026ba90f06f8cf7 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Mon, 30 Jun 2025 18:18:06 +0300 Subject: [PATCH] call the UpdateAlias if the account has an Aias --- handlers/application/menuhandler.go | 37 ++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/handlers/application/menuhandler.go b/handlers/application/menuhandler.go index 29148e0..55e9f19 100644 --- a/handlers/application/menuhandler.go +++ b/handlers/application/menuhandler.go @@ -2570,6 +2570,7 @@ func (h *MenuHandlers) persistLanguageCode(ctx context.Context, code string) err // RequestCustomAlias requests an ENS based alias name based on a user's input,then saves it as temporary value func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input []byte) (resource.Result, error) { var res resource.Result + var alias string sessionId, ok := ctx.Value("SessionId").(string) if !ok { return res, fmt.Errorf("missing session") @@ -2601,21 +2602,35 @@ func (h *MenuHandlers) RequestCustomAlias(ctx context.Context, sym string, input } } sanitizedInput := sanitizeAliasHint(string(input)) - aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), sanitizedInput) - if err != nil { - res.FlagSet = append(res.FlagSet, flag_api_error) - logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", string(aliasHint), "error_alias_request", err) - return res, nil + // Check if an alias already exists + existingAlias, err := store.ReadEntry(ctx, sessionId, storedb.DATA_ACCOUNT_ALIAS) + if err == nil && len(existingAlias) > 0 { + // Update existing alias + aliasResult, err := h.accountService.UpdateAlias(ctx, sanitizedInput, string(pubKey)) + if err != nil { + res.FlagSet = append(res.FlagSet, flag_api_error) + logg.ErrorCtxf(ctx, "failed to update alias", "alias", sanitizedInput, "error", err) + return res, nil + } + alias := aliasResult.Alias + logg.InfoCtxf(ctx, "Updated alias", "alias", alias) + } else { + // Register a new alias + aliasResult, err := h.accountService.RequestAlias(ctx, string(pubKey), sanitizedInput) + if err != nil { + res.FlagSet = append(res.FlagSet, flag_api_error) + logg.ErrorCtxf(ctx, "failed to retrieve alias", "alias", sanitizedInput, "error_alias_request", err) + return res, nil + } + res.FlagReset = append(res.FlagReset, flag_api_error) + + alias := aliasResult.Alias + logg.InfoCtxf(ctx, "Suggested alias", "alias", alias) } - res.FlagReset = append(res.FlagReset, flag_api_error) - - alias := aliasResult.Alias - logg.InfoCtxf(ctx, "Suggested alias ", "alias", alias) - //Store the returned alias,wait for user to confirm it as new account alias err = store.WriteEntry(ctx, sessionId, storedb.DATA_SUGGESTED_ALIAS, []byte(alias)) if err != nil { - logg.ErrorCtxf(ctx, "failed to write account alias", "key", storedb.DATA_TEMPORARY_VALUE, "value", alias, "error", err) + logg.ErrorCtxf(ctx, "failed to write suggested alias", "key", storedb.DATA_SUGGESTED_ALIAS, "value", alias, "error", err) return res, err } }