Check for verified records when creating a contact

This commit is contained in:
Andrea Maria Piana 2021-10-26 11:48:02 +01:00
parent 1cc7546e67
commit be1a261d9b
11 changed files with 111 additions and 20 deletions

View File

@ -32,6 +32,10 @@ func Bytes2Hex(d []byte) string {
// Hex2Bytes returns the bytes represented by the hexadecimal string str.
func Hex2Bytes(str string) []byte {
if has0xPrefix(str) {
str = str[2:]
}
h, _ := hex.DecodeString(str)
return h
}

View File

@ -1036,7 +1036,8 @@ func (s *MessengerCommunitiesSuite) TestShareCommunity() {
// Add bob to contacts so it does not go on activity center
bobPk := common.PubkeyToHex(&s.alice.identity.PublicKey)
_, err = s.alice.AddContact(context.Background(), bobPk)
request := &requests.AddContact{ID: types.Hex2Bytes(bobPk)}
_, err = s.alice.AddContact(context.Background(), request)
s.Require().NoError(err)
// Pull message and make sure org is received

View File

@ -103,3 +103,17 @@ func (p *Persistence) AddRecord(record VerificationRecord) (response *Verificati
_, err = tx.Exec(`INSERT INTO ens_verification_records(public_key, name, clock) VALUES (?,?,?)`, record.PublicKey, record.Name, record.Clock)
return
}
func (p *Persistence) GetVerifiedRecord(publicKey string) (*VerificationRecord, error) {
record := &VerificationRecord{}
err := p.db.QueryRow(`SELECT name, clock FROM ens_verification_records WHERE verified AND public_key = ?`, publicKey).Scan(&record.Name, &record.Clock)
switch err {
case sql.ErrNoRows:
return nil, nil
case nil:
return record, nil
}
return nil, err
}

View File

@ -75,6 +75,10 @@ func (v *Verifier) ENSVerified(pk, ensName string, clock uint64) error {
return nil
}
func (v *Verifier) GetVerifiedRecord(pk string) (*VerificationRecord, error) {
return v.persistence.GetVerifiedRecord(pk)
}
func (v *Verifier) Add(pk, ensName string, clock uint64) (*VerificationRecord, error) {
record := VerificationRecord{PublicKey: pk, Name: ensName, Clock: clock}
return v.persistence.AddRecord(record)

View File

@ -11,6 +11,7 @@ import (
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -71,14 +72,14 @@ func (s *MessengerBackupSuite) TestBackupContacts() {
s.Require().NoError(err)
contactID1 := types.EncodeHex(crypto.FromECDSAPub(&contact1Key.PublicKey))
_, err = bob1.AddContact(context.Background(), contactID1)
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID1)})
s.Require().NoError(err)
contact2Key, err := crypto.GenerateKey()
s.Require().NoError(err)
contactID2 := types.EncodeHex(crypto.FromECDSAPub(&contact2Key.PublicKey))
_, err = bob1.AddContact(context.Background(), contactID2)
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID2)})
s.Require().NoError(err)
s.Require().Len(bob1.Contacts(), 2)
@ -152,7 +153,7 @@ func (s *MessengerBackupSuite) TestBackupContactsGreaterThanBatch() {
s.Require().NoError(err)
contactID := types.EncodeHex(crypto.FromECDSAPub(&contactKey.PublicKey))
_, err = bob1.AddContact(context.Background(), contactID)
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
s.Require().NoError(err)
}
@ -201,14 +202,14 @@ func (s *MessengerBackupSuite) TestBackupRemovedContact() {
s.Require().NoError(err)
contactID1 := types.EncodeHex(crypto.FromECDSAPub(&contact1Key.PublicKey))
_, err = bob1.AddContact(context.Background(), contactID1)
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID1)})
s.Require().NoError(err)
contact2Key, err := crypto.GenerateKey()
s.Require().NoError(err)
contactID2 := types.EncodeHex(crypto.FromECDSAPub(&contact2Key.PublicKey))
_, err = bob1.AddContact(context.Background(), contactID2)
_, err = bob1.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID2)})
s.Require().NoError(err)
s.Require().Len(bob1.Contacts(), 2)
@ -224,7 +225,7 @@ func (s *MessengerBackupSuite) TestBackupRemovedContact() {
// Bob 2 add one of the same contacts
_, err = bob2.AddContact(context.Background(), contactID2)
_, err = bob2.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID2)})
s.Require().NoError(err)
// Bob 1 now removes one of the contact that was also on bob 2

