Skip to content
This repository was archived by the owner on Dec 3, 2025. It is now read-only.

Commit 5b6fa2f

Browse files
devcelinebkrem
andauthored
chore: be more vigilante against inconsistent state (#94)
* chore: be more vigilante against inconsistent state * chore: add comments regarding explaining cleanup process * chore: run prettier * chore: remove duplicate log * Update packages/notify-client/src/controllers/engine.ts Co-authored-by: Ben Kremer <ben@walletconnect.com> * chore(release): 0.16.3 --------- Co-authored-by: Ben Kremer <ben@walletconnect.com>
1 parent ba08d91 commit 5b6fa2f

3 files changed

Lines changed: 43 additions & 32 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/notify-client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@walletconnect/notify-client",
33
"description": "WalletConnect Notify Client",
4-
"version": "0.16.2",
4+
"version": "0.16.3",
55
"author": "WalletConnect, Inc. <walletconnect.com>",
66
"homepage": "https://github.com/walletconnect/notify-client-js/",
77
"license": "Apache-2.0",

packages/notify-client/src/controllers/engine.ts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)