Skip to content

Commit

Permalink
feat_: wallet connect session notification
Browse files Browse the repository at this point in the history
Signed-off-by: yqrashawn <[email protected]>
  • Loading branch information
yqrashawn committed Jul 31, 2024
1 parent bacd4f5 commit ee438c9
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 11 deletions.
4 changes: 4 additions & 0 deletions api/geth_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -2658,6 +2658,10 @@ func (b *GethStatusBackend) injectAccountsIntoWakuService(w types.WakuKeyManager
b.statusNode.ChatService(accDB).Init(messenger)
b.statusNode.EnsService().Init(messenger.SyncEnsNamesWithDispatchMessage)
b.statusNode.CommunityTokensService().Init(messenger)

if walletService := b.statusNode.WalletService(); walletService != nil {
walletService.InjectMessenger(messenger)
}
}

return nil
Expand Down
10 changes: 8 additions & 2 deletions protocol/activity_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const (
ActivityCenterNotificationTypeFirstCommunityTokenReceived
ActivityCenterNotificationTypeCommunityBanned
ActivityCenterNotificationTypeCommunityUnbanned
ActivityCenterNotificationTypeDAppConnected
ActivityCenterNotificationTypeDAppDisconnected
)

type ActivityCenterMembershipStatus int
Expand Down Expand Up @@ -100,8 +102,12 @@ type ActivityCenterNotification struct {
TokenData *ActivityTokenData `json:"tokenData"`
//Used for synchronization. Each update should increment the UpdatedAt.
//The value should represent the time when the update occurred.
UpdatedAt uint64 `json:"updatedAt"`
AlbumMessages []*common.Message `json:"albumMessages"`
UpdatedAt uint64 `json:"updatedAt"`
AlbumMessages []*common.Message `json:"albumMessages"`
WalletProviderSessionTopic string `json:"walletProviderSessionTopic"`
DAppURL string `json:"dappURL,omitempty"`
DAppName string `json:"dappName,omitempty"`
DAppIconURL string `json:"dappIconURL,omitempty"`
}

func (n *ActivityCenterNotification) IncrementUpdatedAt(timesource common.TimeSource) {
Expand Down
42 changes: 39 additions & 3 deletions protocol/activity_center_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

const allFieldsForTableActivityCenterNotification = `id, timestamp, notification_type, chat_id, read, dismissed, accepted, message, author,
reply_message, community_id, membership_status, contact_verification_status, token_data, deleted, updated_at`
reply_message, community_id, membership_status, contact_verification_status, token_data, wallet_provider_session_topic, dapp_url, dapp_name, dapp_icon_url, deleted, updated_at`

var emptyNotifications = make([]*ActivityCenterNotification, 0)

Expand Down Expand Up @@ -151,10 +151,14 @@ func (db sqlitePersistence) SaveActivityCenterNotification(notification *Activit
accepted,
dismissed,
token_data,
wallet_provider_session_topic,
dapp_url,
dapp_name,
dapp_icon_url,
deleted,
updated_at
updated_at
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
`,
notification.ID,
notification.Timestamp,
Expand All @@ -170,6 +174,10 @@ func (db sqlitePersistence) SaveActivityCenterNotification(notification *Activit
notification.Accepted,
notification.Dismissed,
encodedTokenData,
notification.WalletProviderSessionTopic,
notification.DAppURL,
notification.DAppName,
notification.DAppIconURL,
notification.Deleted,
notification.UpdatedAt,
)
Expand Down Expand Up @@ -215,6 +223,10 @@ func (db sqlitePersistence) parseRowFromTableActivityCenterNotification(rows *sq
&notification.MembershipStatus,
&notification.ContactVerificationStatus,
&tokenDataBytes,
&notification.WalletProviderSessionTopic,
&notification.DAppURL,
&notification.DAppName,
&notification.DAppIconURL,
&notification.Deleted,
&notification.UpdatedAt,
)
Expand Down Expand Up @@ -292,6 +304,10 @@ func (db sqlitePersistence) unmarshalActivityCenterNotificationRow(row *sql.Row)
&name,
&author,
&tokenDataBytes,
&notification.WalletProviderSessionTopic,
&notification.DAppURL,
&notification.DAppName,
&notification.DAppIconURL,
&notification.UpdatedAt)