View File

@ -11,6 +11,7 @@ import (
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -72,7 +73,7 @@ func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
theirContactID := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey))
response, err := theirMessenger.AddContact(context.Background(), contactID)
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
s.Require().NoError(err)
s.Require().NotNil(response)
@ -134,7 +135,7 @@ func (s *MessengerContactUpdateSuite) TestAddContact() {
_, err := theirMessenger.Start()
s.Require().NoError(err)
response, err := theirMessenger.AddContact(context.Background(), contactID)
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
s.Require().NoError(err)
s.Require().NotNil(response)
@ -158,3 +159,41 @@ func (s *MessengerContactUpdateSuite) TestAddContact() {
receivedContact := response.Contacts[0]
s.Require().NotEmpty(receivedContact.LastUpdated)
}
func (s *MessengerContactUpdateSuite) TestAddContactWithENS() {
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
ensName := "blah.stateofus.eth"
theirMessenger := s.newMessenger(s.shh)
_, err := theirMessenger.Start()
s.Require().NoError(err)
s.Require().NoError(theirMessenger.ENSVerified(contactID, ensName))
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
s.Require().NoError(err)
s.Require().NotNil(response)
s.Require().Len(response.Contacts, 1)
s.Require().Equal(ensName, response.Contacts[0].Name)
s.Require().True(response.Contacts[0].ENSVerified)
s.Require().Len(response.Contacts, 1)
contact := response.Contacts[0]
// It adds the profile chat and the one to one chat
s.Require().Len(response.Chats(), 2)
// It should add the contact
s.Require().True(contact.Added)
// Wait for the message to reach its destination
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
"contact request not received",
)
s.Require().NoError(err)
receivedContact := response.Contacts[0]
s.Require().NotEmpty(receivedContact.LastUpdated)
}

View File

@ -29,6 +29,10 @@ func (m *Messenger) AddContact(ctx context.Context, request *requests.AddContact
}
}
if err := m.addENSNameToContact(contact); err != nil {
return nil, err
}
if len(request.Nickname) != 0 {
contact.LocalNickname = request.Nickname
}
@ -233,6 +237,10 @@ func (m *Messenger) SetContactLocalNickname(request *requests.SetContactLocalNic
}
}
if err := m.addENSNameToContact(contact); err != nil {
return nil, err
}
clock := m.getTimesource().GetCurrentTime()
contact.LocalNickname = nickname
contact.LastUpdatedLocally = clock
@ -390,3 +398,20 @@ func (m *Messenger) sendContactUpdate(ctx context.Context, chatID, ensName, prof
}
return &response, nil
}
func (m *Messenger) addENSNameToContact(contact *Contact) error {
// Check if there's already a verified record
ensRecord, err := m.ensVerifier.GetVerifiedRecord(contact.ID)
if err != nil {
return err
}
if ensRecord == nil {
return nil
}
contact.Name = ensRecord.Name
contact.ENSVerified = true
return nil
}

View File

@ -18,6 +18,7 @@ import (
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -318,7 +319,7 @@ func (s *MessengerProfilePictureHandlerSuite) TestE2eSendingReceivingProfilePict
s.logger.Debug("bob add contact before")
if bc {
s.logger.Debug("bob has contact to add")
_, err = s.bob.AddContact(context.Background(), s.generateKeyUID(&s.alice.identity.PublicKey))
_, err = s.bob.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(s.generateKeyUID(&s.alice.identity.PublicKey))})
s.Require().NoError(err)
s.logger.Debug("bob add contact after")
}
@ -365,7 +366,7 @@ func (s *MessengerProfilePictureHandlerSuite) TestE2eSendingReceivingProfilePict
s.logger.Debug("alice add contact before")
if ac {
s.logger.Debug("alice has contact to add")
_, err = s.alice.AddContact(context.Background(), s.generateKeyUID(&s.bob.identity.PublicKey))
_, err = s.alice.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(s.generateKeyUID(&s.bob.identity.PublicKey))})
s.Require().NoError(err)
s.logger.Debug("alice add contact after")
}

