From 9fc9fd91919453097072ba20960712967ac2752f Mon Sep 17 00:00:00 2001 From: iamacook Date: Tue, 26 Sep 2023 16:39:55 +0200 Subject: [PATCH] fix: notification preferences + post-switch registration --- .../__tests__/useNotificationRegistrations.test.ts | 4 ++-- .../hooks/useNotificationPreferences.ts | 7 +------ .../hooks/useNotificationRegistrations.ts | 8 ++------ .../settings/PushNotifications/logic.test.ts | 14 ++++++++++---- src/components/settings/PushNotifications/logic.ts | 14 +++++++++----- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/components/settings/PushNotifications/hooks/__tests__/useNotificationRegistrations.test.ts b/src/components/settings/PushNotifications/hooks/__tests__/useNotificationRegistrations.test.ts index b9ac1646c5..58cd032e87 100644 --- a/src/components/settings/PushNotifications/hooks/__tests__/useNotificationRegistrations.test.ts +++ b/src/components/settings/PushNotifications/hooks/__tests__/useNotificationRegistrations.test.ts @@ -30,12 +30,12 @@ describe('useNotificationRegistrations', () => { describe('registerNotifications', () => { beforeEach(() => { const mockProvider = new Web3Provider(jest.fn()) - jest.spyOn(web3, 'useWeb3').mockImplementation(() => mockProvider) + jest.spyOn(web3, 'createWeb3').mockImplementation(() => mockProvider) jest.spyOn(wallet, 'default').mockImplementation( () => ({ label: 'MetaMask', - } as unknown as ConnectedWallet), + } as ConnectedWallet), ) }) diff --git a/src/components/settings/PushNotifications/hooks/useNotificationPreferences.ts b/src/components/settings/PushNotifications/hooks/useNotificationPreferences.ts index e74d6f4c03..1c46e87783 100644 --- a/src/components/settings/PushNotifications/hooks/useNotificationPreferences.ts +++ b/src/components/settings/PushNotifications/hooks/useNotificationPreferences.ts @@ -17,7 +17,6 @@ import { } from '@/services/push-notifications/preferences' import { logError } from '@/services/exceptions' import ErrorCodes from '@/services/exceptions/ErrorCodes' -import useIsSafeOwner from '@/hooks/useIsSafeOwner' import type { PushNotificationPreferences, PushNotificationPrefsKey } from '@/services/push-notifications/preferences' import type { NotifiableSafes } from '../logic' @@ -59,7 +58,6 @@ export const useNotificationPreferences = (): { // State const uuid = useUuid() const preferences = usePreferences() - const isOwner = useIsSafeOwner() // Getters const getPreferences = (chainId: string, safeAddress: string) => { @@ -152,10 +150,7 @@ export const useNotificationPreferences = (): { const defaultPreferences: PushNotificationPreferences[PushNotificationPrefsKey] = { chainId, safeAddress, - preferences: { - ...DEFAULT_NOTIFICATION_PREFERENCES, - [WebhookType.CONFIRMATION_REQUEST]: isOwner, - }, + preferences: DEFAULT_NOTIFICATION_PREFERENCES, } return [key, defaultPreferences] diff --git a/src/components/settings/PushNotifications/hooks/useNotificationRegistrations.ts b/src/components/settings/PushNotifications/hooks/useNotificationRegistrations.ts index 200006681c..d9d67e3f15 100644 --- a/src/components/settings/PushNotifications/hooks/useNotificationRegistrations.ts +++ b/src/components/settings/PushNotifications/hooks/useNotificationRegistrations.ts @@ -1,6 +1,5 @@ import { registerDevice, unregisterDevice, unregisterSafe } from '@safe-global/safe-gateway-typescript-sdk' -import { useWeb3 } from '@/hooks/wallets/web3' import { useAppDispatch } from '@/store' import { showNotification } from '@/store/notificationsSlice' import { useNotificationPreferences } from './useNotificationPreferences' @@ -10,7 +9,6 @@ import { getRegisterDevicePayload } from '../logic' import { logError } from '@/services/exceptions' import ErrorCodes from '@/services/exceptions/ErrorCodes' import useWallet from '@/hooks/wallets/useWallet' -import { isLedger } from '@/utils/wallets' import type { NotifiableSafes } from '../logic' const registrationFlow = async (registrationFn: Promise, callback: () => void): Promise => { @@ -39,13 +37,12 @@ export const useNotificationRegistrations = (): { unregisterDeviceNotifications: (chainId: string) => Promise } => { const dispatch = useAppDispatch() - const web3 = useWeb3() const wallet = useWallet() const { uuid, _createPreferences, _deletePreferences, _deleteAllPreferences } = useNotificationPreferences() const registerNotifications = async (safesToRegister: NotifiableSafes) => { - if (!uuid || !web3 || !wallet) { + if (!uuid || !wallet) { return } @@ -53,8 +50,7 @@ export const useNotificationRegistrations = (): { const payload = await getRegisterDevicePayload({ uuid, safesToRegister, - web3, - isLedger: isLedger(wallet), + wallet, }) return registerDevice(payload) diff --git a/src/components/settings/PushNotifications/logic.test.ts b/src/components/settings/PushNotifications/logic.test.ts index c88f3b1a17..16353d8969 100644 --- a/src/components/settings/PushNotifications/logic.test.ts +++ b/src/components/settings/PushNotifications/logic.test.ts @@ -5,7 +5,9 @@ import { Web3Provider } from '@ethersproject/providers' import type { JsonRpcSigner } from '@ethersproject/providers' import * as logic from './logic' +import * as web3 from '@/hooks/wallets/web3' import packageJson from '../../../../package.json' +import type { ConnectedWallet } from '@/services/onboard' jest.mock('firebase/messaging') @@ -128,6 +130,7 @@ describe('Notifications', () => { signMessage: jest.fn().mockResolvedValueOnce(MM_SIGNATURE), } as unknown as JsonRpcSigner), ) + jest.spyOn(web3, 'createWeb3').mockImplementation(() => mockProvider) const uuid = crypto.randomUUID() @@ -137,8 +140,9 @@ describe('Notifications', () => { ['2']: [hexZeroPad('0x1', 20)], }, uuid, - web3: mockProvider, - isLedger: false, + wallet: { + label: 'MetaMask', + } as ConnectedWallet, }) expect(payload).toStrictEqual({ @@ -176,6 +180,7 @@ describe('Notifications', () => { signMessage: jest.fn().mockResolvedValueOnce(LEDGER_SIGNATURE), } as unknown as JsonRpcSigner), ) + jest.spyOn(web3, 'createWeb3').mockImplementation(() => mockProvider) const uuid = crypto.randomUUID() @@ -185,8 +190,9 @@ describe('Notifications', () => { ['2']: [hexZeroPad('0x1', 20)], }, uuid, - web3: mockProvider, - isLedger: true, + wallet: { + label: 'Ledger', + } as ConnectedWallet, }) expect(payload).toStrictEqual({ diff --git a/src/components/settings/PushNotifications/logic.ts b/src/components/settings/PushNotifications/logic.ts index 1e1b57bc9d..7256658a89 100644 --- a/src/components/settings/PushNotifications/logic.ts +++ b/src/components/settings/PushNotifications/logic.ts @@ -9,6 +9,9 @@ import packageJson from '../../../../package.json' import { logError } from '@/services/exceptions' import ErrorCodes from '@/services/exceptions/ErrorCodes' import { checksumAddress } from '@/utils/addresses' +import { isLedger } from '@/utils/wallets' +import { createWeb3 } from '@/hooks/wallets/web3' +import type { ConnectedWallet } from '@/services/onboard' type WithRequired = T & { [P in K]-?: T[P] } @@ -85,13 +88,11 @@ export type NotifiableSafes = { [chainId: string]: Array } export const getRegisterDevicePayload = async ({ safesToRegister, uuid, - web3, - isLedger, + wallet, }: { safesToRegister: NotifiableSafes uuid: string - web3: Web3Provider - isLedger: boolean + wallet: ConnectedWallet }): Promise => { const BUILD_NUMBER = '0' // Required value, but does not exist on web const BUNDLE = 'safe' @@ -107,6 +108,9 @@ export const getRegisterDevicePayload = async ({ serviceWorkerRegistration, }) + const web3 = createWeb3(wallet.provider) + const isLedgerWallet = isLedger(wallet) + // If uuid is not provided a new device will be created. // If a uuid for an existing Safe is provided the FirebaseDevice will be updated with all the new data provided. // Safes provided on the request are always added and never removed/replaced @@ -125,7 +129,7 @@ export const getRegisterDevicePayload = async ({ uuid, timestamp, token, - isLedger, + isLedger: isLedgerWallet, }) return {