@@ -135,53 +135,64 @@ export class NotifyEngine extends INotifyEngine {
135135
136136 public unregister : INotifyEngine [ "unregister" ] = async ( { account } ) => {
137137 try {
138- // Get information about watching subscriptions of account
139- const watchedAccount = this . client . watchedAccounts . get ( account ) ;
138+ if ( ! ( await this . client . identityKeys . hasIdentity ( { account } ) ) ) {
139+ return ;
140+ }
140141
141- // If account was watched
142- if ( watchedAccount ) {
143- this . client . logger . info (
144- `[Notify] unregister > account ${ watchedAccount . account } was previously watched. Unsubscribing from watch topics`
145- ) ;
146- // and subscribed to a notify server watch topic
147- if (
148- await this . client . core . relayer . subscriber . isSubscribed (
149- watchedAccount . resTopic
150- )
151- ) {
152- // unsubscribe from watch topic
153- await this . client . core . relayer . unsubscribe ( watchedAccount . resTopic ) ;
154- }
142+ // If user has watched their subscriptions before, stop watching.
143+ // We can not assume that every registered user has a watchedAccount
144+ // due to the fact that the stores for watchedAccounts and identityKeys
145+ // are entirely separate.
146+ if ( this . client . watchedAccounts . keys . includes ( account ) ) {
147+ const watchedAccount = this . client . watchedAccounts . get ( account ) ;
155148
156- // If account was the last to be watched
157- if ( watchedAccount . lastWatched ) {
149+ // If account was watched: stop watching it.
150+ if ( watchedAccount ) {
158151 this . client . logger . info (
159- `[Notify] unregister > account ${ watchedAccount . account } was last to be watched. Unmarking as last watched `
152+ `[Notify] unregister > account ${ watchedAccount . account } was previously watched. Unsubscribing from watch topics `
160153 ) ;
161- // Remove last watched flag, to prevent watching on next init.
162- await this . client . watchedAccounts . update ( watchedAccount . account , {
163- lastWatched : false ,
164- } ) ;
154+ // and subscribed to a notify server watch topic
155+ if (
156+ await this . client . core . relayer . subscriber . isSubscribed (
157+ watchedAccount . resTopic
158+ )
159+ ) {
160+ // unsubscribe from watch topic
161+ await this . client . core . relayer . unsubscribe ( watchedAccount . resTopic ) ;
162+ }
163+
164+ // If account was the last to be watched
165+ if ( watchedAccount . lastWatched ) {
166+ // Remove last watched flag, to prevent watching on next init.
167+ await this . client . watchedAccounts . update ( watchedAccount . account , {
168+ lastWatched : false ,
169+ } ) ;
170+ this . client . logger . info (
171+ `[Notify] unregister > account ${ watchedAccount . account } was last to be watched. Unmarking as last watched`
172+ ) ;
173+ }
165174 }
166175 }
167176
168- this . client . logger . info (
169- `[Notify] unregister > account ${ watchedAccount . account } was last to be watched. Unmarking as last watched`
170- ) ;
171-
172177 // Unsubscribe from subscription topics
173178 for ( const sub of Object . values (
174179 this . getActiveSubscriptions ( { account } )
175180 ) ) {
176181 await this . client . core . relayer . unsubscribe ( sub . topic ) ;
177182 }
178183
179- this . client . logger . info (
180- `[Notify] unregister > account ${ watchedAccount . account } was last to be watched. Unmarking as last watched`
181- ) ;
182184 // unregister from identity server
183185 await this . client . identityKeys . unregisterIdentity ( { account } ) ;
184186
187+ // If user has registration data, clear it to prevent false
188+ // data regarding staleness of identity
189+ if ( this . client . registrationData . keys . includes ( account ) ) {
190+ this . client . registrationData . delete ( account , {
191+ code : - 1 ,
192+ message : "Wallet was unregistered" ,
193+ } ) ;
194+ }
195+
185196 this . client . logger . info (
186197 `Engine.unregister > Successfully unregistered account ${ account } `
187198 ) ;
0 commit comments