View File

@ -13,6 +13,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
@ -103,7 +104,7 @@ func (s *MessengerInstallationSuite) TestReceiveInstallation() {
contact, err := BuildContactFromPublicKey(&contactKey.PublicKey)
s.Require().NoError(err)
_, err = s.m.AddContact(context.Background(), contact.ID)
_, err = s.m.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contact.ID)})
s.Require().NoError(err)
// Wait for the message to reach its destination
@ -145,9 +146,9 @@ func (s *MessengerInstallationSuite) TestSyncInstallation() {
contact, err := BuildContactFromPublicKey(&contactKey.PublicKey)
s.Require().NoError(err)
contact.LocalNickname = "Test Nickname"
_, err = s.m.AddContact(context.Background(), contact.ID)
_, err = s.m.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contact.ID)})
s.Require().NoError(err)
_, err = s.m.SetContactLocalNickname(contact.ID, contact.LocalNickname)
_, err = s.m.SetContactLocalNickname(&requests.SetContactLocalNickname{ID: []byte(contact.ID), Nickname: contact.LocalNickname})
s.Require().NoError(err)
// add chat

View File

@ -28,6 +28,7 @@ import (
"github.com/status-im/status-go/params"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
v1protocol "github.com/status-im/status-go/protocol/v1"
"github.com/status-im/status-go/waku"
@ -254,7 +255,7 @@ func (s *MessengerSuite) TestInit() {
Prep: func() {
key, err := crypto.GenerateKey()
s.Require().NoError(err)
_, err = s.m.AddContact(context.Background(), types.EncodeHex(crypto.FromECDSAPub(&key.PublicKey)))
_, err = s.m.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(types.EncodeHex(crypto.FromECDSAPub(&key.PublicKey)))})
s.Require().NoError(err)
},
AddedFilters: 2,
@ -1343,7 +1344,7 @@ func (s *MessengerSuite) TestBlockContact() {
s.Require().NoError(s.m.SaveChat(chat2))
s.Require().NoError(s.m.SaveChat(chat3))
_, err := s.m.AddContact(context.Background(), contact.ID)
_, err := s.m.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contact.ID)})
s.Require().NoError(err)
contact.Name = "blocked"
@ -1473,7 +1474,7 @@ func (s *MessengerSuite) TestBlockContact() {
}
func (s *MessengerSuite) TestContactPersistence() {
_, err := s.m.AddContact(context.Background(), testPK)
_, err := s.m.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(testPK)})
s.Require().NoError(err)
savedContacts := s.m.Contacts()

View File

@ -300,7 +300,7 @@ func (s *MessengerPushNotificationSuite) TestReceivePushNotificationFromContactO
Added: true,
}
_, err = bob.AddContact(context.Background(), aliceContact.ID)
_, err = bob.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(aliceContact.ID)})
s.Require().NoError(err)
// Enable from contacts only
@ -446,7 +446,7 @@ func (s *MessengerPushNotificationSuite) TestReceivePushNotificationRetries() {
Added: true,
}
_, err = bob.AddContact(context.Background(), aliceContact.ID)
_, err = bob.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(aliceContact.ID)})
s.Require().NoError(err)
// Add frank has a contact
@ -456,7 +456,7 @@ func (s *MessengerPushNotificationSuite) TestReceivePushNotificationRetries() {
Added: true,
}
_, err = bob.AddContact(context.Background(), frankContact.ID)
_, err = bob.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(frankContact.ID)})
s.Require().NoError(err)
// Enable from contacts only