From 9a6ab7e6e2e8f212298c497b2385901fcc708278 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 26 Jun 2025 09:52:31 +0300 Subject: [PATCH 1/6] add the endpoint for updating the alias --- config/config.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/config.go b/config/config.go index 6f81fa7..5ac8438 100644 --- a/config/config.go +++ b/config/config.go @@ -25,6 +25,7 @@ const ( AliasRegistrationPrefix = "/api/v1/internal/register" AliasResolverPrefix = "/api/v1/resolve" ExternalSMSPrefix = "/api/v1/external" + AliasUpdatePrefix = "/api/v1/internal/update" ) var ( @@ -55,6 +56,7 @@ var ( AliasRegistrationURL string AliasResolverURL string ExternalSMSURL string + AliasUpdateURL string ) func setBase() error { @@ -102,6 +104,7 @@ func LoadConfig() error { AliasRegistrationURL, _ = url.JoinPath(aliasEnsURLBase, AliasRegistrationPrefix) AliasResolverURL, _ = url.JoinPath(aliasEnsURLBase, AliasResolverPrefix) ExternalSMSURL, _ = url.JoinPath(externalSMSBase, ExternalSMSPrefix) + AliasUpdateURL, _ = url.JoinPath(aliasEnsURLBase, AliasUpdatePrefix) return nil } From 49a8184d020a0a38712e6c0b39dfd5dcf572d352 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 26 Jun 2025 09:53:26 +0300 Subject: [PATCH 2/6] add the UpdateAlias function --- remote/account_service.go | 1 + remote/http/service.go | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/remote/account_service.go b/remote/account_service.go index d9708ef..c33fec3 100644 --- a/remote/account_service.go +++ b/remote/account_service.go @@ -17,6 +17,7 @@ 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) + UpdateAlias(ctx context.Context, name 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 diff --git a/remote/http/service.go b/remote/http/service.go index 1c41ab7..59bf18a 100644 --- a/remote/http/service.go +++ b/remote/http/service.go @@ -559,6 +559,54 @@ func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*model return &r, nil } +func (as *HTTPAccountService) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) { + if as.SS == nil { + return nil, fmt.Errorf("The storage service cannot be nil") + } + if as.UseApi { + if !strings.Contains(name, ".") { + name = as.ToFqdn(name) + } + enr, err := updateEnsAlias(ctx, publicKey, name) + 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, name) + } +} + +func updateEnsAlias(ctx context.Context, publicKey string, name string) (*models.AliasEnsResult, error) { + var r models.AliasEnsResult + + endpoint := config.AliasRegistrationURL + + logg.InfoCtxf(ctx, "updating alias", "endpoint", endpoint, "name", name) + + payload := map[string]string{ + "name": name, + "address": publicKey, + } + payloadBytes, err := json.Marshal(payload) + if err != nil { + return nil, err + } + req, err := http.NewRequest("POST", endpoint, 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 updated", "alias", r.Name) + return &r, nil +} + // SendSMS calls the API to send out an SMS. // Parameters: // - inviterPhone: The user initiating the SMS. From 57ee409f96297298397bbbfbd3228ea9b3fa9b6a Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Thu, 26 Jun 2025 09:54:19 +0300 Subject: [PATCH 3/6] add UpdateAlias to the DevAccountService and mocks --- dev/api.go | 7 +++++++ testutil/mocks/api_mock.go | 4 ++++ testutil/mocks/service_mock.go | 5 +++++ testutil/testservice/account_service.go | 4 ++++ 4 files changed, 20 insertions(+) diff --git a/dev/api.go b/dev/api.go index 26e1e4c..6000896 100644 --- a/dev/api.go +++ b/dev/api.go @@ -806,6 +806,13 @@ func (das *DevAccountService) RequestAlias(ctx context.Context, publicKey string }, nil } +func (das *DevAccountService) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) { + logg.DebugCtxf(ctx, "Updated the alias", "address", publicKey, "name", name) + return &models.RequestAliasResult{ + Alias: name, + }, 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{ diff --git a/testutil/mocks/api_mock.go b/testutil/mocks/api_mock.go index 2293aa6..c2f8e39 100644 --- a/testutil/mocks/api_mock.go +++ b/testutil/mocks/api_mock.go @@ -58,6 +58,10 @@ func (m MockApi) RequestAlias(ctx context.Context, publicKey string, hint string return nil, nil } +func (m MockApi) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) { + return nil, nil +} + func (m MockApi) TokenTransfer(ctx context.Context, amount, from, to, tokenAddress string) (*models.TokenTransferResponse, error) { return nil, nil } diff --git a/testutil/mocks/service_mock.go b/testutil/mocks/service_mock.go index 93d0358..09ab50b 100644 --- a/testutil/mocks/service_mock.go +++ b/testutil/mocks/service_mock.go @@ -58,6 +58,11 @@ func (m *MockAccountService) RequestAlias(ctx context.Context, publicKey string, return args.Get(0).(*models.RequestAliasResult), args.Error(1) } +func (m *MockAccountService) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) { + args := m.Called(publicKey, name) + 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) diff --git a/testutil/testservice/account_service.go b/testutil/testservice/account_service.go index 935df73..4294b6b 100644 --- a/testutil/testservice/account_service.go +++ b/testutil/testservice/account_service.go @@ -69,6 +69,10 @@ func (m *TestAccountService) RequestAlias(ctx context.Context, publicKey string, return &models.RequestAliasResult{}, nil } +func (m *TestAccountService) UpdateAlias(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 } From 50ee455e7069b166ce16af383bc11c3e4a6564da Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Jul 2025 00:31:35 +0300 Subject: [PATCH 4/6] fix: use the correct AliasUpdateURL --- remote/http/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remote/http/service.go b/remote/http/service.go index 59bf18a..d8d6d75 100644 --- a/remote/http/service.go +++ b/remote/http/service.go @@ -581,7 +581,7 @@ func (as *HTTPAccountService) UpdateAlias(ctx context.Context, publicKey string, func updateEnsAlias(ctx context.Context, publicKey string, name string) (*models.AliasEnsResult, error) { var r models.AliasEnsResult - endpoint := config.AliasRegistrationURL + endpoint := config.AliasUpdateURL logg.InfoCtxf(ctx, "updating alias", "endpoint", endpoint, "name", name) From 12940bb5f2847e9c4b7f6fda43a2213d226230b7 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Jul 2025 00:36:06 +0300 Subject: [PATCH 5/6] fix: use the correct PUT request method --- remote/http/service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/remote/http/service.go b/remote/http/service.go index d8d6d75..df85cbd 100644 --- a/remote/http/service.go +++ b/remote/http/service.go @@ -593,7 +593,7 @@ func updateEnsAlias(ctx context.Context, publicKey string, name string) (*models if err != nil { return nil, err } - req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(payloadBytes)) + req, err := http.NewRequest("PUT", endpoint, bytes.NewBuffer(payloadBytes)) if err != nil { return nil, err } From 814bef2b209ad9ce5c360ec99f9158349a411498 Mon Sep 17 00:00:00 2001 From: alfred-mk Date: Tue, 1 Jul 2025 00:49:12 +0300 Subject: [PATCH 6/6] fix: have the same order of received variables --- remote/http/service.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/remote/http/service.go b/remote/http/service.go index df85cbd..883d5bc 100644 --- a/remote/http/service.go +++ b/remote/http/service.go @@ -559,7 +559,7 @@ func requestEnsAlias(ctx context.Context, publicKey string, hint string) (*model return &r, nil } -func (as *HTTPAccountService) UpdateAlias(ctx context.Context, publicKey string, name string) (*models.RequestAliasResult, error) { +func (as *HTTPAccountService) UpdateAlias(ctx context.Context, name string, publicKey string) (*models.RequestAliasResult, error) { if as.SS == nil { return nil, fmt.Errorf("The storage service cannot be nil") } @@ -567,7 +567,7 @@ func (as *HTTPAccountService) UpdateAlias(ctx context.Context, publicKey string, if !strings.Contains(name, ".") { name = as.ToFqdn(name) } - enr, err := updateEnsAlias(ctx, publicKey, name) + enr, err := updateEnsAlias(ctx, name, publicKey) if err != nil { return nil, err } @@ -578,7 +578,7 @@ func (as *HTTPAccountService) UpdateAlias(ctx context.Context, publicKey string, } } -func updateEnsAlias(ctx context.Context, publicKey string, name string) (*models.AliasEnsResult, error) { +func updateEnsAlias(ctx context.Context, name string, publicKey string) (*models.AliasEnsResult, error) { var r models.AliasEnsResult endpoint := config.AliasUpdateURL