fix(ens): support ens names with dashes in them

* fix: parseENSAddress #1200

* fix: lint error #1200

* fix: parseENSAddress - allow to match domains with twice characters; disallow more than once dash next to each other
This commit is contained in:
Grzegorz Kućmierz 2020-11-07 17:19:01 +01:00 committed by GitHub
parent d9825622f1
commit eb4c305eff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 2 deletions

@ -10,5 +10,10 @@ describe('parseENSAddress', () => {
expect(parseENSAddress('abso.lutely.eth')).toEqual({ ensName: 'abso.lutely.eth', ensPath: undefined })
expect(parseENSAddress('eth')).toEqual(undefined)
expect(parseENSAddress('eth/hello-world')).toEqual(undefined)
expect(parseENSAddress('hello-world.eth')).toEqual({ ensName: 'hello-world.eth', ensPath: undefined })
expect(parseENSAddress('-prefix-dash.eth')).toEqual(undefined)
expect(parseENSAddress('suffix-dash-.eth')).toEqual(undefined)
expect(parseENSAddress('it.eth')).toEqual({ ensName: 'it.eth', ensPath: undefined })
expect(parseENSAddress('only-single--dash.eth')).toEqual(undefined)
})
})

@ -1,7 +1,7 @@
const ENS_NAME_REGEX = /^(([a-zA-Z0-9]+\.)+)eth(\/.*)?$/
const ENS_NAME_REGEX = /^(([a-zA-Z0-9]+(-[a-zA-Z0-9]+)*\.)+)eth(\/.*)?$/
export function parseENSAddress(ensAddress: string): { ensName: string; ensPath: string | undefined } | undefined {
const match = ensAddress.match(ENS_NAME_REGEX)
if (!match) return undefined
return { ensName: `${match[1].toLowerCase()}eth`, ensPath: match[3] }
return { ensName: `${match[1].toLowerCase()}eth`, ensPath: match[4] }
}