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.
This commit is contained in:
Godfrain Jacques 2024-05-15 12:37:04 -07:00 committed by GitHub
parent c9ee0e04c2
commit bf56cb7ee2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 7 deletions

View File

@ -15,6 +15,8 @@ var ErrInvalidDisplayNameRegExp = errors.New("only letters, numbers, underscores
var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`) var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are not allowed`)
var ErrInvalidDisplayNameNotAllowed = errors.New("name is 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) { func RecoverKey(m *protobuf.ApplicationMetadataMessage) (*ecdsa.PublicKey, error) {
if m.Signature == nil { if m.Signature == nil {
return nil, 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 // .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") { for _, ext := range DISPLAY_NAME_EXT {
return ErrInvalidDisplayNameEthSuffix if strings.HasSuffix(*displayName, ext) {
return ErrInvalidDisplayNameEthSuffix
}
} }
if alias.IsAlias(name) { if alias.IsAlias(name) {
@ -55,3 +59,16 @@ func ValidateDisplayName(displayName *string) error {
return nil 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
}

View File

@ -31,7 +31,7 @@ func (m *Messenger) SetDisplayName(displayName string) error {
return err return err
} }
if currDisplayName == displayName { if utils.IsENSName(displayName) || currDisplayName == displayName {
return nil // Do nothing return nil // Do nothing
} }

View File

@ -204,10 +204,6 @@ func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameRestrictions()
s.Require().ErrorIs(err, expectedErr) 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("dot.not", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("t", utils.ErrInvalidDisplayNameRegExp) setInvalidName("t", utils.ErrInvalidDisplayNameRegExp)
setInvalidName("tt", utils.ErrInvalidDisplayNameRegExp) setInvalidName("tt", utils.ErrInvalidDisplayNameRegExp)
@ -221,3 +217,35 @@ func (s *MessengerProfileDisplayNameHandlerSuite) TestDisplayNameRestrictions()
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Equal("name with space", displayName) 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)
}