From 083d567f5e88a6b274e15a26e7f01a3683e15083 Mon Sep 17 00:00:00 2001 From: Jordan Ribbink Date: Tue, 27 Aug 2024 07:34:21 -0700 Subject: [PATCH] Add excluded wallets --- helpers/__tests__/services.test.ts | 58 ++++++++++++++++++++++++++---- helpers/services.js | 6 ++++ helpers/wallet-pipes.ts | 5 +++ hooks/useFcl.tsx | 2 ++ hooks/useWallets.ts | 2 ++ pages/api/[network]/_common.ts | 2 ++ 6 files changed, 68 insertions(+), 7 deletions(-) diff --git a/helpers/__tests__/services.test.ts b/helpers/__tests__/services.test.ts index 6c0862a3..9d83e360 100644 --- a/helpers/__tests__/services.test.ts +++ b/helpers/__tests__/services.test.ts @@ -2,6 +2,7 @@ import { Service } from '../../types' import { injectedServiceToWallet } from '../inject-wallets' import { combineServices, + filterExcludedServices, filterOptInServices, filterServicesByPlatform, filterUniqueServices, @@ -46,7 +47,7 @@ describe('services helpers: filterUniqueServices', () => { const serviceList = [serviceA, serviceB, serviceC, serviceD] const expectedList = [serviceA, serviceB, serviceC] const filteredList = filterUniqueServices({ address: true, uid: false })( - serviceList + serviceList, ) expect(filteredList).toEqual(expectedList) @@ -91,7 +92,7 @@ describe('services helpers: filterUniqueServices', () => { const serviceList = [serviceA, serviceB, serviceC, serviceD] const expectedList = [serviceA, serviceB, serviceC] const filteredList = filterUniqueServices({ address: true, uid: true })( - serviceList + serviceList, ) expect(filteredList).toEqual(expectedList) @@ -131,10 +132,10 @@ describe('services helpers: combineServices', () => { const expectedListTwo = [serviceC, serviceA, serviceB] expect(combineServices(serviceListOne, serviceListTwo)).toEqual( - expectedListOne + expectedListOne, ) expect(combineServices(serviceListOne, serviceListTwo, true)).toEqual( - expectedListTwo + expectedListTwo, ) }) }) @@ -202,10 +203,10 @@ describe('services helpers: filterOptInServices', () => { const expectedResponseB = [serviceA, serviceB, serviceC] const walletsA = serviceListA.map(x => - injectedServiceToWallet(x as Service) + injectedServiceToWallet(x as Service), ) const walletsB = serviceListB.map(x => - injectedServiceToWallet(x as Service) + injectedServiceToWallet(x as Service), ) const filterOptInServicesA = filterOptInServices({ @@ -224,6 +225,49 @@ describe('services helpers: filterOptInServices', () => { }) }) +describe('services helpers: filterExcludedServices', () => { + it('should filter out excluded services', () => { + const excludeList = ['0xC'] + + const serviceA = { + uid: 'a', + type: 'authn', + provider: { + address: '0xA', + }, + } + + const serviceB = { + uid: 'b', + type: 'authz', + provider: { + address: '0xB', + }, + } + + const serviceC = { + uid: 'c', + type: 'pre-authz', + provider: { + address: '0xC', + }, + } + + const serviceList = [serviceA, serviceB, serviceC] + const expectedResponse = [serviceA, serviceB] + + const wallets = serviceList.map(x => injectedServiceToWallet(x as Service)) + + const result = filterExcludedServices({ + wallets, + excludeList: excludeList, + })(serviceList) + + expect(result.length).toEqual(2) + expect(result).toEqual(expectedResponse) + }) +}) + describe('services helpers: filterServicesByPlatform', () => { it('should filter services if they do not have required platform', () => { const platform = 'chrome' @@ -266,7 +310,7 @@ describe('services helpers: filterServicesByPlatform', () => { const expectedRes = [serviceA, serviceB, serviceC] expect(filterServicesByPlatform({ wallets, platform })(services)).toEqual( - expectedRes + expectedRes, ) }) }) diff --git a/helpers/services.js b/helpers/services.js index 614c1549..aa94665a 100644 --- a/helpers/services.js +++ b/helpers/services.js @@ -53,6 +53,12 @@ export const filterOptInServices = ({ wallets, includeList = [] }) => return true }) +export const filterExcludedServices = ({ wallets, excludeList = [] }) => + filter(service => { + const wallet = wallets?.find(w => w.uid === service.walletUid) + return !excludeList.includes(service?.provider?.address || wallet?.address) + }) + export const isExtension = service => service?.method === FCL_SERVICE_METHODS.EXT diff --git a/helpers/wallet-pipes.ts b/helpers/wallet-pipes.ts index 3de79929..54c884a1 100644 --- a/helpers/wallet-pipes.ts +++ b/helpers/wallet-pipes.ts @@ -12,6 +12,7 @@ import { filterServicesByPlatform, filterOptInServices, filterUninstalledServices, + filterExcludedServices, } from './services' import { NETWORKS, @@ -32,6 +33,7 @@ export const getWalletPipes = ({ fclVersion, discoveryType, include, + exclude, userAgent, clientServices, supportedStrategies, @@ -39,6 +41,7 @@ export const getWalletPipes = ({ portOverride, includeUninstalledServices, }) => { + console.log(clientServices.map(s => s.uid)) const platform = getBrowserFromUserAgent(userAgent)?.toLowerCase() const isLocal = network === NETWORKS.LOCAL || network === NETWORKS.EMULATOR @@ -124,6 +127,8 @@ export const getWalletPipes = ({ filterSupportedStrategies(supportedStrategies), // Remove opt in services unless marked as include, if supported filterOptInServices({ wallets, includeList: include }), + // Remove any excluded wallets, if supported + filterExcludedServices({ wallets, excludeList: exclude }), ), ), removeEmptyWallets, diff --git a/hooks/useFcl.tsx b/hooks/useFcl.tsx index 8c40ec9d..09c81d41 100644 --- a/hooks/useFcl.tsx +++ b/hooks/useFcl.tsx @@ -21,6 +21,7 @@ export interface FclConfig { clientConfig: { [key: string]: any } appVersion: string walletInclude: string[] + walletExclude: string[] clientServices: Service[] supportedStrategies: FCL_SERVICE_METHODS[] rpcEnabled?: boolean @@ -46,6 +47,7 @@ export function useFcl() { config.discoveryAuthnInclude || config.client?.discoveryAuthnInclude || [], + walletExclude: config.discoveryAuthnExclude || [], clientServices: config.client?.clientServices || config.client?.extensions || diff --git a/hooks/useWallets.ts b/hooks/useWallets.ts index 8582fd43..829ab437 100644 --- a/hooks/useWallets.ts +++ b/hooks/useWallets.ts @@ -27,6 +27,7 @@ export function useWallets() { appVersion, clientConfig, walletInclude, + walletExclude, clientServices, supportedStrategies, network, @@ -38,6 +39,7 @@ export function useWallets() { type: ['authn'], fclVersion: appVersion, include: walletInclude, + exclude: walletExclude, features: { suggested: clientConfig?.discoveryFeaturesSuggested || [], }, diff --git a/pages/api/[network]/_common.ts b/pages/api/[network]/_common.ts index 27855407..225ff7f8 100644 --- a/pages/api/[network]/_common.ts +++ b/pages/api/[network]/_common.ts @@ -29,6 +29,7 @@ export async function getWalletsFromRequest( const { fclVersion, include, + exclude, extensions, userAgent, clientServices, @@ -56,6 +57,7 @@ export async function getWalletsFromRequest( fclVersion, discoveryType: discoveryRequestType, include, + exclude, userAgent, clientServices: services, supportedStrategies,