diff --git a/services/wallet/api.go b/services/wallet/api.go index 9161e6b01..c7a531d57 100644 --- a/services/wallet/api.go +++ b/services/wallet/api.go @@ -27,6 +27,7 @@ import ( "github.com/status-im/status-go/services/wallet/thirdparty" "github.com/status-im/status-go/services/wallet/token" "github.com/status-im/status-go/services/wallet/transfer" + "github.com/status-im/status-go/services/wallet/walletconnect" wc "github.com/status-im/status-go/services/wallet/walletconnect" "github.com/status-im/status-go/services/wallet/walletevent" "github.com/status-im/status-go/transactions" @@ -681,7 +682,7 @@ func (api *API) WCPairSessionProposal(ctx context.Context, sessionProposalJSON s return api.s.walletConnect.PairSessionProposal(data) } -// WCPairEstablished confirms that a pairing has been established +// WCRecordSuccessfulPairing confirms that a pairing has been established func (api *API) WCRecordSuccessfulPairing(ctx context.Context, sessionProposalJSON string) error { log.Debug("wallet.api.wc.RecordSuccessfulPairing", "proposal.len", len(sessionProposalJSON)) @@ -694,7 +695,13 @@ func (api *API) WCRecordSuccessfulPairing(ctx context.Context, sessionProposalJS return api.s.walletConnect.RecordSuccessfulPairing(data) } -// WCSessionRequest responds to "session_request" event +// WCChangePairingState changes the active state of a pairing +func (api *API) WCChangePairingState(ctx context.Context, topic walletconnect.Topic, active bool) error { + log.Debug("wallet.api.wc.ChangePairingState", "topic", topic, "active", active) + + return api.s.walletConnect.ChangePairingState(topic, active) +} + func (api *API) WCHasActivePairings(ctx context.Context) (bool, error) { log.Debug("wallet.api.wc.HasActivePairings") diff --git a/services/wallet/walletconnect/database.go b/services/wallet/walletconnect/database.go index 4b0a0afcb..7094a70d4 100644 --- a/services/wallet/walletconnect/database.go +++ b/services/wallet/walletconnect/database.go @@ -2,6 +2,7 @@ package walletconnect import ( "database/sql" + "errors" ) type Pairing struct { @@ -22,6 +23,25 @@ func InsertPairing(db *sql.DB, pairing Pairing) error { return err } +func ChangePairingState(db *sql.DB, topic Topic, active bool) error { + stmt, err := db.Prepare("UPDATE wallet_connect_pairings SET active = ? WHERE topic = ?") + if err != nil { + return err + } + + res, err := stmt.Exec(active, topic) + if err != nil { + return err + } + + rowsAffected, err := res.RowsAffected() + if rowsAffected == 0 { + return errors.New("unable to locate pairing entry for DB state change") + } + + return nil +} + func GetPairingByTopic(db *sql.DB, topic Topic) (*Pairing, error) { querySQL := `SELECT topic, expiry_timestamp, active, app_name, url, description, icon, verified_is_scam, verified_origin, verified_verify_url, verified_validation FROM wallet_connect_pairings WHERE topic = ?` diff --git a/services/wallet/walletconnect/database_test.go b/services/wallet/walletconnect/database_test.go index c1e1bbaa4..bfe15fb00 100644 --- a/services/wallet/walletconnect/database_test.go +++ b/services/wallet/walletconnect/database_test.go @@ -66,6 +66,23 @@ func TestInsertAndGetPairing(t *testing.T) { require.Equal(t, entry, *retrievedPairing) } +func TestChangePairingState(t *testing.T) { + db, close := setupTestDB(t) + defer close() + + entry := generateTestData(1)[0] + err := InsertPairing(db, entry) + require.NoError(t, err) + + err = ChangePairingState(db, entry.Topic, false) + require.NoError(t, err) + + retrievedPairing, err := GetPairingByTopic(db, entry.Topic) + require.NoError(t, err) + + require.Equal(t, false, retrievedPairing.Active) +} + func TestGet(t *testing.T) { db, close := setupTestDB(t) defer close() diff --git a/services/wallet/walletconnect/rpc.go b/services/wallet/walletconnect/rpc.go index e9ad81728..e5f7ecd9b 100644 --- a/services/wallet/walletconnect/rpc.go +++ b/services/wallet/walletconnect/rpc.go @@ -112,7 +112,7 @@ func (s *Service) buildTransaction(request SessionRequest) (response *SessionReq Address: account.Address, AddressPath: account.Path, SignOnKeycard: kp.MigratedToKeycard(), - MesageToSign: signer.Hash(txBeingSigned), + MessageToSign: signer.Hash(txBeingSigned), }, nil } @@ -183,6 +183,6 @@ func (s *Service) buildMessage(request SessionRequest, addressIndex int, message Address: account.Address, AddressPath: account.Path, SignOnKeycard: kp.MigratedToKeycard(), - MesageToSign: types.HexBytes(hash), + MessageToSign: types.HexBytes(hash), }, nil } diff --git a/services/wallet/walletconnect/service.go b/services/wallet/walletconnect/service.go index 028571d7f..bd1c19219 100644 --- a/services/wallet/walletconnect/service.go +++ b/services/wallet/walletconnect/service.go @@ -200,6 +200,10 @@ func (s *Service) RecordSuccessfulPairing(proposal SessionProposal) error { }) } +func (s *Service) ChangePairingState(topic Topic, active bool) error { + return ChangePairingState(s.db, topic, active) +} + func (s *Service) HasActivePairings() (bool, error) { return HasActivePairings(s.db, time.Now().Unix()) } diff --git a/services/wallet/walletconnect/walletconnect.go b/services/wallet/walletconnect/walletconnect.go index c024c4455..75fddcaf9 100644 --- a/services/wallet/walletconnect/walletconnect.go +++ b/services/wallet/walletconnect/walletconnect.go @@ -107,7 +107,7 @@ type SessionRequestResponse struct { Address types.Address `json:"address,omitempty"` AddressPath string `json:"addressPath,omitempty"` SignOnKeycard bool `json:"signOnKeycard,omitempty"` - MesageToSign interface{} `json:"messageToSign,omitempty"` + MessageToSign interface{} `json:"messageToSign,omitempty"` } // Valid namespace