From bf56cb7ee2aba439e6caf1a24e5c7e4ce25845ee Mon Sep 17 00:00:00 2001 From: Godfrain Jacques Date: Wed, 15 May 2024 12:37:04 -0700 Subject: [PATCH] fix_: saving profile image changes ens display name to non-ens (#5156) When a user updates the preferred name, we should just ignore if the name is an ENS name, because the ENS name is set only in the ENS popup. But this also means that if a user set the profile name to an ENS name, this change will also be ignored. --- common/utils.go | 21 +++++++++-- protocol/messenger_identity.go | 2 +- .../messenger_identity_display_name_test.go | 36 ++++++++++++++++--- 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/common/utils.go b/common/utils.go index 45b1249f1..011da7a61 100644 --- a/common/utils.go +++ b/common/utils.go @@ -15,6 +15,8 @@ var ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`) var ErrInvalidDisplayNameNotAllowed = errors.New("name is not allowed") +var DISPLAY_NAME_EXT = []string{"_eth", ".eth", "-eth"} + func RecoverKey(m *protobuf.ApplicationMetadataMessage) (*ecdsa.PublicKey, error) { if m.Signature == nil { return nil, nil @@ -45,8 +47,10 @@ func ValidateDisplayName(displayName *string) error { } // .eth should not happen due to the regexp above, but let's keep it here in case the regexp is changed in the future - if strings.HasSuffix(name, "_eth") || strings.HasSuffix(name, ".eth") || strings.HasSuffix(name, "-eth") { - return ErrInvalidDisplayNameEthSuffix + for _, ext := range DISPLAY_NAME_EXT { + if strings.HasSuffix(*displayName, ext) { + return ErrInvalidDisplayNameEthSuffix + } } if alias.IsAlias(name) { @@ -55,3 +59,16 @@ func ValidateDisplayName(displayName *string) error { return nil } + +// implementation referenced from https://github.com/embarklabs/embark/blob/master/packages/plugins/ens/src/index.js +func IsENSName(displayName string) bool { + if len(displayName) == 0 { + return false + } + + if strings.HasSuffix(displayName, ".eth") { + return true + } + + return false +} diff --git a/protocol/messenger_identity.go b/protocol/messenger_identity.go index 2b136c194..f8e2e75bd 100644 --- a/protocol/messenger_identity.go +++ b/protocol/messenger_identity.go @@ -31,7 +31,7 @@ func (m *Messenger) SetDisplayName(displayName string) error { return err } - if currDisplayName == displayName { + if utils.IsENSName(displayName) || currDisplayName == displayName { return nil // Do nothing } diff --git a/protocol/messenger_identity_display_name_test.go b/protocol/messenger_identity_display_name_test.go index 7ac81ddef..25a687434 100644 --- a/protocol/messenger_identity_display_name_test.go +++ b/protocol/messenger_identity_display_name_test.go @@ -204,10 +204,6 @@ func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameRestrictions() s.Require().ErrorIs(err, expectedErr) } - setInvalidName("test.eth", utils.ErrInvalidDisplayNameRegExp) - setInvalidName("test-eth", utils.ErrInvalidDisplayNameEthSuffix) - setInvalidName("test_eth", utils.ErrInvalidDisplayNameEthSuffix) - setInvalidName("dot.not", utils.ErrInvalidDisplayNameRegExp) setInvalidName("t", utils.ErrInvalidDisplayNameRegExp) setInvalidName("tt", utils.ErrInvalidDisplayNameRegExp) @@ -221,3 +217,35 @@ func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameRestrictions() s.Require().NoError(err) s.Require().Equal("name with space", displayName) } + +func (s *MessengerProfileDisplayNameHandlerSuite) TestUpdateImageWhenEnsNameIsSet() { + err := s.m.settings.SaveSetting("preferred-name", "kounkou.stateofus.eth") + s.Require().NoError(err) + + // check display name for the created instance + displayName, err := s.m.settings.GetPreferredUsername() + s.Require().NoError(err) + s.Require().Equal("kounkou.stateofus.eth", displayName) + + // add profile keypair + profileKp := accounts.GetProfileKeypairForTest(true, false, false) + profileKp.KeyUID = s.m.account.KeyUID + profileKp.Name = DefaultProfileDisplayName + profileKp.Accounts[0].KeyUID = s.m.account.KeyUID + + err = s.m.settings.SaveOrUpdateKeypair(profileKp) + s.Require().NoError(err) + + // check account is present in the db + dbProfileKp, err := s.m.settings.GetKeypairByKeyUID(profileKp.KeyUID) + s.Require().NoError(err) + s.Require().True(accounts.SameKeypairs(profileKp, dbProfileKp)) + + // save account will create the account + err = s.m.multiAccounts.SaveAccount(*s.m.account) + s.Require().NoError(err) + + // set new description + err = s.m.SetDisplayName("godfrain.stateofus.eth") + s.Require().NoError(err) +}