Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add excluded wallets #257

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 51 additions & 7 deletions helpers/__tests__/services.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Service } from '../../types'
import { injectedServiceToWallet } from '../inject-wallets'
import {
combineServices,
filterExcludedServices,
filterOptInServices,
filterServicesByPlatform,
filterUniqueServices,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
})
})
Expand Down Expand Up @@ -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({
Expand All @@ -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'
Expand Down Expand Up @@ -266,7 +310,7 @@ describe('services helpers: filterServicesByPlatform', () => {
const expectedRes = [serviceA, serviceB, serviceC]

expect(filterServicesByPlatform({ wallets, platform })(services)).toEqual(
expectedRes
expectedRes,
)
})
})
6 changes: 6 additions & 0 deletions helpers/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions helpers/wallet-pipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
filterServicesByPlatform,
filterOptInServices,
filterUninstalledServices,
filterExcludedServices,
} from './services'
import {
NETWORKS,
Expand All @@ -32,13 +33,15 @@ export const getWalletPipes = ({
fclVersion,
discoveryType,
include,
exclude,
userAgent,
clientServices,
supportedStrategies,
network,
portOverride,
includeUninstalledServices,
}) => {
console.log(clientServices.map(s => s.uid))
const platform = getBrowserFromUserAgent(userAgent)?.toLowerCase()
const isLocal = network === NETWORKS.LOCAL || network === NETWORKS.EMULATOR

Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions hooks/useFcl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -46,6 +47,7 @@ export function useFcl() {
config.discoveryAuthnInclude ||
config.client?.discoveryAuthnInclude ||
[],
walletExclude: config.discoveryAuthnExclude || [],
clientServices:
config.client?.clientServices ||
config.client?.extensions ||
Expand Down
2 changes: 2 additions & 0 deletions hooks/useWallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function useWallets() {
appVersion,
clientConfig,
walletInclude,
walletExclude,
clientServices,
supportedStrategies,
network,
Expand All @@ -38,6 +39,7 @@ export function useWallets() {
type: ['authn'],
fclVersion: appVersion,
include: walletInclude,
exclude: walletExclude,
features: {
suggested: clientConfig?.discoveryFeaturesSuggested || [],
},
Expand Down
2 changes: 2 additions & 0 deletions pages/api/[network]/_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function getWalletsFromRequest(
const {
fclVersion,
include,
exclude,
extensions,
userAgent,
clientServices,
Expand Down Expand Up @@ -56,6 +57,7 @@ export async function getWalletsFromRequest(
fclVersion,
discoveryType: discoveryRequestType,
include,
exclude,
userAgent,
clientServices: services,
supportedStrategies,
Expand Down
Loading