From 3f989810e87fbf1f6e19ecd73f14ed02bff134e9 Mon Sep 17 00:00:00 2001 From: tomiir Date: Fri, 24 May 2024 17:28:56 -0600 Subject: [PATCH] chore: restrict names (#2325) --- packages/common/src/utils/ConstantsUtil.ts | 3 ++- packages/core/src/controllers/EnsController.ts | 12 ++++++++++++ .../core/tests/controllers/EnsController.test.ts | 12 +++++------- .../src/views/w3m-account-settings-view/index.ts | 7 ++++--- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/packages/common/src/utils/ConstantsUtil.ts b/packages/common/src/utils/ConstantsUtil.ts index 0dc1cf96cc..6e084fb703 100644 --- a/packages/common/src/utils/ConstantsUtil.ts +++ b/packages/common/src/utils/ConstantsUtil.ts @@ -1,3 +1,4 @@ export const ConstantsUtil = { - WC_NAME_SUFFIX: '.wcn.id' + WC_NAME_SUFFIX: '.wcn.id', + WC_NAMES_ALLOWED_DOMAINS: ['walletconnect.com'] } diff --git a/packages/core/src/controllers/EnsController.ts b/packages/core/src/controllers/EnsController.ts index 0654183f54..756c8d106b 100644 --- a/packages/core/src/controllers/EnsController.ts +++ b/packages/core/src/controllers/EnsController.ts @@ -102,12 +102,17 @@ export const EnsController = { if (!network) { throw new Error('Network not found') } + const address = AccountController.state.address const emailConnector = ConnectorController.getAuthConnector() if (!address || !emailConnector) { throw new Error('Address or auth connector not found') } + if (!this.isAllowedToRegisterName()) { + throw new Error('Not allowed to register name') + } + state.loading = true try { @@ -158,5 +163,12 @@ export const EnsController = { const ensError = error as BlockchainApiEnsError return ensError?.reasons?.[0]?.description || defaultError + }, + isAllowedToRegisterName() { + const emailConnector = ConnectorController.getAuthConnector() + const email = emailConnector?.provider.getEmail() || '' + const domain = email.split('@')?.[1] + + return domain && ConstantsUtil.WC_NAMES_ALLOWED_DOMAINS.includes(domain) } } diff --git a/packages/core/tests/controllers/EnsController.test.ts b/packages/core/tests/controllers/EnsController.test.ts index 1d8352ceed..e68701be5f 100644 --- a/packages/core/tests/controllers/EnsController.test.ts +++ b/packages/core/tests/controllers/EnsController.test.ts @@ -137,13 +137,11 @@ describe('EnsController', () => { // Setup NetworkController.setCaipNetwork({ id: 'test:123' }) AccountController.setCaipAddress('eip155:1:0x123') - const getAuthConnectorSpy = vi - .spyOn(ConnectorController, 'getAuthConnector') - .mockResolvedValueOnce({ - provider: new W3mFrameProvider(''), - id: 'w3mAuth', - type: 'AUTH' - }) + const getAuthConnectorSpy = vi.spyOn(ConnectorController, 'getAuthConnector').mockReturnValue({ + provider: { getEmail: () => 'test@walletconnect.com' } as unknown as W3mFrameProvider, + id: 'w3mAuth', + type: 'AUTH' + }) const signMessageSpy = vi .spyOn(ConnectionController, 'signMessage') .mockResolvedValueOnce('0x123123123') diff --git a/packages/scaffold/src/views/w3m-account-settings-view/index.ts b/packages/scaffold/src/views/w3m-account-settings-view/index.ts index 0fb20b4c8b..b5d71aff4c 100644 --- a/packages/scaffold/src/views/w3m-account-settings-view/index.ts +++ b/packages/scaffold/src/views/w3m-account-settings-view/index.ts @@ -10,7 +10,8 @@ import { SnackController, StorageUtil, ConnectorController, - SendController + SendController, + EnsController } from '@web3modal/core' import { UiHelperUtil, customElement } from '@web3modal/ui' import { LitElement, html } from 'lit' @@ -151,8 +152,8 @@ export class W3mAccountSettingsView extends LitElement { private chooseNameButtonTemplate() { const type = StorageUtil.getConnectedConnector() const authConnector = ConnectorController.getAuthConnector() - - if (!authConnector || type !== 'AUTH' || this.profileName) { + const isAllowed = EnsController.isAllowedToRegisterName() + if (!authConnector || type !== 'AUTH' || this.profileName || !isAllowed) { return null }