From 5459fa4c39e72b5146b9dc7570ac21bdd20d5b41 Mon Sep 17 00:00:00 2001 From: Celine Sarafa Date: Tue, 16 Jan 2024 13:03:54 +0100 Subject: [PATCH 1/4] feat: reregister if no registration while subscribing --- src/contexts/W3iContext/hooks/notifyHooks.ts | 19 ++++++++++ src/w3iProxy/listenerTypes.ts | 10 +++++- .../notifyProviders/internalNotifyProvider.ts | 35 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/contexts/W3iContext/hooks/notifyHooks.ts b/src/contexts/W3iContext/hooks/notifyHooks.ts index f7ae65e4..0cd4c933 100644 --- a/src/contexts/W3iContext/hooks/notifyHooks.ts +++ b/src/contexts/W3iContext/hooks/notifyHooks.ts @@ -153,6 +153,24 @@ export const useNotifyState = (w3iProxy: Web3InboxProxy, proxyReady: boolean) => } }) + const notifyReregisterSub = notifyClient.observe('notify_reregister', { + next: (params) => { + handleRegistration(params.userPubkey).then(() => { + switch(params.nextAction.type) { + case "subscribe": + notifyClient.subscribe(params.nextAction.params) + break; + case "deleteSubscription": + notifyClient.deleteSubscription(params.nextAction.params) + break; + case "update": + notifyClient.update(params.nextAction.params) + break; + } + }) + } + }) + const syncUpdateSub = notifyClient.observe('sync_update', { next: refreshNotifyState }) @@ -165,6 +183,7 @@ export const useNotifyState = (w3iProxy: Web3InboxProxy, proxyReady: boolean) => notifySubsChanged.unsubscribe() notifySignatureRequestedSub.unsubscribe() notifySignatureRequestCancelledSub.unsubscribe() + notifyReregisterSub.unsubscribe() } }, [notifyClient, refreshNotifyState, setWatchSubscriptionsComplete]) diff --git a/src/w3iProxy/listenerTypes.ts b/src/w3iProxy/listenerTypes.ts index b136064b..5b085b26 100644 --- a/src/w3iProxy/listenerTypes.ts +++ b/src/w3iProxy/listenerTypes.ts @@ -1,4 +1,4 @@ -import type { NotifyClientTypes } from '@walletconnect/notify-client' +import type { NotifyClient, NotifyClientTypes } from '@walletconnect/notify-client' import type { ChatClientTypes } from './chatProviders/types' @@ -21,6 +21,13 @@ export interface ChatFacadeEvents { sync_update: never } +type NextAction = { + type: T, + params: Parameters[0] +} + +type NextActions = NextAction<"subscribe"> | NextAction<"update"> | NextAction<"deleteSubscription"> + export interface NotifyFacadeEvents { notify_message: NotifyClientTypes.EventArguments['notify_message'] notify_subscription: NotifyClientTypes.EventArguments['notify_subscription'] @@ -29,5 +36,6 @@ export interface NotifyFacadeEvents { notify_subscriptions_changed: NotifyClientTypes.EventArguments['notify_subscriptions_changed'] notify_signature_requested: { message: string } notify_signature_request_cancelled: never + notify_reregister: { userPubkey: string, nextAction: NextActions} sync_update: never } diff --git a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts index 685b9d88..2e2bdba7 100644 --- a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts +++ b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts @@ -53,6 +53,11 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { this.emitter.emit('sync_update', {}) }) + // @ts-ignore + window.forceUnregister = (account:string) => { + this.unregister({account}) + } + // Ensure we have a registration with echo (if we need it) this.ensureEchoRegistration() updateSymkeyState() @@ -163,6 +168,14 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { throw new Error(this.formatClientRelatedError('subscribe')) } + if(!this.notifyClient.isRegistered({ account: params.account, domain: window.location.hostname, allApps: true })) { + this.emitter.emit("notify_reregister", {userPubkey: params.account, nextAction: { + type: "subscribe", + params, + }}) + return false; + } + let subscribed: boolean = false try { subscribed = await this.notifyClient.subscribe({ @@ -187,6 +200,17 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { throw new Error(this.formatClientRelatedError('update')) } + const account = this.notifyClient.subscriptions.keys.includes(params.topic)? + this.notifyClient.subscriptions.get(params.topic).account : "" + + if(!this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true })) { + this.emitter.emit("notify_reregister", {userPubkey: account, nextAction: { + type: "update", + params + }}) + return false; + } + const updated = await this.notifyClient.update(params) return updated @@ -197,6 +221,17 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { throw new Error(this.formatClientRelatedError('deleteSubscription')) } + const account = this.notifyClient.subscriptions.keys.includes(params.topic)? + this.notifyClient.subscriptions.get(params.topic).account : "" + + if(!this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true })) { + this.emitter.emit("notify_reregister", {userPubkey: account, nextAction: { + type: "deleteSubscription", + params + }}) + return; + } + return this.notifyClient.deleteSubscription(params) } From 2227581460a2525e885de0dc7dd02a8439c055f9 Mon Sep 17 00:00:00 2001 From: Celine Sarafa Date: Tue, 16 Jan 2024 13:06:10 +0100 Subject: [PATCH 2/4] chore: remove test code --- src/w3iProxy/notifyProviders/internalNotifyProvider.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts index 2e2bdba7..3514fab1 100644 --- a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts +++ b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts @@ -53,11 +53,6 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { this.emitter.emit('sync_update', {}) }) - // @ts-ignore - window.forceUnregister = (account:string) => { - this.unregister({account}) - } - // Ensure we have a registration with echo (if we need it) this.ensureEchoRegistration() updateSymkeyState() From 9f8c8279454893f1b4a645cda8fe90b28a4e1e10 Mon Sep 17 00:00:00 2001 From: Celine Sarafa Date: Tue, 16 Jan 2024 13:10:37 +0100 Subject: [PATCH 3/4] chore: run prettier --- src/constants/projects.ts | 11 ++- src/contexts/W3iContext/hooks/notifyHooks.ts | 28 ++++---- src/w3iProxy/listenerTypes.ts | 8 +-- .../notifyProviders/internalNotifyProvider.ts | 69 ++++++++++++------- 4 files changed, 67 insertions(+), 49 deletions(-) diff --git a/src/constants/projects.ts b/src/constants/projects.ts index 3e2f72c2..2af1f89d 100644 --- a/src/constants/projects.ts +++ b/src/constants/projects.ts @@ -47,7 +47,7 @@ export const COMING_SOON_PROJECTS: Array = [ id: 'avive', name: 'Avive', description: - 'One of the world\'s largest UBI & DePin Projects, with over 9 million worldwide users.', + "One of the world's largest UBI & DePin Projects, with over 9 million worldwide users.", url: 'https://www.avive.world', isComingSoon: true, isVerified: false, @@ -57,8 +57,7 @@ export const COMING_SOON_PROJECTS: Array = [ { id: 'maverick', name: 'Maverick Protocol', - description: - 'Maverick Protocol is a leading provider of smart contract solutions in DeFi.', + description: 'Maverick Protocol is a leading provider of smart contract solutions in DeFi.', url: 'https://app.mav.xyz', isComingSoon: true, isVerified: false, @@ -68,8 +67,7 @@ export const COMING_SOON_PROJECTS: Array = [ { id: 'loopring-pro', name: 'Loopring Pro', - description: - 'Explore the Loopring DEX, Earn, and NFT Management.', + description: 'Explore the Loopring DEX, Earn, and NFT Management.', url: 'https://loopring.io/#/pro', isComingSoon: true, isVerified: false, @@ -79,8 +77,7 @@ export const COMING_SOON_PROJECTS: Array = [ { id: 'loopring-earn', name: 'Loopring Earn', - description: - 'Access the most innovative structural products brought to the DeFi world.', + description: 'Access the most innovative structural products brought to the DeFi world.', url: 'https://earn.loopring.io/', isComingSoon: true, isVerified: false, diff --git a/src/contexts/W3iContext/hooks/notifyHooks.ts b/src/contexts/W3iContext/hooks/notifyHooks.ts index 0cd4c933..7a1adf35 100644 --- a/src/contexts/W3iContext/hooks/notifyHooks.ts +++ b/src/contexts/W3iContext/hooks/notifyHooks.ts @@ -154,20 +154,20 @@ export const useNotifyState = (w3iProxy: Web3InboxProxy, proxyReady: boolean) => }) const notifyReregisterSub = notifyClient.observe('notify_reregister', { - next: (params) => { - handleRegistration(params.userPubkey).then(() => { - switch(params.nextAction.type) { - case "subscribe": - notifyClient.subscribe(params.nextAction.params) - break; - case "deleteSubscription": - notifyClient.deleteSubscription(params.nextAction.params) - break; - case "update": - notifyClient.update(params.nextAction.params) - break; - } - }) + next: params => { + handleRegistration(params.userPubkey).then(() => { + switch (params.nextAction.type) { + case 'subscribe': + notifyClient.subscribe(params.nextAction.params) + break + case 'deleteSubscription': + notifyClient.deleteSubscription(params.nextAction.params) + break + case 'update': + notifyClient.update(params.nextAction.params) + break + } + }) } }) diff --git a/src/w3iProxy/listenerTypes.ts b/src/w3iProxy/listenerTypes.ts index 5b085b26..886b2198 100644 --- a/src/w3iProxy/listenerTypes.ts +++ b/src/w3iProxy/listenerTypes.ts @@ -21,12 +21,12 @@ export interface ChatFacadeEvents { sync_update: never } -type NextAction = { - type: T, +type NextAction = { + type: T params: Parameters[0] } -type NextActions = NextAction<"subscribe"> | NextAction<"update"> | NextAction<"deleteSubscription"> +type NextActions = NextAction<'subscribe'> | NextAction<'update'> | NextAction<'deleteSubscription'> export interface NotifyFacadeEvents { notify_message: NotifyClientTypes.EventArguments['notify_message'] @@ -36,6 +36,6 @@ export interface NotifyFacadeEvents { notify_subscriptions_changed: NotifyClientTypes.EventArguments['notify_subscriptions_changed'] notify_signature_requested: { message: string } notify_signature_request_cancelled: never - notify_reregister: { userPubkey: string, nextAction: NextActions} + notify_reregister: { userPubkey: string; nextAction: NextActions } sync_update: never } diff --git a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts index 3514fab1..f7c3ebdd 100644 --- a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts +++ b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts @@ -163,12 +163,21 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { throw new Error(this.formatClientRelatedError('subscribe')) } - if(!this.notifyClient.isRegistered({ account: params.account, domain: window.location.hostname, allApps: true })) { - this.emitter.emit("notify_reregister", {userPubkey: params.account, nextAction: { - type: "subscribe", - params, - }}) - return false; + if ( + !this.notifyClient.isRegistered({ + account: params.account, + domain: window.location.hostname, + allApps: true + }) + ) { + this.emitter.emit('notify_reregister', { + userPubkey: params.account, + nextAction: { + type: 'subscribe', + params + } + }) + return false } let subscribed: boolean = false @@ -195,15 +204,21 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { throw new Error(this.formatClientRelatedError('update')) } - const account = this.notifyClient.subscriptions.keys.includes(params.topic)? - this.notifyClient.subscriptions.get(params.topic).account : "" - - if(!this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true })) { - this.emitter.emit("notify_reregister", {userPubkey: account, nextAction: { - type: "update", - params - }}) - return false; + const account = this.notifyClient.subscriptions.keys.includes(params.topic) + ? this.notifyClient.subscriptions.get(params.topic).account + : '' + + if ( + !this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true }) + ) { + this.emitter.emit('notify_reregister', { + userPubkey: account, + nextAction: { + type: 'update', + params + } + }) + return false } const updated = await this.notifyClient.update(params) @@ -216,15 +231,21 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { throw new Error(this.formatClientRelatedError('deleteSubscription')) } - const account = this.notifyClient.subscriptions.keys.includes(params.topic)? - this.notifyClient.subscriptions.get(params.topic).account : "" - - if(!this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true })) { - this.emitter.emit("notify_reregister", {userPubkey: account, nextAction: { - type: "deleteSubscription", - params - }}) - return; + const account = this.notifyClient.subscriptions.keys.includes(params.topic) + ? this.notifyClient.subscriptions.get(params.topic).account + : '' + + if ( + !this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true }) + ) { + this.emitter.emit('notify_reregister', { + userPubkey: account, + nextAction: { + type: 'deleteSubscription', + params + } + }) + return } return this.notifyClient.deleteSubscription(params) From ea83d6da5f2c4a9274e0b4c1f64834c4d7a8fc9f Mon Sep 17 00:00:00 2001 From: Celine Sarafa Date: Tue, 16 Jan 2024 13:19:23 +0100 Subject: [PATCH 4/4] Update src/w3iProxy/notifyProviders/internalNotifyProvider.ts Co-authored-by: Enes --- src/w3iProxy/notifyProviders/internalNotifyProvider.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts index f7c3ebdd..d8fb5121 100644 --- a/src/w3iProxy/notifyProviders/internalNotifyProvider.ts +++ b/src/w3iProxy/notifyProviders/internalNotifyProvider.ts @@ -208,9 +208,13 @@ export default class InternalNotifyProvider implements W3iNotifyProvider { ? this.notifyClient.subscriptions.get(params.topic).account : '' - if ( - !this.notifyClient.isRegistered({ account, domain: window.location.hostname, allApps: true }) - ) { + const isRegistered = this.notifyClient.isRegistered({ + account, + domain: window.location.hostname, + allApps: true + }) + + if (!isRegistered) { this.emitter.emit('notify_reregister', { userPubkey: account, nextAction: {