if err != nil {
Expand Down Expand Up @@ -381,6 +397,10 @@ func (db sqlitePersistence) unmarshalActivityCenterNotificationRows(rows *sql.Ro
&name,
&author,
&tokenDataBytes,
&notification.WalletProviderSessionTopic,
&notification.DAppURL,
&notification.DAppName,
&notification.DAppIconURL,
&latestCursor,
&notification.UpdatedAt)
if err != nil {
Expand Down Expand Up @@ -542,6 +562,10 @@ func (db sqlitePersistence) buildActivityCenterQuery(tx *sql.Tx, params activity
c.name,
a.author,
a.token_data,
a.wallet_provider_session_topic,
a.dapp_url,
a.dapp_name,
a.dapp_icon_url,
substr('0000000000000000000000000000000000000000000000000000000000000000' || a.timestamp, -64, 64) || hex(a.id) as cursor,
a.updated_at
FROM activity_center_notifications a
Expand Down Expand Up @@ -663,6 +687,10 @@ func (db sqlitePersistence) GetActivityCenterNotificationsByID(ids []types.HexBy
c.name,
a.author,
a.token_data,
a.wallet_provider_session_topic,
a.dapp_url,
a.dapp_name,
a.dapp_icon_url,
substr('0000000000000000000000000000000000000000000000000000000000000000' || a.timestamp, -64, 64) || hex(a.id) as cursor,
a.updated_at
FROM activity_center_notifications a
Expand Down Expand Up @@ -704,6 +732,10 @@ func (db sqlitePersistence) GetActivityCenterNotificationByID(id types.HexBytes)
c.name,
a.author,
a.token_data,
a.wallet_provider_session_topic,
a.dapp_url,
a.dapp_name,
a.dapp_icon_url,
a.updated_at
FROM activity_center_notifications a
LEFT JOIN chats c
Expand Down Expand Up @@ -1338,6 +1370,10 @@ func (db sqlitePersistence) ActiveContactRequestNotification(contactID string) (
c.name,
a.author,
a.token_data,
a.wallet_provider_session_topic,
a.dapp_url,
a.dapp_name,
a.dapp_icon_url,
a.updated_at
FROM activity_center_notifications a
LEFT JOIN chats c ON c.id = a.chat_id
Expand Down
24 changes: 24 additions & 0 deletions protocol/messenger_walletconnect.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package protocol

import (
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/services/wallet/walletconnect"
)

type WalletConnectSession struct {
Expand Down Expand Up @@ -30,6 +32,28 @@ func (m *Messenger) AddWalletConnectSession(request *requests.AddWalletConnectSe
return m.persistence.InsertWalletConnectSession(session)
}

func (m *Messenger) NewWalletConnectV2SessionCreatedNotification(session walletconnect.Session) error {
now := m.GetCurrentTimeInMillis()

notification := &ActivityCenterNotification{
ID: types.FromHex(string(session.Topic) + "_dapp_connected"),
Type: ActivityCenterNotificationTypeDAppConnected,
DAppURL: session.Peer.Metadata.URL,
DAppName: session.Peer.Metadata.Name,
WalletProviderSessionTopic: string(session.Topic),
Timestamp: now,
UpdatedAt: now,
}

if len(session.Peer.Metadata.Icons) > 0 {
notification.DAppIconURL = session.Peer.Metadata.Icons[0]
}

_, err := m.persistence.SaveActivityCenterNotification(notification, true)

return err
}

func (m *Messenger) GetWalletConnectSession() ([]WalletConnectSession, error) {

return m.getWalletConnectSession()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE activity_center_notifications ADD COLUMN dapp_url TEXT NULL;
ALTER TABLE activity_center_notifications ADD COLUMN dapp_name TEXT NULL;
ALTER TABLE activity_center_notifications ADD COLUMN dapp_icon_url TEXT NULL;
ALTER TABLE activity_center_notifications ADD COLUMN wallet_provider_session_topic TEXT NULL;
11 changes: 10 additions & 1 deletion services/wallet/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,10 +829,19 @@ func (api *API) getVerifiedWalletAccount(address, password string) (*account.Sel
}, nil
}

type InjectedMessenger interface {
NewWalletConnectV2SessionCreatedNotification(session walletconnect.Session) error
}

// AddWalletConnectSession adds or updates a session wallet connect session
func (api *API) AddWalletConnectSession(ctx context.Context, session_json string) error {
log.Debug("wallet.api.AddWalletConnectSession", "rpcURL", len(session_json))
return walletconnect.AddSession(api.s.db, api.s.config.Networks, session_json)
session, err := walletconnect.AddSession(api.s.db, api.s.config.Networks, session_json)
if err != nil {
return err
}

return api.s.injectedMessenger.NewWalletConnectV2SessionCreatedNotification(session)
}

// DisconnectWalletConnectSession removes a wallet connect session
Expand Down
6 changes: 6 additions & 0 deletions services/wallet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ func NewService(
keycardPairings: NewKeycardPairings(),
config: config,
featureFlags: featureFlags,
injectedMessenger: nil,
}
}

Expand Down Expand Up @@ -244,6 +245,7 @@ type Service struct {
keycardPairings *KeycardPairings
config *params.NodeConfig
featureFlags *protocolCommon.FeatureFlags
injectedMessenger InjectedMessenger
}

// Start signals transmitter.
Expand Down Expand Up @@ -342,3 +344,7 @@ func (s *Service) GetEnsService() *ens.Service {
func (s *Service) GetStickersService() *stickers.Service {
return s.stickers
}

func (s *Service) InjectMessenger(injectedMessenger InjectedMessenger) {
s.injectedMessenger = injectedMessenger
}
8 changes: 4 additions & 4 deletions services/wallet/walletconnect/walletconnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ func (p *SessionProposal) ValidateProposal() bool {
}

// AddSession adds a new active session to the database
func AddSession(db *sql.DB, networks []params.Network, session_json string) error {
func AddSession(db *sql.DB, networks []params.Network, session_json string) (Session, error) {
var session Session
err := json.Unmarshal([]byte(session_json), &session)
if err != nil {
return fmt.Errorf("unmarshal session: %v", err)
return session, fmt.Errorf("unmarshal session: %v", err)
}

chains := supportedChainsInSession(session)
testChains, err := areTestChains(networks, chains)
if err != nil {
return fmt.Errorf("areTestChains: %v", err)
return session, fmt.Errorf("areTestChains: %v", err)
}

rowEntry := DBSession{
Expand All @@ -213,7 +213,7 @@ func AddSession(db *sql.DB, networks []params.Network, session_json string) erro
rowEntry.IconURL = session.Peer.Metadata.Icons[0]
}

return UpsertSession(db, rowEntry)
return session, UpsertSession(db, rowEntry)
}

// areTestChains assumes chains to tests are all testnets or all mainnets
Expand Down
2 changes: 1 addition & 1 deletion services/wallet/walletconnect/walletconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func Test_AddSession(t *testing.T) {
{ChainID: uint64(chainID), IsTest: true},
}
timestampBeforeAddSession := time.Now().Unix()
err := AddSession(db, networks, sessionJSON)
_, err := AddSession(db, networks, sessionJSON)
assert.NoError(t, err)

// Validate that session was written correctly to the database
Expand Down

0 comments on commit ee438c9

Please sign in to comment.