Skip to content

Commit

Permalink
improvement: validate mock address (#2401)
Browse files Browse the repository at this point in the history
  • Loading branch information
namgold authored Nov 24, 2023
1 parent ef8ce26 commit b2ee1ac
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 29 deletions.
6 changes: 4 additions & 2 deletions src/constants/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ChainId } from '@kyberswap/ks-sdk-core'
import invariant from 'tiny-invariant'

import { PrivateAnnouncementType } from 'components/Announcement/type'
import { isAddressString } from 'utils/address'

import { ENV_TYPE } from './type'

Expand Down Expand Up @@ -162,8 +164,8 @@ export const getAnnouncementsTemplateIds = (type: keyof TemplateConfig) => {
}

const mock = localStorage.getItem('mock')?.split(',') ?? []
export const MOCK_ACCOUNT_EVM = mock[0] ?? ''
export const MOCK_ACCOUNT_SOLANA = mock[1] ?? ''
export const MOCK_ACCOUNT_EVM = isAddressString(ChainId.MAINNET, mock[0]?.trim())
export const MOCK_ACCOUNT_SOLANA = isAddressString(ChainId.SOLANA, mock[1]?.trim())

const isSupportTestNet = ENV_LEVEL < ENV_TYPE.PROD && new URLSearchParams(window.location.search).get('test')
export const CROSS_CHAIN_CONFIG = {
Expand Down
29 changes: 29 additions & 0 deletions src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ChainId, Token } from '@kyberswap/ks-sdk-core'
import { PublicKey } from '@solana/web3.js'

export const isWalletAddressSolana = async (addr: string) => {
try {
if (!addr) return false
const publicKey = new PublicKey(addr)
return await PublicKey.isOnCurve(publicKey.toBytes())
} catch (err) {
return false
}
}

// returns the checksummed address if the address is valid, otherwise returns false
export function isAddress(chainId: ChainId, value: any): string | false {
try {
return new Token(chainId, value, 0).address
} catch {
return false
}
}

export function isAddressString(chainId: ChainId, value: any): string {
try {
return new Token(chainId, value, 0).address
} catch {
return ''
}
}
30 changes: 3 additions & 27 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { ApolloClient, NormalizedCacheObject } from '@apollo/client'
import { BigNumber } from '@ethersproject/bignumber'
import { ChainId, Currency, CurrencyAmount, Percent, Token, WETH } from '@kyberswap/ks-sdk-core'
import { ChainId, Currency, CurrencyAmount, Percent, WETH } from '@kyberswap/ks-sdk-core'
import { WalletReadyState } from '@solana/wallet-adapter-base'
import { PublicKey } from '@solana/web3.js'
import dayjs from 'dayjs'
import JSBI from 'jsbi'
import Numeral from 'numeral'
Expand All @@ -26,32 +25,9 @@ import store from 'state'
import { GroupedTxsByHash, TransactionDetails } from 'state/transactions/type'
import { chunk } from 'utils/array'

export const isWalletAddressSolana = async (addr: string) => {
try {
if (!addr) return false
const publicKey = new PublicKey(addr)
return await PublicKey.isOnCurve(publicKey.toBytes())
} catch (err) {
return false
}
}
import { isAddress, isAddressString, isWalletAddressSolana } from './address'

// returns the checksummed address if the address is valid, otherwise returns false
export function isAddress(chainId: ChainId, value: any): string | false {
try {
return new Token(chainId, value, 0).address
} catch {
return false
}
}

export function isAddressString(chainId: ChainId, value: any): string {
try {
return new Token(chainId, value, 0).address
} catch {
return ''
}
}
export { isAddress, isWalletAddressSolana, isAddressString }

export function getEtherscanLink(
chainId: ChainId,
Expand Down

0 comments on commit b2ee1ac

Please sign in to comment.