Skip to content

Commit

Permalink
fix: Handle special characters for prefix in address input (#4287)
Browse files Browse the repository at this point in the history
  • Loading branch information
usame-algan authored Oct 4, 2024
1 parent cfd86bc commit 5db2132
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
95 changes: 94 additions & 1 deletion src/utils/__tests__/addresses.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checksumAddress, isChecksummedAddress, parsePrefixedAddress, sameAddress } from '../addresses'
import { checksumAddress, cleanInputValue, isChecksummedAddress, parsePrefixedAddress, sameAddress } from '../addresses'

describe('Addresses', () => {
describe('checksumAddress', () => {
Expand Down Expand Up @@ -99,4 +99,97 @@ describe('Addresses', () => {
expect(address).toBe('sdfgsdfg')
})
})

describe('cleanInputValue', () => {
it('should return the address when input is a valid address without prefix', () => {
const input = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe('0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the address with prefix when input has a valid prefix', () => {
const input = 'prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe('prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the matched address when input contains text before the match', () => {
const input = 'some text prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe('prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the matched address when input contains text after the match', () => {
const input = 'prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd some text'
const output = cleanInputValue(input)

expect(output).toBe('prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the original value when input does not match the regex', () => {
const input = 'invalid input'
const output = cleanInputValue(input)

expect(output).toBe('invalid input')
})

it('should handle prefixes with hyphens', () => {
const input = 'uh-huh:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe('uh-huh:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the address when input has uppercase letters', () => {
const input = '0xABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD'
const output = cleanInputValue(input)

expect(output).toBe('0xABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCD')
})

it('should return the original value when Ethereum address is invalid (too short)', () => {
const input = '0x123'
const output = cleanInputValue(input)

expect(output).toBe('0x123')
})

it('should trim spaces and return the address when input has leading and trailing spaces', () => {
const input = ' 0xabcdefabcdefabcdefabcdefabcdefabcdefabcd '
const output = cleanInputValue(input)

expect(output).toBe('0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the first matched address when input contains multiple addresses', () => {
const input = '0xabcdefabcdefabcdefabcdefabcdefabcdefabcd 0x1234567890abcdef1234567890abcdef12345678'
const output = cleanInputValue(input)

expect(output).toBe('0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the address with numeric prefix', () => {
const input = '12345:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe('12345:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the address when prefix is missing colon', () => {
const input = 'prefix0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe('0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
})

it('should return the original value when prefix contains invalid characters', () => {
const input = 'invalid!prefix:0xabcdefabcdefabcdefabcdefabcdefabcdefabcd'
const output = cleanInputValue(input)

expect(output).toBe(input)
})
})
})
2 changes: 1 addition & 1 deletion src/utils/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const formatPrefixedAddress = (address: string, prefix?: string): string
}

export const cleanInputValue = (value: string): string => {
const regex = /(?:([a-z0-9]+):)?(0x[a-f0-9]{40})\b/i
const regex = /(?:([^\s:]+):)?(0x[a-f0-9]{40})\b/i
const match = value.match(regex)
// if match, return the address with optional prefix
if (match) return match[0]
Expand Down

0 comments on commit 5db2132

Please sign in to comment.