Skip to content

Commit

Permalink
chore: restrict names (#2325)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed May 24, 2024
1 parent 86e9c71 commit 3f98981
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/common/src/utils/ConstantsUtil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const ConstantsUtil = {
WC_NAME_SUFFIX: '.wcn.id'
WC_NAME_SUFFIX: '.wcn.id',
WC_NAMES_ALLOWED_DOMAINS: ['walletconnect.com']
}
12 changes: 12 additions & 0 deletions packages/core/src/controllers/EnsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
12 changes: 5 additions & 7 deletions packages/core/tests/controllers/EnsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: () => '[email protected]' } as unknown as W3mFrameProvider,
id: 'w3mAuth',
type: 'AUTH'
})
const signMessageSpy = vi
.spyOn(ConnectionController, 'signMessage')
.mockResolvedValueOnce('0x123123123')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 3f98981

Please sign in to comment.