Skip to content

Commit

Permalink
fix: validation mechanism to accept ENS and CIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechsimetka committed Apr 27, 2022
1 parent d869749 commit 961866c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/pages/files/Download.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { History } from '../../components/History'
import { Context, defaultUploadOrigin } from '../../providers/File'
import { Context as SettingsContext } from '../../providers/Settings'
import { ROUTES } from '../../routes'
import { recognizeEnsOrSwarmHash } from '../../utils'
import { recognizeEnsOrSwarmHash, regexpEns } from '../../utils'
import { determineHistoryName, HISTORY_KEYS, putHistory } from '../../utils/local-storage'
import { FileNavigation } from './FileNavigation'

Expand All @@ -23,10 +23,17 @@ export function Download(): ReactElement {
const navigate = useNavigate()

const validateChange = (value: string) => {
if (Utils.isHexString(value, 64) || Utils.isHexString(value, 128) || !value.trim().length) {
if (
Utils.isHexString(value, 64) ||
Utils.isHexString(value, 128) ||
!value.trim().length ||
regexpEns.test(value)
) {
setReferenceError(undefined)
} else {
setReferenceError('Incorrect format of swarm hash. Expected 64 or 128 hexstring characters.')
setReferenceError(
'Incorrect format of swarm hash. Expected 64 or 128 hexstring characters, bzz.link url or ENS domain.',
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function extractSwarmCid(s: string): string | undefined {

// Matches any number of subdomains ending with .eth
// e.g. this.is.just-a-test.eth
const regexpEns = /((?:(?!-)[a-z0-9-]{1,63}(?<!-)\.)+eth)(?:$|[/?:#].*)/i
export const regexpEns = /((?:(?!-)[a-z0-9-]{1,63}(?<!-)\.)+eth)(?:$|[/?:#].*)/i

export function extractEns(value: string): string | undefined {
const matches = value.match(regexpEns)
Expand Down

0 comments on commit 961866c

Please sign in to comment.