feat: use widget validate function (#4909)

* feat: use widget validate function

* load GA scripts
This commit is contained in:
Connor McEwen 2022-10-19 13:30:42 -04:00 committed by GitHub
parent 443cfe7540
commit afaa52e5e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 3 additions and 100 deletions

@ -169,7 +169,6 @@
"@web3-react/types": "^8.0.20-beta.0",
"@web3-react/url": "^8.0.25-beta.0",
"@web3-react/walletconnect": "^8.0.35-beta.0",
"ajv": "^6.12.3",
"array.prototype.flat": "^1.2.4",
"array.prototype.flatmap": "^1.2.4",
"cids": "^1.0.0",

@ -17,6 +17,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta name="theme-color" content="#FC72FF" />
<meta http-equiv="Content-Security-Policy" content="script-src 'self' https://www.google-analytics.com https://www.googletagmanager.com 'unsafe-inline'" />
<!--
manifest.json provides metadata used when the app is installed as a PWA.

@ -1,10 +1,9 @@
import type { TokenList } from '@uniswap/token-lists'
import { validateTokenList } from '@uniswap/widgets'
import contenthashToUri from 'lib/utils/contenthashToUri'
import parseENSAddress from 'lib/utils/parseENSAddress'
import uriToHttp from 'lib/utils/uriToHttp'
import validateTokenList from './validateTokenList'
export const DEFAULT_TOKEN_LIST = 'https://gateway.ipfs.io/ipns/tokens.uniswap.org'
const listCache = new Map<string, TokenList>()

@ -1,42 +0,0 @@
import { TokenInfo } from '@uniswap/token-lists'
import { validateTokens } from './validateTokenList'
const INVALID_TOKEN: TokenInfo = {
name: 'Dai Stablecoin',
address: '0xD3ADB33F',
symbol: 'DAI',
decimals: 18,
chainId: 1,
}
const INLINE_TOKEN_LIST = [
{
name: 'Dai Stablecoin',
address: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
symbol: 'DAI',
decimals: 18,
chainId: 1,
logoURI:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x6B175474E89094C44Da98b954EedeAC495271d0F/logo.png',
},
{
name: 'USDCoin',
address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
symbol: 'USDC',
decimals: 6,
chainId: 1,
logoURI:
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48/logo.png',
},
]
describe('validateTokens', () => {
it('throws on invalid tokens', async () => {
await expect(validateTokens([INVALID_TOKEN])).rejects.toThrowError(/^Token list failed validation:.*address/)
})
it('validates the passed token info', async () => {
await expect(validateTokens(INLINE_TOKEN_LIST)).resolves.toBe(INLINE_TOKEN_LIST)
})
})

@ -1,54 +0,0 @@
import type { TokenInfo, TokenList } from '@uniswap/token-lists'
import type { Ajv, ValidateFunction } from 'ajv'
enum ValidationSchema {
LIST = 'list',
TOKENS = 'tokens',
}
const validator = new Promise<Ajv>(async (resolve) => {
const [ajv, schema] = await Promise.all([import('ajv'), import('@uniswap/token-lists/src/tokenlist.schema.json')])
const validator = new ajv.default({ allErrors: true })
.addSchema(schema, ValidationSchema.LIST)
// Adds a meta scheme of Pick<TokenList, 'tokens'>
.addSchema(
{
...schema,
$id: schema.$id + '#tokens',
required: ['tokens'],
},
ValidationSchema.TOKENS
)
resolve(validator)
})
function getValidationErrors(validate: ValidateFunction | undefined): string {
return (
validate?.errors?.map((error) => [error.dataPath, error.message].filter(Boolean).join(' ')).join('; ') ??
'unknown error'
)
}
/**
* Validates an array of tokens.
* @param json the TokenInfo[] to validate
*/
export async function validateTokens(json: TokenInfo[]): Promise<TokenInfo[]> {
const validate = (await validator).getSchema(ValidationSchema.TOKENS)
if (validate?.({ tokens: json })) {
return json
}
throw new Error(`Token list failed validation: ${getValidationErrors(validate)}`)
}
/**
* Validates a token list.
* @param json the TokenList to validate
*/
export default async function validateTokenList(json: TokenList): Promise<TokenList> {
const validate = (await validator).getSchema(ValidationSchema.LIST)
if (validate?.(json)) {
return json
}
throw new Error(`Token list failed validation: ${getValidationErrors(validate)}`)
}

@ -5220,7 +5220,7 @@ ajv@6.5.3:
json-schema-traverse "^0.4.1"
uri-js "^4.2.2"
ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.3, ajv@^6.12.4, ajv@^6.12.5:
ajv@^6.1.0, ajv@^6.10.0, ajv@^6.10.2, ajv@^6.12.4, ajv@^6.12.5:
version "6.12.6"
resolved "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz"
integrity sha512-j3fVLgvTo527anyYyJOGTYJbG+vnnQYvE0m5mmkc1TK+nxAppkCLMIL0aZ4dblVCNoGShhm+kzE4ZUykBoMg4g==