trustwallet-assets/script-old/helpers.ts
Adam R 4f487cd054
[internal] Black/whitelist rename (#3424)
* Rename white/black* to allow/deny*.

* White/black to allow/deny renames.

* Duplicate new allowlist.json/denylist.json files under the old names

* Fix allowed-files check

Co-authored-by: Catenocrypt <catenocrypt@users.noreply.github.com>
2020-08-18 08:50:32 +02:00

96 lines
3.9 KiB
TypeScript

import * as fs from "fs"
import * as path from "path"
const Web3 = require('web3')
const web3 = new Web3('ws://localhost:8546');
export const logoName = `logo`
export const infoName = `info`
export const logoExtension = "png"
export const jsonExtension = "json"
const allowList = `allowlist.${jsonExtension}`
const denyList = `denylist.${jsonExtension}`
export const logo = `${logoName}.${logoExtension}`
export const info = `${infoName}.${jsonExtension}`
export const root = './'
export const chainsFolderPath = path.join(process.cwd(), '/blockchains')
export const getChainLogoPath = (chain: string): string => `${chainsFolderPath}/${chain}/info/${logo}`
export const getChainInfoPath = (chain: string): string => `${chainsFolderPath}/${chain}/info/${info}`
export const getChainAssetsPath = (chain: string): string => `${chainsFolderPath}/${chain}/assets`
export const getChainPath = (chain: string): string => `${chainsFolderPath}/${chain}`
export const getChainAssetPath = (chain: string, address: string) => `${getChainAssetsPath(chain)}/${address}`
export const getAllChainsList = (): string[] => readDirSync(chainsFolderPath)
export const getChainAssetLogoPath = (chain: string, address: string) => `${getChainAssetsPath(chain)}/${address}/${logo}`
export const getChainAllowlistPath = (chain: string): string => `${chainsFolderPath}/${chain}/${allowList}`
export const getChainDenylistPath = (chain: string): string => `${chainsFolderPath}/${chain}/${denyList}`
export const getChainAllowlist = (chain: string): string[] => {
if (isChainAllowlistExistSync(chain)) {
return JSON.parse(readFileSync(getChainAllowlistPath(chain)))
}
return []
}
export const getChainDenylist = (chain: string): string[] => {
if (isChainDenylistExistSync(chain)) {
return JSON.parse(readFileSync(getChainDenylistPath(chain)))
}
return []
}
export const getRootDirFilesList = (): string[] => readDirSync(root)
export const readDirSync = (path: string): string[] => fs.readdirSync(path)
export const makeDirSync = (path: string) => fs.mkdirSync(path)
export const isPathExistsSync = (path: string): boolean => fs.existsSync(path)
export const isDirContainLogo = (path: string): boolean => fs.existsSync(`${path}/${logo}`)
export const isChainAllowlistExistSync = (chain: string): boolean => isPathExistsSync(getChainAllowlistPath(chain))
export const isChainDenylistExistSync = (chain: string): boolean => isPathExistsSync(getChainDenylistPath(chain))
export const isChainInfoExistSync = (chain: string): boolean => isPathExistsSync(getChainInfoPath(chain))
export const readFileSync = (path: string) => fs.readFileSync(path, 'utf8')
export const isChecksum = (address: string): boolean => web3.utils.checkAddressChecksum(address)
export const toChecksum = (address: string): string => web3.utils.toChecksumAddress(address)
export const isEthereumAddress = (address: string): boolean => {
return web3.utils.isAddress(address)
}
export const isPathDir = (path: string): boolean => {
try {
return fs.lstatSync(path).isDirectory()
} catch (e) {
console.log(`Path: ${path} is not a directory with error: ${e.message}`)
return false
}
}
export const isPathDirEmpty = (path: string): boolean => {
try {
if (isPathDir(path)) {
return fs.readdirSync(path).length == 0
} else {
false
}
} catch (error) {
console.log(`Error isPathDirEmpty`, error)
process.exit(1)
}
}
export const removeDir = (path: string) => {
fs.rmdirSync(path, {recursive: true})
}
export const makeDirIfDoestExist = async (dirPath: string, dirName: string) => {
const path = `${dirPath}/${dirName}`
await fs.mkdir(path, {recursive: true}, (err) => {
if (err) {
console.error(`Error creating dir at path ${path} with result ${err}`)
} else {
console.log(`Created direcotry at ${path}`)
}
})
}